KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > datashare > MultiCastSocket


1 /* ----- BEGIN LICENSE BLOCK -----
2  * Version: MPL 1.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the DataShare server.
15  *
16  * The Initial Developer of the Original Code is
17  * Ball Aerospace & Technologies Corp, Fairborn, Ohio
18  * Portions created by the Initial Developer are Copyright (C) 2001
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s): Charles Wood <cwood@ball.com>
22  *
23  * ----- END LICENSE BLOCK ----- */

24 /* RCS $Id: MultiCastSocket.java,v 1.2 2002/01/29 20:50:17 lizellaman Exp $
25  * $Log: MultiCastSocket.java,v $
26  * Revision 1.2 2002/01/29 20:50:17 lizellaman
27  * Added LoggingInterface, modified the PropertiesInterface implementation
28  *
29  * Revision 1.1.1.1 2001/10/23 13:37:19 lizellaman
30  * initial sourceforge release
31  *
32  */

33
34 package org.datashare;
35
36 import java.net.MulticastSocket JavaDoc;
37 import java.net.DatagramPacket JavaDoc;
38 import java.net.InetAddress JavaDoc;
39
40 import java.io.EOFException JavaDoc;
41 import java.io.InvalidClassException JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import org.datashare.objects.DataShareObject;
45 import org.datashare.objects.ChannelDescription;
46
47 public class MultiCastSocket extends SocketAdapter
48    {
49    MulticastSocket JavaDoc socket;
50    DatagramPacket JavaDoc sndPacket;
51    MulticastSocketServer server;
52    private TransmitDataThread tdt = null;
53    boolean running = true;
54
55    public MultiCastSocket(MulticastSocketServer server, MulticastSocket JavaDoc socket, DatagramPacket JavaDoc packet, DataReceiverInterface dri, InetAddress JavaDoc myIpAddress)
56       {
57       this.server = server;
58       this.socket = socket;
59       this.dri = dri;
60       this.localIP = myIpAddress;
61       // note that socket getAddress returns zeros, packet getAddress returns client's real IP, not Multicast IP
62
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       // create packet for sending, will overwrite byte[] and length prior to sending
72
sndPacket = new DatagramPacket JavaDoc(new byte[1], 1, remoteIP, remotePort);
73
74       // create our thread for buffering and transmiting data
75
tdt = new TransmitDataThread(this);
76       tdt.setName("XmitThread for " + keyValue);
77       try{
78          tdt.setPriority(Thread.currentThread().getPriority() + SessionUtilities.SocketXmtRelativePriority); // make transmitting lower priority, since it has buffering
79
}
80       catch(Exception JavaDoc 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    /**
91     * call this method when data is to be sent over this connection
92     */

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    /**
105     * this is the method that actually sends the data
106     */

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 JavaDoc ioe)
118             {
119             ioe.printStackTrace();
120             // don't close the DatagramSocket because other connections use it
121
SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
122                SessionUtilities.getLoggingInterface().NETWORK,
123                "Closing this connection: " + keyValue);
124             close();
125             }
126          }
127       }
128
129    /**
130     * returns the type for this instance
131     */

132    public int getType()
133       {
134       return ChannelDescription.MULTICAST;
135       }
136
137    /**
138     * called by MulticastSocketServer when new data from this instance's client has been received
139     */

140    public void newData(DatagramPacket JavaDoc packet)
141       {
142       if(running)
143          {
144          try{
145             Object JavaDoc object = SessionUtilities.retrieveObject(packet.getData());
146             try{
147                DataShareObject dso = (DataShareObject)object;
148                dri.clientDataReceived(dso, this);
149                }
150             catch(ClassCastException JavaDoc 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 JavaDoc cnfe)
158             {
159             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
160                SessionUtilities.getLoggingInterface().NETWORK,
161                "Problem: " + cnfe.getMessage() + " for connection " + keyValue);
162             }
163          catch(InvalidClassException JavaDoc ice)
164             {
165             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
166                SessionUtilities.getLoggingInterface().NETWORK,
167                "Problem: " + ice.getMessage() + " for connection " + keyValue);
168             }
169          catch(Exception JavaDoc 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); // doesn't really close our one socket, but removes the Consumer
183
tdt.stopThread();
184          dri.connectionLost(this);
185          }
186       }
187
188    }
189
Popular Tags