IQRF Gateway Daemon
UdpMessaging.h
Go to the documentation of this file.
1 
17 #pragma once
18 
20 #include "IDpaMessageForwarding.h"
21 #include "IDpaExclusiveAccess.h"
22 #include "UdpChannel.h"
23 #include "IMessaging.h"
24 #include "TaskQueue.h"
25 #include <string>
26 #include <atomic>
27 
28 class DaemonController;
29 
46 {
47 public:
53  enum class Mode {
55  Service,
57  };
58 
59  UdpMessaging() = delete;
60  UdpMessaging(const std::string& name);
61 
62  virtual ~UdpMessaging();
63 
65  void start() override;
66  void stop() override;
67  void update(const rapidjson::Value& cfg) override;
68  const std::string& getName() const override { return m_name; }
69  void registerMessageHandler(MessageHandlerFunc hndl) override;
70  void unregisterMessageHandler() override;
71  void sendMessage(const ustring& msg) override;
72 
73  // IDpaMessageForwarding overriden methods
74  std::unique_ptr<DpaTransaction> getDpaTransactionForward(DpaTransaction* forwarded) override;
75 
76  // IDpaExclusiveAccess overriden methods
77  void setExclusive(IChannel* chan) override;
78  void resetExclusive() override;
79 
82  void sendDpaMessageToUdp(const DpaMessage& dpaMessage);
83 
85  void setDaemon(IDaemon* d) { m_daemon = d; }
86 
87 private:
90  void getGwIdent(ustring& message);
91 
94  void getGwStatus(ustring& message);
95 
97  int handleMessageFromUdp(const ustring& udpMessage);
98 
102  void encodeMessageUdp(ustring& udpMessage, const ustring& message = ustring());
103 
107  void decodeMessageUdp(const ustring& udpMessage, ustring& message);
108 
109  IDaemon *m_daemon = nullptr;
110  UdpMessagingTransaction* m_operationalTransaction = nullptr;
111 
112  IChannel* m_exclusiveChannel = nullptr;
113  UdpChannel* m_udpChannel = nullptr;
114  IMessaging::MessageHandlerFunc m_messageHandlerFunc;
115  TaskQueue<ustring> *m_toUdpMessageQueue = nullptr;
116 
117  // configuration
118  std::string m_name;
119  unsigned m_remotePort = 55000;
120  unsigned m_localPort = 55300;
121 };
Mode
operational mode
Definition: UdpMessaging.h:53
Create component instances.
Definition: DaemonController.h:97
void sendDpaMessageToUdp(const DpaMessage &dpaMessage)
Send DpaMessage to UDP.
Definition: UdpMessaging.cpp:32
Definition: UdpMessagingTransaction.h:26
void setDaemon(IDaemon *d)
set daemon instance
Definition: UdpMessaging.h:85
void resetExclusive() override
Reset exclusive access.
Definition: UdpMessaging.cpp:68
virtual ~UdpMessaging()
Definition: UdpMessaging.cpp:203
std::function< void(const ustring &)> MessageHandlerFunc
Incoming message handler functional type.
Definition: IMessaging.h:56
std::unique_ptr< DpaTransaction > getDpaTransactionForward(DpaTransaction *forwarded) override
Get DPA transaction forwarding object.
Definition: UdpMessaging.cpp:43
void unregisterMessageHandler() override
Unregister message handler.
Definition: UdpMessaging.cpp:298
IDaemon interface.
Definition: IDaemon.h:31
int handleMessageFromUdp(const ustring &udpMessage)
Definition: main.cpp:49
const std::string & getName() const override
Get name of the instance.
Definition: UdpMessaging.h:68
UDP messaging.
Definition: UdpMessaging.h:45
void encodeMessageUdp(unsigned char command, unsigned char subcommand, const ustring &message, ustring &udpMessage)
Definition: main.cpp:28
IDpaExclusiveAccess interface.
Definition: IDpaExclusiveAccess.h:26
void setExclusive(IChannel *chan) override
Set exclusive access.
Definition: UdpMessaging.cpp:56
IMessaging interface.
Definition: IMessaging.h:29
void stop() override
Stop Messaging instance.
Definition: UdpMessaging.cpp:275
void registerMessageHandler(MessageHandlerFunc hndl) override
Register message handler.
Definition: UdpMessaging.cpp:293
void sendMessage(const ustring &msg) override
send message
Definition: UdpMessaging.cpp:304
UdpMessaging()=delete
void update(const rapidjson::Value &cfg) override
Update configuration.
Definition: UdpMessaging.cpp:285
IDpaMessageForwarding interface.
Definition: IDpaMessageForwarding.h:28
std::basic_string< unsigned char > ustring
Definition: ProtocolBridgeClientService.h:33
std::basic_string< unsigned char > ustring
Definition: IMessaging.h:54
void start() override
IMessaging overriden methods.
Definition: UdpMessaging.cpp:255