Skip to content

Working #12

Merged
merged 2 commits into from Dec 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions breadcrumbs/CMakeConfig.h.in
@@ -1,4 +1,6 @@
// the configured options and settings for Tutorial

#define Tutorial_VERSION_MAJOR @Bfs_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Bfs_VERSION_MINOR@
#define VERSION_MAJOR @Bfs_VERSION_MAJOR@
#define VERSION_MINOR @Bfs_VERSION_MINOR@

#define ALGORITHM_SERVER_PORT @Bfs_ALGORITHM_SERVER_PORT@
2 changes: 2 additions & 0 deletions breadcrumbs/CMakeLists.txt
Expand Up @@ -26,6 +26,8 @@ message("Library directory: ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
message("Header file directory: ${INCLUDES_DIRECTORY}")

# Configure a header file to pass some of the CMake settings to the source code
set (Bfs_ALGORITHM_SERVER_PORT \"27634\")

configure_file (
"${PROJECT_SOURCE_DIR}/CMakeConfig.h.in"
"${CMAKE_INCLUDE_PATH}/CMakeConfig.h"
Expand Down
4 changes: 2 additions & 2 deletions breadcrumbs/include/AlgoBreadcrumbs.hpp
Expand Up @@ -9,8 +9,8 @@ class AlgoBreadcrumbs : public Algorithm
public:
using Algorithm::Algorithm;

VOID loop();
BOOLEAN loopCondition();
void loop();
bool loopCondition();
private:
INT iterations = 1;
};
Expand Down
22 changes: 7 additions & 15 deletions breadcrumbs/include/Algorithm.hpp
Expand Up @@ -2,27 +2,19 @@
#ifndef ALGORITHM_HPP
#define ALGORITHM_HPP

#include "IOProcessor.hpp"
#include "AlgorithmServer.hpp"

class Algorithm
{
public:
explicit Algorithm(IOProcessor* ins, SIZE_T num_inputs, IOProcessor* outs, SIZE_T num_outputs);
explicit Algorithm(size_t numProcs);
~Algorithm();

virtual VOID loop() = 0;
virtual BOOLEAN loopCondition() { return FALSE; };

VOID startIOProcessors();
VOID waitForIOProcessors();
VOID stopIOProcessors();

// Updates Algorithm key/value store with IO key/value stores
VOID pollInputs();
// Updates IO key/value stores with Algorithm key/value store
VOID pollOutputs();
virtual void loop() = 0;
virtual bool loopCondition() { return false; };
private:
SIZE_T numInputs, numOutputs;
IOProcessor* inputs, *outputs;
size_t numIoProcs = 0;
AlgorithmServer* server;
};

#endif
53 changes: 53 additions & 0 deletions breadcrumbs/include/AlgorithmServer.hpp
@@ -0,0 +1,53 @@

#ifndef ALGORITHM_SERVER_HPP
#define ALGORITHM_SERVER_HPP


#undef UNICODE

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>

#include "CMakeConfig.h"
#include "DataSyncThread.hpp"

#define ALGORITHM_PORT "10101"
#define MAX_ACCEPT_FAILURES 5

#pragma comment (lib, "Ws2_32.lib")

class AlgorithmServer
{
public:
AlgorithmServer(size_t numClients);
~AlgorithmServer();

void serverThreadRuntime();
static DWORD serverThreadInit(LPVOID pThreadArgs)
{
AlgorithmServer* pAlgorithmServerThread = (AlgorithmServer*)pThreadArgs;
pAlgorithmServerThread->serverThreadRuntime();
return NULL;
}

// Updates Algorithm key/value store with IO key/value updates
void pollForUpdates();
void startServer();
void stopServer();

private:
HANDLE hThread;
DWORD dwThreadId;
bool continueThread = false;

size_t numClients;
std::vector<DataSyncThread> clientThreads;
};

#endif
10 changes: 10 additions & 0 deletions breadcrumbs/include/Attribute.hpp
@@ -0,0 +1,10 @@

#ifndef ATTRIBUTE_HPP
#define ATTRIBUTE_HPP

typedef struct BinaryAttributeStructure {
char key[8];
unsigned char length[2];
} bAttrib;

#endif
6 changes: 4 additions & 2 deletions breadcrumbs/include/CMakeConfig.h
@@ -1,4 +1,6 @@
// the configured options and settings for Tutorial

#define Tutorial_VERSION_MAJOR 1
#define Tutorial_VERSION_MINOR 0
#define VERSION_MAJOR 1
#define VERSION_MINOR 0

#define ALGORITHM_SERVER_PORT "27634"
51 changes: 51 additions & 0 deletions breadcrumbs/include/DataSyncThread.hpp
@@ -0,0 +1,51 @@

#ifndef DATA_SYNC_THREAD_HPP
#define DATA_SYNC_THREAD_HPP

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

#include "Attribute.hpp"
#include "CMakeConfig.h"

// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

/*
This class contains the thread for communicating back and forth along a socket to sync attributes

*/
class DataSyncThread
{
private:
SOCKET sock;
HANDLE hThread;
DWORD dwThreadId;
bool continueThread = false;
public:
DataSyncThread(SOCKET s) { sock = s; };

// Synchronous functions
void threadRuntime();
static DWORD threadInit(LPVOID pThreadArgs)
{
DataSyncThread* pDataSyncThread = (DataSyncThread*)pThreadArgs;
pDataSyncThread->threadRuntime();
return NULL;
}
int recvBytes(void* buffer, size_t numBytes);

// Async control
void startComms();
bool stopComms();
int connectToAlgorithm(char* serverName);
};

#endif
36 changes: 10 additions & 26 deletions breadcrumbs/include/IOProcessor.hpp
@@ -1,41 +1,25 @@
/*
*
* Stores the interface for building a sensor thread
* implementation.
*
*/


#ifndef SENSOR_INTERFACE_HPP
#define SENSOR_INTERFACE_HPP
#ifndef IO_PROCESSOR_HPP
#define IO_PROCESSOR_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(LPCVOID pThreadArgs) { threadArgs = pThreadArgs; };

// Thread routine for implementation to override
virtual VOID threadRuntime(IOProcessor *ioProc) = 0;
// Thread initialization, should not be called directly!
static DWORD threadInit(LPVOID pThreadArgs);
IOProcessor() {};

// Async control
UINT8 startThread();
BOOL waitForThread();

// Sync control
BOOLEAN bufferDataAvailable();
SIZE_T getBufferData(LPCSTR* bufferKeyArray, LPCSTR* bufferValueArray);
VOID setDataStoreValue(LPCSTR key, LPCVOID value, SIZE_T valueSize);
protected:
LPCVOID threadArgs;
DWORD dwThreadId;
HANDLE hThread;
unsigned int startComms();
bool stopComms();

};

Expand Down
2 changes: 1 addition & 1 deletion breadcrumbs/include/VirtualOutputProcessor.hpp
Expand Up @@ -13,7 +13,7 @@ class VirtualOutputProcessor : public IOProcessor
public:
using IOProcessor::IOProcessor;

VOID threadRuntime(IOProcessor* ioProc);
void threadRuntime(IOProcessor* ioProc);
};

