Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove websocket headers from autonet #45

Merged
merged 4 commits into from
Aug 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ set(autowiring_VERSION_MAJOR 0)
set(autowiring_VERSION_MINOR 1)
set(autowiring_VERSION_PATCH 0)

# If there's an external libraries directory path, use it as a search prefix
if(EXTERNAL_LIBRARY_DIR)
list(APPEND CMAKE_PREFIX_PATH ${EXTERNAL_LIBRARY_DIR})
list(APPEND CMAKE_INCLUDE_PATH ${EXTERNAL_LIBRARY_DIR})
endif()

# Clang needs special additional flags to build with C++11
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
message("Clang C++11")
Expand Down
168 changes: 8 additions & 160 deletions autowiring/AutoNetServer.h
Original file line number Diff line number Diff line change
@@ -1,177 +1,25 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include "CoreThread.h"
#include "Autowired.h"
#include "AutowiringEvents.h"
#include <json11/json11.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include <map>
#include <set>

struct TypeIdentifierBase;

class AutoNetServer:
public CoreThread,
public virtual AutowiringEvents
public CoreThread
{
public:
protected:
AutoNetServer();
virtual ~AutoNetServer();

//Types
typedef websocketpp::server<websocketpp::config::asio> server;
typedef server::message_ptr message_ptr;

// Functions from CoreContext
virtual void Run(void) override;
virtual void OnStop(void) override;
public:
virtual ~AutoNetServer();
static AutoNetServer* New(void);

// Server Handler functions
void OnOpen(websocketpp::connection_hdl hdl);
void OnClose(websocketpp::connection_hdl hdl);
void OnMessage(websocketpp::connection_hdl hdl, message_ptr p_message);

/// <summary>
/// Waits until resume message is sent from the visualizer
/// </summary>
/// <param name="name">The identifier for this breakpoint</param>
void Breakpoint(std::string name);
virtual void Breakpoint(std::string name) = 0;

/// <summary>
/// Updates server when a new context is created
/// </summary>
/// <param name="ctxt">The new context</param>
virtual void NewContext(CoreContext& ctxt) override;

/// <summary>
/// Updates server when a context has expired
/// </summary>
/// <param name="ctxt">The expired context</param>
virtual void ExpiredContext(CoreContext& ctxt) override;

/// <summary>
/// Updates server when a new object is created
/// </summary>
/// <param name="ctxt">Context containing the object</param>
/// <param name="obj">The object</param>
virtual void NewObject(CoreContext& ctxt, const AnySharedPointer& obj) override;

/// <summary>
/// Updates server when a context has expired
/// </summary>
/// <param name="ctxt">The expired context</param>
/// <param name="evtInfo">The event type</param>
virtual void EventFired(CoreContext& ctxt, const std::type_info& evtInfo) override;
// Allows a breakpoint previously set with Breakpoint to be resumed
virtual void HandleResumeFromBreakpoint(std::string name) = 0;

protected:
/// <summary>
/// Sends a message to specified client.
/// </summary>
/// <param name="hdl">Connection pointer on which to send message</param>
/// <param name="pRecipient">Message name in CamelCase</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... Args>
void SendMessage(websocketpp::connection_hdl hdl, const char* p_type, Args&&... args){
using json11::Json;

Json msg = Json::object {
{"type", p_type},
{"args", Json::array{args...}}
};

m_Server->send(hdl, msg.dump(), websocketpp::frame::opcode::text);
}

/// <summary>
/// Broadcast a message to all subscribers.
/// </summary>
/// <param name="pRecipient">Message name in CamelCase</param>
/// <param name="args...">An arg to be passed to client side event handler</param>
template<typename ...Args>
void BroadcastMessage(const char* p_type, Args&&... args) {
for (websocketpp::connection_hdl ptr : m_Subscribers)
SendMessage(ptr, p_type, std::forward<Args>(args)...);
}

/// <summary>
/// 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);

/// <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);

/// <summary>
/// Called when a "terminateContext" event is sent from a client
/// </summary>
/// <param name="contextID">ID of the context to be terminated</param>
void HandleTerminateContext(int contextID);

/// <summary>
/// Called when a "injectContextMember" event is sent from a client
/// </summary>
/// <param name="contextID">ID of the context to inject the type</param>
/// <param name="typeName">Name of the type to inject. From std::type_info.name()</param>
void HandleInjectContextMember(int contextID, std::string typeName);

/// <summary>
/// Called when a "injectContextMember" event is sent from a client
/// </summary>
/// <param name="name">Name of the breakpoint to resume</param>
void HandleResumeFromBreakpoint(std::string name);

/// <summary>
/// Assigns each context a unique ID number
/// </summary>
/// <param name="ctxt">Client that sent event</param>
int ResolveContextID(CoreContext* ctxt);
CoreContext* ResolveContextID(int id);

/// <summary>
/// Append a lambda to this queue that will poll CoreThreads for their utilization
/// </summary>
void PollThreadUtilization(std::chrono::milliseconds period);


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

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

// one-to-one map of contexts to integers
std::map<CoreContext*, int> m_ContextIDs;
std::map<int, CoreContext*> m_ContextPtrs;

// All event types
std::set<std::shared_ptr<TypeIdentifierBase>> m_EventTypes;

// All ContextMembers
std::map<std::string, std::function<void(void)>> m_AllTypes;

// All CoreThreads
struct ThreadStats {
// Last amount of time the thread was known to be running
std::chrono::milliseconds m_lastRuntimeKM;
std::chrono::milliseconds m_lastRuntimeUM;
};
std::map<std::weak_ptr<BasicThread>, ThreadStats, std::owner_less<std::weak_ptr<BasicThread>>> m_Threads;

// Breakpoint functionality
std::mutex m_mutex;
std::condition_variable m_breakpoint_cv;
std::set<std::string> m_breakpoints;

// The actual server
std::shared_ptr<server> m_Server;
const int m_Port; // Port to listen on
};
1 change: 1 addition & 0 deletions examples/AutoNetExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <autowiring/Autowired.h>
#include <autowiring/AutoNetServer.h>
#include <iostream>
#include THREAD_HEADER

//
// AutoNetServer
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ add_executable(AutoFilterExample AutoFilterExample.cpp)
target_link_libraries(AutoFilterExample Autowiring)
set_property(TARGET AutoFilterExample PROPERTY FOLDER "Examples")

find_package(Boost QUIET COMPONENTS system)
find_package(Boost QUIET)
if(NOT Boost_FOUND)
message("Cannot build AutoNetExample, boost not installed on this system")
return()
Expand Down
6 changes: 5 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ include_directories(
${PROJECT_SOURCE_DIR}/autowiring
)

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

if(APPLE)
# Boost is actually required on Mac
find_package(Boost REQUIRED)
else()
find_package(Boost QUIET)
find_package(Boost)
endif()

# If we can find boost, then go ahead and pull in its include directory
Expand Down
Loading