KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > datashare > UdpSocket


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: UdpSocket.java,v 1.2 2002/01/29 20:50:17 lizellaman Exp $
25  * $Log: UdpSocket.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:16 lizellaman
30  * initial sourceforge release
31  *
32  */

33
34 package org.datashare;
35
36 import java.net.DatagramSocket 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 UdpSocket extends SocketAdapter
48    {
49    DatagramSocket JavaDoc socket;
50    DatagramPacket JavaDoc sndPacket;
51    UdpSocketServer server;
52    private TransmitDataThread tdt = null;
53    boolean running = true;
54
55    public UdpSocket(UdpSocketServer server, DatagramSocket 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; // socket.getLocalAddress() does not seem to work, even for bound sockets
61
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       // create packet for sending, will overwrite byte[] and length prior to sending
67
sndPacket = new DatagramPacket JavaDoc(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       // create our thread for buffering and transmiting data
73
tdt = new TransmitDataThread(this);
74       tdt.setName("XmitThread for " + keyValue);
75       try{
76          tdt.setPriority(Thread.currentThread().getPriority() + SessionUtilities.SocketXmtRelativePriority); // make transmitting lower priority, since it has buffering
77
}
78       catch(Exception JavaDoc 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    /**
89     * call this method when data is to be sent over this connection
90     */

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

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

130    public int getType()
131       {
132       return ChannelDescription.UDP;
133       }
134
135    /**
136     * called by UdpServer when new data from this client has been received
137     */

138    public void newData(DatagramPacket JavaDoc packet)
139       {
140       if(running)
141          {
142          try{
143             Object JavaDoc object = SessionUtilities.retrieveObject(packet.getData());
144             try{
145                DataShareObject dso = (DataShareObject)object;
146                dri.clientDataReceived(dso, this);
147                }
148             catch(ClassCastException JavaDoc cce)
149                {
150                SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
151                   SessionUtilities.getLoggingInterface().NETWORK,
152                   "Problem: " + cce + " for connection " + keyValue);
153                }
154             }
155          catch(InvalidClassException JavaDoc ice)
156             {
157             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
158                SessionUtilities.getLoggingInterface().NETWORK,
159                "Problem: " + ice + " for connection " + keyValue);
160             }
161          catch(EOFException JavaDoc eofe)
162             {
163             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
164                SessionUtilities.getLoggingInterface().NETWORK,
165                "Problem: " + eofe + " for connection " + keyValue);
166             }
167          catch(Exception JavaDoc e)
168             {
169             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
170                SessionUtilities.getLoggingInterface().NETWORK,
171                "Problem: " + e + " for connection " + keyValue);
172             //e.printStackTrace();
173
}
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