ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filter/TCPReader.java
Revision: 1.7
Committed: Thu Jan 18 23:15:50 2001 UTC (23 years, 4 months ago) by tdb
Branch: MAIN
Changes since 1.6: +4 -3 lines
Log Message:
Changes to reflect move of Component, ComponentStartException, and the
ReferenceManager from util to componentmanager.

File Contents

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