ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/clientinterface/TCPClientListener.java
Revision: 1.9
Committed: Wed Feb 21 19:11:28 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.8: +11 -5 lines
Log Message:
Modification to the Queue system;
- interval of monitor is now configurable.
- attempt to identify each Queue better, although this is still hard.

File Contents

# User Rev Content
1 ajm 1.2 //---PACKAGE DECLARATION---
2     package uk.ac.ukc.iscream.clientinterface;
3    
4     //---IMPORTS---
5     import uk.ac.ukc.iscream.util.*;
6     import uk.ac.ukc.iscream.core.*;
7 tdb 1.3 import uk.ac.ukc.iscream.componentmanager.*;
8 ajm 1.2 import java.io.IOException;
9     import java.net.Socket;
10     import java.net.ServerSocket;
11     import java.net.InetAddress;
12     import java.net.UnknownHostException;
13    
14     /**
15 tdb 1.8 * Listener for incoming TCP clients, and startup a Handler
16     * a connection is received.
17 ajm 1.2 *
18 tdb 1.4 * @author $Author: tdb1 $
19 tdb 1.9 * @version $Id: TCPClientListener.java,v 1.8 2001/01/27 23:30:40 tdb1 Exp $
20 ajm 1.2 */
21 tdb 1.5 class TCPClientListener extends Thread {
22 ajm 1.2
23     //---FINAL ATTRIBUTES---
24    
25     /**
26     * The current CVS revision of this class
27     */
28 tdb 1.9 public final String REVISION = "$Revision: 1.8 $";
29 ajm 1.2
30     //---STATIC METHODS---
31    
32     //---CONSTRUCTORS---
33    
34     /**
35 tdb 1.5 * Constructs a new TCPClientListener
36 ajm 1.2 *
37     * @param port The port that the server will listen on.
38 tdb 1.6 * @param queue A Queue to utilise
39 tdb 1.9 * @param queueMonitorInterval The interval at which to monitor Queues
40 ajm 1.2 */
41 tdb 1.9 public TCPClientListener(int port, PacketSorter packetSorter, int queueMonitorInterval) {
42 ajm 1.2 _port = port;
43 tdb 1.8 _packetSorter = packetSorter;
44 tdb 1.9 _queueMonitorInterval = queueMonitorInterval;
45 ajm 1.2 _logger.write(toString(), Logger.SYSINIT, "started");
46     }
47    
48     //---PUBLIC METHODS---
49    
50     /**
51     * The run() method is the main loop for this thread, and we
52     * will remain in here until such a point as something goes
53     * wrong with the listening. After initially setting up the
54     * ServerSocket we go round a while loop receiving connections
55 tdb 1.8 * and then passing them off to handler processes to deal with.
56 ajm 1.2 */
57     public void run(){
58     ServerSocket listenPort=null;
59     // We use this boolean so we can break out of the while loop if we want
60     boolean run = true;
61     try{
62     // Setup the ServerSocket so that clients can connect
63     listenPort = new ServerSocket(_port);
64     }
65     catch(IOException e){
66     }
67     // Log what machine/port we're listening on
68     try{
69     _logger.write(toString(), Logger.SYSMSG, "ClientListener listening on "
70     +InetAddress.getLocalHost().getHostName()
71     +"/"+InetAddress.getLocalHost().getHostAddress()
72     +" port "+listenPort.getLocalPort());
73     }
74     catch(UnknownHostException e){
75     _logger.write(toString(), Logger.SYSMSG, "ClientListener listening on UnknownHost "
76     +"port "+listenPort.getLocalPort());
77     }
78     // Loop round constantly until we decide to stop
79     while(run){
80     Socket hostSocket=null;
81     try{
82     _logger.write(toString(), Logger.SYSMSG, "Waiting for Connection");
83     // This will block until a host connects - at which point we get a Socket
84     hostSocket = listenPort.accept();
85     _logger.write(toString(), Logger.SYSMSG, "Connection accepted from: " + hostSocket.toString());
86     }
87     catch(IOException e){
88     // Something went wrong with the ServerSocket, so we'll stop listening
89     run=false;
90     }
91     // If we've stopped on the line above we won't want to try this !
92     if(run){
93     try {
94 tdb 1.8 // start up a ControlHandler to chat to the new Client
95 tdb 1.9 TCPControlHandler ctl = new TCPControlHandler(hostSocket, _packetSorter, _queueMonitorInterval);
96 tdb 1.4 ctl.start();
97 ajm 1.2 } catch (IOException e) {
98     _logger.write(toString(), Logger.ERROR, e.toString());
99     }
100     }
101     }
102     // Best log the fact that we're stopping
103     _logger.write(toString(), Logger.FATAL, "Fatal error, shutdown pending");
104     }
105    
106     /**
107     * Overrides the {@link java.lang.Object#toString() Object.toString()}
108     * method to provide clean logging (every class should have this).
109     *
110     * This uses the uk.ac.ukc.iscream.util.NameFormat class
111     * to format the toString()
112     *
113     * @return the name of this class and its CVS revision
114     */
115     public String toString() {
116     return FormatName.getName(
117     _name,
118     getClass().getName(),
119     REVISION);
120     }
121    
122     //---PRIVATE METHODS---
123    
124     //---ACCESSOR/MUTATOR METHODS---
125    
126     //---ATTRIBUTES---
127    
128     /**
129     * This is the friendly identifier of the
130     * component this class is running in.
131     * eg, a Filter may be called "filter1",
132     * If this class does not have an owning
133     * component, a name from the configuration
134     * can be placed here. This name could also
135     * be changed to null for utility classes.
136     */
137     private String _name = ClientInterfaceMain.NAME;
138    
139     /**
140     * This holds a reference to the
141     * system logger that is being used.
142     */
143     private Logger _logger = ReferenceManager.getInstance().getLogger();
144    
145     /**
146     * The port on which the server should listen.
147     */
148     private int _port;
149    
150     /**
151 tdb 1.7 * A reference to the PacketSorter.
152 ajm 1.2 */
153 tdb 1.8 private PacketSorter _packetSorter;
154 tdb 1.9
155     /**
156     * The interval at which to monitor Queues.
157     */
158     private int _queueMonitorInterval;
159 ajm 1.2 //---STATIC ATTRIBUTES---
160    
161     }