1 24 33 34 package org.datashare; 35 36 import java.net.MulticastSocket ; 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 MultiCastSocket extends SocketAdapter 48 { 49 MulticastSocket socket; 50 DatagramPacket sndPacket; 51 MulticastSocketServer server; 52 private TransmitDataThread tdt = null; 53 boolean running = true; 54 55 public MultiCastSocket(MulticastSocketServer server, MulticastSocket socket, DatagramPacket packet, DataReceiverInterface dri, InetAddress myIpAddress) 56 { 57 this.server = server; 58 this.socket = socket; 59 this.dri = dri; 60 this.localIP = myIpAddress; 61 this.localPort = socket.getLocalPort(); 63 this.remoteIP = packet.getAddress(); 64 this.remotePort = packet.getPort(); 65 this.keyValue = "Socket-Multicast-" + localIP.getHostAddress() + ":" + localPort + 66 "-" + remoteIP.getHostAddress() + ":" + remotePort; 67 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 68 SessionUtilities.getLoggingInterface().NETWORK, 69 "New connection: " + keyValue + " with active set to " + (this.getActive()?"true":"false")); 70 71 sndPacket = new DatagramPacket (new byte[1], 1, remoteIP, remotePort); 73 74 tdt = new TransmitDataThread(this); 76 tdt.setName("XmitThread for " + keyValue); 77 try{ 78 tdt.setPriority(Thread.currentThread().getPriority() + SessionUtilities.SocketXmtRelativePriority); } 80 catch(Exception e) 81 { 82 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 83 SessionUtilities.getLoggingInterface().NETWORK, 84 "Problems setting Xmt Thread priority:"); 85 e.printStackTrace(); 86 } 87 tdt.start(); 88 } 89 90 93 public void sendData(DataShareObject dsObject) 94 { 95 if(running) 96 { 97 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 98 SessionUtilities.getLoggingInterface().NETWORK, 99 "3-Adding "+ dsObject.sendingClientKey +"'s object to "+this.getClientKey() +"'s xmt buffer"); 100 tdt.addData(dsObject); 101 } 102 } 103 104 107 protected void xmitData(DataShareObject dsObject) 108 { 109 if(running) 110 { 111 try{ 112 byte[] theseBytes = SessionUtilities.convertObjectToByteArray(dsObject); 113 sndPacket.setData(theseBytes); 114 sndPacket.setLength(theseBytes.length); 115 socket.send(sndPacket); 116 } 117 catch(IOException ioe) 118 { 119 ioe.printStackTrace(); 120 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG, 122 SessionUtilities.getLoggingInterface().NETWORK, 123 "Closing this connection: " + keyValue); 124 close(); 125 } 126 } 127 } 128 129 132 public int getType() 133 { 134 return ChannelDescription.MULTICAST; 135 } 136 137 140 public void newData(DatagramPacket packet) 141 { 142 if(running) 143 { 144 try{ 145 Object object = SessionUtilities.retrieveObject(packet.getData()); 146 try{ 147 DataShareObject dso = (DataShareObject)object; 148 dri.clientDataReceived(dso, this); 149 } 150 catch(ClassCastException cce) 151 { 152 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 153 SessionUtilities.getLoggingInterface().NETWORK, 154 "Problem: " + cce.getMessage() + " for connection " + keyValue); 155 } 156 } 157 catch(ClassNotFoundException cnfe) 158 { 159 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 160 SessionUtilities.getLoggingInterface().NETWORK, 161 "Problem: " + cnfe.getMessage() + " for connection " + keyValue); 162 } 163 catch(InvalidClassException ice) 164 { 165 SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR, 166 SessionUtilities.getLoggingInterface().NETWORK, 167 "Problem: " + ice.getMessage() + " for connection " + keyValue); 168 } 169 catch(Exception e) 170 { 171 e.printStackTrace(); 172 } 173 } 174 } 175 176 177 public void close() 178 { 179 if(running) 180 { 181 running = false; 182 server.closeSocket(this); tdt.stopThread(); 184 dri.connectionLost(this); 185 } 186 } 187 188 } 189 | Popular Tags |