-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pubsubio.cpp
138 lines (113 loc) · 3.11 KB
/
Pubsubio.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Pubsubio.cpp - Pubsub.io client library.
Created by Ian Jørgensen, August 10, 2011.
MIT License
*/
#include "WProgram.h"
#include "Pubsubio.h"
int _id = 0;
typedef void (*EventDelegate)(String data);
EventDelegate _subs[10];
byte _hubAddress[] = { 79, 125, 4, 43 }; // hub.pubsub.io
String _message = "";
Pubsubio::Pubsubio(): _client(_hubAddress, 10547) {
}
Pubsubio::Pubsubio(byte hub[]): _client(hub, 10547) {
}
Pubsubio::Pubsubio(byte hub[], int port) : _client(hub, port) {
}
boolean Pubsubio::connect() {
return connect("/");
}
boolean Pubsubio::connect(String sub) {
if (_client.connect()) {
send("{\"sub\":\"" + sub + "\"}");
return true;
} else {
return false;
}
}
boolean Pubsubio::connected() {
return _client.connected();
}
void Pubsubio::stop() {
_client.stop();
}
void Pubsubio::publish(String body) {
return send("{\"name\":\"publish\", \"doc\":" + body + "}");
}
int Pubsubio::subscribe(String query, EventDelegate delegate) {
_id++;
_subs[_id] = delegate;
send("{\"name\":\"subscribe\", \"query\":" + query + ", \"id\":" + String(_id, DEC) + "}");
return _id;
}
void Pubsubio::unsubscribe(int id) {
// todo remove from hashmap
send("{\"name\":\"unsubscribe\", \"id\":" + String(id, DEC) + "}");
}
void Pubsubio::monitor() {
if(_client.available()) {
char c = _client.read();
int i = c - '0';
if(c == 0x000000) {
_message = "";
return;
}
if(i == -49) {
int id = parseInt(parseProperty("id", _message));
String doc = parseProperty("doc", _message);
doc = doc.substring(0,doc.length() - 1);
EventDelegate delegate = _subs[id];
if (delegate != NULL) {
delegate(doc);
}
_message = "";
return;
}
_message = _message + c;
}
return;
}
int Pubsubio::parseInt(String text) {
char temp[20];
text.toCharArray(temp, 19);
int x = atoi(temp);
if (x == 0 && text != "0")
{
x = -1;
}
return x;
}
String Pubsubio::parseProperty(String property, String data) {
property = "\"" + property + "\"";
int propertyDataStart = data.indexOf(property) + property.length();
char currentCharacter;
do {
propertyDataStart++;
currentCharacter = data.charAt(propertyDataStart);
} while (currentCharacter == ' ' || currentCharacter == ':' || currentCharacter == '\"');
int propertyDataEnd = propertyDataStart;
bool isString = data.charAt(propertyDataStart-1) == '\"';
if (!isString) {
do {
propertyDataEnd++;
currentCharacter = data.charAt(propertyDataEnd);
} while (currentCharacter != ' ' && currentCharacter != ',');
}
else {
char previousCharacter;
currentCharacter = ' ';
do {
propertyDataEnd++;
previousCharacter = currentCharacter;
currentCharacter = data.charAt(propertyDataEnd);
} while (currentCharacter != '"' || previousCharacter == '\\');
}
return data.substring(propertyDataStart, propertyDataEnd).replace("\\\"", "\"");
}
void Pubsubio::send(String body) {
_client.print(0x000000, BYTE);
_client.print(body);
_client.print(0xFFFFFD, BYTE);
}