1 |
tdb |
1.6 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.filter; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
pjm2 |
1.1 |
import java.io.*; |
6 |
|
|
import java.net.*; |
7 |
|
|
import java.util.*; |
8 |
tdb |
1.2 |
import uk.ac.ukc.iscream.core.*; |
9 |
tdb |
1.13 |
import uk.ac.ukc.iscream.componentmanager.*; |
10 |
tdb |
1.2 |
import uk.ac.ukc.iscream.filter.*; |
11 |
ajm |
1.9 |
import uk.ac.ukc.iscream.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.15 |
* @version $Id: UDPReader.java,v 1.14 2001/01/28 05:34:38 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.15 |
public final String REVISION = "$Revision: 1.14 $"; |
28 |
|
|
|
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.8 |
_port = port; |
46 |
tdb |
1.12 |
_queue = queue; |
47 |
tdb |
1.15 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
48 |
pjm2 |
1.1 |
} |
49 |
|
|
|
50 |
tdb |
1.6 |
//---PUBLIC METHODS--- |
51 |
tdb |
1.14 |
|
52 |
|
|
/** |
53 |
|
|
* The main method in the class. Reads and queues XML sent |
54 |
|
|
* over UDP. |
55 |
|
|
*/ |
56 |
pjm2 |
1.1 |
public void run() { |
57 |
tdb |
1.14 |
|
58 |
|
|
// setup a Datagram socket |
59 |
pjm2 |
1.1 |
DatagramSocket socket = null; |
60 |
|
|
try { |
61 |
tdb |
1.8 |
socket = new DatagramSocket(_port); |
62 |
pjm2 |
1.1 |
} |
63 |
|
|
catch (BindException e){ |
64 |
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."); |
65 |
pjm2 |
1.1 |
return; |
66 |
|
|
} |
67 |
|
|
catch (Exception e){ |
68 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
69 |
pjm2 |
1.1 |
return; |
70 |
|
|
} |
71 |
|
|
|
72 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
73 |
tdb |
1.15 |
|
74 |
pjm2 |
1.1 |
byte[] buf; |
75 |
|
|
|
76 |
tdb |
1.14 |
// read UDP packets and queue them |
77 |
pjm2 |
1.1 |
boolean running = true; |
78 |
|
|
while (running){ |
79 |
|
|
try { |
80 |
tdb |
1.15 |
|
81 |
|
|
// receive request and put it in the Queue |
82 |
pjm2 |
1.1 |
buf = new byte[packetSizeLimit]; |
83 |
|
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
84 |
|
|
socket.receive(packet); |
85 |
tdb |
1.12 |
String xml = new String(packet.getData()); |
86 |
|
|
_queue.add(xml); |
87 |
pjm2 |
1.1 |
} |
88 |
|
|
catch (IOException e) { |
89 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
90 |
tdb |
1.14 |
running = false; |
91 |
pjm2 |
1.1 |
} |
92 |
|
|
} |
93 |
|
|
socket.close(); |
94 |
|
|
} |
95 |
tdb |
1.15 |
|
96 |
tdb |
1.6 |
/** |
97 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
98 |
|
|
* method to provide clean logging (every class should have this). |
99 |
|
|
* |
100 |
ajm |
1.11 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
101 |
|
|
* to format the toString() |
102 |
|
|
* |
103 |
tdb |
1.6 |
* @return the name of this class and its CVS revision |
104 |
|
|
*/ |
105 |
|
|
public String toString() { |
106 |
ajm |
1.11 |
return FormatName.getName( |
107 |
|
|
_name, |
108 |
|
|
getClass().getName(), |
109 |
|
|
REVISION); |
110 |
tdb |
1.6 |
} |
111 |
|
|
|
112 |
|
|
//---PRIVATE METHODS--- |
113 |
|
|
|
114 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
115 |
|
|
|
116 |
|
|
//---ATTRIBUTES--- |
117 |
|
|
|
118 |
ajm |
1.11 |
/** |
119 |
|
|
* This is the friendly identifier of the |
120 |
|
|
* component this class is running in. |
121 |
|
|
* eg, a Filter may be called "filter1", |
122 |
|
|
* If this class does not have an owning |
123 |
|
|
* component, a name from the configuration |
124 |
|
|
* can be placed here. This name could also |
125 |
|
|
* be changed to null for utility classes. |
126 |
|
|
*/ |
127 |
|
|
private String _name = FilterMain.NAME; |
128 |
tdb |
1.15 |
|
129 |
ajm |
1.11 |
/** |
130 |
|
|
* This holds a reference to the |
131 |
|
|
* system logger that is being used. |
132 |
|
|
*/ |
133 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* The port that this reader is using |
137 |
|
|
*/ |
138 |
tdb |
1.8 |
int _port; |
139 |
ajm |
1.11 |
|
140 |
|
|
/** |
141 |
tdb |
1.12 |
* The Queue object |
142 |
ajm |
1.11 |
*/ |
143 |
tdb |
1.15 |
Queue _queue; |
144 |
tdb |
1.6 |
|
145 |
|
|
//---STATIC ATTRIBUTES--- |
146 |
|
|
|
147 |
pjm2 |
1.1 |
} |