KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > datashare > TcpSocket


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: TcpSocket.java,v 1.3 2002/01/29 20:50:17 lizellaman Exp $
25  * $Log: TcpSocket.java,v $
26  * Revision 1.3 2002/01/29 20:50:17 lizellaman
27  * Added LoggingInterface, modified the PropertiesInterface implementation
28  *
29  * Revision 1.2 2002/01/20 23:26:29 lizellaman
30  * add command line parameter that causes an plain DataShareObject to be sent to 'inactive' TCP connections after X milliseconds on inactivity
31  *
32  * Revision 1.1.1.1 2001/10/23 13:37:18 lizellaman
33  * initial sourceforge release
34  *
35  */

36
37 package org.datashare;
38
39 import java.net.Socket JavaDoc;
40 import java.net.SocketException JavaDoc;
41 import java.io.EOFException JavaDoc;
42 import java.io.InterruptedIOException JavaDoc;
43 import java.io.ObjectInputStream JavaDoc;
44 import java.io.ObjectOutputStream JavaDoc;
45 import java.io.InvalidClassException JavaDoc;
46
47 import org.datashare.objects.ChannelDescription;
48 import org.datashare.objects.DataShareObject;
49
50 /**
51  * @date March 01, 2001
52  */

53 public class TcpSocket extends SocketAdapter implements Runnable JavaDoc
54    {
55    private Socket JavaDoc socket;
56    private int priority;
57    boolean running = true;
58    private ObjectInputStream JavaDoc ois = null;
59    private ObjectOutputStream JavaDoc oos = null;
60    private String JavaDoc myThreadName = "";
61    private Thread JavaDoc myThread = null;
62    private TransmitDataThread tdt = null;
63
64    public TcpSocket(Socket JavaDoc socket, DataReceiverInterface dri, int priority)
65       {
66       this.socket = socket;
67       try{
68          this.socket.setSoTimeout(SessionUtilities.getTCPSocketReadTimeout()); // we may want reads to timeout
69
}
70       catch(SocketException JavaDoc 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 JavaDoc(socket.getInputStream());
84          oos = new ObjectOutputStream JavaDoc(socket.getOutputStream());
85          }
86       catch(Exception JavaDoc 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       // create our thread for buffering and transmiting data
97
tdt = new TransmitDataThread(this);
98       tdt.setName("XmitThread for " + keyValue);
99       try{
100          tdt.setPriority(Thread.currentThread().getPriority() + SessionUtilities.SocketXmtRelativePriority); // make transmitting lower priority, since it has buffering
101
}
102       catch(Exception JavaDoc 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    /**
113     * returns the type socket for this instance
114     */

115    int getType()
116       {
117       return ChannelDescription.TCP;
118       }
119
120    /**
121     * call this method when data is to be sent over this connection, puts it into a queue
122     * so it can be sent from a xmit thread
123     */

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

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 JavaDoc e)
145          {
146          SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().ERROR,
147             SessionUtilities.getLoggingInterface().NETWORK,
148             "Problem in Socket " + this.keyValue + ":");
149 // e.printStackTrace();
150
stopThreadAndCloseSocket();
151          }
152       }
153
154    /**
155     * our thread loops while receiving data from Client
156     */

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 JavaDoc object = ois.readObject(); // blocks until an object is received
171
try{
172                   DataShareObject dso = (DataShareObject)object;
173                   dri.clientDataReceived(dso,this); // send it to whoever registered for these objects
174
Thread.currentThread().yield();
175                   }
176                catch(ClassCastException JavaDoc 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 JavaDoc cnfe)
184                {
185                SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING,
186                   SessionUtilities.getLoggingInterface().NETWORK,
187                   "Problem reading Tcp Stream: "+cnfe.getMessage());
188                }
189             catch(InvalidClassException JavaDoc ice)
190                {
191                SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING,
192                   SessionUtilities.getLoggingInterface().NETWORK,
193                   "Problem reading Tcp Stream: "+ice.getMessage());
194                }
195             catch(InterruptedIOException JavaDoc iioe)
196                {
197                SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
198                   SessionUtilities.getLoggingInterface().NETWORK,
199                   "Tcp socket " + this.keyValue + " timedOut");
200                this.xmitData(new DataShareObject()); // send a packet to keep the connection alive (idle too long!)
201
}
202             }
203          SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
204             SessionUtilities.getLoggingInterface().NETWORK,
205             "Thread " + Thread.currentThread().getName() + " is stopping (scheduled)");
206          }
207       catch(SocketException JavaDoc se)
208          {
209          SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
210             SessionUtilities.getLoggingInterface().NETWORK,
211             "Lost connection: " + keyValue);
212          stopThreadAndCloseSocket();
213          }
214       catch(EOFException JavaDoc eofe)
215          {
216          SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
217             SessionUtilities.getLoggingInterface().NETWORK,
218             "Lost connection: " + keyValue);
219          stopThreadAndCloseSocket();
220          }
221       catch(Exception JavaDoc 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    /**
235     * closes socket and stops this thread
236     */

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 JavaDoc e){}
251          try{
252             if(oos != null)
253                oos.close();
254             }
255          catch(Exception JavaDoc e){}
256          try{
257             if(ois != null)
258                ois.close();
259             }
260          catch(Exception JavaDoc e){}
261          finally
262             {
263             tdt.stopThread();
264             dri.connectionLost(this);
265             }
266          }
267       }
268
269    /**
270     * close socket, stop thread, and notify server
271     */

272    public void close()
273       {
274       stopThreadAndCloseSocket();
275       }
276
277    } //// end of class
278
Popular Tags