1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.filter; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.io.*; |
6 |
import java.net.*; |
7 |
import java.util.*; |
8 |
import uk.ac.ukc.iscream.core.*; |
9 |
import uk.ac.ukc.iscream.componentmanager.*; |
10 |
import uk.ac.ukc.iscream.filter.*; |
11 |
import uk.ac.ukc.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.14 2001/01/28 05:34:38 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.14 $"; |
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 |
_port = port; |
46 |
_queue = queue; |
47 |
_logger.write(toString(), Logger.SYSINIT, "started"); |
48 |
} |
49 |
|
50 |
//---PUBLIC METHODS--- |
51 |
|
52 |
/** |
53 |
* The main method in the class. Reads and queues XML sent |
54 |
* over UDP. |
55 |
*/ |
56 |
public void run() { |
57 |
|
58 |
// setup a Datagram socket |
59 |
DatagramSocket socket = null; |
60 |
try { |
61 |
socket = new DatagramSocket(_port); |
62 |
} |
63 |
catch (BindException e){ |
64 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
65 |
return; |
66 |
} |
67 |
catch (Exception e){ |
68 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
69 |
return; |
70 |
} |
71 |
|
72 |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
73 |
|
74 |
byte[] buf; |
75 |
|
76 |
// read UDP packets and queue them |
77 |
boolean running = true; |
78 |
while (running){ |
79 |
try { |
80 |
|
81 |
// receive request and put it in the Queue |
82 |
buf = new byte[packetSizeLimit]; |
83 |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
84 |
socket.receive(packet); |
85 |
String xml = new String(packet.getData()); |
86 |
_queue.add(xml); |
87 |
} |
88 |
catch (IOException e) { |
89 |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
90 |
running = false; |
91 |
} |
92 |
} |
93 |
socket.close(); |
94 |
} |
95 |
|
96 |
/** |
97 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
98 |
* method to provide clean logging (every class should have this). |
99 |
* |
100 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
101 |
* to format the toString() |
102 |
* |
103 |
* @return the name of this class and its CVS revision |
104 |
*/ |
105 |
public String toString() { |
106 |
return FormatName.getName( |
107 |
_name, |
108 |
getClass().getName(), |
109 |
REVISION); |
110 |
} |
111 |
|
112 |
//---PRIVATE METHODS--- |
113 |
|
114 |
//---ACCESSOR/MUTATOR METHODS--- |
115 |
|
116 |
//---ATTRIBUTES--- |
117 |
|
118 |
/** |
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 |
|
129 |
/** |
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 |
int _port; |
139 |
|
140 |
/** |
141 |
* The Queue object |
142 |
*/ |
143 |
Queue _queue; |
144 |
|
145 |
//---STATIC ATTRIBUTES--- |
146 |
|
147 |
} |