From 1583b46e75ae9b9adf9241b3a43dcc3d8e90b82c Mon Sep 17 00:00:00 2001 From: Matthew F Scalzo Date: Fri, 11 Oct 2019 21:26:05 -0400 Subject: [PATCH 01/13] README Text (#2) --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e69de29..662600e 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,4 @@ +# Sensory Biofeedback System +## Nicholas C. Chan, Gregory M. Foss, Matthew F. Scalzo & Nathan A. Shaw + +This is our Senior Design Project. \ No newline at end of file From 93d9107486fd7291852291d124aa6faf7b10c8e0 Mon Sep 17 00:00:00 2001 From: Matt Scalzo Date: Fri, 11 Oct 2019 21:28:56 -0400 Subject: [PATCH 02/13] Changed Names --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 662600e..3c94901 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # Sensory Biofeedback System -## Nicholas C. Chan, Gregory M. Foss, Matthew F. Scalzo & Nathan A. Shaw +## Nich Chan, Greg Foss, Matt Scalzo & Nate Shaw This is our Senior Design Project. \ No newline at end of file From cf97abf4a044056d05804559f87d8aa6321b3558 Mon Sep 17 00:00:00 2001 From: Matt Scalzo Date: Fri, 11 Oct 2019 21:35:14 -0400 Subject: [PATCH 03/13] Added to Description --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c94901..516484f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # Sensory Biofeedback System ## Nich Chan, Greg Foss, Matt Scalzo & Nate Shaw -This is our Senior Design Project. \ No newline at end of file +This is our Senior Design Project. Our goal is to create system that can interpret 3D data and give meaningful feedback through a belt of tactile motors. \ No newline at end of file From ef378e0097b360a5d03423ab1554c833dc551e27 Mon Sep 17 00:00:00 2001 From: Matt Scalzo Date: Fri, 11 Oct 2019 21:38:04 -0400 Subject: [PATCH 04/13] Fixed typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 516484f..244749f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # Sensory Biofeedback System ## Nich Chan, Greg Foss, Matt Scalzo & Nate Shaw -This is our Senior Design Project. Our goal is to create system that can interpret 3D data and give meaningful feedback through a belt of tactile motors. \ No newline at end of file +This is our Senior Design Project. Our goal is to create a system that can interpret 3D data and give meaningful feedback through a belt of tactile motors. \ No newline at end of file From 1e1831cd263423e080ab83f7a07ec30cdea29bea Mon Sep 17 00:00:00 2001 From: Nate Shaw Date: Thu, 17 Oct 2019 14:17:00 -0400 Subject: [PATCH 05/13] testing git setup --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 244749f..ed74551 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # Sensory Biofeedback System ## Nich Chan, Greg Foss, Matt Scalzo & Nate Shaw +this is nate testing + This is our Senior Design Project. Our goal is to create a system that can interpret 3D data and give meaningful feedback through a belt of tactile motors. \ No newline at end of file From 92cc48b739a1e76558fc4dc50ce5e879fb1d978d Mon Sep 17 00:00:00 2001 From: Nate Shaw Date: Thu, 17 Oct 2019 14:59:58 -0400 Subject: [PATCH 06/13] done testing --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index ed74551..20da242 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Sensory Biofeedback System ## Nich Chan, Greg Foss, Matt Scalzo & Nate Shaw -this is nate testing This is our Senior Design Project. Our goal is to create a system that can interpret 3D data and give meaningful feedback through a belt of tactile motors. \ No newline at end of file From 03d6f5e0f8972a3c2e1897eef73d22616f92ef44 Mon Sep 17 00:00:00 2001 From: Gregory M Foss Date: Sat, 19 Oct 2019 16:41:38 -0400 Subject: [PATCH 07/13] Folder simplification build structure (#6) * Updating file structure and adding basic build config * Updating file structure and adding basic build config --- .gitignore | 6 +++ breadcrumbs/CMakeConfig.h.in | 4 ++ breadcrumbs/CMakeLists.txt | 40 +++++++++++++++++++ breadcrumbs/CMakeSettings.json | 16 ++++++++ breadcrumbs/bfs/io_proc/.blank | 0 breadcrumbs/bfs/lib/.blank | 0 breadcrumbs/bfs/out_proc/.blank | 0 breadcrumbs/bfs/scripts/.blank | 0 breadcrumbs/{algos => bin}/.blank | 0 breadcrumbs/in_procs/.blank | 0 breadcrumbs/include/CMakeConfig.h | 4 ++ breadcrumbs/out_procs/.blank | 0 breadcrumbs/src/Breadcrumbs.cpp | 7 ++++ breadcrumbs/{bfs => src/algos}/.blank | 0 .../{bfs/algo => src/io/in_procs}/.blank | 0 .../{bfs/in_proc => src/io/out_procs}/.blank | 0 16 files changed, 77 insertions(+) create mode 100644 breadcrumbs/CMakeConfig.h.in create mode 100644 breadcrumbs/CMakeLists.txt create mode 100644 breadcrumbs/CMakeSettings.json delete mode 100644 breadcrumbs/bfs/io_proc/.blank delete mode 100644 breadcrumbs/bfs/lib/.blank delete mode 100644 breadcrumbs/bfs/out_proc/.blank delete mode 100644 breadcrumbs/bfs/scripts/.blank rename breadcrumbs/{algos => bin}/.blank (100%) delete mode 100644 breadcrumbs/in_procs/.blank create mode 100644 breadcrumbs/include/CMakeConfig.h delete mode 100644 breadcrumbs/out_procs/.blank create mode 100644 breadcrumbs/src/Breadcrumbs.cpp rename breadcrumbs/{bfs => src/algos}/.blank (100%) rename breadcrumbs/{bfs/algo => src/io/in_procs}/.blank (100%) rename breadcrumbs/{bfs/in_proc => src/io/out_procs}/.blank (100%) diff --git a/.gitignore b/.gitignore index d8a0746..a59d439 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ .vs/* + +# Excluding the build and executable folders +breadcrumbs/build/* +!breadcrumbs/build/.blank +breadcrumbs/bin/* +!breadcrumbs/bin/.blank diff --git a/breadcrumbs/CMakeConfig.h.in b/breadcrumbs/CMakeConfig.h.in new file mode 100644 index 0000000..e47bb02 --- /dev/null +++ b/breadcrumbs/CMakeConfig.h.in @@ -0,0 +1,4 @@ +// the configured options and settings for Tutorial + +#define Tutorial_VERSION_MAJOR @Bfs_VERSION_MAJOR@ +#define Tutorial_VERSION_MINOR @Bfs_VERSION_MINOR@ \ No newline at end of file diff --git a/breadcrumbs/CMakeLists.txt b/breadcrumbs/CMakeLists.txt new file mode 100644 index 0000000..611a3e7 --- /dev/null +++ b/breadcrumbs/CMakeLists.txt @@ -0,0 +1,40 @@ +# This file controls the build process. When you save it, VS figures out what types of executables that it builds. +# Then, you can switch the executable that you want to run at the top from the drop down and hit run to build and +# run it. +# + +cmake_minimum_required (VERSION 2.6) + +message("Starting CMAKE") +project (Breadcrumbs) +# The version number. +set (Bfs_VERSION_MAJOR 1) +set (Bfs_VERSION_MINOR 0) + +# Setting paths +message("Setting paths...") +set(CMAKE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src) # Code directory +set(CMAKE_BINARY_DIR ${PROJECT_SOURCE_DIR}/build) # Object files and such (.o) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) # Compiled executables for execution and test (.exe) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib) # Compiled libraries (.lib and .dll) +set(CMAKE_INCLUDE_PATH ${PROJECT_SOURCE_DIR}/include) # Publicly accessible header files +message("Root directory: ${PROJECT_SOURCE_DIR}") +message("Source directory: ${CMAKE_SOURCE_DIR}") +message("Build directory: ${CMAKE_BINARY_DIR}") +message("Executable directory: ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") +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 +configure_file ( + "${PROJECT_SOURCE_DIR}/CMakeConfig.h.in" + "${CMAKE_INCLUDE_PATH}/CMakeConfig.h" + ) + +# Adding public includes to include search path +include_directories("${CMAKE_INCLUDE_PATH}") +# Adding private include files from source tree +include_directories("${CMAKE_SOURCE_DIR}") + +# Adding executables +add_executable(Breadcrumbs ${CMAKE_SOURCE_DIR}/Breadcrumbs.cpp) diff --git a/breadcrumbs/CMakeSettings.json b/breadcrumbs/CMakeSettings.json new file mode 100644 index 0000000..ae612b0 --- /dev/null +++ b/breadcrumbs/CMakeSettings.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "basic_build", + "generator": "Ninja", + "configurationType": "Debug", + "inheritEnvironments": [ "msvc_x64_x64" ], + "buildRoot": "${projectDir}\\build\\${name}", + "installRoot": "${projectDir}\\install\\${name}", + "cmakeCommandArgs": "", + "buildCommandArgs": "-v", + "ctestCommandArgs": "", + "variables": [] + } + ] +} diff --git a/breadcrumbs/bfs/io_proc/.blank b/breadcrumbs/bfs/io_proc/.blank deleted file mode 100644 index e69de29..0000000 diff --git a/breadcrumbs/bfs/lib/.blank b/breadcrumbs/bfs/lib/.blank deleted file mode 100644 index e69de29..0000000 diff --git a/breadcrumbs/bfs/out_proc/.blank b/breadcrumbs/bfs/out_proc/.blank deleted file mode 100644 index e69de29..0000000 diff --git a/breadcrumbs/bfs/scripts/.blank b/breadcrumbs/bfs/scripts/.blank deleted file mode 100644 index e69de29..0000000 diff --git a/breadcrumbs/algos/.blank b/breadcrumbs/bin/.blank similarity index 100% rename from breadcrumbs/algos/.blank rename to breadcrumbs/bin/.blank diff --git a/breadcrumbs/in_procs/.blank b/breadcrumbs/in_procs/.blank deleted file mode 100644 index e69de29..0000000 diff --git a/breadcrumbs/include/CMakeConfig.h b/breadcrumbs/include/CMakeConfig.h new file mode 100644 index 0000000..520c12d --- /dev/null +++ b/breadcrumbs/include/CMakeConfig.h @@ -0,0 +1,4 @@ +// the configured options and settings for Tutorial + +#define Tutorial_VERSION_MAJOR 1 +#define Tutorial_VERSION_MINOR 0 diff --git a/breadcrumbs/out_procs/.blank b/breadcrumbs/out_procs/.blank deleted file mode 100644 index e69de29..0000000 diff --git a/breadcrumbs/src/Breadcrumbs.cpp b/breadcrumbs/src/Breadcrumbs.cpp new file mode 100644 index 0000000..8f4ffc9 --- /dev/null +++ b/breadcrumbs/src/Breadcrumbs.cpp @@ -0,0 +1,7 @@ +#include + + +int main() { + printf("Hello world!\n"); + return 0; +} diff --git a/breadcrumbs/bfs/.blank b/breadcrumbs/src/algos/.blank similarity index 100% rename from breadcrumbs/bfs/.blank rename to breadcrumbs/src/algos/.blank diff --git a/breadcrumbs/bfs/algo/.blank b/breadcrumbs/src/io/in_procs/.blank similarity index 100% rename from breadcrumbs/bfs/algo/.blank rename to breadcrumbs/src/io/in_procs/.blank diff --git a/breadcrumbs/bfs/in_proc/.blank b/breadcrumbs/src/io/out_procs/.blank similarity index 100% rename from breadcrumbs/bfs/in_proc/.blank rename to breadcrumbs/src/io/out_procs/.blank From 46cfff0b605a4817e1f68f3e3af7a452d7661c46 Mon Sep 17 00:00:00 2001 From: Gregory M Foss Date: Mon, 21 Oct 2019 11:49:07 -0400 Subject: [PATCH 08/13] Adding simple style guides (#7) * Adding simple style guides * Update style.hpp --- breadcrumbs/doc/style/style.cpp | 20 ++++++++++++++++++++ breadcrumbs/doc/style/style.hpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 breadcrumbs/doc/style/style.cpp create mode 100644 breadcrumbs/doc/style/style.hpp diff --git a/breadcrumbs/doc/style/style.cpp b/breadcrumbs/doc/style/style.cpp new file mode 100644 index 0000000..26df8b9 --- /dev/null +++ b/breadcrumbs/doc/style/style.cpp @@ -0,0 +1,20 @@ +/* + +This file correspondes to style.hpp and is an example of +the .cpp files that would correspond to the .hpp file. + +*/ + + +#include "style.hpp" + + +char TestClass::attrTwoTimesTwo() +{ + return c_attrTwo << 1 +} + +int TestClass::getAttrSum() +{ + return c_attrTwo + i_attrOne +} diff --git a/breadcrumbs/doc/style/style.hpp b/breadcrumbs/doc/style/style.hpp new file mode 100644 index 0000000..a4b4c78 --- /dev/null +++ b/breadcrumbs/doc/style/style.hpp @@ -0,0 +1,30 @@ +/* + +This file contains the style of each programming construct +to use in this project. + +It should be followed to the letter! All spaces are +necessary! Make sure to take note of camel case and other +variable naming schemes! + +*/ + +// Header File: +class TestClass +{ +public: + // Constructors + TestClass(int AttrOne, char AttrTwo); + // Mutator methods + int getAttrSum(); + char attrTwoTimesTwo(); + void setAttrOne(int newAttrOne) { i_attrOne = newAttrOne } + void setAttrTwo(char newAttrTwo) { c_attrTwo = newAttrTwo } + // Accessor methods + int getAttrOne() { return i_attrOne } + char getAttrTwo() { return c_attrTwo } +private: + // Private variables + int i_attrOne = 0; + char c_attrTwo; +}; From af68184db91f92860f642609bfd1c39bedfbae42 Mon Sep 17 00:00:00 2001 From: Gregory M Foss Date: Mon, 4 Nov 2019 16:38:07 -0500 Subject: [PATCH 09/13] Adding simple processor thread interface (#8) * Adding simple sensor thread interface * Adding simple style guides (#7) * Adding simple style guides * Update style.hpp * [Broken]: For Display purposes only * IO Processor interface * Adding cpp file for IOProc and moving to includes * Adding simple sensor thread interface * [Broken]: For Display purposes only * IO Processor interface * Adding cpp file for IOProc and moving to includes * Fixing include gaurds * Removing unnecessary spaces in cmakelists --- breadcrumbs/CMakeLists.txt | 11 +++-- breadcrumbs/include/IOProcessor.hpp | 40 +++++++++++++++++ breadcrumbs/src/Breadcrumbs.cpp | 12 +++++- breadcrumbs/src/io/IOProcessor.cpp | 43 +++++++++++++++++++ .../io/out_procs/VirtualOutputProcessor.cpp | 8 ++++ .../io/out_procs/VirtualOutputProcessor.hpp | 17 ++++++++ 6 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 breadcrumbs/include/IOProcessor.hpp create mode 100644 breadcrumbs/src/io/IOProcessor.cpp create mode 100644 breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp create mode 100644 breadcrumbs/src/io/out_procs/VirtualOutputProcessor.hpp diff --git a/breadcrumbs/CMakeLists.txt b/breadcrumbs/CMakeLists.txt index 611a3e7..d214ccf 100644 --- a/breadcrumbs/CMakeLists.txt +++ b/breadcrumbs/CMakeLists.txt @@ -30,11 +30,16 @@ configure_file ( "${PROJECT_SOURCE_DIR}/CMakeConfig.h.in" "${CMAKE_INCLUDE_PATH}/CMakeConfig.h" ) - + # Adding public includes to include search path include_directories("${CMAKE_INCLUDE_PATH}") # Adding private include files from source tree 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 SOURCES CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/*.cpp") +message("Source files: ${SOURCES}") + # Adding executables -add_executable(Breadcrumbs ${CMAKE_SOURCE_DIR}/Breadcrumbs.cpp) +add_executable(Breadcrumbs ${SOURCES}) diff --git a/breadcrumbs/include/IOProcessor.hpp b/breadcrumbs/include/IOProcessor.hpp new file mode 100644 index 0000000..042c2ac --- /dev/null +++ b/breadcrumbs/include/IOProcessor.hpp @@ -0,0 +1,40 @@ +/* + * + * Stores the interface for building a sensor thread + * implementation. + * + */ + + +#ifndef SENSOR_INTERFACE_HPP +#define SENSOR_INTERFACE_HPP + + +#include +#include + + +class IOProcessor +{ +public: + // Thread routine for implementation to override + virtual VOID threadRuntime() = 0; + // Thread initialization, should not be called directly! + static DWORD threadInit(LPVOID pIOProcessor); + + // Async control + UINT8 startThread(LPVOID pThreadArgs); + 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; + +}; + +#endif diff --git a/breadcrumbs/src/Breadcrumbs.cpp b/breadcrumbs/src/Breadcrumbs.cpp index 8f4ffc9..4d360ac 100644 --- a/breadcrumbs/src/Breadcrumbs.cpp +++ b/breadcrumbs/src/Breadcrumbs.cpp @@ -1,7 +1,17 @@ #include +#include "io/out_procs/VirtualOutputProcessor.hpp" + int main() { - printf("Hello world!\n"); + + int threadID = 0; + IOProcessor *processor; + processor = new VirtualOutputProcessor(); + processor->startThread(&threadID); + + processor->waitForThread(); + delete processor; + return 0; } diff --git a/breadcrumbs/src/io/IOProcessor.cpp b/breadcrumbs/src/io/IOProcessor.cpp new file mode 100644 index 0000000..7693957 --- /dev/null +++ b/breadcrumbs/src/io/IOProcessor.cpp @@ -0,0 +1,43 @@ + +#include "IOProcessor.hpp" + + +DWORD IOProcessor::threadInit(LPVOID pIOProcessor) +{ + ((IOProcessor*) pIOProcessor)->threadRuntime(); + return 0; +} + +UINT8 IOProcessor::startThread(LPVOID pThreadArgs) +{ + this->threadArgs = pThreadArgs; + hThread = CreateThread( + NULL, // default security attributes + 0, // use default stack size + threadInit, // thread function name + this, // argument to thread function + 0, // use default creation flags + &dwThreadId); // returns the thread identifier + return TRUE; +} + +BOOL IOProcessor::waitForThread() +{ + WaitForSingleObject(hThread, INFINITE); + return TRUE; +} + +BOOLEAN bufferDataAvailable() +{ + return FALSE; +} + +SIZE_T getBufferData(LPCSTR* bufferKeyArray, LPCSTR* bufferValueArray) +{ + return 0; +} + +VOID setDataStoreValue(LPCSTR key, LPCVOID value, SIZE_T valueSize) +{ + +} diff --git a/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp b/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp new file mode 100644 index 0000000..8237789 --- /dev/null +++ b/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp @@ -0,0 +1,8 @@ + +#include "VirtualOutputProcessor.hpp" + + +void VirtualOutputProcessor::threadRuntime() +{ + printf("VirtualOutputProcessor %d started.\n", *((int*) threadArgs)); +} diff --git a/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.hpp b/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.hpp new file mode 100644 index 0000000..b0d9feb --- /dev/null +++ b/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.hpp @@ -0,0 +1,17 @@ + +#ifndef VIRTUAL_OUTPUT_PROCESSOR_HPP +#define VIRTUAL_OUTPUT_PROCESSOR_HPP + + +#include + +#include "IOProcessor.hpp" + + +class VirtualOutputProcessor : public IOProcessor +{ +public: + void threadRuntime(); +}; + +#endif From b3e0beb06240b8b387cbaf19b852568bd0eec97f Mon Sep 17 00:00:00 2001 From: gregfoss Date: Mon, 4 Nov 2019 16:48:52 -0500 Subject: [PATCH 10/13] Adding config and logging folders to git --- breadcrumbs/src/config/.blank | 0 breadcrumbs/src/logging/.blank | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 breadcrumbs/src/config/.blank create mode 100644 breadcrumbs/src/logging/.blank diff --git a/breadcrumbs/src/config/.blank b/breadcrumbs/src/config/.blank new file mode 100644 index 0000000..e69de29 diff --git a/breadcrumbs/src/logging/.blank b/breadcrumbs/src/logging/.blank new file mode 100644 index 0000000..e69de29 From 698611a51be0b0b7ef9027e998a0937c0372b0bb Mon Sep 17 00:00:00 2001 From: Matthew F Scalzo Date: Mon, 4 Nov 2019 16:56:51 -0500 Subject: [PATCH 11/13] Header File Duplication Avoidance (#10) * Adding simple style guides * Update style.hpp * Header file duplication avoidance * Fixed typos * Added Includes * Fixed includes * Fixed * alignment * Fixed * again --- breadcrumbs/doc/style/style.hpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/breadcrumbs/doc/style/style.hpp b/breadcrumbs/doc/style/style.hpp index a4b4c78..dd26d76 100644 --- a/breadcrumbs/doc/style/style.hpp +++ b/breadcrumbs/doc/style/style.hpp @@ -1,13 +1,20 @@ /* + * + * This file contains the style of each programming construct + * to use in this project. + * + * It should be followed to the letter! All spaces are + * necessary! Make sure to take note of camel case and other + * variable naming schemes! + * + */ -This file contains the style of each programming construct -to use in this project. +#ifndef STYLE_HPP +#define STYLE_HPP -It should be followed to the letter! All spaces are -necessary! Make sure to take note of camel case and other -variable naming schemes! +#include -*/ +#include "style.hpp" // Header File: class TestClass @@ -28,3 +35,5 @@ private: int i_attrOne = 0; char c_attrTwo; }; + +#endif From 8bcf32fcd8710141df2508ed67f2719ce9e8104f Mon Sep 17 00:00:00 2001 From: Gregory M Foss Date: Tue, 12 Nov 2019 14:14:16 -0500 Subject: [PATCH 12/13] Algorithm class abstraction (#11) * Abstracting algorithm construct into object * Fixing virtual function override problem * Fixing return types of some functions --- breadcrumbs/include/AlgoBreadcrumbs.hpp | 18 ++++++++ breadcrumbs/include/Algorithm.hpp | 28 +++++++++++++ breadcrumbs/include/IOProcessor.hpp | 8 ++-- .../VirtualOutputProcessor.hpp | 4 +- breadcrumbs/src/Breadcrumbs.cpp | 25 ++++++++--- breadcrumbs/src/algos/AlgoBreadcrumbs.cpp | 13 ++++++ breadcrumbs/src/algos/Algorithm.cpp | 41 +++++++++++++++++++ breadcrumbs/src/io/IOProcessor.cpp | 8 ++-- .../io/out_procs/VirtualOutputProcessor.cpp | 2 +- 9 files changed, 133 insertions(+), 14 deletions(-) create mode 100644 breadcrumbs/include/AlgoBreadcrumbs.hpp create mode 100644 breadcrumbs/include/Algorithm.hpp rename breadcrumbs/{src/io/out_procs => include}/VirtualOutputProcessor.hpp (67%) create mode 100644 breadcrumbs/src/algos/AlgoBreadcrumbs.cpp create mode 100644 breadcrumbs/src/algos/Algorithm.cpp diff --git a/breadcrumbs/include/AlgoBreadcrumbs.hpp b/breadcrumbs/include/AlgoBreadcrumbs.hpp new file mode 100644 index 0000000..e9a0995 --- /dev/null +++ b/breadcrumbs/include/AlgoBreadcrumbs.hpp @@ -0,0 +1,18 @@ + +#ifndef ALGO_BREADCRUMBS_HPP +#define ALGO_BREADCRUMBS_HPP + +#include "Algorithm.hpp" + +class AlgoBreadcrumbs : public Algorithm +{ +public: + using Algorithm::Algorithm; + + VOID loop(); + BOOLEAN loopCondition(); +private: + INT iterations = 1; +}; + +#endif diff --git a/breadcrumbs/include/Algorithm.hpp b/breadcrumbs/include/Algorithm.hpp new file mode 100644 index 0000000..43b92ce --- /dev/null +++ b/breadcrumbs/include/Algorithm.hpp @@ -0,0 +1,28 @@ + +#ifndef ALGORITHM_HPP +#define ALGORITHM_HPP + +#include "IOProcessor.hpp" + +class Algorithm +{ +public: + explicit Algorithm(IOProcessor* ins, SIZE_T num_inputs, IOProcessor* outs, SIZE_T num_outputs); + + 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(); +private: + SIZE_T numInputs, numOutputs; + IOProcessor* inputs, *outputs; +}; + +#endif diff --git a/breadcrumbs/include/IOProcessor.hpp b/breadcrumbs/include/IOProcessor.hpp index 042c2ac..f7a2cce 100644 --- a/breadcrumbs/include/IOProcessor.hpp +++ b/breadcrumbs/include/IOProcessor.hpp @@ -17,13 +17,15 @@ class IOProcessor { public: + IOProcessor(LPCVOID pThreadArgs) { threadArgs = pThreadArgs; }; + // Thread routine for implementation to override - virtual VOID threadRuntime() = 0; + virtual VOID threadRuntime(IOProcessor *ioProc) = 0; // Thread initialization, should not be called directly! - static DWORD threadInit(LPVOID pIOProcessor); + static DWORD threadInit(LPVOID pThreadArgs); // Async control - UINT8 startThread(LPVOID pThreadArgs); + UINT8 startThread(); BOOL waitForThread(); // Sync control diff --git a/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.hpp b/breadcrumbs/include/VirtualOutputProcessor.hpp similarity index 67% rename from breadcrumbs/src/io/out_procs/VirtualOutputProcessor.hpp rename to breadcrumbs/include/VirtualOutputProcessor.hpp index b0d9feb..b8a8ac7 100644 --- a/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.hpp +++ b/breadcrumbs/include/VirtualOutputProcessor.hpp @@ -11,7 +11,9 @@ class VirtualOutputProcessor : public IOProcessor { public: - void threadRuntime(); + using IOProcessor::IOProcessor; + + VOID threadRuntime(IOProcessor* ioProc); }; #endif diff --git a/breadcrumbs/src/Breadcrumbs.cpp b/breadcrumbs/src/Breadcrumbs.cpp index 4d360ac..71f5809 100644 --- a/breadcrumbs/src/Breadcrumbs.cpp +++ b/breadcrumbs/src/Breadcrumbs.cpp @@ -1,15 +1,30 @@ + #include -#include "io/out_procs/VirtualOutputProcessor.hpp" +#include "AlgoBreadcrumbs.hpp" +#include "VirtualOutputProcessor.hpp" -int main() { +int main() +{ + // Initialization int threadID = 0; - IOProcessor *processor; - processor = new VirtualOutputProcessor(); - processor->startThread(&threadID); + IOProcessor *processor = new VirtualOutputProcessor((LPCVOID) &threadID); + Algorithm* algorithm = new AlgoBreadcrumbs(NULL, 0, processor, 1); + + // Starting + algorithm->startIOProcessors(); + + // Loop + while (algorithm->loopCondition()) + { + algorithm->pollInputs(); + algorithm->loop(); + algorithm->pollOutputs(); + } + // Cleanup processor->waitForThread(); delete processor; diff --git a/breadcrumbs/src/algos/AlgoBreadcrumbs.cpp b/breadcrumbs/src/algos/AlgoBreadcrumbs.cpp new file mode 100644 index 0000000..2aba742 --- /dev/null +++ b/breadcrumbs/src/algos/AlgoBreadcrumbs.cpp @@ -0,0 +1,13 @@ + +#include "AlgoBreadcrumbs.hpp" + + +VOID AlgoBreadcrumbs::loop() +{ + printf("Breadcrumbs algorithm loop!\n"); +} + +BOOLEAN AlgoBreadcrumbs::loopCondition() +{ + return --iterations >= 0; +} diff --git a/breadcrumbs/src/algos/Algorithm.cpp b/breadcrumbs/src/algos/Algorithm.cpp new file mode 100644 index 0000000..5487e55 --- /dev/null +++ b/breadcrumbs/src/algos/Algorithm.cpp @@ -0,0 +1,41 @@ + +#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() +{ + +} + +VOID Algorithm::pollOutputs() +{ + +} diff --git a/breadcrumbs/src/io/IOProcessor.cpp b/breadcrumbs/src/io/IOProcessor.cpp index 7693957..24c70cd 100644 --- a/breadcrumbs/src/io/IOProcessor.cpp +++ b/breadcrumbs/src/io/IOProcessor.cpp @@ -2,15 +2,15 @@ #include "IOProcessor.hpp" -DWORD IOProcessor::threadInit(LPVOID pIOProcessor) +DWORD IOProcessor::threadInit(LPVOID pThreadArgs) { - ((IOProcessor*) pIOProcessor)->threadRuntime(); + IOProcessor* pIOProcessor = (IOProcessor*)pThreadArgs; + pIOProcessor->threadRuntime(pIOProcessor); return 0; } -UINT8 IOProcessor::startThread(LPVOID pThreadArgs) +UINT8 IOProcessor::startThread() { - this->threadArgs = pThreadArgs; hThread = CreateThread( NULL, // default security attributes 0, // use default stack size diff --git a/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp b/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp index 8237789..e6905c0 100644 --- a/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp +++ b/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp @@ -2,7 +2,7 @@ #include "VirtualOutputProcessor.hpp" -void VirtualOutputProcessor::threadRuntime() +VOID VirtualOutputProcessor::threadRuntime(IOProcessor* ioProc) { printf("VirtualOutputProcessor %d started.\n", *((int*) threadArgs)); } From 1eb01e12ac96a8520a40c0300ea802825f7856e9 Mon Sep 17 00:00:00 2001 From: Gregory M Foss Date: Thu, 5 Dec 2019 12:52:11 -0500 Subject: [PATCH 13/13] Working (#12) * First draft adding client code, skeleton of server code. * Updating working branch with newest commit --- breadcrumbs/CMakeConfig.h.in | 6 +- breadcrumbs/CMakeLists.txt | 2 + breadcrumbs/include/AlgoBreadcrumbs.hpp | 4 +- breadcrumbs/include/Algorithm.hpp | 22 +-- breadcrumbs/include/AlgorithmServer.hpp | 53 ++++++ breadcrumbs/include/Attribute.hpp | 10 ++ breadcrumbs/include/CMakeConfig.h | 6 +- breadcrumbs/include/DataSyncThread.hpp | 51 ++++++ breadcrumbs/include/IOProcessor.hpp | 36 ++-- .../include/VirtualOutputProcessor.hpp | 2 +- breadcrumbs/scripts/startbfs.py | 24 +++ breadcrumbs/src/Breadcrumbs.cpp | 15 +- breadcrumbs/src/algos/AlgoBreadcrumbs.cpp | 4 +- breadcrumbs/src/algos/Algorithm.cpp | 38 +---- breadcrumbs/src/comms/AlgorithmServer.cpp | 159 ++++++++++++++++++ breadcrumbs/src/comms/DataSyncThread.cpp | 144 ++++++++++++++++ breadcrumbs/src/io/IOProcessor.cpp | 39 ----- .../io/out_procs/VirtualOutputProcessor.cpp | 4 +- 18 files changed, 482 insertions(+), 137 deletions(-) create mode 100644 breadcrumbs/include/AlgorithmServer.hpp create mode 100644 breadcrumbs/include/Attribute.hpp create mode 100644 breadcrumbs/include/DataSyncThread.hpp create mode 100644 breadcrumbs/scripts/startbfs.py create mode 100644 breadcrumbs/src/comms/AlgorithmServer.cpp create mode 100644 breadcrumbs/src/comms/DataSyncThread.cpp diff --git a/breadcrumbs/CMakeConfig.h.in b/breadcrumbs/CMakeConfig.h.in index e47bb02..be22a44 100644 --- a/breadcrumbs/CMakeConfig.h.in +++ b/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@ \ No newline at end of file +#define VERSION_MAJOR @Bfs_VERSION_MAJOR@ +#define VERSION_MINOR @Bfs_VERSION_MINOR@ + +#define ALGORITHM_SERVER_PORT @Bfs_ALGORITHM_SERVER_PORT@ \ No newline at end of file diff --git a/breadcrumbs/CMakeLists.txt b/breadcrumbs/CMakeLists.txt index d214ccf..94f900a 100644 --- a/breadcrumbs/CMakeLists.txt +++ b/breadcrumbs/CMakeLists.txt @@ -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" diff --git a/breadcrumbs/include/AlgoBreadcrumbs.hpp b/breadcrumbs/include/AlgoBreadcrumbs.hpp index e9a0995..3502ff5 100644 --- a/breadcrumbs/include/AlgoBreadcrumbs.hpp +++ b/breadcrumbs/include/AlgoBreadcrumbs.hpp @@ -9,8 +9,8 @@ class AlgoBreadcrumbs : public Algorithm public: using Algorithm::Algorithm; - VOID loop(); - BOOLEAN loopCondition(); + void loop(); + bool loopCondition(); private: INT iterations = 1; }; diff --git a/breadcrumbs/include/Algorithm.hpp b/breadcrumbs/include/Algorithm.hpp index 43b92ce..8edd655 100644 --- a/breadcrumbs/include/Algorithm.hpp +++ b/breadcrumbs/include/Algorithm.hpp @@ -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 diff --git a/breadcrumbs/include/AlgorithmServer.hpp b/breadcrumbs/include/AlgorithmServer.hpp new file mode 100644 index 0000000..85520d0 --- /dev/null +++ b/breadcrumbs/include/AlgorithmServer.hpp @@ -0,0 +1,53 @@ + +#ifndef ALGORITHM_SERVER_HPP +#define ALGORITHM_SERVER_HPP + + +#undef UNICODE + +#define WIN32_LEAN_AND_MEAN + +#include +#include +#include +#include +#include +#include + +#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 clientThreads; +}; + +#endif diff --git a/breadcrumbs/include/Attribute.hpp b/breadcrumbs/include/Attribute.hpp new file mode 100644 index 0000000..717ae02 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/breadcrumbs/include/CMakeConfig.h b/breadcrumbs/include/CMakeConfig.h index 520c12d..b625ea5 100644 --- a/breadcrumbs/include/CMakeConfig.h +++ b/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" diff --git a/breadcrumbs/include/DataSyncThread.hpp b/breadcrumbs/include/DataSyncThread.hpp new file mode 100644 index 0000000..ba17598 --- /dev/null +++ b/breadcrumbs/include/DataSyncThread.hpp @@ -0,0 +1,51 @@ + +#ifndef DATA_SYNC_THREAD_HPP +#define DATA_SYNC_THREAD_HPP + +#define WIN32_LEAN_AND_MEAN + +#include +#include +#include +#include +#include + +#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 \ No newline at end of file diff --git a/breadcrumbs/include/IOProcessor.hpp b/breadcrumbs/include/IOProcessor.hpp index f7a2cce..00fead2 100644 --- a/breadcrumbs/include/IOProcessor.hpp +++ b/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 #include +/* +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(); }; diff --git a/breadcrumbs/include/VirtualOutputProcessor.hpp b/breadcrumbs/include/VirtualOutputProcessor.hpp index b8a8ac7..067fef4 100644 --- a/breadcrumbs/include/VirtualOutputProcessor.hpp +++ b/breadcrumbs/include/VirtualOutputProcessor.hpp @@ -13,7 +13,7 @@ class VirtualOutputProcessor : public IOProcessor public: using IOProcessor::IOProcessor; - VOID threadRuntime(IOProcessor* ioProc); + void threadRuntime(IOProcessor* ioProc); }; #endif diff --git a/breadcrumbs/scripts/startbfs.py b/breadcrumbs/scripts/startbfs.py new file mode 100644 index 0000000..618e877 --- /dev/null +++ b/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() diff --git a/breadcrumbs/src/Breadcrumbs.cpp b/breadcrumbs/src/Breadcrumbs.cpp index 71f5809..cec95a9 100644 --- a/breadcrumbs/src/Breadcrumbs.cpp +++ b/breadcrumbs/src/Breadcrumbs.cpp @@ -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; } diff --git a/breadcrumbs/src/algos/AlgoBreadcrumbs.cpp b/breadcrumbs/src/algos/AlgoBreadcrumbs.cpp index 2aba742..7278eb5 100644 --- a/breadcrumbs/src/algos/AlgoBreadcrumbs.cpp +++ b/breadcrumbs/src/algos/AlgoBreadcrumbs.cpp @@ -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; } diff --git a/breadcrumbs/src/algos/Algorithm.cpp b/breadcrumbs/src/algos/Algorithm.cpp index 5487e55..7ae56b8 100644 --- a/breadcrumbs/src/algos/Algorithm.cpp +++ b/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; } diff --git a/breadcrumbs/src/comms/AlgorithmServer.cpp b/breadcrumbs/src/comms/AlgorithmServer.cpp new file mode 100644 index 0000000..61f43db --- /dev/null +++ b/breadcrumbs/src/comms/AlgorithmServer.cpp @@ -0,0 +1,159 @@ + +#include "AlgorithmServer.hpp" + +AlgorithmServer::AlgorithmServer(size_t numClients) +{ + this->hThread = NULL; + this->dwThreadId = 0; + this->numClients = numClients; + + clientThreads.reserve(numClients); +} + +AlgorithmServer::~AlgorithmServer() +{ + this->stopServer(); +} + +void AlgorithmServer::serverThreadRuntime() +{ + int iResult; + + SOCKET ListenSocket = INVALID_SOCKET; + SOCKET ClientSocket = INVALID_SOCKET; + + struct addrinfo* result = NULL; + struct addrinfo hints; + + int iSendResult; + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + + // Resolve the server address and port + iResult = getaddrinfo(NULL, ALGORITHM_SERVER_PORT, &hints, &result); + if (iResult != 0) { + printf("getaddrinfo failed with error: %d\n", iResult); + WSACleanup(); + return; + } + + // Create a SOCKET for connecting to server + ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + if (ListenSocket == INVALID_SOCKET) { + printf("socket failed with error: %ld\n", WSAGetLastError()); + freeaddrinfo(result); + WSACleanup(); + return; + } + + // Setup the TCP listening socket + iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); + if (iResult == SOCKET_ERROR) { + printf("bind failed with error: %d\n", WSAGetLastError()); + freeaddrinfo(result); + closesocket(ListenSocket); + WSACleanup(); + return; + } + freeaddrinfo(result); + + iResult = listen(ListenSocket, SOMAXCONN); + if (iResult == SOCKET_ERROR) { + printf("listen failed with error: %d\n", WSAGetLastError()); + closesocket(ListenSocket); + WSACleanup(); + return; + } + + // Select stuff + FD_SET readSet; + + int AcceptFailures = 0; + // Accept a client socket + while (continueThread) + { + int fds; + + FD_ZERO(&readSet); + FD_SET(ListenSocket, &readSet); + + if ((fds = select(0, &readSet, NULL, NULL, NULL)) == SOCKET_ERROR) + { + printf("select() returned with error %d\n", WSAGetLastError()); + break; + } + + if (fds != 0) + { + ClientSocket = accept(ListenSocket, NULL, NULL); + if (ClientSocket == INVALID_SOCKET) { + printf("accept failed with error: %d\n", WSAGetLastError()); + AcceptFailures++; + if (AcceptFailures >= MAX_ACCEPT_FAILURES) + break; + } + else { + AcceptFailures = 0; + } + + // Creating a new Data sync thread + if (clientThreads.size() < numClients) { + DataSyncThread client = *new DataSyncThread(ClientSocket); + clientThreads.push_back(client); + client.startComms(); + } + else { + printf("Client attempted connection when (%d) clients are already connected", numClients); + } + } + } + + // cleanup + closesocket(ListenSocket); + WSACleanup(); + continueThread = false; + return; +} + +void AlgorithmServer::pollForUpdates() +{ + /* + Polls DataSyncThreads for new updates + Updates master storage accordingly + */ + for (DataSyncThread dst : clientThreads) + { + // TODO: Implement this! + } +} + +void AlgorithmServer::startServer() +{ + + // Initialize Winsock + WSADATA wsaData; + int result; + if (result = WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + printf("WSAStartup failed with error: %d\n", result); + return; + } + + // Starts the server thread: + continueThread = true; + hThread = CreateThread( + NULL, // default security attributes + 0, // use default stack size + serverThreadInit, // thread function name + this, // argument to thread function + 0, // use default creation flags + &dwThreadId); // returns the thread identifier +} + +void AlgorithmServer::stopServer() +{ + continueThread = false; +} diff --git a/breadcrumbs/src/comms/DataSyncThread.cpp b/breadcrumbs/src/comms/DataSyncThread.cpp new file mode 100644 index 0000000..a5efa72 --- /dev/null +++ b/breadcrumbs/src/comms/DataSyncThread.cpp @@ -0,0 +1,144 @@ + +#include "DataSyncThread.hpp" + + +void DataSyncThread::threadRuntime() +{ + /* + Handles the data sync between the other end of the socket + */ + int iResult = 1; + + while (continueThread && iResult > 0) + { + // Master while loop that will receive incoming messages + char commandByte; + iResult = recvBytes(&commandByte, 1); + if (iResult <= 0) + break; + + switch (commandByte) + { + case 0: + bAttrib attrib; + iResult = recvBytes(&attrib, sizeof(bAttrib)); + if (iResult <= 0) + break; + printf("Attribute update received, key:%.8s len:%us\n", attrib.key, (unsigned short) attrib.length); + void* value = malloc((unsigned short)attrib.length); + iResult = recvBytes(value, (unsigned short)attrib.length); + if (iResult <= 0) + break; + // TODO: Storing the attrib update + + break; + } + } + + continueThread = false; +} + +int DataSyncThread::recvBytes(void* buffer, size_t numBytes) +{ + size_t bytesRecved = 0; + int iResult; + + while (bytesRecved < numBytes) + { + iResult = recv(sock, ((char*) buffer) + bytesRecved, numBytes - bytesRecved, 0); + if (iResult < 0) + { + printf("recv failed with error: %d\n", WSAGetLastError()); + return iResult; + } + else if (iResult == 0) { + printf("recv returned 0, connection closed.\n"); + return iResult; + } + else + bytesRecved += iResult; + } + return numBytes; +} + +void DataSyncThread::startComms() +{ + /* + Initializes the communication thread + */ + continueThread = true; + hThread = CreateThread( + NULL, // default security attributes + 0, // use default stack size + threadInit, // thread function name + this, // argument to thread function + 0, // use default creation flags + &dwThreadId); // returns the thread identifier +} + +bool DataSyncThread::stopComms() +{ + /* + Stops the communication thread + */ + continueThread = false; + return true; +} + +int DataSyncThread::connectToAlgorithm(char* serverName) +{ + sock = INVALID_SOCKET; + WSADATA wsaData; + struct addrinfo* result = NULL,* ptr = NULL, hints; + int iResult; + + // Initialize Winsock + iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (iResult != 0) { + printf("WSAStartup failed with error: %d\n", iResult); + return 1; + } + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + // Resolve the server address and port + iResult = getaddrinfo(serverName, ALGORITHM_SERVER_PORT, &hints, &result); + if (iResult != 0) { + printf("getaddrinfo failed with error: %d\n", iResult); + WSACleanup(); + return 1; + } + + // Attempt to connect to an address until one succeeds + for (ptr = result; ptr != NULL; ptr = ptr->ai_next) { + + // Create a SOCKET for connecting to server + sock = socket(ptr->ai_family, ptr->ai_socktype, + ptr->ai_protocol); + if (sock == INVALID_SOCKET) { + printf("socket failed with error: %ld\n", WSAGetLastError()); + WSACleanup(); + return 1; + } + + // Connect to server. + iResult = connect(sock, ptr->ai_addr, (int)ptr->ai_addrlen); + if (iResult == SOCKET_ERROR) { + closesocket(sock); + sock = INVALID_SOCKET; + continue; + } + break; + } + + freeaddrinfo(result); + + if (sock == INVALID_SOCKET) { + printf("Unable to connect to server!\n"); + WSACleanup(); + return 1; + } +} diff --git a/breadcrumbs/src/io/IOProcessor.cpp b/breadcrumbs/src/io/IOProcessor.cpp index 24c70cd..437de23 100644 --- a/breadcrumbs/src/io/IOProcessor.cpp +++ b/breadcrumbs/src/io/IOProcessor.cpp @@ -2,42 +2,3 @@ #include "IOProcessor.hpp" -DWORD IOProcessor::threadInit(LPVOID pThreadArgs) -{ - IOProcessor* pIOProcessor = (IOProcessor*)pThreadArgs; - pIOProcessor->threadRuntime(pIOProcessor); - return 0; -} - -UINT8 IOProcessor::startThread() -{ - hThread = CreateThread( - NULL, // default security attributes - 0, // use default stack size - threadInit, // thread function name - this, // argument to thread function - 0, // use default creation flags - &dwThreadId); // returns the thread identifier - return TRUE; -} - -BOOL IOProcessor::waitForThread() -{ - WaitForSingleObject(hThread, INFINITE); - return TRUE; -} - -BOOLEAN bufferDataAvailable() -{ - return FALSE; -} - -SIZE_T getBufferData(LPCSTR* bufferKeyArray, LPCSTR* bufferValueArray) -{ - return 0; -} - -VOID setDataStoreValue(LPCSTR key, LPCVOID value, SIZE_T valueSize) -{ - -} diff --git a/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp b/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp index e6905c0..01bf3cc 100644 --- a/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp +++ b/breadcrumbs/src/io/out_procs/VirtualOutputProcessor.cpp @@ -2,7 +2,7 @@ #include "VirtualOutputProcessor.hpp" -VOID VirtualOutputProcessor::threadRuntime(IOProcessor* ioProc) +void VirtualOutputProcessor::threadRuntime(IOProcessor* ioProc) { - printf("VirtualOutputProcessor %d started.\n", *((int*) threadArgs)); + printf("VirtualOutputProcessor started.\n"); }