ControllerBase.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. namespace sccdlib
  4. {
  5. public abstract class ControllerBase
  6. {
  7. protected ObjectManagerBase object_manager;
  8. protected bool done = false;
  9. protected List<string> input_ports = new List<string>();
  10. protected EventQueue input_queue = new EventQueue();
  11. protected List<string> output_ports = new List<string>();
  12. protected List<IOutputListener> output_listeners = new List<IOutputListener>();
  13. public ControllerBase ()
  14. {
  15. }
  16. protected void addInputPort(string port_name)
  17. {
  18. this.input_ports.Add(port_name);
  19. }
  20. protected void addOutputPort(string port_name)
  21. {
  22. this.output_ports.Add(port_name);
  23. }
  24. public void broadcast(Event new_event)
  25. {
  26. this.object_manager.broadcast(new_event);
  27. }
  28. public virtual void start()
  29. {
  30. this.object_manager.start();
  31. }
  32. public virtual void stop()
  33. {
  34. }
  35. public void outputEvent(Event output_event)
  36. {
  37. foreach (IOutputListener listener in this.output_listeners)
  38. {
  39. listener.add(output_event);
  40. }
  41. }
  42. public IOutputListener addOutputListener(string[] ports)
  43. {
  44. IOutputListener listener = this.createOutputListener(ports);
  45. this.output_listeners.Add(listener);
  46. return listener;
  47. }
  48. protected virtual IOutputListener createOutputListener (string[] ports)
  49. {
  50. return new OutputListener(ports);
  51. }
  52. public virtual void addInput(Event input_event, double time_offset = 0.0)
  53. {
  54. if ( input_event.getName() == "" )
  55. throw new InputException("Input event can't have an empty name.");
  56. if ( !this.input_ports.Contains (input_event.getPort()) )
  57. throw new InputException("Input port mismatch.");
  58. this.input_queue.Add(input_event, time_offset);
  59. }
  60. public virtual void addEventList(List<Tuple<Event,double>> event_list)
  61. {
  62. foreach (Tuple<Event,double> event_tuple in event_list)
  63. {
  64. this.addInput (event_tuple.Item1, event_tuple.Item2);
  65. }
  66. }
  67. public ObjectManagerBase getObjectManager ()
  68. {
  69. return this.object_manager;
  70. }
  71. }
  72. }