1 24 36 37 package org.datashare; 38 39 import java.net.Socket ; 40 import java.net.SocketException ; 41 import java.io.EOFException ; 42 import java.io.InterruptedIOException ; 43 import java.io.ObjectInputStream ; 44 import java.io.ObjectOutputStream ; 45 import java.io.InvalidClassException ; 46 47 import org.datashare.objects.ChannelDescription; 48 import org.datashare.objects.DataShareObject; 49 50 53 public class TcpSocket extends SocketAdapter implements Runnable 54 { 55 private Socket socket; 56 private int priority; 57 boolean running = true; 58 private ObjectInputStream ois = null; 59 private ObjectOutputStream oos = null; 60 private String myThreadName = ""; 61 private Thread myThread = null; 62 private TransmitDataThread tdt = null; 63 64 public TcpSocket(Socket socket, DataReceiverInterface dri, int priority) 65 { 66 this.socket = socket; 67 try{ 68 this.socket.setSoTimeout(SessionUtilities.getTCPSocketReadTimeout()); } 70 catch(SocketException se) 71 { 72 se.printStackTrace(); 73 } 74 this.dri = dri; 75 this.priority = priority; 76 this.localIP = socket.getLocalAddress(); 77 this.localPort = socket.getLocalPort(); 78 this.remoteIP = socket.getInetAddress(); 79 this.remotePort = socket.getPort(); 80 this.keyValue = "Socket-TCP-" + localIP.getHostAddress() + ":" + localPort + 81 "-" + remoteIP.getHostAddress() + ":" + remotePort; 82 try{ 83 ois = new ObjectInputStream (socket.getInputStream()); 84 oos = new ObjectOutputStream (socket.getOutputStream()); 85 } 86 catch(Exception e) 87 { 88 e.printStackTrace(); 89 stopThreadAndCloseSocket(); 90 } 91 92 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 93 SessionUtilities.getLoggingInterface().NETWORK, 94 "New connection: " + keyValue + " with active set to " + (this.getActive()?"true":"false")); 95 96 tdt = new TransmitDataThread(this); 98 tdt.setName("XmitThread for " + keyValue); 99 try{ 100 tdt.setPriority(Thread.currentThread().getPriority() + SessionUtilities.SocketXmtRelativePriority); } 102 catch(Exception e) 103 { 104 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 105 SessionUtilities.getLoggingInterface().NETWORK, 106 "Problems setting Xmt Thread priority:"); 107 e.printStackTrace(); 108 } 109 tdt.start(); 110 } 111 112 115 int getType() 116 { 117 return ChannelDescription.TCP; 118 } 119 120 124 void sendData(DataShareObject dsObject) 125 { 126 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 127 SessionUtilities.getLoggingInterface().NETWORK, 128 "3-Adding "+ dsObject.sendingClientKey +"'s object to "+this.getClientKey() +"'s xmt buffer"); 129 tdt.addData(dsObject); 130 } 131 132 135 protected void xmitData(DataShareObject dsObject) 136 { 137 try{ 138 if(running) 139 { 140 oos.writeObject(dsObject); 141 oos.flush(); 142 } 143 } 144 catch(Exception e) 145 { 146 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 147 SessionUtilities.getLoggingInterface().NETWORK, 148 "Problem in Socket " + this.keyValue + ":"); 149 stopThreadAndCloseSocket(); 151 } 152 } 153 154 157 public void run() 158 { 159 myThreadName = Thread.currentThread().getName(); 160 myThread = Thread.currentThread(); 161 162 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 163 SessionUtilities.getLoggingInterface().NETWORK, 164 "Setting TCP socket " + this.keyValue + " to priority " + priority); 165 try{ 166 Thread.currentThread().setPriority(priority); 167 while(running) 168 { 169 try{ 170 Object object = ois.readObject(); try{ 172 DataShareObject dso = (DataShareObject)object; 173 dri.clientDataReceived(dso,this); Thread.currentThread().yield(); 175 } 176 catch(ClassCastException cce) 177 { 178 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING, 179 SessionUtilities.getLoggingInterface().NETWORK, 180 "Problem-TcpSocket "+this.keyValue + " received unknown object"); 181 } 182 } 183 catch(ClassNotFoundException cnfe) 184 { 185 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING, 186 SessionUtilities.getLoggingInterface().NETWORK, 187 "Problem reading Tcp Stream: "+cnfe.getMessage()); 188 } 189 catch(InvalidClassException ice) 190 { 191 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING, 192 SessionUtilities.getLoggingInterface().NETWORK, 193 "Problem reading Tcp Stream: "+ice.getMessage()); 194 } 195 catch(InterruptedIOException iioe) 196 { 197 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 198 SessionUtilities.getLoggingInterface().NETWORK, 199 "Tcp socket " + this.keyValue + " timedOut"); 200 this.xmitData(new DataShareObject()); } 202 } 203 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 204 SessionUtilities.getLoggingInterface().NETWORK, 205 "Thread " + Thread.currentThread().getName() + " is stopping (scheduled)"); 206 } 207 catch(SocketException se) 208 { 209 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 210 SessionUtilities.getLoggingInterface().NETWORK, 211 "Lost connection: " + keyValue); 212 stopThreadAndCloseSocket(); 213 } 214 catch(EOFException eofe) 215 { 216 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 217 SessionUtilities.getLoggingInterface().NETWORK, 218 "Lost connection: " + keyValue); 219 stopThreadAndCloseSocket(); 220 } 221 catch(Exception e) 222 { 223 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 224 SessionUtilities.getLoggingInterface().NETWORK, 225 "Problems reading Tcp data:"); 226 e.printStackTrace(); 227 stopThreadAndCloseSocket(); 228 } 229 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 230 SessionUtilities.getLoggingInterface().NETWORK, 231 "Thread " + Thread.currentThread().getName() + " has stopped"); 232 } 233 234 237 void stopThreadAndCloseSocket() 238 { 239 if(running == true) 240 { 241 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 242 SessionUtilities.getLoggingInterface().NETWORK, 243 "removing/closing socket " + this.keyValue); 244 running = false; 245 myThread.setName("Stopped--"+myThreadName); 246 try{ 247 if(socket != null) 248 socket.close(); 249 } 250 catch(Exception e){} 251 try{ 252 if(oos != null) 253 oos.close(); 254 } 255 catch(Exception e){} 256 try{ 257 if(ois != null) 258 ois.close(); 259 } 260 catch(Exception e){} 261 finally 262 { 263 tdt.stopThread(); 264 dri.connectionLost(this); 265 } 266 } 267 } 268 269 272 public void close() 273 { 274 stopThreadAndCloseSocket(); 275 } 276 277 } | Popular Tags |