// Copyright (c) 2009-2012 David Koontz, Dan Peschman, and Logan Barnett
// Please direct any bugs/comments/suggestions to david@koontzfamily.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
///
/// Class for broadcasting messages to subscribers. Clients of the EventRouter register
/// themselves by calling Subscribe and passing in the even they are interested in along with
/// a delegate to be called back when the event is received. Events are published via the
/// Publish method which allows arbitrary data to be passed along with the event to be interpreted
/// by the event receiver. Events do not have to be pre-defined.
///
/// Publish and Subscribe methods also have an optional id parameter which will filter the events
/// sent to just those that match the provided id. Subscribing to an event with no id will result
/// in receiving all events of the specified type.
///
///
/// Example of a simple publish and subscribe class, not using any id filtering.
///
/// Sender.cs
///
///
/// public enum SenderEvent {
/// Test
/// }
///
/// public class Sender {
/// public void Send() {
/// EventRouter.Publish(SenderEvent.Test, "Hello World");
/// }
/// }
///
///
/// Receiver.cs
///
///
/// public class Receiver {
/// public Receiver() {
/// EventRouter.Subscribe(SenderEvent.Test, OnSenderEvent);
/// }
///
/// void OnSenderEvent(EventRouter.Event evt) {
/// if(evt.HasData) {
/// Console.WriteLine("Received event: " + evt.Type + " with data: " + evt.GetData(0));
/// }
/// else {
/// Console.WriteLine("Received event: " + evt.Type + " with no data");
/// }
/// }
/// }
///
///
///
///
/// Example of a publish and subscribe using id's to filter specific messages.
///
/// Sender.cs
///
///
/// public enum SenderEvent {
/// Test
/// }
///
/// public class Sender {
/// public void SendA() {
/// EventRouter.Publish("A", SenderEvent.Test);
/// }
///
/// public void SendB() {
/// EventRouter.Publish("B", SenderEvent.Test);
/// }
/// }
///
///
/// Receiver.cs
///
///
/// public class Receiver {
/// public Receiver() {
/// EventRouter.Subscribe("A", SenderEvent.Test, OnSenderEventA);
/// EventRouter.Subscribe("B", SenderEvent.Test, OnSenderEventB);
/// }
///
/// void OnSenderEventA(EventRouter.Event evt) {
/// Console.WriteLine("Received SenderEvent.Test with id 'A'");
/// }
///
/// void OnSenderEventB(EventRouter.Event evt) {
/// Console.WriteLine("Received SenderEvent.Test with id 'B'");
/// }
/// }
///
///
public class EventRouter {
///
/// Event data class, passed in to the subscriber whenever an event occours.
///
public class Event {
public string Type;
public string Id;
public object[] Data;
public bool HasData {
get { return Data != null && Data.Length > 0; }
}
public T GetData(int index) {
return (T)Data[index];
}
}
public delegate void Handler(Event e);
public static bool LogEvents = false;
static Dictionary> handlers = new Dictionary>();
static Dictionary