1 24 33 34 package org.datashare; 35 36 import java.net.DatagramSocket ; 37 import java.net.DatagramPacket ; 38 import java.net.InetAddress ; 39 40 import java.io.EOFException ; 41 import java.io.InvalidClassException ; 42 import java.io.IOException ; 43 44 import org.datashare.objects.DataShareObject; 45 import org.datashare.objects.ChannelDescription; 46 47 public class UdpSocket extends SocketAdapter 48 { 49 DatagramSocket socket; 50 DatagramPacket sndPacket; 51 UdpSocketServer server; 52 private TransmitDataThread tdt = null; 53 boolean running = true; 54 55 public UdpSocket(UdpSocketServer server, DatagramSocket socket, DatagramPacket packet, DataReceiverInterface dri, InetAddress myIpAddress) 56 { 57 this.server = server; 58 this.socket = socket; 59 this.dri = dri; 60 this.localIP = myIpAddress; this.localPort = socket.getLocalPort(); 62 this.remoteIP = packet.getAddress(); 63 this.remotePort = packet.getPort(); 64 this.keyValue = "Socket-UDP-" + localIP.getHostAddress() + ":" + localPort + 65 "-" + remoteIP.getHostAddress() + ":" + remotePort; 66 sndPacket = new DatagramPacket (new byte[1], 1, remoteIP, remotePort); 68 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 69 SessionUtilities.getLoggingInterface().NETWORK, 70 "New connection: " + keyValue + " with active set to " + (this.getActive()?"true":"false")); 71 72 tdt = new TransmitDataThread(this); 74 tdt.setName("XmitThread for " + keyValue); 75 try{ 76 tdt.setPriority(Thread.currentThread().getPriority() + SessionUtilities.SocketXmtRelativePriority); } 78 catch(Exception e) 79 { 80 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 81 SessionUtilities.getLoggingInterface().NETWORK, 82 "Problems setting Xmt Thread priority:"); 83 e.printStackTrace(); 84 } 85 tdt.start(); 86 } 87 88 91 public void sendData(DataShareObject dsObject) 92 { 93 if(running) 94 { 95 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 96 SessionUtilities.getLoggingInterface().NETWORK, 97 "3-Adding "+ dsObject.sendingClientKey +"'s object to "+this.getClientKey() +"'s xmt buffer"); 98 tdt.addData(dsObject); 99 } 100 } 101 102 105 protected void xmitData(DataShareObject dsObject) 106 { 107 if(running) 108 { 109 try{ 110 byte[] theseBytes = SessionUtilities.convertObjectToByteArray(dsObject); 111 sndPacket.setData(theseBytes); 112 sndPacket.setLength(theseBytes.length); 113 socket.send(sndPacket); 114 } 115 catch(IOException ioe) 116 { 117 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 119 SessionUtilities.getLoggingInterface().NETWORK, 120 "Problems (" + ioe + "), closing this connection: " + keyValue); 121 close(); 123 } 124 } 125 } 126 127 130 public int getType() 131 { 132 return ChannelDescription.UDP; 133 } 134 135 138 public void newData(DatagramPacket packet) 139 { 140 if(running) 141 { 142 try{ 143 Object object = SessionUtilities.retrieveObject(packet.getData()); 144 try{ 145 DataShareObject dso = (DataShareObject)object; 146 dri.clientDataReceived(dso, this); 147 } 148 catch(ClassCastException cce) 149 { 150 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 151 SessionUtilities.getLoggingInterface().NETWORK, 152 "Problem: " + cce + " for connection " + keyValue); 153 } 154 } 155 catch(InvalidClassException ice) 156 { 157 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 158 SessionUtilities.getLoggingInterface().NETWORK, 159 "Problem: " + ice + " for connection " + keyValue); 160 } 161 catch(EOFException eofe) 162 { 163 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 164 SessionUtilities.getLoggingInterface().NETWORK, 165 "Problem: " + eofe + " for connection " + keyValue); 166 } 167 catch(Exception e) 168 { 169 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 170 SessionUtilities.getLoggingInterface().NETWORK, 171 "Problem: " + e + " for connection " + keyValue); 172 } 174 } 175 } 176 177 178 public void close() 179 { 180 if(running) 181 { 182 running = false; 183 server.closeSocket(this); 184 tdt.stopThread(); 185 dri.connectionLost(this); 186 } 187 } 188 189 } 190 | Popular Tags |