Skip to content

Commit dc18c99

Browse files
committed
Change startPushDebuggerSocket() to also receive proxy replies
Expects proxy replies to be json.
1 parent e7b738b commit dc18c99

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/Edward/proxy_supervisor.cpp

+24-11
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,23 @@ ProxySupervisor::ProxySupervisor(Channel *duplex, pthread_mutex_t *mutex) {
6060
printf("Started supervisor.\n");
6161
this->channel = duplex;
6262
this->mutex = mutex;
63+
this->proxyResult = nullptr;
6364

6465
pthread_create(&this->threadid, nullptr, readSocket, this);
6566
}
6667

68+
bool isEvent(nlohmann::basic_json<> parsed) {
69+
return parsed.find("topic") != parsed.end();
70+
}
71+
72+
bool isReply(nlohmann::basic_json<> parsed) {
73+
return parsed.find("success") != parsed.end();
74+
}
75+
6776
void ProxySupervisor::startPushDebuggerSocket() {
6877
char _char;
6978
uint32_t buf_idx = 0;
7079
const uint32_t start_size = 1024;
71-
uint32_t current_size = start_size;
7280
char *buffer = (char *)malloc(start_size);
7381

7482
printf("Started listening for events from proxy device.\n");
@@ -89,9 +97,19 @@ void ProxySupervisor::startPushDebuggerSocket() {
8997
// first len argument
9098
buffer[buf_idx] = '\0';
9199
try {
92-
Event *event = parseJSON(buffer);
93-
CallbackHandler::push_event(event);
94-
WARDuino::instance()->debugger->notifyPushedEvent();
100+
nlohmann::basic_json<> parsed = nlohmann::json::parse(buffer);
101+
debug("parseJSON: %s\n", parsed.dump().c_str());
102+
103+
if (isEvent(parsed)) {
104+
CallbackHandler::push_event(new Event(
105+
*parsed.find("topic"), *parsed.find("payload")));
106+
WARDuino::instance()->debugger->notifyPushedEvent();
107+
}
108+
109+
if (isReply(parsed)) {
110+
this->proxyResult = parsed;
111+
}
112+
95113
buf_idx = 0;
96114
} catch (const nlohmann::detail::parse_error &e) {
97115
}
@@ -105,13 +123,8 @@ bool ProxySupervisor::send(
105123
return n == size;
106124
}
107125

108-
char *ProxySupervisor::readReply(short int amount) {
109-
char *buffer = new char[amount + 1];
110-
bzero(buffer, amount + 1);
111-
ssize_t n = this->channel->read(buffer, amount);
112-
if (n > 0) return buffer;
113-
114-
delete[] buffer;
126+
char *ProxySupervisor::readReply() {
127+
// TODO use this->proxyResult
115128
return nullptr;
116129
}
117130

src/Edward/proxy_supervisor.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "../Utils/sockets.h"
88
#include "RFC.h"
9+
#include "nlohmann/json.hpp"
910
#include "pthread.h"
1011
#include "sys/types.h"
1112

@@ -16,6 +17,8 @@ class ProxySupervisor {
1617
pthread_mutex_t *mutex;
1718
std::set<uint32_t> *proxied = new std::set<uint32_t>();
1819

20+
nlohmann::basic_json<> proxyResult;
21+
1922
struct SerializeData *serializeRFC(RFC *callee);
2023
void deserializeRFCResult(RFC *rfc);
2124

@@ -25,7 +28,7 @@ class ProxySupervisor {
2528
void startPushDebuggerSocket();
2629

2730
bool send(void *t_buffer, int t_size);
28-
char *readReply(short int amount = 1024);
31+
char *readReply();
2932

3033
pthread_t getThreadID();
3134

0 commit comments

Comments
 (0)