| 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 |
< |
// This class contains the main method to be run by |
| 14 |
< |
// the filter children. It harvests UDP traffic. |
| 15 |
< |
// |
| 13 |
> |
/** |
| 14 |
> |
* This class contains the main method to be run by |
| 15 |
> |
* the filter children. It harvests UDP traffic. |
| 16 |
> |
* |
| 17 |
> |
* @author $Author$ |
| 18 |
> |
* @version $Id$ |
| 19 |
> |
*/ |
| 20 |
|
public class UDPReader extends Thread{ |
| 21 |
|
|
| 22 |
< |
// It is normal to use this constructor in preference |
| 23 |
< |
// to any other in this class. |
| 24 |
< |
public UDPReader(int port, Filter parent, Logger logger){ |
| 25 |
< |
this.logger = logger; |
| 26 |
< |
this.port = port; |
| 27 |
< |
this.parent = parent; |
| 28 |
< |
} |
| 22 |
> |
//---FINAL ATTRIBUTES--- |
| 23 |
> |
|
| 24 |
> |
/** |
| 25 |
> |
* The current CVS revision of this class |
| 26 |
> |
*/ |
| 27 |
> |
public final String REVISION = "$Revision$"; |
| 28 |
> |
|
| 29 |
> |
/** |
| 30 |
> |
* The maximum size of a packet |
| 31 |
> |
*/ |
| 32 |
> |
private final int packetSizeLimit = 8192; |
| 33 |
|
|
| 34 |
< |
public UDPReader(Filter parent, Logger logger){ |
| 35 |
< |
this(4589, parent, logger); |
| 34 |
> |
//---STATIC METHODS--- |
| 35 |
> |
|
| 36 |
> |
//---CONSTRUCTORS--- |
| 37 |
> |
|
| 38 |
> |
/** |
| 39 |
> |
* It is normal to use this constructor in preference |
| 40 |
> |
* to any other in this class. |
| 41 |
> |
*/ |
| 42 |
> |
public UDPReader(int port, Queue queue){ |
| 43 |
> |
_port = port; |
| 44 |
> |
_queue = queue; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
+ |
//---PUBLIC METHODS--- |
| 48 |
+ |
|
| 49 |
|
public void run() { |
| 50 |
|
|
| 51 |
|
DatagramSocket socket = null; |
| 52 |
|
try { |
| 53 |
< |
socket = new DatagramSocket(port); |
| 53 |
> |
socket = new DatagramSocket(_port); |
| 54 |
|
} |
| 55 |
|
catch (BindException e){ |
| 56 |
< |
logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+port+" as this port was already in use."); |
| 56 |
> |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
| 57 |
|
return; |
| 58 |
|
} |
| 59 |
|
catch (Exception e){ |
| 60 |
< |
logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+port+"."); |
| 60 |
> |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
| 61 |
|
return; |
| 62 |
|
} |
| 63 |
|
|
| 64 |
< |
logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+port); |
| 64 |
> |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
| 65 |
|
|
| 66 |
|
byte[] buf; |
| 67 |
|
|
| 69 |
|
while (running){ |
| 70 |
|
try { |
| 71 |
|
|
| 72 |
< |
// receive request and pass on to the FilterThread. |
| 72 |
> |
// receive request and put it in the Queue |
| 73 |
|
buf = new byte[packetSizeLimit]; |
| 74 |
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
| 75 |
|
socket.receive(packet); |
| 76 |
< |
FilterThread t = new FilterThread(parent, packet); |
| 77 |
< |
t.start(); |
| 53 |
< |
|
| 76 |
> |
String xml = new String(packet.getData()); |
| 77 |
> |
_queue.add(xml); |
| 78 |
|
} |
| 79 |
|
catch (IOException e) { |
| 80 |
< |
logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
| 80 |
> |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
| 81 |
|
return; |
| 82 |
|
} |
| 83 |
|
} |
| 84 |
|
socket.close(); |
| 85 |
|
} |
| 86 |
+ |
|
| 87 |
+ |
/** |
| 88 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 89 |
+ |
* method to provide clean logging (every class should have this). |
| 90 |
+ |
* |
| 91 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 92 |
+ |
* to format the toString() |
| 93 |
+ |
* |
| 94 |
+ |
* @return the name of this class and its CVS revision |
| 95 |
+ |
*/ |
| 96 |
+ |
public String toString() { |
| 97 |
+ |
return FormatName.getName( |
| 98 |
+ |
_name, |
| 99 |
+ |
getClass().getName(), |
| 100 |
+ |
REVISION); |
| 101 |
+ |
} |
| 102 |
+ |
|
| 103 |
+ |
//---PRIVATE METHODS--- |
| 104 |
+ |
|
| 105 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
| 106 |
+ |
|
| 107 |
+ |
//---ATTRIBUTES--- |
| 108 |
+ |
|
| 109 |
+ |
/** |
| 110 |
+ |
* This is the friendly identifier of the |
| 111 |
+ |
* component this class is running in. |
| 112 |
+ |
* eg, a Filter may be called "filter1", |
| 113 |
+ |
* If this class does not have an owning |
| 114 |
+ |
* component, a name from the configuration |
| 115 |
+ |
* can be placed here. This name could also |
| 116 |
+ |
* be changed to null for utility classes. |
| 117 |
+ |
*/ |
| 118 |
+ |
private String _name = FilterMain.NAME; |
| 119 |
+ |
|
| 120 |
+ |
/** |
| 121 |
+ |
* This holds a reference to the |
| 122 |
+ |
* system logger that is being used. |
| 123 |
+ |
*/ |
| 124 |
+ |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 125 |
|
|
| 126 |
+ |
/** |
| 127 |
+ |
* The port that this reader is using |
| 128 |
+ |
*/ |
| 129 |
+ |
int _port; |
| 130 |
|
|
| 131 |
< |
Logger logger; |
| 132 |
< |
int port; |
| 133 |
< |
Filter parent; |
| 131 |
> |
/** |
| 132 |
> |
* The Queue object |
| 133 |
> |
*/ |
| 134 |
> |
Queue _queue; |
| 135 |
|
|
| 136 |
< |
final int packetSizeLimit = 8192; |
| 137 |
< |
|
| 136 |
> |
//---STATIC ATTRIBUTES--- |
| 137 |
> |
|
| 138 |
|
} |