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.2
Committed: Wed Dec 13 12:57:38 2000 UTC (23 years, 5 months ago) by ajm
Branch: MAIN
Changes since 1.1: +158 -140 lines
Log Message:
now componenterized
also supports new toString standard

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