1 |
tdb |
1.6 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.18 |
package uk.org.iscream.cms.server.filter; |
3 |
tdb |
1.6 |
|
4 |
|
|
//---IMPORTS--- |
5 |
pjm2 |
1.1 |
import java.io.*; |
6 |
|
|
import java.net.*; |
7 |
|
|
import java.util.*; |
8 |
tdb |
1.18 |
import uk.org.iscream.cms.server.core.*; |
9 |
|
|
import uk.org.iscream.cms.server.componentmanager.*; |
10 |
|
|
import uk.org.iscream.cms.server.filter.*; |
11 |
|
|
import uk.org.iscream.cms.server.util.*; |
12 |
pjm2 |
1.1 |
|
13 |
tdb |
1.6 |
/** |
14 |
|
|
* This class contains the main method to be run by |
15 |
tdb |
1.14 |
* the filterd. It harvests UDP traffic, and queues it. |
16 |
tdb |
1.6 |
* |
17 |
tdb |
1.13 |
* @author $Author: tdb1 $ |
18 |
tdb |
1.18 |
* @version $Id: UDPReader.java,v 1.17 2001/03/14 23:25:29 tdb1 Exp $ |
19 |
tdb |
1.6 |
*/ |
20 |
pjm2 |
1.1 |
public class UDPReader extends Thread{ |
21 |
|
|
|
22 |
tdb |
1.6 |
//---FINAL ATTRIBUTES--- |
23 |
|
|
|
24 |
|
|
/** |
25 |
|
|
* The current CVS revision of this class |
26 |
|
|
*/ |
27 |
tdb |
1.18 |
public final String REVISION = "$Revision: 1.17 $"; |
28 |
tdb |
1.15 |
|
29 |
ajm |
1.11 |
/** |
30 |
|
|
* The maximum size of a packet |
31 |
|
|
*/ |
32 |
|
|
private final int packetSizeLimit = 8192; |
33 |
tdb |
1.6 |
|
34 |
|
|
//---STATIC METHODS--- |
35 |
|
|
|
36 |
|
|
//---CONSTRUCTORS--- |
37 |
|
|
|
38 |
ajm |
1.10 |
/** |
39 |
tdb |
1.14 |
* Constructs a new UDPReader. |
40 |
|
|
* |
41 |
|
|
* @param port The port on which we listen for UDP data |
42 |
|
|
* @param queue The queue which we are using |
43 |
ajm |
1.10 |
*/ |
44 |
tdb |
1.12 |
public UDPReader(int port, Queue queue){ |
45 |
tdb |
1.16 |
// set the Thread name |
46 |
|
|
setName("filter.UDPReader"); |
47 |
|
|
|
48 |
tdb |
1.8 |
_port = port; |
49 |
tdb |
1.12 |
_queue = queue; |
50 |
tdb |
1.15 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
51 |
pjm2 |
1.1 |
} |
52 |
|
|
|
53 |
tdb |
1.6 |
//---PUBLIC METHODS--- |
54 |
tdb |
1.14 |
|
55 |
|
|
/** |
56 |
|
|
* The main method in the class. Reads and queues XML sent |
57 |
|
|
* over UDP. |
58 |
|
|
*/ |
59 |
pjm2 |
1.1 |
public void run() { |
60 |
tdb |
1.14 |
|
61 |
|
|
// setup a Datagram socket |
62 |
pjm2 |
1.1 |
DatagramSocket socket = null; |
63 |
|
|
try { |
64 |
tdb |
1.8 |
socket = new DatagramSocket(_port); |
65 |
pjm2 |
1.1 |
} |
66 |
|
|
catch (BindException e){ |
67 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
68 |
pjm2 |
1.1 |
return; |
69 |
|
|
} |
70 |
|
|
catch (Exception e){ |
71 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
72 |
pjm2 |
1.1 |
return; |
73 |
|
|
} |
74 |
|
|
|
75 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
76 |
tdb |
1.15 |
|
77 |
pjm2 |
1.1 |
byte[] buf; |
78 |
|
|
|
79 |
tdb |
1.14 |
// read UDP packets and queue them |
80 |
pjm2 |
1.1 |
boolean running = true; |
81 |
|
|
while (running){ |
82 |
|
|
try { |
83 |
tdb |
1.15 |
|
84 |
|
|
// receive request and put it in the Queue |
85 |
pjm2 |
1.1 |
buf = new byte[packetSizeLimit]; |
86 |
|
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
87 |
|
|
socket.receive(packet); |
88 |
tdb |
1.12 |
String xml = new String(packet.getData()); |
89 |
|
|
_queue.add(xml); |
90 |
pjm2 |
1.1 |
} |
91 |
|
|
catch (IOException e) { |
92 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
93 |
tdb |
1.14 |
running = false; |
94 |
pjm2 |
1.1 |
} |
95 |
|
|
} |
96 |
|
|
socket.close(); |
97 |
|
|
} |
98 |
tdb |
1.15 |
|
99 |
tdb |
1.6 |
/** |
100 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
101 |
|
|
* method to provide clean logging (every class should have this). |
102 |
|
|
* |
103 |
tdb |
1.18 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
104 |
ajm |
1.11 |
* to format the toString() |
105 |
|
|
* |
106 |
tdb |
1.6 |
* @return the name of this class and its CVS revision |
107 |
|
|
*/ |
108 |
|
|
public String toString() { |
109 |
ajm |
1.11 |
return FormatName.getName( |
110 |
|
|
_name, |
111 |
|
|
getClass().getName(), |
112 |
|
|
REVISION); |
113 |
tdb |
1.6 |
} |
114 |
|
|
|
115 |
|
|
//---PRIVATE METHODS--- |
116 |
|
|
|
117 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
118 |
|
|
|
119 |
|
|
//---ATTRIBUTES--- |
120 |
|
|
|
121 |
ajm |
1.11 |
/** |
122 |
|
|
* This is the friendly identifier of the |
123 |
|
|
* component this class is running in. |
124 |
|
|
* eg, a Filter may be called "filter1", |
125 |
|
|
* If this class does not have an owning |
126 |
|
|
* component, a name from the configuration |
127 |
|
|
* can be placed here. This name could also |
128 |
|
|
* be changed to null for utility classes. |
129 |
|
|
*/ |
130 |
|
|
private String _name = FilterMain.NAME; |
131 |
tdb |
1.15 |
|
132 |
ajm |
1.11 |
/** |
133 |
|
|
* This holds a reference to the |
134 |
|
|
* system logger that is being used. |
135 |
|
|
*/ |
136 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
137 |
|
|
|
138 |
|
|
/** |
139 |
|
|
* The port that this reader is using |
140 |
|
|
*/ |
141 |
tdb |
1.8 |
int _port; |
142 |
ajm |
1.11 |
|
143 |
|
|
/** |
144 |
tdb |
1.12 |
* The Queue object |
145 |
ajm |
1.11 |
*/ |
146 |
tdb |
1.15 |
Queue _queue; |
147 |
tdb |
1.6 |
|
148 |
|
|
//---STATIC ATTRIBUTES--- |
149 |
|
|
|
150 |
pjm2 |
1.1 |
} |