Skip to content

Commit

Permalink
Created AutoNetExample
Browse files Browse the repository at this point in the history
Moved example from AutoNetTest
  • Loading branch information
gtremper committed Aug 4, 2014
1 parent 685400d commit 76b4490
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#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>
#include <thread>

//
// 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 +39,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 +102,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

0 comments on commit 76b4490

Please sign in to comment.