1 19 package gcc.rmi.iiop.server; 20 21 import gcc.*; 22 import gcc.properties.*; 23 import gcc.rmi.iiop.*; 24 import java.net.*; 25 26 public class SocketListener extends Thread 27 { 28 public static SocketListener getInstance() 29 { 30 return new SocketListener(); 31 } 32 33 35 private String _name; 36 37 private String _host; 38 39 private int _port; 40 41 private int _listenBacklog; 42 43 private java.net.ServerSocket _listener; 44 45 47 protected void init() 48 { 49 _host = "localhost"; 50 _port = 2000; 51 _listenBacklog = 10; 52 setDaemon(true); 53 } 54 55 57 public void setHost(String host) 58 { 59 _host = host; 60 } 61 62 public void setPort(int port) 63 { 64 _port = port; 65 } 66 67 public void setListenBacklog(int backlog) 68 { 69 _listenBacklog = backlog; 70 } 71 72 public void run() 73 { 74 String iiopURL = "iiop://" + _host + ":" + _port; 75 ListenerInfo listenerInfo = new ListenerInfo(); 76 listenerInfo.protocol = Protocol.IIOP; listenerInfo.host = _host; 78 listenerInfo.port = _port; 79 try 80 { 81 InetAddress addr = InetAddress.getByName(_host); 82 _listener = new java.net.ServerSocket(_port, _listenBacklog, addr); 83 } 84 catch (Exception ex) 85 { 86 System.out.println( "SocketListener: Error creating server socket." ); 87 ex.printStackTrace(); 88 try 89 { 90 Socket socket = new Socket(_host, _port); 91 socket.close(); 92 System.out.println( "SocketListener: Error server already running: " + iiopURL ); 93 ex.printStackTrace(); 94 } 95 catch (Exception ignore) 96 { 97 } 98 return; 99 } 100 new CheckConnect().start(); 101 for (;;) 102 { 103 java.net.Socket socket; 104 try 105 { 106 socket = _listener.accept(); 107 } 108 catch (Exception ex) 109 { 110 throw new SystemException(ex); } 112 MessageHandler.getInstance(listenerInfo, socket).start(); 113 } 114 } 115 116 private class CheckConnect extends Thread 117 { 118 public void run() 119 { 120 try 121 { 122 Socket socket = new Socket(_host, _port); 123 socket.close(); 124 if (! SystemProperties.quiet()) 125 { 126 System.out.println(formatAcceptingIiopConnections()); 127 } 128 } 129 catch (Exception ex) 130 { 131 warnConnectFailed(_host, _port); 132 } 133 } 134 } 135 136 138 protected String formatAcceptingIiopConnections() 139 { 140 String msg = "SocketListener.formatAcceptingIiopConnection(): "; 141 return msg; 142 } 143 144 146 protected void warnConnectFailed(String host, int port) 147 { 148 System.out.println( "SocketListener.warnConnectFailed(): host = " + host + ", port = " + port ); 149 } 150 } 151 | Popular Tags |