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.18
Committed: Sun Sep 25 09:57:41 2005 UTC (18 years, 7 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +4 -3 lines
Log Message:
Fix compile problems on j2se 1.5 - our Queue class conflicted with one in
java.util. Also fix an API issue when running the server on Windows - the
println method sends '\r\n' on Windows instead of '\n' on Unix, which
confuses applications such as ihost.

Patch provided by: skel

File Contents

# User Rev Content
1 tdb 1.14 /*
2     * i-scream central monitoring system
3 tdb 1.17 * http://www.i-scream.org
4 tdb 1.14 * Copyright (C) 2000-2002 i-scream
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.13 package uk.org.iscream.cms.server.clientinterface;
23 tdb 1.1
24     //---IMPORTS---
25 tdb 1.16 import uk.org.iscream.cms.util.*;
26 tdb 1.13 import uk.org.iscream.cms.server.componentmanager.*;
27     import uk.org.iscream.cms.server.core.*;
28 tdb 1.1 import java.net.Socket;
29     import java.io.InputStream;
30     import java.io.OutputStream;
31     import java.io.IOException;
32     import java.io.BufferedReader;
33     import java.io.PrintWriter;
34     import java.io.InputStreamReader;
35    
36     /**
37 tdb 1.6 * Acts as a Data Handler to a TCP based client, providing
38     * it with a constant stream of XML data for the hosts the
39     * client has requested.
40 tdb 1.1 *
41 tdb 1.14 * @author $Author: tdb $
42 tdb 1.18 * @version $Id: TCPDataHandler.java,v 1.17 2004/08/01 10:40:48 tdb Exp $
43 tdb 1.1 */
44 tdb 1.2 class TCPDataHandler extends Thread {
45 tdb 1.1
46     //---FINAL ATTRIBUTES---
47    
48     /**
49     * The current CVS revision of this class
50     */
51 tdb 1.18 public final String REVISION = "$Revision: 1.17 $";
52 tdb 1.1
53     //---STATIC METHODS---
54    
55     //---CONSTRUCTORS---
56 tdb 1.6
57     /**
58     * Construct a new TCPDataHandler with a Socket provided
59     * by the Control Handler.
60     *
61     * @param socket The socket to which the Client has connected
62     * @throws IOException if something goes wrong, the Control Handler can deal with it
63     */
64 tdb 1.4 public TCPDataHandler(Socket socket) throws IOException {
65 tdb 1.10 // set the Thread name
66     setName("clientinterface.TCPDataHandler");
67    
68 tdb 1.12 ConfigurationProxy cp = ConfigurationProxy.getInstance();
69     String configName = "ClientInterface";
70     // see if this Queue needs a size limit
71     try {
72     int queueSizeLimit = Integer.parseInt(cp.getProperty(configName, "Queue.SizeLimit"));
73     String queueRemoveAlgorithm = cp.getProperty(configName, "Queue.RemoveAlgorithm");
74     int algorithm = StringUtils.getStringPos(queueRemoveAlgorithm, Queue.algorithms);
75     if(algorithm != -1) {
76     _logger.write(toString(), Logger.DEBUG, "Starting Queue with size limit of "+queueSizeLimit+", using remove algorithm "+queueRemoveAlgorithm);
77     // we have valid values, so lets start it.
78     _queue = new Queue(queueSizeLimit, algorithm);
79     }
80     else {
81     _logger.write(toString(), Logger.WARNING, "Bad Queue Algorithm configuration, not known: "+queueRemoveAlgorithm);
82     // just don't activate a limit
83     _queue = new Queue();
84     }
85    
86     } catch (PropertyNotFoundException e) {
87     _logger.write(toString(), Logger.DEBUG, "Optional config not set: "+e);
88     // just don't activate a limit
89     _queue = new Queue();
90     } catch (NumberFormatException e) {
91     _logger.write(toString(), Logger.WARNING, "Bad Queue SizeLimit configuration: "+e);
92     // just don't activate a limit
93     _queue = new Queue();
94     }
95    
96 tdb 1.1 _socket = socket;
97 tdb 1.6 // setup the reader and writer
98 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
99 tdb 1.8 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
100 tdb 1.1 _logger.write(toString(), Logger.SYSINIT, "created");
101     }
102    
103     //---PUBLIC METHODS---
104 tdb 1.6 /**
105     * Main loop for the Data Handler, keeps sending data from
106     * it's local Queue.
107     */
108 tdb 1.2 public void run() {
109     run = true;
110 tdb 1.5 _queueID = _queue.getQueue();
111 tdb 1.6 // run until we get told to stop
112 tdb 1.2 while(run) {
113 tdb 1.6 // get some data, and send some data
114 tdb 1.2 try {
115 tdb 1.5 String xml = (String) _queue.get(_queueID);
116 tdb 1.7 // if we're shutting down and we've been "bumped"
117     // it's likely we'll get a null back, and we don't
118     // want to send that on to the Client
119     if(xml != null) {
120 tdb 1.18 _socketOut.print(xml + "\n");
121     _socketOut.flush();
122 tdb 1.7 }
123 tdb 1.2 }
124     catch(InvalidQueueException e) {
125     run = false;
126     _logger.write(toString(), Logger.ERROR, "Queue failure: "+e);
127     }
128     }
129 tdb 1.6 // if we get here we've been told to stop
130 tdb 1.1 _logger.write(toString(), Logger.SYSMSG, "Shutting Down");
131 tdb 1.6 // shut down the reader, writer and Socket
132 tdb 1.2 try {
133     _socketOut.close();
134     _socketIn.close();
135     _socket.close();
136     }
137     catch(IOException e) {
138     _logger.write(toString(), Logger.ERROR, "Exception whilst shutting down: "+e);
139     }
140 tdb 1.6 // remove ourselves from the queue
141 tdb 1.5 _queue.removeQueue(_queueID);
142 tdb 1.9 _queue.stopMonitor();
143 tdb 1.2 }
144 tdb 1.6
145     /**
146     * Method to shutdown this Data Handler. All this actually does
147     * is set a flag which the main loop will see and commence shutting
148     * down. This method will return before the main loop stops.
149     */
150 tdb 1.2 public void shutdown() {
151 tdb 1.6 // set the main loop to stop
152 tdb 1.2 run=false;
153 tdb 1.6 // if the main loop is waiting for data it won't notice the
154     // above flag to stop. This bumps it out of the blocked get.
155 tdb 1.5 _queue.releaseQueue(_queueID);
156 tdb 1.1 }
157    
158     /**
159     * Overrides the {@link java.lang.Object#toString() Object.toString()}
160     * method to provide clean logging (every class should have this).
161     *
162 tdb 1.16 * This uses the uk.org.iscream.cms.util.NameFormat class
163 tdb 1.1 * to format the toString()
164     *
165     * @return the name of this class and its CVS revision
166     */
167     public String toString() {
168     return FormatName.getName(
169     _name,
170     getClass().getName(),
171     REVISION);
172     }
173    
174     //---PRIVATE METHODS---
175    
176     //---ACCESSOR/MUTATOR METHODS---
177 tdb 1.6
178     /**
179     * Accessor to our Queue. The Control Handler needs to
180     * get this reference to register us.
181     *
182     * @return a reference to our Queue
183     */
184 tdb 1.4 public Queue getQueue() {
185     return _queue;
186     }
187    
188 tdb 1.1 //---ATTRIBUTES---
189    
190     /**
191     * This is the friendly identifier of the
192     * component this class is running in.
193     * eg, a Filter may be called "filter1",
194     * If this class does not have an owning
195     * component, a name from the configuration
196     * can be placed here. This name could also
197     * be changed to null for utility classes.
198     */
199     private String _name = ClientInterfaceMain.NAME;
200    
201     /**
202     * This holds a reference to the
203     * system logger that is being used.
204     */
205     private Logger _logger = ReferenceManager.getInstance().getLogger();
206    
207     /**
208     * A hook to the inbound data from the socket
209     */
210     private BufferedReader _socketIn;
211    
212     /**
213     * A hook to the outbound stream for the socket
214     */
215     private PrintWriter _socketOut;
216    
217 tdb 1.6 /**
218     * A reference to the Socket connected to the client
219     */
220 tdb 1.1 private Socket _socket;
221 tdb 1.2
222 tdb 1.6 /**
223     * A reference to our Queue
224     */
225 tdb 1.2 private Queue _queue;
226 tdb 1.6
227     /**
228     * The flag that dictates whether we should be running
229     */
230 tdb 1.2 private boolean run;
231 tdb 1.6
232     /**
233     * Our queue number within our Queue
234     */
235 tdb 1.5 private int _queueID;
236 tdb 1.1
237     //---STATIC ATTRIBUTES---
238    
239     }