Skip to content

Commit 6f2eb88

Browse files
implement json protocol for receiving
1 parent 888254d commit 6f2eb88

5 files changed

Lines changed: 64 additions & 42 deletions

File tree

plugins/helix/include/helix/gui_extension_helix.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ namespace hal
3737
class HelixPlugin;
3838
}
3939
namespace hal
40-
{
41-
class Module;
42-
}
43-
namespace hal
4440
{
4541
class Netlist;
4642
}

plugins/helix/include/helix/helix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace hal
5252

5353
Helix();
5454

55-
~Helix() = default;
55+
~Helix();
5656

5757
static Helix *instance();
5858

plugins/helix/src/gui_extension_helix.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
#include "helix/helix.h"
99
#include "helix/plugin_helix.h"
1010

11-
#include <QJsonArray>
12-
#include <QJsonDocument>
13-
#include <QJsonObject>
1411
#include <istream>
12+
#include <qbytearray.h>
13+
#include <qglobal.h>
14+
#include <qjsonarray.h>
15+
#include <qjsondocument.h>
16+
#include <qjsonobject.h>
17+
#include <qjsonvalue.h>
18+
#include <qstring.h>
1519
#include <string>
1620
#include <vector>
1721

plugins/helix/src/helix.cc

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
#include "helix/helix.h"
22

3-
#include "gui/content_manager/content_manager.h"
4-
#include "gui/graph_tab_widget/graph_tab_widget.h"
5-
#include "gui/graph_widget/contexts/graph_context.h"
6-
#include "gui/graph_widget/graph_context_manager.h"
7-
#include "gui/graph_widget/layouters/net_layout_point.h"
83
#include "gui/gui_globals.h"
9-
#include "gui/user_action/action_add_items_to_object.h"
10-
#include "gui/user_action/action_create_object.h"
11-
#include "gui/user_action/user_action_compound.h"
12-
#include "gui/user_action/user_action_object.h"
13-
#include "hal_core/netlist/gate.h"
144
#include "hal_core/netlist/netlist.h"
155
#include "hal_core/utilities/log.h"
166

17-
#include <algorithm>
7+
#include <assert.h>
188
#include <cstring>
199
#include <errno.h>
2010
#include <event2/event.h>
@@ -25,43 +15,45 @@
2515
#include <hiredis/read.h>
2616
#include <mutex>
2717
#include <ostream>
28-
#include <qnamespace.h>
29-
#include <qobjectdefs.h>
30-
#include <qset.h>
18+
#include <qjsondocument.h>
19+
#include <qjsonobject.h>
20+
#include <qjsonvalue.h>
3121
#include <qstring.h>
32-
#include <stdexcept>
33-
#include <unordered_set>
34-
#include <utility>
35-
36-
#define MAX_HOST_SIZE 1024LLU
3722

3823
namespace hal
3924
{
4025
namespace
4126
{
4227
void connect_callback( const redisAsyncContext *ctx, int status )
4328
{
44-
u16 port = 0;
45-
char host[MAX_HOST_SIZE] = { 0 };
46-
4729
if( status != REDIS_OK )
4830
{
4931
log_error( "helix", "could not connect to redis instance (async): {}", ctx->errstr );
5032
return;
5133
}
34+
log_debug( "helix", "connected to redis instance (async)" );
5235
}
5336

5437
void disconnect_callback( const redisAsyncContext *ctx, int status )
5538
{
5639
if( status != REDIS_OK )
5740
{
58-
log_error( "helix", "disconnected from redis instance with error: {}", ctx->errstr );
41+
log_error( "helix", "disconnected from redis instance (async) with error: {}", ctx->errstr );
5942
}
6043
else
6144
{
62-
log_info( "helix", "disconnected from redis instance" );
45+
log_debug( "helix", "disconnected from redis instance (async)" );
6346
}
6447
}
48+
49+
std::string build_subscribe_command( const std::vector<std::string> &channels )
50+
{
51+
std::ostringstream oss;
52+
oss << "";
53+
for( auto &ch : channels )
54+
oss << " " << ch;
55+
return "SUBSCRIBE " + oss.str();
56+
}
6557
} // namespace
6658

6759
namespace helix
@@ -91,12 +83,34 @@ namespace hal
9183
return;
9284
}
9385

94-
if( strcmp( msg->element[0]->str, "message" ) != 0 )
86+
std::string msg_type{ msg->element[0]->str };
87+
std::string channel{ msg->element[1]->str };
88+
89+
if( msg_type != "message" )
9590
{
9691
return;
9792
}
9893

99-
log_info( "helix", "received message {} on channel {}", msg->element[2]->str, msg->element[1]->str );
94+
std::string payload_json_str{ msg->element[2]->str };
95+
96+
QJsonParseError parse_error;
97+
QString payload_json_qstr = QString::fromStdString( payload_json_str );
98+
QJsonDocument payload_json = QJsonDocument::fromJson( payload_json_qstr.toUtf8(), &parse_error );
99+
100+
if( parse_error.error != QJsonParseError::NoError )
101+
{
102+
log_error( "helix", "QJsonDocument::fromJson: {}", parse_error.errorString().toStdString() );
103+
return;
104+
}
105+
106+
assert( payload_json.isObject() );
107+
108+
QJsonObject payload = payload_json.object();
109+
110+
log_info( "helix",
111+
"received command '{}' on channel '{}'",
112+
payload["command"].toString().toStdString(),
113+
channel );
100114
}
101115

102116
void subscriber( const Netlist *netlist,
@@ -110,21 +124,24 @@ namespace hal
110124
return;
111125
}
112126

127+
if( ctx == nullptr )
128+
{
129+
log_error( "helix", "no redis context provided" );
130+
return;
131+
}
132+
113133
if( base == nullptr )
114134
{
115135
log_error( "helix", "no event base provided" );
116136
return;
117137
}
118138

119-
std::ostringstream oss;
120-
oss << "";
121-
for( auto &ch : channels )
122-
oss << " " << ch;
123-
std::string cmd = "SUBSCRIBE " + oss.str();
139+
std::string subscribe_command = build_subscribe_command( channels );
124140

125-
if( redisAsyncCommand( ctx, subscriber_callback, (void *) netlist, cmd.c_str() ) != REDIS_OK )
141+
if( redisAsyncCommand( ctx, subscriber_callback, (void *) netlist, subscribe_command.c_str() )
142+
!= REDIS_OK )
126143
{
127-
log_error( "helix", "TODO" );
144+
log_error( "helix", "async subscribe to channels {} failed", subscribe_command.substr( 10 ) );
128145
return;
129146
}
130147

@@ -159,6 +176,11 @@ namespace hal
159176
m_pctx = nullptr;
160177
}
161178

179+
Helix::~Helix()
180+
{
181+
this->stop();
182+
}
183+
162184
void Helix::start( const Netlist *netlist,
163185
const std::string &host,
164186
const u16 port,

plugins/helix/src/plugin_helix.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace hal
5252
void HelixPlugin::on_unload()
5353
{
5454
delete_extension( m_gui_extension );
55-
// delete m_helix;
55+
delete m_helix;
5656
}
5757

5858
void HelixPlugin::initialize()

0 commit comments

Comments
 (0)