PicoScenes API Docs
 
Loading...
Searching...
No Matches
PicoScenesMessagingService.hxx
Go to the documentation of this file.
1//
2// Created by Zhiping Jiang on 7/17/22.
3//
4
5#ifndef PICOSCENES_PLATFORM_PICOSCENESMESSAGINGSERVICE_HXX
6#define PICOSCENES_PLATFORM_PICOSCENESMESSAGINGSERVICE_HXX
7
8#include <any>
9#include "BoostHeaders.hxx"
10#include "NICPortal.hxx"
11
20using UniversalMessageType = std::tuple<std::any, std::string, std::string, std::string>;
21
31public:
37 static std::shared_ptr<PicoScenesMessagingService> getInstance(const std::string &messagingChannelId = "default");
38
47 static UniversalMessageType makeMessage(const std::any &object, const std::string &destination, const std::string &source, const std::string &description);
48
55
61
69 void send(const ModularPicoScenesRxFrame &rxFrame, const std::string &destination, const std::string &source, const std::string &description = "");
70
78 void send(const ModularPicoScenesTxFrame &txFrame, const std::string &destination, const std::string &source, const std::string &description = "");
79
87 void send(const std::string &textContent, const std::string &destination, const std::string &source, const std::string &description = "");
88
96 void send(const pt::ptree &ptree, const std::string &destination, const std::string &source, const std::string &description = "");
97
105 void send(const U8Vector &data, const std::string &destination, const std::string &source, const std::string &description = "");
106
115 void send(const uint8_t *data, size_t bufferLength, const std::string &destination, const std::string &source, const std::string &description = "");
116
123 void registerMessageHandler(const std::string &handlerName, std::function<void(const UniversalMessageType &)> handler);
124
130 void unregisterHandler(const std::string &handlerName);
131
132private:
135
136 bool serviceState = true;
137 std::deque<UniversalMessageType> appendingMessageQueue;
138 std::mutex queueMutex;
139 std::mutex handleMutex;
140 std::deque<UniversalMessageType> messageQueue;
141 std::map<std::string, std::function<void(const UniversalMessageType &)>> handlers;
142
143 std::mutex queueOperationMutex;
144 std::condition_variable queueOperationCV;
145
151 void messagingServiceLoop();
152};
153
154
155#endif //PICOSCENES_PLATFORM_PICOSCENESMESSAGINGSERVICE_HXX
std::tuple< std::any, std::string, std::string, std::string > UniversalMessageType
Universal message type for PicoScenes platform.
Messaging service for PicoScenes platform.
void send(const U8Vector &data, const std::string &destination, const std::string &source, const std::string &description="")
Send binary data message (vector)
void registerMessageHandler(const std::string &handlerName, std::function< void(const UniversalMessageType &)> handler)
Register message handler.
void clearMessageQueue()
Clear all pending messages @thread_safety Thread-safe operation.
void send(const std::string &textContent, const std::string &destination, const std::string &source, const std::string &description="")
Send text message.
void send(const ModularPicoScenesRxFrame &rxFrame, const std::string &destination, const std::string &source, const std::string &description="")
Send Rx frame message.
void send(const uint8_t *data, size_t bufferLength, const std::string &destination, const std::string &source, const std::string &description="")
Send binary data message (raw buffer)
static std::shared_ptr< PicoScenesMessagingService > getInstance(const std::string &messagingChannelId="default")
Get singleton instance for specified channel.
void send(const pt::ptree &ptree, const std::string &destination, const std::string &source, const std::string &description="")
Send property tree message.
void append2MessageQueue(const UniversalMessageType &message)
Add message to queue.
void unregisterHandler(const std::string &handlerName)
Unregister message handler.
static UniversalMessageType makeMessage(const std::any &object, const std::string &destination, const std::string &source, const std::string &description)
Create a universal message.
void send(const ModularPicoScenesTxFrame &txFrame, const std::string &destination, const std::string &source, const std::string &description="")
Send Tx frame message.