Skip to content

Commit

Permalink
Abstracting IO Processor into its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
grf14003 committed Feb 6, 2020
1 parent 5112b4f commit c0cd1e9
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 52 deletions.
3 changes: 2 additions & 1 deletion breadcrumbs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ include_directories("${CMAKE_SOURCE_DIR}")
# puts all .cpp files inside src to the SOURCES variable
# TODO: replace this with a script for collecting cpp files
file(GLOB_RECURSE ALGOS CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/algos/*.cpp")
file(GLOB_RECURSE IO_PROCS CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/io_procs/*.cpp")
file(GLOB_RECURSE COMMS CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/comms/*.cpp")
file(GLOB_RECURSE CONFIG CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/config/*.cpp")
file(GLOB_RECURSE LOG CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/logging/*.cpp")
Expand All @@ -62,6 +63,6 @@ file(GLOB EXECS CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/main/*.cpp")
foreach(X IN LISTS EXECS)
get_filename_component(N ${X} NAME_WE)
message(STATUS "Generating Executable: ${N}.exe Main File: ${X}"})
add_executable(${N} ${X} ${ALGOS} ${COMMS} ${CONFIG} ${LOG})
add_executable(${N} ${X} ${ALGOS} ${IO_PROCS} ${COMMS} ${CONFIG} ${LOG})
target_link_libraries(${N} tinyxml2)
endforeach()
22 changes: 22 additions & 0 deletions breadcrumbs/config/sample_config_file.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version = "1.0"?>
<algorithm>
<name>TestAlgo</name>
<type>Breadcrumbs</type>
<logger>
<level>4</level>
</logger>
</algorithm>
<io_processor>
<name>VirtualInputProcessor0</name>
<type>VirtualInputProcessor</type>
<logger>
<level>2</level>
</logger>
</io_processor>
<io_processor>
<name>VirtualOutputProcessor0</name>
<type>VirtualOutputProcessor</type>
<logger>
<level>1</level>
</logger>
</io_processor>
26 changes: 12 additions & 14 deletions breadcrumbs/include/IOProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
#ifndef IO_PROCESSOR_HPP
#define IO_PROCESSOR_HPP

#include "DataSyncThread.hpp"

#include <stdio.h>
#include <Windows.h>


/*
This class is the class that abstracts the io processor side of the data exchange between
IO processors and algorithms
*/
class IOProcessor
{
public:
IOProcessor() {};

// Async control
unsigned int startComms();
bool stopComms();

IOProcessor();
~IOProcessor();

virtual void loop() {};
virtual bool loopCondition() { return false; };

int init();
int close();
DataSyncThread* getComms() { return comms; }
private:
DataSyncThread* comms;
};

#endif
18 changes: 18 additions & 0 deletions breadcrumbs/include/VirtualOutputIOProcessor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef VIRTUAL_OUTPUT_IO_PROCESSOR_HPP
#define VIRTUAL_OUTPUT_IO_PROCESSOR_HPP

#include "IOProcessor.hpp"

class VirtualOutputIOProcessor : public IOProcessor
{
public:
using IOProcessor::IOProcessor;

void loop();
bool loopCondition();

private:
int iterations = 10;
};

#endif
19 changes: 0 additions & 19 deletions breadcrumbs/include/VirtualOutputProcessor.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion breadcrumbs/src/algos/AlgoBreadcrumbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
void AlgoBreadcrumbs::loop()
{
vector<Attribute>* attribs = pollForAttributes();
// printf("Attrib length: %d\n", attribs->size());
if (attribs->size() > 0)
{
printf("Attrib length: %d\n", attribs->size());
}
_sleep(1000);
}

bool AlgoBreadcrumbs::loopCondition()
Expand Down
28 changes: 28 additions & 0 deletions breadcrumbs/src/io_procs/IOProcessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#include "IOProcessor.hpp"


IOProcessor::IOProcessor()
{
comms = new DataSyncThread(NULL);
}

IOProcessor::~IOProcessor()
{
delete comms;
}

int IOProcessor::init()
{
int result = comms->connectToAlgorithm("localhost");
if (!result)
comms->startComms();
return result;
}

int IOProcessor::close()
{
int result = static_cast<int>(comms->stopComms());
WSACleanup();
return result;
}
16 changes: 16 additions & 0 deletions breadcrumbs/src/io_procs/VirtualOutputIOProcessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include "VirtualOutputIOProcessor.hpp"


void VirtualOutputIOProcessor::loop()
{
char testValue = 'a' + iterations;
Attribute attrib("testKey1", 1, &testValue);
getComms()->sendAttribute(attrib);
iterations--;
}

bool VirtualOutputIOProcessor::loopCondition()
{
return iterations > 0;
}
2 changes: 0 additions & 2 deletions breadcrumbs/src/main/Breadcrumbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "Config.hpp"
#include "AlgoBreadcrumbs.hpp"
#include "VirtualOutputProcessor.hpp"


int main()
Expand All @@ -14,7 +13,6 @@ int main()
while (algorithm->loopCondition())
{
algorithm->loop();
_sleep(1000);
}

return 0;
Expand Down
27 changes: 12 additions & 15 deletions breadcrumbs/src/main/VirtualOutputProcessor.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@

#include "DataSyncThread.hpp"
#include <iostream>

#include "DataSyncThread.hpp"
#include "VirtualOutputIOProcessor.hpp"


int main()
{
DataSyncThread client(NULL);
if (!client.connectToAlgorithm("localhost"))
IOProcessor* client = new VirtualOutputIOProcessor;

if (!client->init())
{
client.startComms();

for (int i = 0; i < 10; i++)
{
char testValue = 'a' + i;
Attribute attrib("testKey1", 1, &testValue);
client.sendAttribute(attrib);
}

client.stopComms();
WSACleanup();
while (client->loopCondition())
client->loop();

return 0;
int result = client->close();
delete client;
return result;
}

return 1;
}

0 comments on commit c0cd1e9

Please sign in to comment.