restructuring
[lwes-java.git] / src / main / java / org / lwes / listener / DatagramEventListener.java
blob3168fd65345e5d0221fbc70a00ebb6eb66e5cbeb
1 package org.lwes.listener;
3 import java.net.InetAddress;
5 import org.lwes.EventSystemException;
7 /**
8 * This is an event listener that handles UDP packets. Automatically
9 * detects multicast addresses and joins those groups.
11 * Sample code that prints multicast events to stdout:
12 * <pre>
13 * EventHandler myHandler = new EventPrintingHandler();
14 * InetAddress address = InetAddress.getByName("224.0.0.69");
16 * DatagramEventListener listener = new DatagramEventListener();
17 * listener.setAddress(address);
18 * listener.setPort(9191);
19 * listener.addHandler(myHandler);
20 * listener.initialize();
21 * </pre>
23 * @author Michael P. Lum
25 public class DatagramEventListener extends ThreadedEventListener {
26 /* the enqueuer to use to acquire multicast packets */
27 private DatagramEnqueuer enqueuer = new DatagramEnqueuer();
29 /* the dequeuer to use to handle packets */
30 private DatagramDequeuer dequeuer = new DatagramDequeuer();
32 /**
33 * Default constructor.
35 public DatagramEventListener() {
38 /**
39 * Gets the address to use for this listener
40 * @return the address
42 public InetAddress getAddress() {
43 if(enqueuer == null) return null;
44 return enqueuer.getAddress();
47 /**
48 * Sets the address to use for this listener
49 * @param address the address
51 public void setAddress(InetAddress address) {
52 if(enqueuer != null) {
53 enqueuer.setAddress(address);
57 /**
58 * Gets the port to use for this listener
59 * @return the port
61 public int getPort() {
62 if(enqueuer == null) return 0;
63 return enqueuer.getPort();
66 /**
67 * Sets the port to use for this listener
68 * @param port the port
70 public void setPort(int port) {
71 if(enqueuer != null) {
72 enqueuer.setPort(port);
76 /**
77 * Get the interface to use for this listener
78 * @return the interface
80 public InetAddress getInterface() {
81 if(enqueuer == null) return null;
82 return enqueuer.getInterface();
85 /**
86 * Sets the interface to use for this listener
87 * @param iface the interface
89 public void setInterface(InetAddress iface) {
90 if(enqueuer != null) {
91 enqueuer.setInterface(iface);
95 /**
96 * Get the TTL to use for this listener. Only applies to
97 * multicast listeners. Typically this does not need to be changed.
98 * @return the interface
100 public int getTimeToLive() {
101 if(enqueuer == null) return 0;
102 return enqueuer.getTimeToLive();
106 * Sets the TTL to use for this listener. Only applies to multicast listeners.
107 * Typically this does not need to be changed.
108 * @param ttl the TTL value
110 public void setTimeToLive(int ttl) {
111 if(enqueuer != null) {
112 enqueuer.setTimeToLive(ttl);
117 * Returns the max number of threads allowed by the system.
118 * @return the allowed threads
120 public int getMaxThreads() {
121 if(dequeuer == null) return 0;
122 return dequeuer.getMaxThreads();
126 * Sets the max number of threads allowed by the system.
127 * @param threads the max number of threads
129 public void setMaxThreads(int threads) {
130 if(dequeuer != null) {
131 dequeuer.setMaxThreads(threads);
136 * Adds an event handler to this listener. This has a callback that will be invoked
137 * for every event coming through the system.
138 * @param handler the EventHandler to add
140 public void addHandler(EventHandler handler) {
141 if(dequeuer != null) {
142 dequeuer.addHandler(handler);
147 * Removes an event handler from the system. This causes the event handler to no longer
148 * receive events coming through the system.
149 * @param handler the EventHandler to remove
151 public void removeHandler(EventHandler handler) {
152 if(dequeuer != null) {
153 dequeuer.removeHandler(handler);
158 * Initializes the listener.
159 * @exception EventSystemException thrown if there is a problem initializing the listener
161 public void initialize() throws EventSystemException {
162 this.setEnqueuer(enqueuer);
163 this.setDequeuer(dequeuer);
164 super.initialize();