You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.0KB

  1. #pragma once
  2. #include "rack.hpp"
  3. #include "deque"
  4. using namespace rack;
  5. namespace Torpedo {
  6. //
  7. // Basic shared functionality
  8. //
  9. struct BasePort {
  10. enum States {
  11. STATE_QUIESCENT,
  12. STATE_HEADER,
  13. STATE_BODY,
  14. STATE_TRAILER,
  15. STATE_ABORTING
  16. };
  17. enum Errors {
  18. ERROR_STATE,
  19. ERROR_COUNTER,
  20. ERROR_LENGTH,
  21. ERROR_CHECKSUM
  22. };
  23. unsigned int _checksum = 0;
  24. Module *_module;
  25. unsigned int _portNum;
  26. unsigned int _state = STATE_QUIESCENT;
  27. unsigned int dbg = 0;
  28. BasePort(Module *module, unsigned int portNum) {
  29. _module = module;
  30. _portNum = portNum;
  31. }
  32. void addCheckSum(unsigned int byte, unsigned int counter);
  33. virtual int isBusy(void) {
  34. return (_state != STATE_QUIESCENT);
  35. }
  36. void raiseError(unsigned int errorType);
  37. virtual void error(unsigned int errorType) {};
  38. };
  39. //
  40. // Raw output port functionality. Encapsulating layers 2-5 of the OSI model
  41. //
  42. struct RawOutputPort : BasePort {
  43. std::string _appId;
  44. unsigned int _counter;
  45. std::string _message;
  46. Output *_port;
  47. RawOutputPort(Module *module, unsigned int portNum) : BasePort(module, portNum) {
  48. _port = &(_module->outputs[_portNum]);
  49. }
  50. virtual void abort();
  51. virtual void appId(std::string app) { _appId.assign(app); }
  52. virtual void completed();
  53. virtual void process();
  54. virtual void send(std::string appId, std::string message);
  55. virtual void send(std::string message);
  56. };
  57. //
  58. // Raw input port functionality. Encapsulating layers 2-5 of the OSI model
  59. //
  60. struct RawInputPort : BasePort {
  61. std::string _appId;
  62. unsigned int _counter;
  63. unsigned int _length;
  64. std::string _message;
  65. Input *_port;
  66. RawInputPort(Module *module, unsigned int portNum) : BasePort(module, portNum) {
  67. _port = &(_module->inputs[_portNum]);
  68. }
  69. void process();
  70. virtual void received(std::string appId, std::string message);
  71. };
  72. //
  73. // Basic text sending.
  74. //
  75. struct TextInputPort : RawInputPort {
  76. TextInputPort(Module *module, unsigned int portNum) : RawInputPort(module, portNum) {}
  77. void received(std::string appId, std::string message) override;
  78. virtual void received(std::string message) {}
  79. };
  80. struct TextOutputPort : RawOutputPort {
  81. TextOutputPort(Module *module, unsigned int portNum) : RawOutputPort(module, portNum) {_appId.assign("TEXT");}
  82. };
  83. //
  84. // Queued sending.
  85. //
  86. struct QueuedOutputPort : RawOutputPort {
  87. std::vector<std::string *> _queue;
  88. unsigned int _replace = 0;
  89. unsigned int _size = 0;
  90. QueuedOutputPort(Module *module, unsigned int portNum) : RawOutputPort(module, portNum) {}
  91. virtual ~QueuedOutputPort() { for (auto i : _queue) delete i; }
  92. void abort() override;
  93. int isBusy() override { return (_state != STATE_QUIESCENT) || _queue.size(); }
  94. virtual int isFul() { return _queue.size() >= _size; }
  95. void process() override;
  96. void replace(unsigned int rep) { _replace = rep; }
  97. void send(std::string message) override;
  98. void size(unsigned int s);
  99. };
  100. //
  101. // Addressed Messages.
  102. //
  103. struct MessageOutputPort : QueuedOutputPort {
  104. MessageOutputPort(Module *module, unsigned int portNum) : QueuedOutputPort(module, portNum) {_appId.assign("MESG");}
  105. virtual void send(std::string pluginName, std::string moduleName, std::string message);
  106. };
  107. struct MessageInputPort : RawInputPort {
  108. MessageInputPort(Module *module, unsigned int portNum) : RawInputPort(module, portNum) {}
  109. void received(std::string appId, std::string message) override;
  110. virtual void received(std::string pluginName, std::string moduleName, std::string message) {}
  111. };
  112. //
  113. // Device Patches.
  114. //
  115. struct PatchOutputPort : QueuedOutputPort {
  116. PatchOutputPort(Module *module, unsigned int portNum) : QueuedOutputPort(module, portNum) {_appId.assign("PTCH");}
  117. virtual void send(std::string pluginName, std::string moduleName, json_t *rootJ);
  118. };
  119. struct PatchInputPort : RawInputPort {
  120. PatchInputPort(Module *module, unsigned int portNum) : RawInputPort(module, portNum) {}
  121. void received(std::string appId, std::string message) override;
  122. virtual void received(std::string pluginName, std::string moduleName, json_t *rootJ) {}
  123. };
  124. }