ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filter/UDPReader.java
Revision: 1.6
Committed: Wed Nov 29 19:26:00 2000 UTC (23 years, 6 months ago) by tdb
Branch: MAIN
Changes since 1.5: +44 -6 lines
Log Message:
Made changes to fit into the new package structure. Also made all classes, namely
the UDPReader and FilterThread, conform to the Template class specification.

File Contents

# User Rev Content
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     import uk.ac.ukc.iscream.filter.*;
10 pjm2 1.1
11 tdb 1.6 /**
12     * This class contains the main method to be run by
13     * the filter children. It harvests UDP traffic.
14     *
15     * @author $Author$
16     * @version $Id$
17     */
18 pjm2 1.1 public class UDPReader extends Thread{
19    
20 tdb 1.6 //---FINAL ATTRIBUTES---
21    
22     /**
23     * The current CVS revision of this class
24     */
25     public final String REVISION = "$Revision: 1.1 $";
26    
27     //---STATIC METHODS---
28    
29     //---CONSTRUCTORS---
30    
31 pjm2 1.1 // It is normal to use this constructor in preference
32     // to any other in this class.
33 tdb 1.2 public UDPReader(int port, Filter parent, Logger logger){
34 pjm2 1.1 this.logger = logger;
35     this.port = port;
36 tdb 1.2 this.parent = parent;
37 pjm2 1.1 }
38    
39 tdb 1.2 public UDPReader(Filter parent, Logger logger){
40     this(4589, parent, logger);
41 pjm2 1.1 }
42    
43 tdb 1.6 //---PUBLIC METHODS---
44    
45 pjm2 1.1 public void run() {
46    
47     DatagramSocket socket = null;
48     try {
49     socket = new DatagramSocket(port);
50     }
51     catch (BindException e){
52 pjm2 1.4 logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+port+" as this port was already in use.");
53 pjm2 1.1 return;
54     }
55     catch (Exception e){
56 pjm2 1.4 logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+port+".");
57 pjm2 1.1 return;
58     }
59    
60     logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+port);
61    
62     byte[] buf;
63    
64     boolean running = true;
65     while (running){
66     try {
67 pjm2 1.3
68     // receive request and pass on to the FilterThread.
69 pjm2 1.1 buf = new byte[packetSizeLimit];
70     DatagramPacket packet = new DatagramPacket(buf, buf.length);
71     socket.receive(packet);
72 pjm2 1.5 FilterThread t = new FilterThread(packet, parent, logger);
73 pjm2 1.3 t.start();
74    
75 pjm2 1.1 }
76     catch (IOException e) {
77 pjm2 1.4 logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e);
78 pjm2 1.1 return;
79     }
80     }
81     socket.close();
82     }
83 tdb 1.6
84     /**
85     * Overrides the {@link java.lang.Object#toString() Object.toString()}
86     * method to provide clean logging (every class should have this).
87     *
88     * @return the name of this class and its CVS revision
89     */
90     public String toString() {
91     return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")";
92     }
93    
94     //---PRIVATE METHODS---
95    
96     //---ACCESSOR/MUTATOR METHODS---
97    
98     //---ATTRIBUTES---
99    
100 pjm2 1.1 Logger logger;
101     int port;
102 tdb 1.2 Filter parent;
103    
104 pjm2 1.1 final int packetSizeLimit = 8192;
105 tdb 1.6
106     //---STATIC ATTRIBUTES---
107    
108 pjm2 1.1 }