Added Heartbeat, Startup and Shutdown events
[lwes-java.git] / src / org / lwes / emitter / MulticastEventEmitter.java
blob0f78ed6850d0fe0c49e7e08359fab529e0118632
1 package org.lwes.emitter;
3 import org.lwes.Event;
4 import org.lwes.EventSystemException;
5 import org.lwes.util.Log;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.net.DatagramPacket;
10 import java.net.InetAddress;
11 import java.net.MulticastSocket;
13 /**
14 * MulticastEventEmitter emits events to multicast groups on the network. This is the most common
15 * class used by users of the Light Weight Event System.
16 * <p/>
17 * Example code:
18 * <pre>
19 * MulticastEventEmitter emitter = new MulticastEventEmitter();
20 * emitter.setESFFilePath("/path/to/esf/file");
21 * emitter.setMulticastAddress(InetAddress.getByName("224.0.0.69"));
22 * emitter.setMulticastPort(9191);
23 * emitter.initialize();
24 * <p/>
25 * Event e = emitter.createEvent("MyEvent", false);
26 * e.setString("key","value");
27 * emitter.emit(e);
28 * </pre>
30 * @author Michael P. Lum
31 * @author Anthony Molinaro
32 * @since 0.0.1
34 public class MulticastEventEmitter extends AbstractEventEmitter {
36 /* the actual multicast socket being used */
37 private MulticastSocket socket = null;
39 /* the multicast address */
40 private InetAddress address = null;
42 /* the multicast port */
43 private int port = 9191;
45 /* the multicast interface */
46 private InetAddress iface = null;
48 /* the multicast time-to-live */
49 private int ttl = 31;
51 /* a lock variable to synchronize events */
52 private final Object lock = new Object();
54 /**
55 * Default constructor.
57 public MulticastEventEmitter() {
60 /**
61 * Sets the multicast address for this emitter.
63 * @param address the multicast address
65 public void setMulticastAddress(InetAddress address) {
66 this.address = address;
69 /**
70 * Gets the multicast address for this emitter.
72 * @return the address
74 public InetAddress getMulticastAddress() {
75 return this.address;
78 /**
79 * Sets the multicast port for this emitter.
81 * @param port the multicast port
83 public void setMulticastPort(int port) {
84 this.port = port;
87 /**
88 * Gets the multicast port for this emitter.
90 * @return the multicast port
92 public int getMulticastPort() {
93 return this.port;
96 /**
97 * Sets the network interface for this emitter.
99 * @param iface the network interface
101 public void setInterface(InetAddress iface) {
102 this.iface = iface;
106 * Gets the network interface for this emitter.
108 * @return the interface address
110 public InetAddress getInterface() {
111 return this.iface;
115 * Sets the multicast time-to-live for this emitter.
117 * @param ttl the time to live
119 public void setTimeToLive(int ttl) {
120 this.ttl = ttl;
124 * Gets the multicast time-to-live for this emitter.
126 * @return the time to live
128 public int getTimeToLive() {
129 return this.ttl;
133 * Sets the ESF file used for event validation.
135 * @param esfFilePath the path of the ESF file
137 public void setESFFilePath(String esfFilePath) {
138 if (getFactory() != null) {
139 getFactory().setESFFilePath(esfFilePath);
144 * Gets the ESF file used for event validation
146 * @return the ESF file path
148 public String getESFFilePath() {
149 if (getFactory() != null) {
150 return getFactory().getESFFilePath();
152 else {
153 return null;
158 * Sets an InputStream to be used for event validation.
160 * @param esfInputStream an InputStream used for event validation
162 public void setESFInputStream(InputStream esfInputStream) {
163 if (getFactory() != null) {
164 getFactory().setESFInputStream(esfInputStream);
169 * Gets the InputStream being used for event validation.
171 * @return the InputStream of the ESF validator
173 public InputStream getESFInputStream() {
174 if (getFactory() != null) {
175 return getFactory().getESFInputStream();
177 else {
178 return null;
183 * Initializes the emitter.
185 public void initialize() throws IOException {
186 super.initialize();
187 socket = new MulticastSocket();
189 if (iface != null) {
190 socket.setInterface(iface);
193 socket.setTimeToLive(ttl);
197 * Shuts down the emitter.
199 public void shutdown() throws IOException {
200 socket.close();
201 super.shutdown();
205 * Creates a new event named <tt>eventName</tt>.
207 * @param eventName the name of the event to be created
208 * @return a new Event
209 * @throws EventSystemException if there is a problem creating the event
211 public Event createEvent(String eventName) throws EventSystemException {
212 return createEvent(eventName, true);
216 * Creates a new event named <tt>eventName</tt>.
218 * @param eventName the name of the event to be created
219 * @param validate whether or not to validate the event against the EventTemplateDB
220 * @return a new Event
221 * @throws EventSystemException if there is a problem creating the event
223 public Event createEvent(String eventName, boolean validate) throws EventSystemException {
224 if (getFactory() != null) {
225 return getFactory().createEvent(eventName, validate);
227 else {
228 throw new EventSystemException("EventFactory not initialized");
233 * Emits the event to the network.
235 * @param event the event to emit
236 * @throws IOException throws an IOException is there is a network error.
238 public void emit(Event event) throws IOException {
239 byte[] msg = event.serialize();
241 synchronized (lock) {
242 emit(msg);
243 try {
244 collectStatistics();
246 catch (EventSystemException e) {
247 Log.error(e.getMessage(), e);
253 * Emits a byte array to the network.
255 * @param bytes the byte array to emit
256 * @throws IOException throws an IOException if there is a network error.
258 protected void emit(byte[] bytes) throws IOException {
259 /* don't bother with empty arrays */
260 if (bytes == null) {
261 return;
264 /* construct a datagram */
265 DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
266 socket.send(dp);