KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > datashare > TcpSocketServer


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: TcpSocketServer.java,v 1.3 2002/02/04 13:51:40 lizellaman Exp $
25  * $Log: TcpSocketServer.java,v $
26  * Revision 1.3 2002/02/04 13:51:40 lizellaman
27  * Remove all references to past product names (or)
28  * Add PublicAPI for creating Rendezvous Sessions
29  *
30  * Revision 1.2 2002/01/29 20:50:17 lizellaman
31  * Added LoggingInterface, modified the PropertiesInterface implementation
32  *
33  * Revision 1.1.1.1 2001/10/23 13:37:15 lizellaman
34  * initial sourceforge release
35  *
36  */

37
38 package org.datashare;
39
40 import java.net.ServerSocket JavaDoc;
41 import java.net.Socket JavaDoc;
42 import java.net.BindException JavaDoc;
43 import java.util.Vector JavaDoc;
44
45 /**
46  * One of these is needed for every TCP port that is to used for communicating with Clients.
47  * This class is given a port number, and when Clients establish a connection to that port,
48  * a TCP socket handler is created to communicate over that socket with the Client.
49  * The design of this class is such that if this socket closes (through some type of error),
50  * the socket connections to other clients that have already been established will remain
51  * functional, but no new clients can be added to the connection/channel.
52  * @date March 01, 2001
53  */

54 public class TcpSocketServer extends Thread JavaDoc implements SocketServerInterface
55    {
56    private DataReceiverInterface dri = null; // how we communicate when connections come and go
57
private int port; // our local port number
58
private int priority; // what priority to give the sockets
59
private ServerSocket JavaDoc server = null;
60    public boolean running = true; // true as long as it is desired to handle connections
61
private boolean available = false;
62    private String JavaDoc keyValue; // key value for this TCP server socket, must be unique
63

64    /**
65     * Constructor, creates a server socket on the specified port
66     */

67    public TcpSocketServer(DataReceiverInterface dri, int port, int priority)
68       {
69       boolean stillTrying = true;
70       int count = 0;
71       SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
72          SessionUtilities.getLoggingInterface().NETWORK,
73          "TcpSocketServer created for port " + port);
74       this.dri = dri;
75       this.port = port;
76       this.priority = priority;
77       keyValue = "TCP:"+port;
78       this.setName("DataShare.SocketServer."+keyValue);
79       while(stillTrying && (count++) < 50)
80          {
81          try{
82             server = new ServerSocket JavaDoc(this.port);
83             stillTrying = false;
84             }
85          catch(BindException JavaDoc be)
86             {
87             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().WARNING,
88                SessionUtilities.getLoggingInterface().NETWORK,
89                "TCP Socket appears to be in use, try next one...");
90             // try next port
91
this.port = dri.getNextPort();
92             }
93          catch(Exception JavaDoc e)
94             {
95             e.printStackTrace();
96             dri.lostServerSocket(keyValue);
97             running = false;
98             stillTrying = false;
99             }
100          }
101       }
102
103    /**
104     * return our local port
105     */

106    public int getLocalPort()
107       {
108       return port;
109       }
110
111    /**
112     * returns true if ready to receive connections
113     */

114    public boolean getReady()
115       {
116       return available;
117       }
118
119    /**
120     * returns the unique key value string for this server socket instance
121     */

122    public String JavaDoc getKeyValue()
123       {
124       return keyValue;
125       }
126
127    /**
128     * this method will stop this class from creating sockets and will kill this thread
129     */

130    public void killThread()
131       {
132       running = false;
133       }
134
135    public void run()
136       {
137       SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
138          SessionUtilities.getLoggingInterface().NETWORK,
139          "Setting " + this.keyValue + " to priority " + priority);
140       setPriority(priority);
141       while(running)
142          {
143          try{
144             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
145                SessionUtilities.getLoggingInterface().NETWORK,
146                "waiting for TCP connection on " + port);
147             available = true;
148             Socket JavaDoc socket = server.accept();
149             SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
150                SessionUtilities.getLoggingInterface().CLIENT,
151                "Client connecting to TCP port " + port);
152             // create a thread to handle traffic for new connection, then start it
153
if(socket != null)
154                {
155                TcpSocket newSocket = new TcpSocket(socket, dri, priority);
156                Thread JavaDoc thread = new Thread JavaDoc(newSocket,"DataShare."+newSocket.getKeyValue());
157                thread.start();
158                dri.newConnection(newSocket); // now our new socket is known to the DataReceiverAdapter
159
yield();
160                }
161             }
162          catch(Exception JavaDoc e)
163             {
164             try{
165                server.close();
166                }
167             catch(Exception JavaDoc e2){e2.printStackTrace();}
168             finally
169                {
170                if(running) // true if we called close, and we know it will generate an exception
171
{
172                   dri.lostServerSocket(keyValue);
173                   e.printStackTrace();
174                   running = false;
175                   }
176                }
177             }
178          }
179       SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
180          SessionUtilities.getLoggingInterface().NETWORK,
181          "Thread " + getName() + " has stopped");
182       }
183
184    /**
185     * close the socket, stop any thread, and notify the server
186     */

187    public void close()
188       {
189       SessionUtilities.getLoggingInterface().debugMsg(SessionUtilities.getLoggingInterface().DEBUG,
190          SessionUtilities.getLoggingInterface().NETWORK,
191          "removing socket server " + keyValue);
192       try{
193          running = false;
194          server.close();
195          dri.lostServerSocket(keyValue);
196          }
197       catch(Exception JavaDoc e)
198          {
199          e.printStackTrace();
200          }
201       }
202
203    }
204
Popular Tags