-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsoncomm.hpp
114 lines (82 loc) · 3.6 KB
/
jsoncomm.hpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2013-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <luz@plan44.ch>
//
// This file is part of p44utils.
//
// p44utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44utils is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44utils. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef __p44utils__jsoncomm__
#define __p44utils__jsoncomm__
#include "socketcomm.hpp"
#include "jsonobject.hpp"
using namespace std;
namespace p44 {
class JsonComm;
/// generic callback for delivering a received JSON object or an error occurred when receiving JSON
typedef boost::function<void (ErrorPtr aError, JsonObjectPtr aJsonObject)> JSonMessageCB;
/// generic callback for delivering a received Text line
typedef boost::function<void (ErrorPtr aError, string aTextLine)> TextLineCB;
typedef boost::intrusive_ptr<JsonComm> JsonCommPtr;
/// A class providing low level access to the DALI bus
class JsonComm : public SocketComm
{
typedef SocketComm inherited;
JSonMessageCB jsonMessageHandler;
TextLineCB rawMessageHandler;
// Raw message receiving
string textLine;
// JSON parameters
char mEOM; /// the end-of-message character separating JSON messages in the socket data stream
// JSON parsing
struct json_tokener* tokener;
bool ignoreUntilNextEOM;
// JSON sending
string transmitBuffer;
bool closeWhenSent;
public:
JsonComm(MainLoop &aMainLoop = MainLoop::currentMainLoop());
virtual ~JsonComm();
/// set message separator
/// @param aEOM end of message char. Defaults to '\n' (line feed).
void setEndOfMessageChar(char aEOM) { mEOM = aEOM; };
/// install callback for received JSON messages
/// @param aJsonMessageHandler will be called when a JSON message has been received
/// @note setting the JSON message handler will disable raw message processing
void setMessageHandler(JSonMessageCB aJsonMessageHandler);
/// install callback for received raw messages (single line)
/// @param aRawMessageHandler will be called when a complete text line has been received
/// @note setting the raw message handler will disable JSON message processing
void setRawMessageHandler(TextLineCB aRawMessageHandler);
/// send a JSON message
/// @param aJsonObject the JSON that is to be sent
/// @result empty or Error object in case of error sending message
ErrorPtr sendMessage(JsonObjectPtr aJsonObject);
/// send raw text
/// @param aRawBytes bytes to be sent
/// @result empty or Error object in case of error sending raw data
ErrorPtr sendRaw(string &aRawBytes);
/// request closing connection after last message has been sent
void closeAfterSend();
/// clear all callbacks
/// @note this is important because handlers might cause retain cycles when they have smart ptr arguments
virtual void clearCallbacks() { jsonMessageHandler = NoOP; inherited::clearCallbacks(); }
private:
void gotData(ErrorPtr aError);
void canSendData(ErrorPtr aError);
};
} // namespace p44
#endif /* defined(__p44utils__jsoncomm__) */