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

# Content
1 //---PACKAGE DECLARATION---
2 package uk.org.iscream.clientinterface;
3
4 //---IMPORTS---
5 import uk.org.iscream.util.*;
6 import uk.org.iscream.componentmanager.*;
7 import uk.org.iscream.core.*;
8 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 * 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 *
21 * @author $Author: tdb1 $
22 * @version $Id: TCPDataHandler.java,v 1.10 2001/03/13 02:19:44 tdb1 Exp $
23 */
24 class TCPDataHandler extends Thread {
25
26 //---FINAL ATTRIBUTES---
27
28 /**
29 * The current CVS revision of this class
30 */
31 public final String REVISION = "$Revision: 1.10 $";
32
33 //---STATIC METHODS---
34
35 //---CONSTRUCTORS---
36
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 public TCPDataHandler(Socket socket) throws IOException {
45 // set the Thread name
46 setName("clientinterface.TCPDataHandler");
47
48 _socket = socket;
49 _queue = new Queue();
50 // setup the reader and writer
51 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
52 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
53 _logger.write(toString(), Logger.SYSINIT, "created");
54 }
55
56 //---PUBLIC METHODS---
57 /**
58 * Main loop for the Data Handler, keeps sending data from
59 * it's local Queue.
60 */
61 public void run() {
62 run = true;
63 _queueID = _queue.getQueue();
64 // run until we get told to stop
65 while(run) {
66 // get some data, and send some data
67 try {
68 String xml = (String) _queue.get(_queueID);
69 // 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 }
76 catch(InvalidQueueException e) {
77 run = false;
78 _logger.write(toString(), Logger.ERROR, "Queue failure: "+e);
79 }
80 }
81 // if we get here we've been told to stop
82 _logger.write(toString(), Logger.SYSMSG, "Shutting Down");
83 // shut down the reader, writer and Socket
84 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 // remove ourselves from the queue
93 _queue.removeQueue(_queueID);
94 _queue.stopMonitor();
95 }
96
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 public void shutdown() {
103 // set the main loop to stop
104 run=false;
105 // 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 _queue.releaseQueue(_queueID);
108 }
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 * This uses the uk.org.iscream.util.NameFormat class
115 * 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
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 public Queue getQueue() {
137 return _queue;
138 }
139
140 //---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 /**
170 * A reference to the Socket connected to the client
171 */
172 private Socket _socket;
173
174 /**
175 * A reference to our Queue
176 */
177 private Queue _queue;
178
179 /**
180 * The flag that dictates whether we should be running
181 */
182 private boolean run;
183
184 /**
185 * Our queue number within our Queue
186 */
187 private int _queueID;
188
189 //---STATIC ATTRIBUTES---
190
191 }