1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.filter; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.io.*; |
6 |
import java.net.*; |
7 |
import java.util.*; |
8 |
import uk.org.iscream.core.*; |
9 |
import uk.org.iscream.componentmanager.*; |
10 |
import uk.org.iscream.filter.*; |
11 |
import uk.org.iscream.util.*; |
12 |
|
13 |
/** |
14 |
* This class contains the main method to be run by |
15 |
* the filterd. It harvests UDP traffic, and queues it. |
16 |
* |
17 |
* @author $Author: tdb1 $ |
18 |
* @version $Id: UDPReader.java,v 1.16 2001/03/13 02:19:46 tdb1 Exp $ |
19 |
*/ |
20 |
public class UDPReader extends Thread{ |
21 |
|
22 |
//---FINAL ATTRIBUTES--- |
23 |
|
24 |
/** |
25 |
* The current CVS revision of this class |
26 |
*/ |
27 |
public final String REVISION = "$Revision: 1.16 $"; |
28 |
|
29 |
/** |
30 |
* The maximum size of a packet |
31 |
*/ |
32 |
private final int packetSizeLimit = 8192; |
33 |
|
34 |
//---STATIC METHODS--- |
35 |
|
36 |
//---CONSTRUCTORS--- |
37 |
|
38 |
/** |
39 |
* 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 |
*/ |
44 |
public UDPReader(int port, Queue queue){ |
45 |
// set the Thread name |
46 |
setName("filter.UDPReader"); |
47 |
|
48 |
_port = port; |
49 |
_queue = queue; |
50 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
51 |
} |
52 |
|
53 |
//---PUBLIC METHODS--- |
54 |
|
55 |
/** |
56 |
* The main method in the class. Reads and queues XML sent |
57 |
* over UDP. |
58 |
*/ |
59 |
public void run() { |
60 |
|
61 |
// setup a Datagram socket |
62 |
DatagramSocket socket = null; |
63 |
try { |
64 |
socket = new DatagramSocket(_port); |
65 |
} |
66 |
catch (BindException e){ |
67 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
68 |
return; |
69 |
} |
70 |
catch (Exception e){ |
71 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
72 |
return; |
73 |
} |
74 |
|
75 |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
76 |
|
77 |
byte[] buf; |
78 |
|
79 |
// read UDP packets and queue them |
80 |
boolean running = true; |
81 |
while (running){ |
82 |
try { |
83 |
|
84 |
// receive request and put it in the Queue |
85 |
buf = new byte[packetSizeLimit]; |
86 |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
87 |
socket.receive(packet); |
88 |
String xml = new String(packet.getData()); |
89 |
_queue.add(xml); |
90 |
} |
91 |
catch (IOException e) { |
92 |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
93 |
running = false; |
94 |
} |
95 |
} |
96 |
socket.close(); |
97 |
} |
98 |
|
99 |
/** |
100 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
101 |
* method to provide clean logging (every class should have this). |
102 |
* |
103 |
* This uses the uk.org.iscream.util.NameFormat class |
104 |
* to format the toString() |
105 |
* |
106 |
* @return the name of this class and its CVS revision |
107 |
*/ |
108 |
public String toString() { |
109 |
return FormatName.getName( |
110 |
_name, |
111 |
getClass().getName(), |
112 |
REVISION); |
113 |
} |
114 |
|
115 |
//---PRIVATE METHODS--- |
116 |
|
117 |
//---ACCESSOR/MUTATOR METHODS--- |
118 |
|
119 |
//---ATTRIBUTES--- |
120 |
|
121 |
/** |
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 |
|
132 |
/** |
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 |
int _port; |
142 |
|
143 |
/** |
144 |
* The Queue object |
145 |
*/ |
146 |
Queue _queue; |
147 |
|
148 |
//---STATIC ATTRIBUTES--- |
149 |
|
150 |
} |