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/TCPDataHandler.java
Revision: 1.11
Committed: Wed Mar 14 23:25:29 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.10: +7 -7 lines
Log Message:
The whole server package structure has been changed.
Old Package: uk.ac.ukc.iscream.*
New Package: uk.org.iscream.*

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.11 package uk.org.iscream.clientinterface;
3 tdb 1.1
4     //---IMPORTS---
5 tdb 1.11 import uk.org.iscream.util.*;
6     import uk.org.iscream.componentmanager.*;
7     import uk.org.iscream.core.*;
8 tdb 1.1 import java.net.Socket;
9     import java.io.InputStream;
10     import java.io.OutputStream;
11     import java.io.IOException;
12     import java.io.BufferedReader;
13     import java.io.PrintWriter;
14     import java.io.InputStreamReader;
15    
16     /**
17 tdb 1.6 * Acts as a Data Handler to a TCP based client, providing
18     * it with a constant stream of XML data for the hosts the
19     * client has requested.
20 tdb 1.1 *
21 tdb 1.2 * @author $Author: tdb1 $
22 tdb 1.11 * @version $Id: TCPDataHandler.java,v 1.10 2001/03/13 02:19:44 tdb1 Exp $
23 tdb 1.1 */
24 tdb 1.2 class TCPDataHandler extends Thread {
25 tdb 1.1
26     //---FINAL ATTRIBUTES---
27    
28     /**
29     * The current CVS revision of this class
30     */
31 tdb 1.11 public final String REVISION = "$Revision: 1.10 $";
32 tdb 1.1
33     //---STATIC METHODS---
34    
35     //---CONSTRUCTORS---
36 tdb 1.6
37     /**
38     * Construct a new TCPDataHandler with a Socket provided
39     * by the Control Handler.
40     *
41     * @param socket The socket to which the Client has connected
42     * @throws IOException if something goes wrong, the Control Handler can deal with it
43     */
44 tdb 1.4 public TCPDataHandler(Socket socket) throws IOException {
45 tdb 1.10 // set the Thread name
46     setName("clientinterface.TCPDataHandler");
47    
48 tdb 1.1 _socket = socket;
49 tdb 1.4 _queue = new Queue();
50 tdb 1.6 // setup the reader and writer
51 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
52 tdb 1.8 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
53 tdb 1.1 _logger.write(toString(), Logger.SYSINIT, "created");
54     }
55    
56     //---PUBLIC METHODS---
57 tdb 1.6 /**
58     * Main loop for the Data Handler, keeps sending data from
59     * it's local Queue.
60     */
61 tdb 1.2 public void run() {
62     run = true;
63 tdb 1.5 _queueID = _queue.getQueue();
64 tdb 1.6 // run until we get told to stop
65 tdb 1.2 while(run) {
66 tdb 1.6 // get some data, and send some data
67 tdb 1.2 try {
68 tdb 1.5 String xml = (String) _queue.get(_queueID);
69 tdb 1.7 // if we're shutting down and we've been "bumped"
70     // it's likely we'll get a null back, and we don't
71     // want to send that on to the Client
72     if(xml != null) {
73     _socketOut.println(xml);
74     }
75 tdb 1.2 }
76     catch(InvalidQueueException e) {
77     run = false;
78     _logger.write(toString(), Logger.ERROR, "Queue failure: "+e);
79     }
80     }
81 tdb 1.6 // if we get here we've been told to stop
82 tdb 1.1 _logger.write(toString(), Logger.SYSMSG, "Shutting Down");
83 tdb 1.6 // shut down the reader, writer and Socket
84 tdb 1.2 try {
85     _socketOut.close();
86     _socketIn.close();
87     _socket.close();
88     }
89     catch(IOException e) {
90     _logger.write(toString(), Logger.ERROR, "Exception whilst shutting down: "+e);
91     }
92 tdb 1.6 // remove ourselves from the queue
93 tdb 1.5 _queue.removeQueue(_queueID);
94 tdb 1.9 _queue.stopMonitor();
95 tdb 1.2 }
96 tdb 1.6
97     /**
98     * Method to shutdown this Data Handler. All this actually does
99     * is set a flag which the main loop will see and commence shutting
100     * down. This method will return before the main loop stops.
101     */
102 tdb 1.2 public void shutdown() {
103 tdb 1.6 // set the main loop to stop
104 tdb 1.2 run=false;
105 tdb 1.6 // if the main loop is waiting for data it won't notice the
106     // above flag to stop. This bumps it out of the blocked get.
107 tdb 1.5 _queue.releaseQueue(_queueID);
108 tdb 1.1 }
109    
110     /**
111     * Overrides the {@link java.lang.Object#toString() Object.toString()}
112     * method to provide clean logging (every class should have this).
113     *
114 tdb 1.11 * This uses the uk.org.iscream.util.NameFormat class
115 tdb 1.1 * to format the toString()
116     *
117     * @return the name of this class and its CVS revision
118     */
119     public String toString() {
120     return FormatName.getName(
121     _name,
122     getClass().getName(),
123     REVISION);
124     }
125    
126     //---PRIVATE METHODS---
127    
128     //---ACCESSOR/MUTATOR METHODS---
129 tdb 1.6
130     /**
131     * Accessor to our Queue. The Control Handler needs to
132     * get this reference to register us.
133     *
134     * @return a reference to our Queue
135     */
136 tdb 1.4 public Queue getQueue() {
137     return _queue;
138     }
139    
140 tdb 1.1 //---ATTRIBUTES---
141    
142     /**
143     * This is the friendly identifier of the
144     * component this class is running in.
145     * eg, a Filter may be called "filter1",
146     * If this class does not have an owning
147     * component, a name from the configuration
148     * can be placed here. This name could also
149     * be changed to null for utility classes.
150     */
151     private String _name = ClientInterfaceMain.NAME;
152    
153     /**
154     * This holds a reference to the
155     * system logger that is being used.
156     */
157     private Logger _logger = ReferenceManager.getInstance().getLogger();
158    
159     /**
160     * A hook to the inbound data from the socket
161     */
162     private BufferedReader _socketIn;
163    
164     /**
165     * A hook to the outbound stream for the socket
166     */
167     private PrintWriter _socketOut;
168    
169 tdb 1.6 /**
170     * A reference to the Socket connected to the client
171     */
172 tdb 1.1 private Socket _socket;
173 tdb 1.2
174 tdb 1.6 /**
175     * A reference to our Queue
176     */
177 tdb 1.2 private Queue _queue;
178 tdb 1.6
179     /**
180     * The flag that dictates whether we should be running
181     */
182 tdb 1.2 private boolean run;
183 tdb 1.6
184     /**
185     * Our queue number within our Queue
186     */
187 tdb 1.5 private int _queueID;
188 tdb 1.1
189     //---STATIC ATTRIBUTES---
190    
191     }