interfacing between transports written in different programming languages
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.

117 lines
4.5 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. #ifndef __PEP_TRANSPORT_HH__
  2. #define __PEP_TRANSPORT_HH__
  3. #include <exception>
  4. #include <functional>
  5. #include <pEp/types.hh>
  6. #include <pEp/transport.h>
  7. namespace pEp {
  8. class pEpTransport {
  9. public:
  10. // Types
  11. // -----
  12. struct TransportError : std::runtime_error {
  13. const PEP_transport_status_code tsc;
  14. explicit TransportError(PEP_transport_status_code tsc) :
  15. std::runtime_error("transport error"), tsc(tsc)
  16. {
  17. }
  18. };
  19. struct ConfigError : std::logic_error {
  20. ConfigError() : std::logic_error("config error") {}
  21. };
  22. // Abs. Baseclass for config
  23. class Config {
  24. public:
  25. Config() = default;
  26. virtual ~Config() = 0;
  27. };
  28. // Ctors
  29. // -----
  30. explicit pEpTransport(PEP_transport_id id) : id(id), current_status(PEP_tsc_shut_down) {}
  31. pEpTransport(const pEpTransport&) = delete;
  32. pEpTransport& operator=(const pEpTransport&) = delete;
  33. virtual ~pEpTransport() = default;
  34. // required for compat with the c interface transport.h because of missing polymorphism in c
  35. PEP_transport_id get_id()
  36. {
  37. return id;
  38. }
  39. // Callbacks
  40. // ---------
  41. // the signal_ function register signal handlers
  42. // they must be called while a transport is running, otherwise they
  43. // throw std::logic_error in case the transport is shut down
  44. // TODO: heck: "They" refers to the handlers, not the register functions
  45. // can we call them "register_"
  46. // equivalent of transport.h:
  47. // typedef PEP_STATUS (*signal_statuschange_t)(PEP_transport_id id, PEP_transport_status_code tsc);
  48. virtual void signal_statuschange(
  49. std::function<void(const PEP_transport_id& id, const PEP_transport_status_code& tsc)>
  50. handler) = 0;
  51. // equivalent of transport.h:
  52. // typedef PEP_STATUS (*signal_sendto_result_t)(
  53. // PEP_transport_id id,
  54. // char* message_id,
  55. // char* address,
  56. // PEP_rating rating,
  57. // PEP_transport_status_code tsc);
  58. // TODO: libpEpDatatypes is missing pEpRating
  59. virtual void signal_sendto_result(std::function<void(
  60. const PEP_transport_id& id,
  61. const std::string& message_id,
  62. const std::string& address,
  63. /*pEpRating */
  64. const PEP_transport_status_code& tsc)> handler) = 0;
  65. // Called on every message added to rx-queue
  66. // The message can be fetched using recvnext()
  67. // in case of callback_execution:::PEP_cbe_polling
  68. // This callback is not expected to be used
  69. // equivalent of transport.h:
  70. // typedef PEP_STATUS (
  71. // *signal_incoming_message_t)(PEP_transport_id id, PEP_transport_status_code tsc);
  72. virtual void signal_incoming_message(
  73. std::function<void(const PEP_transport_id& id, const PEP_transport_status_code& tsc)>
  74. handler) = 0;
  75. virtual void configure(const Config& config) = 0;
  76. virtual void startup(const callback_execution& cbe = PEP_cbe_polling) = 0;
  77. virtual void shutdown() = 0;
  78. // non-blocking
  79. // Pushes the msg onto the tx-queue.
  80. // Throws TransportError with tsc tx_queue_overrun if the tx_queue is full.
  81. virtual void sendto(const Message& msg) = 0;
  82. // non-blocking
  83. // pops the next msg off the rx-queue
  84. // Throws TransportError with tsc rx_queue_underrun if there is no message left to be received
  85. // execution modes:
  86. // In case of callback_execution:::PEP_cbe_polling this needs to called repeatedly.
  87. // In case of callback_execution:::PEP_cbe_async this only needs to be called after a
  88. // signal_incoming_message() has been received.
  89. virtual Message recvnext() = 0;
  90. virtual bool shortmsg_supported() = 0;
  91. virtual bool longmsg_supported() = 0;
  92. virtual bool longmsg_formatted_supported() = 0;
  93. virtual PEP_text_format native_text_format() = 0;
  94. protected:
  95. PEP_transport_status_code current_status;
  96. private:
  97. const PEP_transport_id id;
  98. };
  99. } // namespace pEp
  100. #endif // __PEP_TRANSPORT_HH__