1 24 37 38 package org.datashare; 39 40 import java.net.ServerSocket ; 41 import java.net.Socket ; 42 import java.net.BindException ; 43 import java.util.Vector ; 44 45 54 public class TcpSocketServer extends Thread implements SocketServerInterface 55 { 56 private DataReceiverInterface dri = null; private int port; private int priority; private ServerSocket server = null; 60 public boolean running = true; private boolean available = false; 62 private String keyValue; 64 67 public TcpSocketServer(DataReceiverInterface dri, int port, int priority) 68 { 69 boolean stillTrying = true; 70 int count = 0; 71 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 72 SessionUtilities.getLoggingInterface().NETWORK, 73 "TcpSocketServer created for port " + port); 74 this.dri = dri; 75 this.port = port; 76 this.priority = priority; 77 keyValue = "TCP:"+port; 78 this.setName("DataShare.SocketServer."+keyValue); 79 while(stillTrying && (count++) < 50) 80 { 81 try{ 82 server = new ServerSocket (this.port); 83 stillTrying = false; 84 } 85 catch(BindException be) 86 { 87 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING, 88 SessionUtilities.getLoggingInterface().NETWORK, 89 "TCP Socket appears to be in use, try next one..."); 90 this.port = dri.getNextPort(); 92 } 93 catch(Exception e) 94 { 95 e.printStackTrace(); 96 dri.lostServerSocket(keyValue); 97 running = false; 98 stillTrying = false; 99 } 100 } 101 } 102 103 106 public int getLocalPort() 107 { 108 return port; 109 } 110 111 114 public boolean getReady() 115 { 116 return available; 117 } 118 119 122 public String getKeyValue() 123 { 124 return keyValue; 125 } 126 127 130 public void killThread() 131 { 132 running = false; 133 } 134 135 public void run() 136 { 137 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 138 SessionUtilities.getLoggingInterface().NETWORK, 139 "Setting " + this.keyValue + " to priority " + priority); 140 setPriority(priority); 141 while(running) 142 { 143 try{ 144 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 145 SessionUtilities.getLoggingInterface().NETWORK, 146 "waiting for TCP connection on " + port); 147 available = true; 148 Socket socket = server.accept(); 149 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 150 SessionUtilities.getLoggingInterface().CLIENT, 151 "Client connecting to TCP port " + port); 152 if(socket != null) 154 { 155 TcpSocket newSocket = new TcpSocket(socket, dri, priority); 156 Thread thread = new Thread (newSocket,"DataShare."+newSocket.getKeyValue()); 157 thread.start(); 158 dri.newConnection(newSocket); yield(); 160 } 161 } 162 catch(Exception e) 163 { 164 try{ 165 server.close(); 166 } 167 catch(Exception e2){e2.printStackTrace();} 168 finally 169 { 170 if(running) { 172 dri.lostServerSocket(keyValue); 173 e.printStackTrace(); 174 running = false; 175 } 176 } 177 } 178 } 179 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 180 SessionUtilities.getLoggingInterface().NETWORK, 181 "Thread " + getName() + " has stopped"); 182 } 183 184 187 public void close() 188 { 189 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 190 SessionUtilities.getLoggingInterface().NETWORK, 191 "removing socket server " + keyValue); 192 try{ 193 running = false; 194 server.close(); 195 dri.lostServerSocket(keyValue); 196 } 197 catch(Exception e) 198 { 199 e.printStackTrace(); 200 } 201 } 202 203 } 204 | Popular Tags |