Skip to content

Commit

Permalink
Fixed websocketpp and switched to json11
Browse files Browse the repository at this point in the history
  • Loading branch information
gtremper committed Jul 30, 2014
1 parent 17c3223 commit ecc83a8
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 156 deletions.
79 changes: 17 additions & 62 deletions autowiring/AutoNetServer.h
Expand Up @@ -4,14 +4,12 @@
#include "Autowired.h"
#include "AutowiringEvents.h"
#include "TypeRegistry.h"
#include <jzon/Jzon.h>
#include <json11/json11.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include <map>
#include <set>

class Value;

class AutoNetServer:
public CoreThread,
public virtual AutowiringEvents
Expand All @@ -21,8 +19,6 @@ class AutoNetServer:
virtual ~AutoNetServer();

//Types
//NOTE: These types have undetermined template parameters,
//so methods depending on these types must appear in the header.
typedef websocketpp::server<websocketpp::config::asio> server;
typedef server::message_ptr message_ptr;

Expand All @@ -31,18 +27,8 @@ class AutoNetServer:
virtual void OnStop(void) override;

// Server Handler functions
void OnOpen(websocketpp::connection_hdl hdl) {
*this += [this, hdl] {
SendMessage(hdl, "opened");
};
}
void OnClose(websocketpp::connection_hdl hdl) {
*this += [this, hdl] {
SendMessage(hdl, "closed");
this->m_Subscribers.erase(hdl);
};
}

void OnOpen(websocketpp::connection_hdl hdl);
void OnClose(websocketpp::connection_hdl hdl);
void OnMessage(websocketpp::connection_hdl hdl, message_ptr p_message);

/// <summary>
Expand Down Expand Up @@ -83,37 +69,22 @@ class AutoNetServer:
/// </summary>
/// <param name="hdl">Connection pointer on which to send message</param>
/// <param name="pRecipient">Message name in CamelCase</param>
/// <param name="args...">The first argument to be passed to client side event handler</param>
/// <param name="args...">Remaining arguments to be passed to client side event handler</param>
/// <param name="args...">Arguments to be passed to client side event handler</param>
/// <remarks>
/// Client callback with same number of arguments passed here will be called
/// </remarks>
template<typename Arg, typename... Args>
void SendMessage(websocketpp::connection_hdl hdl, const char* p_type, Arg&& arg, Args&&... args){
Jzon::Object msg;
msg.Add("type", p_type);
template<typename... Args>
void SendMessage(websocketpp::connection_hdl hdl, const char* p_type, Args&&... args){
using json11::Json;

Jzon::Array arguments;
bool dummy[] = {
(arguments.Add(arg),false),
(arguments.Add(args),false)...
Json msg = Json::object {
{"type", p_type},
{"args", Json::array{args...}}
};
(void)dummy;

msg.Add("args", arguments);
SendMessage(hdl, msg);
m_Server->send(hdl, msg.dump(), websocketpp::frame::opcode::text);
}

/// <summary>
/// Sends a zero-argument message to specified client.
/// </summary>
void SendMessage(websocketpp::connection_hdl hdl, const char* p_type);

/// <summary>
/// Sends a zero-argument message to specified client.
/// </summary>
void SendMessage(websocketpp::connection_hdl hdl, const Jzon::Object& msg);

/// <summary>
/// Broadcast a message to all subscribers.
/// </summary>
Expand All @@ -129,31 +100,13 @@ class AutoNetServer:
/// Called when a "Subscribe" event is sent from a client
/// </summary>
/// <param name="client">Client that sent event</param>
void HandleSubscribe(websocketpp::connection_hdl hdl) {
m_Subscribers.insert(hdl);

Jzon::Array types;
for (auto type : m_AllTypes) {
types.Add(type.first);
}

SendMessage(hdl, "subscribed", types);
AutoGlobalContext()->BuildCurrentState();

// Send breakpoint message
for (auto breakpoint : m_breakpoints) {
SendMessage(hdl, "breakpoint", breakpoint);
}
}
void HandleSubscribe(websocketpp::connection_hdl hdl);

/// <summary>
/// Called when a "Unsubscribe" event is sent from a client
/// </summary>
/// <param name="client">Client that sent event</param>
void HandleUnsubscribe(websocketpp::connection_hdl hdl) {
this->m_Subscribers.erase(hdl);
SendMessage(hdl, "unsubscribed");
}
void HandleUnsubscribe(websocketpp::connection_hdl hdl);

/// <summary>
/// Called when a "terminateContext" event is sent from a client
Expand Down Expand Up @@ -187,8 +140,10 @@ class AutoNetServer:
void PollThreadUtilization(std::chrono::milliseconds period);


///////////// Member variables /////////////

/*******************************************
* Member variables *
*******************************************/

// Set of all subscribers
std::set<websocketpp::connection_hdl, std::owner_less<websocketpp::connection_hdl>> m_Subscribers;

Expand Down
3 changes: 3 additions & 0 deletions contrib/C++11/boost_functional.h
Expand Up @@ -15,5 +15,8 @@ namespace std {

namespace placeholders {
boost::lambda::placeholder1_type _1;
boost::lambda::placeholder2_type _2;
boost::lambda::placeholder3_type _3;
boost::lambda::placeholder4_type _4;
}
}
3 changes: 1 addition & 2 deletions contrib/CMakeLists.txt
@@ -1,3 +1,2 @@
#add_subdirectory(json11)
add_subdirectory(jzon)
add_subdirectory(json11)
add_subdirectory(C++11)

0 comments on commit ecc83a8

Please sign in to comment.