#endif
24 changes: 24 additions & 0 deletions breadcrumbs/scripts/startbfs.py
@@ -0,0 +1,24 @@

import os
import multiprocessing as mp
import subprocess


def start_program(command):
cmd_list = command.split(" ")
return subprocess.call(cmd_list)


def start_program_no_hang(command):
print("Running %s from cwd %s" % (command, os.getcwd()))
proc = mp.Process(target=start_program, args=(command,))
proc.start()
return proc


def main():
start_program_no_hang("start cmd.exe /k \"..\\bin\\Breadcrumbs.exe\"")


if __name__ == "__main__":
main()
15 changes: 1 addition & 14 deletions breadcrumbs/src/Breadcrumbs.cpp
Expand Up @@ -7,26 +7,13 @@

int main()
{

// Initialization
int threadID = 0;
IOProcessor *processor = new VirtualOutputProcessor((LPCVOID) &threadID);
Algorithm* algorithm = new AlgoBreadcrumbs(NULL, 0, processor, 1);

// Starting
algorithm->startIOProcessors();
Algorithm* algorithm = new AlgoBreadcrumbs(1);

// Loop
while (algorithm->loopCondition())
{
algorithm->pollInputs();
algorithm->loop();
algorithm->pollOutputs();
}

// Cleanup
processor->waitForThread();
delete processor;

return 0;
}
4 changes: 2 additions & 2 deletions breadcrumbs/src/algos/AlgoBreadcrumbs.cpp
Expand Up @@ -2,12 +2,12 @@
#include "AlgoBreadcrumbs.hpp"


VOID AlgoBreadcrumbs::loop()
void AlgoBreadcrumbs::loop()
{
printf("Breadcrumbs algorithm loop!\n");
}

BOOLEAN AlgoBreadcrumbs::loopCondition()
bool AlgoBreadcrumbs::loopCondition()
{
return --iterations >= 0;
}
38 changes: 6 additions & 32 deletions breadcrumbs/src/algos/Algorithm.cpp
@@ -1,41 +1,15 @@

#include "Algorithm.hpp"

Algorithm::Algorithm(IOProcessor* ins, SIZE_T numIns, IOProcessor* outs, SIZE_T numOuts)
{
numInputs = numIns; numOutputs = numOuts;
inputs = ins; outputs = outs;
}

VOID Algorithm::startIOProcessors()
{
int i;
for (i = 0; i < numInputs; i++)
(inputs + i)->startThread();
for (i = 0; i < numOutputs; i++)
(outputs + i)->startThread();
}

VOID Algorithm::waitForIOProcessors()
{
int i;
for (i = 0; i < numInputs; i++)
(inputs + i)->waitForThread();
for (i = 0; i < numOutputs; i++)
(outputs + i)->waitForThread();
}

VOID Algorithm::stopIOProcessors()
{
waitForIOProcessors();
}

VOID Algorithm::pollInputs()
Algorithm::Algorithm(size_t numProcs)
{
numIoProcs = numProcs;
server = new AlgorithmServer(numProcs);

server->startServer();
}

VOID Algorithm::pollOutputs()
Algorithm::~Algorithm()
{

delete server;
}