rev version
[lwes-java.git] / src / org / lwes / emitter / UnicastEventEmitter.java
blob4ff33c794348987880d2effdae5b52d21a3bf815
1 package org.lwes.emitter;
3 import org.lwes.Event;
4 import org.lwes.EventFactory;
5 import org.lwes.EventSystemException;
6 import org.lwes.util.Log;
7 import org.lwes.util.NumberCodec;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.net.DatagramPacket;
12 import java.net.DatagramSocket;
13 import java.net.InetAddress;
15 /**
16 * UnicastEventEmitter emits events as unicast datagrams on the network.
18 * @author Michael P. Lum
19 * @author Anthony Molinaro
20 * @since 0.0.1
22 * Example code:
23 * <pre>
24 * UnicastEventEmitter emitter = new UnicastEventEmitter();
25 * emitter.setESFFilePath("/path/to/esf/file");
26 * emitter.setMulticastAddress(InetAddress.getByName("224.0.0.69"));
27 * emitter.setMulticastPort(9191);
28 * emitter.initialize();
30 * Event e = emitter.createEvent("MyEvent", false);
31 * e.setString("key","value");
32 * emitter.emit(e);
33 * </pre>
36 public class UnicastEventEmitter implements EventEmitter {
37 /* an EventFactory */
38 private EventFactory factory = new EventFactory();
40 /* the unicast socket being used */
41 private DatagramSocket socket = null;
43 /* the address */
44 private InetAddress address = null;
46 /* the port */
47 private int port = 9191;
49 /* a lock variable to synchronize events */
50 private Object lock = new Object();
52 /**
53 * Default constructor.
55 public UnicastEventEmitter() {
58 /**
59 * Sets the destination address for this emitter.
61 * @param address the multicast address
64 public void setAddress(InetAddress address) {
65 this.address = address;
68 /**
69 * Gets the address for this emitter.
71 * @return the address
73 public InetAddress getAddress() {
74 return this.address;
77 /**
78 * Sets the destination port for this emitter.
80 * @param port the multicast port
83 public void setPort(int port) {
84 this.port = port;
87 /**
88 * Gets the destination port for this emitter.
90 * @return the multicast port
92 public int getPort() {
93 return this.port;
96 /**
97 * Sets the ESF file used for event validation.
98 * @param esfFilePath the path of the ESF file
100 public void setESFFilePath(String esfFilePath) {
101 if(factory != null) {
102 factory.setESFFilePath(esfFilePath);
107 * Gets the ESF file used for event validation
108 * @return the ESF file path
110 public String getESFFilePath() {
111 if(factory != null) {
112 return factory.getESFFilePath();
113 } else {
114 return null;
119 * Sets an InputStream to be used for event validation.
120 * @param esfInputStream an InputStream used for event validation
122 public void setESFInputStream(InputStream esfInputStream) {
123 if(factory != null) {
124 factory.setESFInputStream(esfInputStream);
129 * Gets the InputStream being used for event validation.
130 * @return the InputStream of the ESF validator
132 public InputStream getESFInputStream() {
133 if(factory != null) {
134 return factory.getESFInputStream();
135 } else {
136 return null;
142 * Initializes the emitter.
144 public void initialize() throws IOException {
145 try {
146 factory.initialize();
147 socket = new DatagramSocket();
148 socket.setReuseAddress(true);
149 } catch(IOException ie) {
150 throw ie;
151 } catch(Exception e) {
152 Log.error("Unable to initialize UnicastEventEmitter", e);
156 public void shutdown() throws IOException {
157 socket.close();
161 * Creates a new event named <tt>eventName</tt>.
162 * @param eventName the name of the event to be created
163 * @return a new Event
164 * @exception EventSystemException if there is a problem creating the event
166 public Event createEvent(String eventName) throws EventSystemException {
167 return createEvent(eventName, true);
171 * Creates a new event named <tt>eventName</tt>.
172 * @param eventName the name of the event to be created
173 * @param validate whether or not to validate the event against the EventTemplateDB
174 * @return a new Event
175 * @exception EventSystemException if there is a problem creating the event
177 public Event createEvent(String eventName, boolean validate) throws EventSystemException {
178 if(factory != null) {
179 return factory.createEvent(eventName, validate);
180 } else {
181 throw new EventSystemException("EventFactory not initialized");
186 * Emits the event to the network.
188 * @param event the event to emit
189 * @exception IOException throws an IOException is there is a network error.
191 public void emit(Event event) throws IOException {
192 byte[] msg = event.serialize();
194 synchronized(lock) {
195 emit(msg);
200 * Emits a byte array to the network.
202 * @param bytes the byte array to emit
203 * @exception IOException throws an IOException if there is a network error.
205 protected void emit(byte[] bytes) throws IOException {
206 emit(bytes, this.address, this.port);
210 * @param bytes the byte array to emit
211 * @param address the address to use
212 * @param port the port to use
213 * @throws IOException throws an IOException if there is a network error
215 protected void emit(byte[] bytes, InetAddress address, int port) throws IOException {
216 /* don't send null bytes */
217 if(bytes == null) return;
219 DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
220 socket.send(dp);
222 if (Log.isLogTrace()) {
223 Log.trace("Sent to network '" +
224 NumberCodec.byteArrayToHexString(dp.getData(), 0, dp.getLength()));