Skip to content

Commit

Permalink
Merging in new main file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
grf14003 committed Feb 6, 2020
2 parents c0cd1e9 + c046290 commit 0700c5f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
44 changes: 44 additions & 0 deletions breadcrumbs/include/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef LOGGER_HPP
#define LOGGER_HPP

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;



class Logger {
public:
int logging_Level = 1;
string filename = "log.txt";


};

void write_log(string msg, int msg_level, string filename, Logger* logObject) {
string line;
ofstream log_file;
log_file.open(filename, std::ios_base::app);
if (logObject->logging_Level <= msg_level) {
//for (int i = 0; i < &msg.size; i++)
log_file << msg;

}
log_file.close();
}

Logger* logger = NULL;
Logger* getLogger() {
if (logger == NULL) {
logger = new Logger();

}
return logger;
}

//Logger* logger = getLogger();
#endif
15 changes: 15 additions & 0 deletions breadcrumbs/src/comms/AlgorithmServer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

#include "AlgorithmServer.hpp"
#include "Logger.h"

//Logger* logger = getLogger();

AlgorithmServer::AlgorithmServer(size_t numClients)
{
Expand All @@ -17,6 +20,7 @@ AlgorithmServer::~AlgorithmServer()

void AlgorithmServer::serverThreadRuntime()
{
Logger* logger = getLogger();
int iResult;
clientThreadsMutex = CreateMutex(NULL, false, NULL);

Expand Down Expand Up @@ -65,6 +69,7 @@ void AlgorithmServer::serverThreadRuntime()

iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
//write_log("listen failed with error\n", 1, logger->filename, logger);
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
Expand All @@ -75,10 +80,17 @@ void AlgorithmServer::serverThreadRuntime()
// Accept a client socket
while (continueThread)
{
Logger* logger = getLogger();
//logger.filename = "log2.txt";
write_log("Listening for clients...\n", 1, logger->filename, logger);
printf("Listening for clients...\n");
ClientSocket = accept(ListenSocket, NULL, NULL);
write_log("Hello new client!\n", 1, logger->filename, logger);
printf("Hello new client!\n");
if (ClientSocket == INVALID_SOCKET) {
write_log("accept failed with error: ", 1, logger->filename, logger);
write_log(std::to_string(WSAGetLastError()), 1, logger->filename, logger);
write_log("\n", 1, logger->filename, logger);
printf("accept failed with error: %d\n", WSAGetLastError());
AcceptFailures++;
if (AcceptFailures >= MAX_ACCEPT_FAILURES)
Expand Down Expand Up @@ -115,6 +127,7 @@ void AlgorithmServer::serverThreadRuntime()
unlockClientThreadsMutex();
}
else {
write_log("Could not acquire mutex to add new client thread\n", 1, logger->filename, logger);
printf("Could not acquire mutex to add new client thread\n");
}
}
Expand All @@ -134,6 +147,7 @@ vector<Attribute>* AlgorithmServer::getAllIncomingAttributes()
Polls DataSyncThreads for new updates
Updates master storage accordingly
*/
Logger* logger = getLogger();
vector<Attribute>* newAttribs = new vector<Attribute>;
if (lockClientThreadsMutex() == STATUS_WAIT_0)
{
Expand All @@ -145,6 +159,7 @@ vector<Attribute>* AlgorithmServer::getAllIncomingAttributes()
unlockClientThreadsMutex();
}
else {
write_log("Could not acquire mutex to add new client thread\n", 1, logger->filename, logger);
printf("Could not acquire mutex to get incoming attributes.\n");
}
return newAttribs;
Expand Down
7 changes: 5 additions & 2 deletions breadcrumbs/src/comms/DataSyncThread.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include "DataSyncThread.hpp"

#include <Logger.h>

void DataSyncThread::threadRuntime()
{
Expand All @@ -14,7 +14,7 @@ void DataSyncThread::threadRuntime()

while (continueThread && iResult > 0)
{

//write_log("Waiting to receive bytes\n", 1, logger->filename, logger);
printf("Waiting to receive bytes\n");
if (!readyToReceive())
continue;
Expand Down Expand Up @@ -137,6 +137,9 @@ void DataSyncThread::startComms()
/*
Initializes the communication thread
*/
//write_log("Start comms: ", 1, logger->filename, logger);
//write_log(std::to_string(incomingAttributes), 1, logger->filename, logger);

printf("Start comms: %d\n", incomingAttributes);
if (!threadRunning)
{
Expand Down
2 changes: 1 addition & 1 deletion breadcrumbs/src/main/Breadcrumbs.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include <stdio.h>

#include "Config.hpp"

#include "AlgoBreadcrumbs.hpp"


Expand Down

0 comments on commit 0700c5f

Please sign in to comment.