Skip to content

Commit

Permalink
Fixed AutoNetServer to work with new TypeRegistry and EventRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
gtremper committed Aug 1, 2014
1 parent 9223ac8 commit 62a07e3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
5 changes: 3 additions & 2 deletions autowiring/AutoNetServer.h
Expand Up @@ -3,13 +3,14 @@
#include "CoreThread.h"
#include "Autowired.h"
#include "AutowiringEvents.h"
#include "TypeRegistry.h"
#include <json11/json11.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include <map>
#include <set>

struct EventIdentifierBase;

class AutoNetServer:
public CoreThread,
public virtual AutowiringEvents
Expand Down Expand Up @@ -152,7 +153,7 @@ class AutoNetServer:
std::map<int, CoreContext*> m_ContextPtrs;

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

// All ContextMembers
std::map<std::string, std::function<void(void)>> m_AllTypes;
Expand Down
4 changes: 2 additions & 2 deletions autowiring/EventRegistry.h
Expand Up @@ -12,7 +12,7 @@ class Object;

// Checks if an Object* listens to a event T;
struct EventIdentifierBase {
virtual bool Is(const Object* obj) = 0;
virtual bool IsFiredBy(const Object* obj) = 0;
virtual const std::type_info& Type() = 0;
};

Expand All @@ -21,7 +21,7 @@ struct EventIdentifier:
public EventIdentifierBase
{
// true if "obj" is an event receiver for T
bool Is(const Object* obj){
bool IsFiredBy(const Object* obj){
return !!dynamic_cast<const T*>(obj);
}

Expand Down
34 changes: 17 additions & 17 deletions src/autonet/AutoNetServer.cpp
@@ -1,6 +1,8 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include "AutoNetServer.h"
#include "TypeRegistry.h"
#include "EventRegistry.h"
#include "at_exit.h"
#include <iostream>
#include FUTURE_HEADER
Expand All @@ -20,17 +22,14 @@ AutoNetServer::AutoNetServer():
m_Server->set_close_handler(std::bind(&AutoNetServer::OnClose, this, ::_1));
m_Server->set_message_handler(std::bind(&AutoNetServer::OnMessage, this, ::_1, ::_2));

// Generate lists of types from type registry
for (auto type = g_pFirstEntry; type; type = type->pFlink) {
if (type->IsEventReceiver())
m_EventTypes.insert(type->NewTypeIdentifier());

if (type->CanInject()) {
m_AllTypes[type->ti.name()] = [type]{
type->Inject();
};
}
}
// Generate list of all types from type registry
for (auto type = g_pFirstTypeEntry; type; type = type->pFlink)
if (type->CanInject())
m_AllTypes[type->ti.name()] = [type]{ type->Inject(); };

// Generate list of all events from event registry
for (auto event = g_pFirstEventEntry; event; event = event->pFlink)
m_EventTypes.insert(event->NewEventIdentifier());
}

AutoNetServer::~AutoNetServer(){}
Expand Down Expand Up @@ -174,15 +173,16 @@ void AutoNetServer::NewObject(CoreContext& ctxt, const AnySharedPointer& object)
};
}

auto eventRcvr = autowiring::fast_pointer_cast<EventReceiver>(objectPtr);
if (eventRcvr) {
// Check if type receives any events
{
Json::array listenerTypes;
for (auto event : m_EventTypes) {
if (event->Is(objectPtr.get()))
for (auto& event : m_EventTypes) {
if (event->IsFiredBy(objectPtr.get()))
listenerTypes.push_back(event->Type().name());
}

types["eventReceiver"] = listenerTypes;

if (!listenerTypes.empty())
types["eventReceiver"] = listenerTypes;
}

auto filter = autowiring::fast_pointer_cast<ExceptionFilter>(objectPtr);
Expand Down
2 changes: 1 addition & 1 deletion src/autonet/test/AutoNetServerTest.hpp
Expand Up @@ -3,5 +3,5 @@
#include <EnclosedContextTestBase.hpp>

class AutoNetServerTest:
public EnclosedContextTestBase
public testing::Test
{};
2 changes: 2 additions & 0 deletions src/autowiring/test/EnclosedContextTestBase.hpp
@@ -1,5 +1,7 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include <autowiring/ContextMember.h>
#include <autowiring/ExceptionFilter.h>
#include MEMORY_HEADER

/// <summary>
Expand Down

0 comments on commit 62a07e3

Please sign in to comment.