| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Text;
|
|---|
| 4 |
|
|---|
| 5 | using IndianHealthService.BMXNet.Services;
|
|---|
| 6 | using IndianHealthService.BMXNet.WinForm.Model;
|
|---|
| 7 |
|
|---|
| 8 | namespace IndianHealthService.BMXNet.WinForm.Services
|
|---|
| 9 | {
|
|---|
| 10 | internal class EventRegistry
|
|---|
| 11 | {
|
|---|
| 12 |
|
|---|
| 13 | private Log _logger = new NullLog();
|
|---|
| 14 |
|
|---|
| 15 | public Log Logger
|
|---|
| 16 | {
|
|---|
| 17 | get { return _logger; }
|
|---|
| 18 | set { _logger = value; }
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | private Dictionary<String, List<WinSession>> _registry = new Dictionary<string, List<WinSession>>();
|
|---|
| 22 |
|
|---|
| 23 | public Dictionary<String, List<WinSession>> Registry
|
|---|
| 24 | {
|
|---|
| 25 | get { return _registry; }
|
|---|
| 26 | set { _registry = value; }
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | public void Subscribe(String anEvent, WinSession aReceiver)
|
|---|
| 30 | {
|
|---|
| 31 | this.Logger.Log("Event", "Subscribing", aReceiver.ToString() + " subsribed to " + anEvent);
|
|---|
| 32 | List<WinSession> registrations = null;
|
|---|
| 33 |
|
|---|
| 34 | if (!this.Registry.TryGetValue(anEvent, out registrations))
|
|---|
| 35 | {
|
|---|
| 36 | registrations = new List<WinSession>();
|
|---|
| 37 | this.Registry[anEvent] = registrations;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | if (registrations.Contains(aReceiver))
|
|---|
| 41 | {
|
|---|
| 42 | this.Logger.Log("Event", "Subscribing", aReceiver.ToString() + " OVER-subscribed to " + anEvent);
|
|---|
| 43 |
|
|---|
| 44 | }
|
|---|
| 45 | else
|
|---|
| 46 | {
|
|---|
| 47 | registrations.Add(aReceiver);
|
|---|
| 48 | this.Logger.Log("Event", "Subscribing", aReceiver.ToString() + " subscribed to " + anEvent);
|
|---|
| 49 |
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public bool HasSubscribers(string anEvent)
|
|---|
| 54 | {
|
|---|
| 55 | List<WinSession> registrations = null;
|
|---|
| 56 |
|
|---|
| 57 | if (this.Registry.TryGetValue(anEvent, out registrations))
|
|---|
| 58 | {
|
|---|
| 59 | return registrations.Count > 0;
|
|---|
| 60 | }
|
|---|
| 61 | else
|
|---|
| 62 | {
|
|---|
| 63 | return false;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 | public void Unsubscribe(String anEvent, WinSession aReceiver)
|
|---|
| 69 | {
|
|---|
| 70 | List<WinSession> registrations = null;
|
|---|
| 71 |
|
|---|
| 72 | if (this.Registry.TryGetValue(anEvent, out registrations))
|
|---|
| 73 | {
|
|---|
| 74 | if (registrations.Contains(aReceiver))
|
|---|
| 75 | {
|
|---|
| 76 | this.Logger.Log("Event", "Subscription", aReceiver.ToString() + " unsubscribed to " + anEvent);
|
|---|
| 77 | registrations.Remove(aReceiver);
|
|---|
| 78 | }
|
|---|
| 79 | else
|
|---|
| 80 | {
|
|---|
| 81 | this.Logger.Log("Event", "Subscription", aReceiver.ToString() + " UNDER-subscribed to " + anEvent);
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public void TriggerEvent(string anEvent, string aStub)
|
|---|
| 87 | {
|
|---|
| 88 | List<WinSession> registrations = null;
|
|---|
| 89 | this.Logger.Log("BMX WIN", "Local Events", anEvent + " ," + aStub);
|
|---|
| 90 |
|
|---|
| 91 | if (this.Registry.TryGetValue(anEvent, out registrations))
|
|---|
| 92 | {
|
|---|
| 93 | foreach (WinSession each in registrations)
|
|---|
| 94 | {
|
|---|
| 95 | this.Logger.Log("BMX WIN", "Local Events", each.ToString() + " was called for " + anEvent + "::" + aStub);
|
|---|
| 96 | each.IncomingEventCallback(anEvent, aStub);
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|