1 package org
.lwes
.emitter
;
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
;
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.
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();
25 * Event e = emitter.createEvent("MyEvent", false);
26 * e.setString("key","value");
30 * @author Michael P. Lum
31 * @author Anthony Molinaro
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 */
51 /* a lock variable to synchronize events */
52 private final Object lock
= new Object();
55 * Default constructor.
57 public MulticastEventEmitter() {
61 * Sets the multicast address for this emitter.
63 * @param address the multicast address
65 public void setMulticastAddress(InetAddress address
) {
66 this.address
= address
;
70 * Gets the multicast address for this emitter.
74 public InetAddress
getMulticastAddress() {
79 * Sets the multicast port for this emitter.
81 * @param port the multicast port
83 public void setMulticastPort(int port
) {
88 * Gets the multicast port for this emitter.
90 * @return the multicast port
92 public int getMulticastPort() {
97 * Sets the network interface for this emitter.
99 * @param iface the network interface
101 public void setInterface(InetAddress iface
) {
106 * Gets the network interface for this emitter.
108 * @return the interface address
110 public InetAddress
getInterface() {
115 * Sets the multicast time-to-live for this emitter.
117 * @param ttl the time to live
119 public void setTimeToLive(int ttl
) {
124 * Gets the multicast time-to-live for this emitter.
126 * @return the time to live
128 public int getTimeToLive() {
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();
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();
183 * Initializes the emitter.
185 public void initialize() throws IOException
{
187 socket
= new MulticastSocket();
190 socket
.setInterface(iface
);
193 socket
.setTimeToLive(ttl
);
197 * Shuts down the emitter.
199 public void shutdown() throws IOException
{
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
);
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
) {
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 */
264 /* construct a datagram */
265 DatagramPacket dp
= new DatagramPacket(bytes
, bytes
.length
, address
, port
);