-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasenode.h
67 lines (60 loc) · 3.51 KB
/
basenode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#include "protocol.h"
#include "types.h"
#include "olink_common.h"
#include "nlohmann/json.hpp"
#include <cstring>
namespace ApiGear { namespace ObjectLink {
/**
* A base class for client and remote nodes.
* Provides a functionality for sending and receiving messages according to object link protocol.
* Translates the received data and data to send according to selected message format.
*/
class OLINK_EXPORT BaseNode: public LoggerBase,
public IProtocolListener,
public IMessageHandler {
public:
/**
* Network layer implementation should deliver this function,
* with which messages are sent through network.
*/
void onWrite(WriteMessageFunc func);
/**
* Use this function to format message and send it through the network.
* It uses the WriteMessageFunc provided by network layer implementation with onWrite(WriteMessageFunc) call.
* @param j The data to send, translated according to chosen network message format before sending.
*/
virtual void emitWrite(const nlohmann::json& j);
/**
* Use to change messages network format.
*/
void setMessageFormat(MessageFormat format);
// Implementation::IMessageHandler
void handleMessage(const std::string& data) override;
// Empty, logging only implementation of IProtocolListener::handleLink, should be overwritten on server side.
void handleLink(const std::string& objectId) override;
// Empty, logging only implementation of IProtocolListener::handleUnlink, should be overwritten on server side.
void handleUnlink(const std::string& objectId) override;
// Empty, logging only implementation of IProtocolListener::handleInvoke, should be overwritten on server side.
void handleInvoke(unsigned int requestId, const std::string& methodId, const nlohmann::json& args) override;
// Empty, logging only implementation of IProtocolListener::handleSetProperty, should be overwritten on server side.
void handleSetProperty(const std::string& propertyId, const nlohmann::json& value) override;
// Empty, logging only implementation of IProtocolListener::handleInit, should be overwritten on client side.
void handleInit(const std::string& objectId, const nlohmann::json& props) override;
// Empty, logging only implementation of IProtocolListener::handleInvokeReply, should be overwritten on client side.
void handleInvokeReply(unsigned int requestId, const std::string& methodId, const nlohmann::json& value) override;
// Empty, logging only implementation of IProtocolListener::handleSignal, should be overwritten on client side.
void handleSignal(const std::string& signalId, const nlohmann::json& args) override;
// Empty, logging only implementation of IProtocolListener::handlePropertyChange, should be overwritten on client side.
void handlePropertyChange(const std::string& propertyId, const nlohmann::json& value) override;
// Empty, logging only implementation of IProtocolListener::handleError, should be overwritten on both client and server side.
void handleError(int msgType, int requestId, const std::string& error) override;
private:
/** Function with which messages are sent through network after translation to chosen network format */
WriteMessageFunc m_writeFunc = nullptr;
/** A message converter, translates messages to and from chosen network format*/
MessageConverter m_converter = MessageFormat::JSON;
/** ObjectLink protocol*/
Protocol m_protocol;
};
} } // ApiGear::ObjectLink