00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package wttools.remotecons;
00024
00025 import java.util.List;
00026 import java.util.LinkedList;
00027 import java.io.IOException;
00028 import wttools.remotecons.ifc.CommandHandlerIfc;
00029
00184 public class RemoteServer {
00185
00194 protected int server_port = Constants.DEFAULT_SERVER_PORT;
00202 protected boolean as_daemon = true;
00209 protected SocketListener s_listener = null;
00210
00223 public void initialize(List command_handlers) throws IOException {
00224 initialize(server_port, command_handlers);
00225 }
00226
00242 public void initialize(int port_no, List command_handlers)
00243 throws IOException
00244 {
00245 s_listener = new SocketListener();
00246 s_listener.setConn_handler(new ConnectionHandlerImpl());
00247 s_listener.setCommand_handlers(command_handlers);
00248 s_listener.startListening(port_no, as_daemon);
00249 }
00250
00265 public void asyncMessage(String msg) {
00266 synchronized(ConnectionServer.active_connections) {
00267 List l = ConnectionServer.active_connections;
00268 for (int i = 0; i < l.size(); i++) {
00269 ((ConnectionServer)l.get(i)).asyncMessage(msg);
00270 }
00271 }
00272 }
00273
00282 public void disable() {
00283 s_listener.close();
00284 }
00285
00293 public static void main (String[] args) {
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 RemoteServer rs = new RemoteServer();
00305 rs.setServer_port(2702);
00306 rs.setAs_daemon(false);
00307 LinkedList l = new LinkedList();
00308 l.add(rs.new SampleCommandHandler());
00309 try {
00310 rs.initialize(l);
00311 } catch (IOException e) {
00312 e.printStackTrace();
00313 System.exit(1);
00314 }
00315
00316 System.out.println("Remote server started in test mode.");
00317 System.out.println("Waiting for incoming connections on port "+rs.server_port+"...");
00318 }
00319
00320
00326 public int getServer_port() {
00327 return this.server_port;
00328 }
00329
00335 public void setServer_port(int argServer_port){
00336 this.server_port = argServer_port;
00337 }
00338
00339
00345 public boolean isAs_daemon() {
00346 return this.as_daemon;
00347 }
00348
00354 public void setAs_daemon(boolean argAs_daemon){
00355 this.as_daemon = argAs_daemon;
00356 }
00357
00362 public class SampleCommandHandler implements CommandHandlerIfc {
00363
00371 public String handleCommand(String comm) {
00372 if (comm.trim().equals("hello")) {
00373 return "Hello world!\r\n";
00374 }
00375 if (comm.trim().equals("isdaemon")) {
00376 return ""+as_daemon+"\r\n";
00377 }
00378 return null;
00379 }
00380
00387 public String help() {
00388 return
00389 "\r\n"+
00390 " hello - Returns 'Hello world!' string as a result.\r\n"+
00391 " isdaemon - returns info if console started as daemon.\r\n";
00392 }
00393
00403 public String help(String comm) {
00404 return "";
00405 }
00406
00413 public CommandHandlerIfc getInstance() {
00414 return new SampleCommandHandler();
00415 }
00416
00417 }
00418
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462