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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/filter/UDPReader.java (file contents):
Revision 1.2 by tdb, Wed Nov 22 09:32:00 2000 UTC vs.
Revision 1.15 by tdb, Thu Feb 1 00:18:42 2001 UTC

# Line 1 | Line 1
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 < //
16 < //
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$
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;
19 <    }
22 > //---FINAL ATTRIBUTES---
23 >
24 >    /**
25 >     * The current CVS revision of this class
26 >     */
27 >    public final String REVISION = "$Revision$";
28      
29 <    public UDPReader(Filter parent, Logger logger){
30 <        this(4589, parent, logger);
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 <
57 >        
58 >        // setup a Datagram socket
59          DatagramSocket socket = null;
60          try {
61 <            socket = new DatagramSocket(port);
61 >            socket = new DatagramSocket(_port);
62          }
63          catch (BindException e){
64 <            logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+" as this port was already in use.");
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.SYSMSG, "Could not start the UDPReader thread on port "+port+".");
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 <
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];
48                // receive request
83                  DatagramPacket packet = new DatagramPacket(buf, buf.length);
84                  socket.receive(packet);
85 <                FilterThread t = new FilterThread(parent);
86 <                t.run(packet);
85 >                String xml = new String(packet.getData());
86 >                _queue.add(xml);
87              }
88              catch (IOException e) {
89 <                logger.write(this.toString(), Logger.SYSMSG, "The UDPReader thread has been shut down as an exception occured: "+e);
90 <                return;
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 <    Logger logger;
98 <    int port;
99 <    Filter parent;    
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 <    final int packetSizeLimit = 8192;
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines