Skip to content

Commit

Permalink
Merge pull request #29 from leapmotion/example-autonet
Browse files Browse the repository at this point in the history
AutoNet and AutoFilter examples
  • Loading branch information
codemercenary committed Aug 5, 2014
2 parents 54f2252 + 2a47179 commit a340c9d
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 112 deletions.
49 changes: 46 additions & 3 deletions examples/AutoFilterExample.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,51 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include <autowiring/Autowired.h>
#include <iostream>
#include <memory>
#include <string>

//
// AutoFilter
//
// AutoFilter provides a declarative way to to create a filter graph network
// within a context. Packets on this network have the same domain as events
// (snooping works with packets as well). An AutoFilter is made by creating
// a ContextMember that implements the AutoFilter(...) member function.
//

class StringFilter {
public:
void AutoFilter(std::string msg) {
std::cout << "Filter Update: " << msg << std::endl;
}
};

class StringIntFilter {
public:
void AutoFilter(std::string msg, int num) {
std::cout << "Other Filter Update: " << msg << " : " << num << std::endl;
}
};

int main() {
std::cout << "Hello World" << std::endl;
}
// Initiate the current context
AutoCurrentContext()->Initiate();

// Inject our AutoFilter enabled context members into the context
AutoRequired<StringFilter>();
AutoRequired<StringIntFilter>();

// Each context automatically includes an AutoPacketFactory type. This
// can be used to create new packets in the AutoFilter network
Autowired<AutoPacketFactory> factory;

// When decorating a packet, all AutoFilters that have that type as an argument
// will be called. It is only called when all argument types have been decorated
auto packet = factory->NewPacket();

packet->Decorate(std::string("Hello World"));

std::cout << "StringIntFilter not called yet" << std::endl;

// StringIntFilter will now be called since all AutoFilter arguments have ben decorated
packet->Decorate(42);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#include "stdafx.h"
#include "AutoNetServerTest.hpp"
#include "AutoNetServer.h"
#include "Autowired.h"
#include THREAD_HEADER
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include <autowiring/Autowired.h>
#include <autowiring/AutoNetServer.h>
#include <iostream>

//
// AutoNetServer
//
// This example creates a sample context structure to view with the
// AutoNetVisualizer. It creates contexts and adds dummy context members
// at timed intervals to show off the dynamic nature of the visualizer.
// You can view the visualizer at leapmotion.github.io/autonet
//

//
// Declaration of dummy classes to view in the visualizer
//

class TestThread1:
public CoreThread
Expand All @@ -26,63 +38,38 @@ class ContextA {};
class ContextB {};
class ContextC {};

class ExposedAutoNetServer:
public AutoNetServer
{
public:
using AutoNetServer::HandleResumeFromBreakpoint;
};

class BreakpointThread:
public BasicThread
{
void Run(void) override {
Autowired<AutoNetServer> autonet;
autonet->Breakpoint("InThread");
std::cout << "Thread finished" << std::endl;
}
};

class WaitsThenSimulatesResume:
public BasicThread
{
void Run(void) override {
std::this_thread::sleep_for(std::chrono::milliseconds(100));

AutowiredFast<AutoNetServer> autonet;
auto autonetInternal = static_cast<ExposedAutoNetServer*>(autonet.get());
autonetInternal->HandleResumeFromBreakpoint("Main");
autonetInternal->HandleResumeFromBreakpoint("InThread");
}
};

TEST_F(AutoNetServerTest, Breakpoint) {
AutoCurrentContext()->Initiate();
AutoRequired<AutoNetServer> autonet;
AutoRequired<BreakpointThread> thread;
AutoRequired<WaitsThenSimulatesResume>();

autonet->Breakpoint("Main");
}

TEST_F(AutoNetServerTest, SimpleTest) {
int main() {
AutoGlobalContext global;
auto ctxt = global->Create<ContextA>();
CurrentContextPusher pshr(ctxt);

// To use the visualizer, you just need to AutoRequire the AutoNetServer
AutoRequired<AutoNetServer> server;
ctxt->Initiate();

// Initiate context to start threads
ctxt->Initiate();

// Create a bunch of example Contexts and Context Members
auto ctxt2 = ctxt->Create<ContextB>();
auto ctxt3 = ctxt->Create<ContextC>();

ctxt2->Initiate();


{
CurrentContextPusher pshr(ctxt3);
AutoRequired<TestThread2> bar;
}

std::shared_ptr<CoreContext> newContext;

{
CurrentContextPusher pshr(ctxt2);
AutoRequired<TestThread1> foo;

// Give time to open AutoNet Visualizer
std::this_thread::sleep_for(std::chrono::seconds(10));

*foo += std::chrono::seconds(1),[&ctxt]{
ctxt->Inject<ThisClassGetsAddedLater<4>>();
};
Expand Down Expand Up @@ -114,15 +101,8 @@ TEST_F(AutoNetServerTest, SimpleTest) {
ctxt3->SignalShutdown(true);
ctxt3.reset();
};
*foo += std::chrono::seconds(12), [&ctxt]{
ctxt->SignalShutdown();
};
}

{
CurrentContextPusher pshr(ctxt3);
AutoRequired<TestThread2> bar;
}

// This will wait indefinitly untill you manual quit the example
ctxt->Wait();
}
}
14 changes: 14 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ set_property(TARGET EventExample PROPERTY FOLDER "Examples")
add_executable(AutoFilterExample AutoFilterExample.cpp)
target_link_libraries(AutoFilterExample Autowiring)
set_property(TARGET AutoFilterExample PROPERTY FOLDER "Examples")

find_package(Boost QUIET COMPONENTS system)
if(NOT Boost_FOUND)
message("Cannot build AutoNetExample, boost not installed on this system")
return()
endif()

include_directories(
${Boost_INCLUDE_DIR}
)

add_executable(AutoNetExample AutoNetExample.cpp)
target_link_libraries(AutoNetExample Autowiring AutoNet)
set_property(TARGET AutoNetExample PROPERTY FOLDER "Examples")
7 changes: 0 additions & 7 deletions src/autonet/test/AutoNetServerTest.hpp

This file was deleted.

47 changes: 47 additions & 0 deletions src/autonet/test/BreakpointTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "stdafx.h"
#include "AutoNetServer.h"
#include "Autowired.h"
#include THREAD_HEADER

class BreakpointTest:
public testing::Test
{};

class ExposedAutoNetServer:
public AutoNetServer
{
public:
using AutoNetServer::HandleResumeFromBreakpoint;
};

class BreakpointThread:
public BasicThread
{
void Run(void) override {
Autowired<AutoNetServer> autonet;
autonet->Breakpoint("InThread");
std::cout << "Thread finished" << std::endl;
}
};

class WaitsThenSimulatesResume:
public BasicThread
{
void Run(void) override {
std::this_thread::sleep_for(std::chrono::milliseconds(100));

AutowiredFast<AutoNetServer> autonet;
auto autonetInternal = static_cast<ExposedAutoNetServer*>(autonet.get());
autonetInternal->HandleResumeFromBreakpoint("Main");
autonetInternal->HandleResumeFromBreakpoint("InThread");
}
};

TEST_F(BreakpointTest, SimplePauseAndResume) {
AutoCurrentContext()->Initiate();
AutoRequired<AutoNetServer> autonet;
AutoRequired<BreakpointThread> thread;
AutoRequired<WaitsThenSimulatesResume>();

autonet->Breakpoint("Main");
}
3 changes: 1 addition & 2 deletions src/autonet/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ include_directories(

set(
AutoNetTest_SRCS
AutoNetServerTest.hpp
AutoNetServerTest.cpp
BreakpointTest.cpp
gtest-all-guard.cpp
)

Expand Down
Loading

0 comments on commit a340c9d

Please sign in to comment.