From 3499ed711de6c5f5bd3eb6db1aaf806c2f2253c8 Mon Sep 17 00:00:00 2001 From: Duc Date: Fri, 14 Mar 2025 07:38:50 -0700 Subject: [PATCH] Initial check-in --- .gitignore | 2 - CommonLib/CommonLib.pro | 103 ++++ CommonLib/readme.txt | 4 + .../AutomationMsgHandler.cpp | 167 +++++++ .../AutomationMsgHandler.hpp | 103 ++++ .../AutomationMsgParser.cpp | 184 ++++++++ .../AutomationMsgParser.hpp | 94 ++++ .../AutomationMessages/GenericRspMessage.cpp | 119 +++++ .../AutomationMessages/GenericRspMessage.hpp | 138 ++++++ .../CommonLib/AutomationMessages/Message.cpp | 192 ++++++++ .../CommonLib/AutomationMessages/Message.hpp | 257 ++++++++++ .../AutomationMessages/MessageHeader.cpp | 102 ++++ .../AutomationMessages/MessageHeader.hpp | 163 +++++++ .../AutomationMessages/MessageIDs.hpp | 32 ++ .../TransferAFileCmdMessage.cpp | 139 ++++++ .../TransferAFileCmdMessage.hpp | 124 +++++ .../TransferGutsVideosCmdMessage.cpp | 173 +++++++ .../TransferGutsVideosCmdMessage.hpp | 142 ++++++ .../WaitForLastTaskCompletionCmdMessage.cpp | 96 ++++ .../WaitForLastTaskCompletionCmdMessage.hpp | 121 +++++ .../src/CommonLib/Exceptions/Exception.cpp | 108 +++++ .../src/CommonLib/Exceptions/Exception.hpp | 106 +++++ .../src/CommonLib/Exceptions/TimeoutError.cpp | 17 + .../src/CommonLib/Exceptions/TimeoutError.hpp | 37 ++ CommonLib/src/CommonLib/Lib/Condition.cpp | 333 +++++++++++++ CommonLib/src/CommonLib/Lib/Condition.hpp | 106 +++++ .../src/CommonLib/Lib/CustomDataTypes.hpp | 65 +++ CommonLib/src/CommonLib/Lib/ErrorLog.cpp | 87 ++++ CommonLib/src/CommonLib/Lib/ErrorLog.hpp | 38 ++ CommonLib/src/CommonLib/Lib/EventNames.cpp | 42 ++ CommonLib/src/CommonLib/Lib/EventNames.hpp | 74 +++ CommonLib/src/CommonLib/Lib/IniFile.cpp | 442 ++++++++++++++++++ CommonLib/src/CommonLib/Lib/IniFile.hpp | 242 ++++++++++ CommonLib/src/CommonLib/Lib/LockMutex.cpp | 43 ++ CommonLib/src/CommonLib/Lib/LockMutex.hpp | 53 +++ CommonLib/src/CommonLib/Lib/Mutex.cpp | 99 ++++ CommonLib/src/CommonLib/Lib/Mutex.hpp | 87 ++++ CommonLib/src/CommonLib/Lib/OSObject.cpp | 20 + CommonLib/src/CommonLib/Lib/OSObject.hpp | 89 ++++ CommonLib/src/CommonLib/Lib/Timestamp.cpp | 153 ++++++ CommonLib/src/CommonLib/Lib/Timestamp.hpp | 185 ++++++++ .../src/CommonLib/Lib/Util/DateTimeUtil.cpp | 68 +++ .../src/CommonLib/Lib/Util/DateTimeUtil.hpp | 36 ++ .../src/CommonLib/Lib/Util/FileSystemUtil.cpp | 423 +++++++++++++++++ .../src/CommonLib/Lib/Util/FileSystemUtil.hpp | 208 +++++++++ CommonLib/src/CommonLib/Lib/Util/MiscUtil.cpp | 114 +++++ CommonLib/src/CommonLib/Lib/Util/MiscUtil.hpp | 70 +++ .../src/CommonLib/Lib/Util/StringUtil.cpp | 378 +++++++++++++++ .../src/CommonLib/Lib/Util/StringUtil.hpp | 191 ++++++++ CommonLib/src/CommonLib/Proc/Proc.cpp | 111 +++++ CommonLib/src/CommonLib/Proc/Proc.hpp | 74 +++ CommonLib/src/CommonLib/Threading/Thread.cpp | 131 ++++++ CommonLib/src/CommonLib/Threading/Thread.hpp | 162 +++++++ CommonLib/src/Config/ConfigFileManager.cpp | 69 +++ CommonLib/src/Config/ConfigFileManager.hpp | 122 +++++ CommonLib/src/Config/config.ini | 68 +++ CommonLib/src/LinuxProc.cpp | 61 +++ CommonLib/src/LinuxProc.hpp | 60 +++ CommonLib/src/UI/mainwindow.cpp | 15 + CommonLib/src/UI/mainwindow.h | 21 + CommonLib/src/UI/mainwindow.ui | 22 + CommonLib/src/main.cpp | 11 + 62 files changed, 7294 insertions(+), 2 deletions(-) create mode 100644 CommonLib/CommonLib.pro create mode 100644 CommonLib/readme.txt create mode 100644 CommonLib/src/CommonLib/AutomationMessages/AutomationMsgHandler.cpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/AutomationMsgHandler.hpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/AutomationMsgParser.cpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/AutomationMsgParser.hpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/GenericRspMessage.cpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/GenericRspMessage.hpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/Message.cpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/Message.hpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/MessageHeader.cpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/MessageHeader.hpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/MessageIDs.hpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/TransferAFileCmdMessage.cpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/TransferAFileCmdMessage.hpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.cpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.hpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.cpp create mode 100644 CommonLib/src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.hpp create mode 100644 CommonLib/src/CommonLib/Exceptions/Exception.cpp create mode 100644 CommonLib/src/CommonLib/Exceptions/Exception.hpp create mode 100644 CommonLib/src/CommonLib/Exceptions/TimeoutError.cpp create mode 100644 CommonLib/src/CommonLib/Exceptions/TimeoutError.hpp create mode 100644 CommonLib/src/CommonLib/Lib/Condition.cpp create mode 100644 CommonLib/src/CommonLib/Lib/Condition.hpp create mode 100644 CommonLib/src/CommonLib/Lib/CustomDataTypes.hpp create mode 100644 CommonLib/src/CommonLib/Lib/ErrorLog.cpp create mode 100644 CommonLib/src/CommonLib/Lib/ErrorLog.hpp create mode 100644 CommonLib/src/CommonLib/Lib/EventNames.cpp create mode 100644 CommonLib/src/CommonLib/Lib/EventNames.hpp create mode 100644 CommonLib/src/CommonLib/Lib/IniFile.cpp create mode 100644 CommonLib/src/CommonLib/Lib/IniFile.hpp create mode 100644 CommonLib/src/CommonLib/Lib/LockMutex.cpp create mode 100644 CommonLib/src/CommonLib/Lib/LockMutex.hpp create mode 100644 CommonLib/src/CommonLib/Lib/Mutex.cpp create mode 100644 CommonLib/src/CommonLib/Lib/Mutex.hpp create mode 100644 CommonLib/src/CommonLib/Lib/OSObject.cpp create mode 100644 CommonLib/src/CommonLib/Lib/OSObject.hpp create mode 100644 CommonLib/src/CommonLib/Lib/Timestamp.cpp create mode 100644 CommonLib/src/CommonLib/Lib/Timestamp.hpp create mode 100644 CommonLib/src/CommonLib/Lib/Util/DateTimeUtil.cpp create mode 100644 CommonLib/src/CommonLib/Lib/Util/DateTimeUtil.hpp create mode 100644 CommonLib/src/CommonLib/Lib/Util/FileSystemUtil.cpp create mode 100644 CommonLib/src/CommonLib/Lib/Util/FileSystemUtil.hpp create mode 100644 CommonLib/src/CommonLib/Lib/Util/MiscUtil.cpp create mode 100644 CommonLib/src/CommonLib/Lib/Util/MiscUtil.hpp create mode 100644 CommonLib/src/CommonLib/Lib/Util/StringUtil.cpp create mode 100644 CommonLib/src/CommonLib/Lib/Util/StringUtil.hpp create mode 100644 CommonLib/src/CommonLib/Proc/Proc.cpp create mode 100644 CommonLib/src/CommonLib/Proc/Proc.hpp create mode 100644 CommonLib/src/CommonLib/Threading/Thread.cpp create mode 100644 CommonLib/src/CommonLib/Threading/Thread.hpp create mode 100644 CommonLib/src/Config/ConfigFileManager.cpp create mode 100644 CommonLib/src/Config/ConfigFileManager.hpp create mode 100644 CommonLib/src/Config/config.ini create mode 100644 CommonLib/src/LinuxProc.cpp create mode 100644 CommonLib/src/LinuxProc.hpp create mode 100644 CommonLib/src/UI/mainwindow.cpp create mode 100644 CommonLib/src/UI/mainwindow.h create mode 100644 CommonLib/src/UI/mainwindow.ui create mode 100644 CommonLib/src/main.cpp diff --git a/.gitignore b/.gitignore index 583d423..eaa9902 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,6 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore -/CommonLib/ - # User-specific files *.rsuser *.suo diff --git a/CommonLib/CommonLib.pro b/CommonLib/CommonLib.pro new file mode 100644 index 0000000..8169df4 --- /dev/null +++ b/CommonLib/CommonLib.pro @@ -0,0 +1,103 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG(debug, debug|release){ + DESTDIR = build_x64_debug +} else { + DESTDIR = build_x64_release +} + +OBJECTS_DIR = $${DESTDIR}/OBJ +MOC_DIR = $${DESTDIR}/MOC +RCC_DIR = $${DESTDIR}/RCC +UI_DIR = $${DESTDIR}/UI + +TARGET = CommonLib +CONFIG += c++11 + +DEFINES += LINUX_PLATFORM + +QT += testlib + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + src/CommonLib/AutomationMessages/AutomationMsgHandler.cpp \ + src/CommonLib/AutomationMessages/AutomationMsgParser.cpp \ + src/CommonLib/AutomationMessages/GenericRspMessage.cpp \ + src/CommonLib/AutomationMessages/Message.cpp \ + src/CommonLib/AutomationMessages/MessageHeader.cpp \ + src/CommonLib/AutomationMessages/TransferAFileCmdMessage.cpp \ + src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.cpp \ + src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.cpp \ + src/CommonLib/Exceptions/Exception.cpp \ + src/CommonLib/Exceptions/TimeoutError.cpp \ + src/CommonLib/Lib/Condition.cpp \ + src/CommonLib/Lib/ErrorLog.cpp \ + src/CommonLib/Lib/EventNames.cpp \ + src/CommonLib/Lib/IniFile.cpp \ + src/CommonLib/Lib/LockMutex.cpp \ + src/CommonLib/Lib/Mutex.cpp \ + src/CommonLib/Lib/OSObject.cpp \ + src/CommonLib/Lib/Util/DateTimeUtil.cpp \ + src/CommonLib/Lib/Util/FileSystemUtil.cpp \ + src/CommonLib/Lib/Util/MiscUtil.cpp \ + src/CommonLib/Lib/Util/StringUtil.cpp \ + src/CommonLib/Proc/Proc.cpp \ + src/CommonLib/Lib/Timestamp.cpp \ + src/CommonLib/Threading/Thread.cpp \ + src/LinuxProc.cpp \ + src/UI/mainwindow.cpp \ + src/main.cpp + +HEADERS += \ + src/CommonLib/AutomationMessages/AutomationMsgHandler.hpp \ + src/CommonLib/AutomationMessages/AutomationMsgParser.hpp \ + src/CommonLib/AutomationMessages/GenericRspMessage.hpp \ + src/CommonLib/AutomationMessages/Message.hpp \ + src/CommonLib/AutomationMessages/MessageHeader.hpp \ + src/CommonLib/AutomationMessages/TransferAFileCmdMessage.hpp \ + src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.hpp \ + src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.hpp \ + src/CommonLib/Exceptions/Exception.hpp \ + src/CommonLib/Exceptions/TimeoutError.hpp \ + src/CommonLib/Lib/Condition.hpp \ + src/CommonLib/Lib/ErrorLog.hpp \ + src/CommonLib/Lib/EventNames.hpp \ + src/CommonLib/Lib/IniFile.hpp \ + src/CommonLib/Lib/LockMutex.hpp \ + src/CommonLib/Lib/Mutex.hpp \ + src/CommonLib/Lib/OSObject.hpp \ + src/CommonLib/Lib/Util/DateTimeUtil.hpp \ + src/CommonLib/Lib/Util/FileSystemUtil.hpp \ + src/CommonLib/Lib/Util/MiscUtil.hpp \ + src/CommonLib/Lib/Util/StringUtil.hpp \ + src/CommonLib/Proc/Proc.hpp \ + src/CommonLib/Lib/Timestamp.hpp \ + src/CommonLib/Threading/Thread.hpp \ + src/LinuxProc.hpp \ + src/UI/mainwindow.h + +FORMS += \ + src/UI/mainwindow.ui + +INCLUDEPATH += src/ \ + src/UI/ \ + src/AutomationMessages/ \ + src/CommonLib/Exceptions/ \ + src/CommonLib/Lib/ \ + src/CommonLib/Lib/Util/ \ + src/CommonLib/Proc/ \ + src/CommonLib/Threading/ \ + src/AutomationMessages/ + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +DISTFILES += \ + src/Config/config.ini diff --git a/CommonLib/readme.txt b/CommonLib/readme.txt new file mode 100644 index 0000000..45010fc --- /dev/null +++ b/CommonLib/readme.txt @@ -0,0 +1,4 @@ +Before building, go to project settings -> Build Settings and disable Shadow Build +Shadow Build will put all build output files in a location outside of project folder relative to home directory + +This project is set to output files in the project folder so we can easily locate the files diff --git a/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgHandler.cpp b/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgHandler.cpp new file mode 100644 index 0000000..b0627cb --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgHandler.cpp @@ -0,0 +1,167 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "AutomationMsgHandler.hpp" +#include "Exception.hpp" +#include "MessageHeader.hpp" +#include "MessageIDs.hpp" +#include "ErrorLog.hpp" +#include "LinuxProc.hpp" +#include "IniFile.hpp" +#include "GenericRspMessage.hpp" +#include "Condition.hpp" + +#include "TransferGutsVideosCmdMessage.hpp" +#include "TransferAFileCmdMessage.hpp" +#include "WaitForLastTaskCompletionCmdMessage.hpp" + +//----------------------------------------------------------------------------- + +AutomationMsgHandler::AutomationMsgHandler() +{ +} + +//----------------------------------------------------------------------------- + +AutomationMsgHandler::~AutomationMsgHandler(){} + +//----------------------------------------------------------------------------- + +AutomationMsgHandler& AutomationMsgHandler::instance() +{ + static AutomationMsgHandler handler; + return handler; +} + +//----------------------------------------------------------------------------- + +void AutomationMsgHandler::handleMsg(const unsigned char* pData, const unsigned int& id) +{ + try + { + MessageIDs::MsgIds msgId = (MessageIDs::MsgIds)id; + + if (msgId != MessageIDs::WAIT_FOR_COMPLETION_CMD) + WaitForLastTaskCompletionCmdMessage::s_commandCompletionSuccess = true; + + if (msgId == MessageIDs::TRANSFER_GUTS_VIDEOS_CMD) + { + handleTransferGutsVideosMessage(pData); + } + else if (msgId == MessageIDs::TRANSFER_A_FILE_CMD) + { + handleTransferAFileMessage(pData); + } + else if (msgId == MessageIDs::WAIT_FOR_COMPLETION_CMD) + { + handleWaitForLastTaskCompletionMessage(pData); + } + else + { + std::stringstream ss; + ss << "unknown message id: " << id; + + throw Exception (Q_FUNC_INFO, ss.str()); + } + } + catch (Exception& e) + { + LinuxProc::Instance().getEvent(EventNames::COMMAND_EXECUTTION_IN_PROGRESS).reset(); + LinuxProc::Instance().getEvent(EventNames::COMMAND_EXECUTION_COMPLETE).signal(); + + WaitForLastTaskCompletionCmdMessage::s_commandCompletionSuccess = false; + + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + +//----------------------------------------------------------------------------- + +void AutomationMsgHandler::handleTransferGutsVideosMessage(const unsigned char* pData) +{ + try { + TransferGutsVideosCmdMessage cmd(""); + + cmd.parse(pData); + + if (LinuxProc::Instance().getConfig().getBool("GENERAL", "VERBOSE_LOGGING")) + ErrorLog::Instance().log(cmd.toString(), false, ErrorLog::INFO); + + cmd.executeMessage(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + +//----------------------------------------------------------------------------- + +void AutomationMsgHandler::handleTransferAFileMessage(const unsigned char* pData) +{ + try { + TransferAFileCmdMessage cmd; + + cmd.parse(pData); + + if (LinuxProc::Instance().getConfig().getBool("GENERAL", "VERBOSE_LOGGING")) + ErrorLog::Instance().log(cmd.toString(), false, ErrorLog::INFO); + + cmd.executeMessage(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + + +//----------------------------------------------------------------------------- + +void AutomationMsgHandler::handleWaitForLastTaskCompletionMessage(const unsigned char* pData) +{ + try { + WaitForLastTaskCompletionCmdMessage cmd; + + cmd.parse(pData); + + if (LinuxProc::Instance().getConfig().getBool("GENERAL", "VERBOSE_LOGGING")) + ErrorLog::Instance().log(cmd.toString(), false, ErrorLog::INFO); + + cmd.executeMessage(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} diff --git a/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgHandler.hpp b/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgHandler.hpp new file mode 100644 index 0000000..7bc3e6a --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgHandler.hpp @@ -0,0 +1,103 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include "MessageIDs.hpp" +#include + +class AutomationMsgHandler +{ +public: + //>--------------------------------------------------------------------------- + // Function: instance + // + // Purpose: singleton + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + static AutomationMsgHandler& instance(); + + //>--------------------------------------------------------------------------- + // Function: ~Proc + // + // Purpose: Destroyer + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + ~AutomationMsgHandler(); + + //>--------------------------------------------------------------------------- + // Function: parseMsg + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + void handleMsg(const unsigned char* pData, const unsigned int& id); + + private: + //>--------------------------------------------------------------------------- + // Function: AutomationMsgHandler + // + // Purpose: Ctor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + AutomationMsgHandler(); + + //>--------------------------------------------------------------------------- + // Function: HandleTransferGutsVideosMessage + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + void handleTransferGutsVideosMessage(const unsigned char* pData); + + //>--------------------------------------------------------------------------- + // Function: HandleTransferAFileMessage + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + void handleTransferAFileMessage(const unsigned char* pData); + + //>--------------------------------------------------------------------------- + // Function: HandleWaitForLastTaskCompletionMessage + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + void handleWaitForLastTaskCompletionMessage(const unsigned char* pData); +}; + diff --git a/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgParser.cpp b/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgParser.cpp new file mode 100644 index 0000000..6ca10e6 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgParser.cpp @@ -0,0 +1,184 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "AutomationMsgParser.hpp" +#include "Exception.hpp" +#include "MessageHeader.hpp" +#include "MessageIDs.hpp" +#include "ErrorLog.hpp" +#include "StringUtil.hpp" + +//----------------------------------------------------------------------------- + +AutomationMsgParser::AutomationMsgParser() +{ + gatherCmdMessageIds(); +} + +//----------------------------------------------------------------------------- + +AutomationMsgParser::~AutomationMsgParser(){} + +//----------------------------------------------------------------------------- + +AutomationMsgParser& AutomationMsgParser::instance() +{ + static AutomationMsgParser parser; + return parser; +} + +//----------------------------------------------------------------------------- + +void AutomationMsgParser::gatherCmdMessageIds() +{ + try { + msgIdsVec.push_back(MessageIDs::TRANSFER_A_FILE_CMD); + msgIdsVec.push_back(MessageIDs::TRANSFER_A_FOLDER_CMD); + msgIdsVec.push_back(MessageIDs::TRANSFER_GUTS_VIDEOS_CMD); + + msgIdsVec.push_back(MessageIDs::WAIT_FOR_COMPLETION_CMD); + msgIdsVec.push_back(MessageIDs::GENERIC_RSP); + + } catch (Exception& e) { + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + +//----------------------------------------------------------------------------- + +void AutomationMsgParser::parseMsg(unsigned char* pData, unsigned int numBytes, unsigned int* pMsgId) +{ + bool selfThrown = false; + try + { + MessageHeader header; + + if (numBytes < header.getHeaderLength()) + { + std::stringstream ss; + ss << "Number of bytes received: " << numBytes << ". Expected minimum: " << header.getHeaderLength(); + + selfThrown = true; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + // check to see if we recognize the header + unsigned int msgId = 0; + unsigned int msgLen = 0; + + memcpy(&msgId, &pData[0], 4); + memcpy(&msgLen, &pData[4], 4); + + if (std::find(msgIdsVec.begin(), msgIdsVec.end(), msgId) == msgIdsVec.end()) + { + std::stringstream ss; + ss << "unexpected msg id: " << msgId; + + selfThrown = true; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + if ( msgLen != numBytes) + { + std::stringstream ss; + ss << "Number of bytes received: " << numBytes << ". Expected only: " << msgLen; + + selfThrown = true; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + if (numBytes > header.getHeaderLength()) + verifyDataFormat(pData, numBytes); + + *pMsgId = msgId; + } + catch (Exception& e) + { + if (!selfThrown) + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + +//----------------------------------------------------------------------------- + +void AutomationMsgParser::verifyDataFormat(unsigned char* pData, unsigned int numBytes) +{ + MessageHeader header; + bool selfThrown = false; + unsigned char* pTempDataStart = pData + header.getHeaderLength(); + unsigned char* pTempDataEnd = pData + numBytes - 1; + + // data format is + // [data header - 4 bytes][data - variable size] + // [data header][data][data header][data][data header][data][data header][data] + const unsigned int DATA_HEADER_LENGTH = 4; + + try { + std::stringstream ssError; + + ssError << "Data structure is incorrect. Data: " << Util::Strings::ByteArrayToHexString(pData+header.getHeaderLength(), numBytes - header.getHeaderLength()); + + // want to make sure data size is at least 5 bytes + if (pTempDataStart+DATA_HEADER_LENGTH > pTempDataEnd) + { + selfThrown = true; + throw Exception(Q_FUNC_INFO, ssError.str()); + } + + while (pTempDataStart < pTempDataEnd) + { + unsigned int dataSize = *(reinterpret_cast(pTempDataStart)); + + if (dataSize == 0) + { + selfThrown = true; + throw Exception(Q_FUNC_INFO, ssError.str()); + + } + + pTempDataStart += DATA_HEADER_LENGTH + dataSize; + + if (pTempDataStart-1 > pTempDataEnd) + { + selfThrown = true; + throw Exception(Q_FUNC_INFO, ssError.str()); + } + else if (pTempDataStart-1 == pTempDataEnd) + break; + } + } + catch (Exception& e) + { + if (!selfThrown) + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + diff --git a/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgParser.hpp b/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgParser.hpp new file mode 100644 index 0000000..bb2808b --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/AutomationMsgParser.hpp @@ -0,0 +1,94 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include "MessageIDs.hpp" +#include + +class AutomationMsgParser +{ +public: + //>--------------------------------------------------------------------------- + // Function: instance + // + // Purpose: singleton + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + static AutomationMsgParser& instance(); + + //>--------------------------------------------------------------------------- + // Function: ~Proc + // + // Purpose: Destroyer + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + ~AutomationMsgParser(); + + //>--------------------------------------------------------------------------- + // Function: parseMsg + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + void parseMsg(unsigned char* pData, unsigned int numBytes,unsigned int* pMsgId); + + private: + //>--------------------------------------------------------------------------- + // Function: AutomationMsgParser + // + // Purpose: Ctor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + AutomationMsgParser(); + + //>--------------------------------------------------------------------------- + // Function: gatherCmdMessageIds + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + void gatherCmdMessageIds(); + + //>--------------------------------------------------------------------------- + // Function: verifyDataFormat + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + void verifyDataFormat(unsigned char* pData, unsigned int numBytes); + + std::vector msgIdsVec; +}; + diff --git a/CommonLib/src/CommonLib/AutomationMessages/GenericRspMessage.cpp b/CommonLib/src/CommonLib/AutomationMessages/GenericRspMessage.cpp new file mode 100644 index 0000000..42fb400 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/GenericRspMessage.cpp @@ -0,0 +1,119 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#include +#include +#include "GenericRspMessage.hpp" +#include "Exception.hpp" + +//------------------------------------------------------------------------ + +GenericRspMessage::GenericRspMessage(const bool& wasCommandSuccessful): + Message(MessageIDs::GENERIC_RSP, "GENERIC_RSP", sizeof(MessageStruct)), + m_messageStruct() +{ + m_messageStruct.wasCommandSuccessful = static_cast(wasCommandSuccessful); +} + +//------------------------------------------------------------------------ + +GenericRspMessage::GenericRspMessage(const GenericRspMessage& copy) : + Message(copy), + m_messageStruct(copy.m_messageStruct) +{ +} + +//------------------------------------------------------------------------ + +GenericRspMessage::~GenericRspMessage() +{ +} + +//------------------------------------------------------------------------ + +Message* GenericRspMessage::cloneSelf() +{ + GenericRspMessage* pMsg = new GenericRspMessage(*this); + return pMsg; +} + +//------------------------------------------------------------------------ + +void GenericRspMessage::executeMessage() +{ + try + { + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void GenericRspMessage::formatData(unsigned char* pData) +{ + // Here's how the data is formatted + // each data item is preceded by a 4-byte header which tells us the size of the data item + // 1 data item will have [header][data] + // 2 data items will have [header][data][header][data] + unsigned int dataLen = 0; + const unsigned char* pTempDataStart = pData + getHeaderLength(); + + buildByteArray(&pTempDataStart, reinterpret_cast(&m_messageStruct.wasCommandSuccessful), sizeof(m_messageStruct.wasCommandSuccessful), dataLen); + + // we want update total size of the entire message + m_header.setMessageLength(static_cast(dataLen)); + m_header.format(pData); +} + +//------------------------------------------------------------------------ + +void GenericRspMessage::parseData(const unsigned char* pData) +{ + bool selfThrown = false; + const unsigned char* pTempDataStart = pData; + const unsigned char* pTempDataEnd = pData + m_header.getMessageLength() - m_header.getHeaderLength() - 1; + std::stringstream ss; + + int sizeOfData = 0; + try { + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + // make sure byte number match between what we're expected and what we receive + compareByteNumber(sizeof(m_messageStruct.wasCommandSuccessful), sizeOfData); + memcpy(&m_messageStruct.wasCommandSuccessful, pTempDataStart, sizeOfData); + + } + catch (Exception & e) { + if (!selfThrown) + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + +//------------------------------------------------------------------------ + +std::string GenericRspMessage::toString() const +{ + std::stringstream ss; + + ss << Message::toString() << "wasCommandSuccessful: " << m_messageStruct.wasCommandSuccessful; + + return ss.str(); +} diff --git a/CommonLib/src/CommonLib/AutomationMessages/GenericRspMessage.hpp b/CommonLib/src/CommonLib/AutomationMessages/GenericRspMessage.hpp new file mode 100644 index 0000000..c1b1766 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/GenericRspMessage.hpp @@ -0,0 +1,138 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include "Message.hpp" +#include "MessageIDs.hpp" +#include "MessageHeader.hpp" + +class GenericRspMessage : public Message +{ +public: + //>--------------------------------------------------------------------------- + // Function: GenericRspMessage + // + // Purpose: constructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + GenericRspMessage(const bool& wasCommandSuccessful); + + //>--------------------------------------------------------------------------- + // Function: GenericRspMessage + // + // Purpose: copy constructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + GenericRspMessage(const GenericRspMessage ©); + + //>--------------------------------------------------------------------------- + // Function: ~GenericRspMessage + // + // Purpose: destructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual ~GenericRspMessage(); + + //>--------------------------------------------------------------------------- + // Function: getSuccessfulFlag + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + bool getSuccessfulFlag(){return static_cast(m_messageStruct.wasCommandSuccessful);} + + //>--------------------------------------------------------------------------- + // Function: executeMessage + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void executeMessage(); + + //>--------------------------------------------------------------------------- + // Function: toString + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual std::string toString() const; + +protected: + //>--------------------------------------------------------------------------- + // Function: cloneSelf + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual Message* cloneSelf(); + + //>--------------------------------------------------------------------------- + // Function: formatData + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void formatData(unsigned char* pData); + + //>--------------------------------------------------------------------------- + // Function: parseData + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void parseData(const unsigned char* pData); + +private: + // do not allow + GenericRspMessage &operator=(const GenericRspMessage &rhs); + +#pragma pack(1) + struct MessageStruct + { + unsigned int wasCommandSuccessful; + }; +#pragma pack() + + MessageStruct m_messageStruct; +}; diff --git a/CommonLib/src/CommonLib/AutomationMessages/Message.cpp b/CommonLib/src/CommonLib/AutomationMessages/Message.cpp new file mode 100644 index 0000000..d3c295b --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/Message.cpp @@ -0,0 +1,192 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#include + +#include "Message.hpp" +#include "Exception.hpp" +#include "StringUtil.hpp" + +//------------------------------------------------------------------------ + +Message::Message(const MessageIDs::MsgIds& id, + const std::string& description, + const unsigned short& messageLen) : + m_header(id, messageLen), + m_description(description) +{ +} + +//------------------------------------------------------------------------ + +Message::Message(const Message& copy) : + m_header(copy.m_header), + m_description(copy.m_description) +{ +} + +//------------------------------------------------------------------------ + +Message::~Message() +{ +} + +//------------------------------------------------------------------------ + +Message* Message::clone() +{ + return cloneSelf(); +} + +//------------------------------------------------------------------------ + +void Message::format(unsigned char* pBuffer) +{ + try + { + m_header.format(pBuffer); + formatData(pBuffer); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +std::string Message::getDescription() const +{ + return m_description; +} + +//------------------------------------------------------------------------ + +void Message::parse(const unsigned char* pData) +{ + try + { + m_header.parse(pData); + parseData(pData + getHeaderLength()); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void Message::setMessageLength(const unsigned short& length) +{ + m_header.setMessageLength(length); +} + +//------------------------------------------------------------------------ + +std::string Message::toString() const +{ + std::stringstream ss; + ss << "Message: " << getDescription() << ". " << m_header.toString(); + + return ss.str(); +} + +//------------------------------------------------------------------------ + +int Message::getNextDataItem(const unsigned char** pStartBuf, const unsigned char* pEndBuf, int& sizeOfPreviousData) +{ + bool selfThrown = false; + int sizeOfDataHeader = 4; + int sizeOfData = 0; + std::stringstream ssError; + + *pStartBuf += sizeOfPreviousData; + ssError << "Data structure is incorrect. Data: " << Util::Strings::ByteArrayToHexString(const_cast(*pStartBuf), pEndBuf - *pStartBuf + 1); + try { + // make sure 4 bytes of header information is present + if (*pStartBuf + sizeOfDataHeader > pEndBuf) + { + selfThrown = true; + throw Exception(Q_FUNC_INFO, ssError.str()); + } + + sizeOfData = *(*pStartBuf); + + // we want to make sure the nth header and nth data information match + // header tells us how many bytes in the nth data item, and the bytes + // of data is present in the buffer + if (*pStartBuf + sizeOfDataHeader + sizeOfData -1 <= pEndBuf) + { + // points to the address of the nth data item + *pStartBuf += sizeOfDataHeader; + } + else + { + selfThrown = true; + throw Exception(Q_FUNC_INFO, ssError.str()); + } + } catch (Exception& e) { + if (!selfThrown) + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } + + return sizeOfData; +} + +//------------------------------------------------------------------------ + +void Message::buildByteArray(const unsigned char** pStartBuf, const char* data, const int& sizeOfDataBytes, unsigned int& totalBytesInArray) +{ + // copy the header information for this data item + memcpy(const_cast(*pStartBuf), &sizeOfDataBytes, sizeof(sizeOfDataBytes)); + *pStartBuf += sizeof(sizeOfDataBytes); + totalBytesInArray += sizeof(sizeOfDataBytes); + + // copy the data for this data item + memcpy(const_cast(*pStartBuf), data, sizeOfDataBytes); + *pStartBuf += sizeOfDataBytes; + totalBytesInArray += sizeOfDataBytes; +} + +//------------------------------------------------------------------------ + +void Message::compareByteNumber(int expectedNumBytes, int actualNumbyte) +{ + bool selfThrown = false; + std::stringstream ss; + + try + { + if (expectedNumBytes != actualNumbyte) + { + selfThrown = true; + + ss << "Expected number of bytes received: " << expectedNumBytes << ". Actual number of bytes received: " << actualNumbyte; + throw Exception(Q_FUNC_INFO, ss.str()); + } + } + catch (Exception & e) { + if (!selfThrown) + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} diff --git a/CommonLib/src/CommonLib/AutomationMessages/Message.hpp b/CommonLib/src/CommonLib/AutomationMessages/Message.hpp new file mode 100644 index 0000000..9301f43 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/Message.hpp @@ -0,0 +1,257 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include +#include "MessageIDs.hpp" +#include "MessageHeader.hpp" + +class Message +{ +public: + //>--------------------------------------------------------------------- + // Function: ~Message() + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual ~Message(); + + //>--------------------------------------------------------------------- + // Function: Clone() + // + // Purpose: Get a copy of this object + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: a copy of this object + //<--------------------------------------------------------------------- + virtual Message* clone(); + + //>--------------------------------------------------------------------- + // Function: ExecuteMessage() + // + // Purpose: Executes this message + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void executeMessage() = 0; + + //>--------------------------------------------------------------------- + // Function: Format() + // + // Purpose: Formats this message for sending + //---------------------------------------------------------------------- + // Arguments: pBuffer - the buffer to put the formatting data in + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void format(unsigned char* pBuffer); + + //>--------------------------------------------------------------------- + // Function: GetDescription() + // + // Purpose: Getter for this objects description + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: The description + //<--------------------------------------------------------------------- + virtual std::string getDescription() const; + + //>--------------------------------------------------------------------- + // Function: GetMessageID() + // + // Purpose: Getter for this objects message id + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: The id + //<--------------------------------------------------------------------- + virtual MessageIDs::MsgIds getMessageID() { return m_header.getMessageId(); } + + //>--------------------------------------------------------------------- + // Function: GetEntireMessageLength() + // + // Purpose: Getter for this objects message number of bytes, including the header + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: The entire message number of bytes + //<--------------------------------------------------------------------- + virtual unsigned int getEntireMessageLength() const { return m_header.getMessageLength();} + + //>--------------------------------------------------------------------- + // Function: GetHeaderLength() + // + // Purpose: Getter for this objects message number of header bytes + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: The header number of bytes + //<--------------------------------------------------------------------- + virtual unsigned short getHeaderLength() const { return m_header.getHeaderLength();} + + //>--------------------------------------------------------------------- + // Function: Parse() + // + // Purpose: Takes an array of bytes and populates this object + //---------------------------------------------------------------------- + // Arguments: The array of bytes + //---------------------------------------------------------------------- + // Return Value: None + //<--------------------------------------------------------------------- + virtual void parse(const unsigned char* pData); + + //>--------------------------------------------------------------------- + // Function: ToString() + // + // Purpose: Converts this object into a string + //---------------------------------------------------------------------- + // Arguments: None + //---------------------------------------------------------------------- + // Return Value: The string form of this object + //<--------------------------------------------------------------------- + virtual std::string toString() const; + +protected: + //>--------------------------------------------------------------------- + // Function: Constructor + // + // Purpose: The constructor for the childern to call + //---------------------------------------------------------------------- + // Arguments: id - the message id + // description - the message description + // messageLen - the number of bytes in the message payload (not including the header) + //---------------------------------------------------------------------- + // Return Value: The object + //<--------------------------------------------------------------------- + Message(const MessageIDs::MsgIds& id, + const std::string& description, + const unsigned short& messageLen = 0); + + //>--------------------------------------------------------------------- + // Function: Copy Constructor + // + // Purpose: Create a copy of this object + //---------------------------------------------------------------------- + // Arguments: copy - the object to copy + //---------------------------------------------------------------------- + // Return Value: The object + //<--------------------------------------------------------------------- + Message(const Message ©); + + //>--------------------------------------------------------------------- + // Function: CloneSelf + // + // Purpose: Pure virtual function for children to implement. + // Create a clone of this object + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: The object + //<--------------------------------------------------------------------- + virtual Message* cloneSelf() = 0; + + //>--------------------------------------------------------------------- + // Function: FormatData + // + // Purpose: Pure virtual function for children to implement. + // Format the data + //---------------------------------------------------------------------- + // Arguments: pData - the buffer to format to + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void formatData(unsigned char* pData) = 0; + + //>--------------------------------------------------------------------- + // Function: ParseData + // + // Purpose: Pure virtual function for children to implement. + // Parse the data + //---------------------------------------------------------------------- + // Arguments: pData - the buffer to parse from + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void parseData(const unsigned char* pData) = 0; + + //>--------------------------------------------------------------------- + // Function: SetMessageLength + // + // Purpose: Sets the message length of the payload of the message (not the header) + //---------------------------------------------------------------------- + // Arguments: length - the number of bytes in the payload + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void setMessageLength(const unsigned short& length); + + //>--------------------------------------------------------------------- + // Function: getNextDataItem + // + // Purpose: + // get the next data item + // data format: + // [data header][data][data header][data]... + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + int getNextDataItem(const unsigned char** pStartBuf, const unsigned char* pEndBuf, int& sizeOfPreviousData); + + //>--------------------------------------------------------------------- + // Function: buildByteArray + // + // Purpose: + // this will add to the byte array a data item + // data format: + // [data header][data] + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void buildByteArray(const unsigned char** pStartBuf, const char* data, const int& sizeOfDataBytes, unsigned int& totalBytesInArray); + + //>--------------------------------------------------------------------- + // Function: compareByteNumber + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void compareByteNumber(int expectedNumBytes, int actualNumbyte); + + MessageHeader m_header; + +private: + // do not allow + Message(); + Message &operator=(const Message &rhs); + + std::string m_description; +}; diff --git a/CommonLib/src/CommonLib/AutomationMessages/MessageHeader.cpp b/CommonLib/src/CommonLib/AutomationMessages/MessageHeader.cpp new file mode 100644 index 0000000..9b79ccf --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/MessageHeader.cpp @@ -0,0 +1,102 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#include +#include +#include "MessageHeader.hpp" + +//------------------------------------------------------------------------ + +MessageHeader::MessageHeader(const MessageIDs::MsgIds& id, const unsigned short& messageLen) : + m_headerStruct() +{ + m_headerStruct.messageId = id; + m_headerStruct.messageLength = messageLen + getHeaderLength(); +} + +//------------------------------------------------------------------------ + +MessageHeader::MessageHeader() : + m_headerStruct() +{ + m_headerStruct.messageId = 0; + m_headerStruct.messageLength = 0; +} + +//------------------------------------------------------------------------ + +MessageHeader::MessageHeader(const MessageHeader& copy) : + m_headerStruct(copy.m_headerStruct) +{ +} + +//------------------------------------------------------------------------ + +MessageHeader::~MessageHeader() +{ +} + +//------------------------------------------------------------------------ + +void MessageHeader::format(unsigned char* pData) +{ + memcpy(pData, &m_headerStruct, getHeaderLength()); +} + +//------------------------------------------------------------------------ + +MessageIDs::MsgIds MessageHeader::getMessageId() +{ + return (MessageIDs::MsgIds)m_headerStruct.messageId; +} + +//------------------------------------------------------------------------ + +unsigned short MessageHeader::getHeaderLength() const +{ + return sizeof(m_headerStruct); +} + +//------------------------------------------------------------------------ + +unsigned int MessageHeader::getMessageLength() const +{ + return m_headerStruct.messageLength; +} + +//------------------------------------------------------------------------ + +void MessageHeader::parse(const unsigned char* pData) +{ + ::memcpy(&m_headerStruct, pData, getHeaderLength()); +} + +//------------------------------------------------------------------------ + +void MessageHeader::setMessageLength(const unsigned short& length) +{ + m_headerStruct.messageLength = length + getHeaderLength(); +} + +//------------------------------------------------------------------------ + +std::string MessageHeader::toString() const +{ + std::stringstream ss; + ss << "msg id: 0x" << std::hex << std::uppercase << m_headerStruct.messageId << std::nouppercase << ". " << "msg len: " << std::dec << m_headerStruct.messageLength; + + return ss.str(); +} diff --git a/CommonLib/src/CommonLib/AutomationMessages/MessageHeader.hpp b/CommonLib/src/CommonLib/AutomationMessages/MessageHeader.hpp new file mode 100644 index 0000000..be4b4d1 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/MessageHeader.hpp @@ -0,0 +1,163 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include + +#include "MessageIDs.hpp" + +class MessageHeader +{ +public: + //>--------------------------------------------------------------------- + // Function: MessageHeader + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments: id - the message id + // messageLen - the number of bytes of the entire message including the header + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + MessageHeader(const MessageIDs::MsgIds& id, const unsigned short& messageLen); + + //>--------------------------------------------------------------------- + // Function: MessageHeader + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + MessageHeader(); + + //>--------------------------------------------------------------------- + // Function: Copy Constructor + // + // Purpose: Create a copy of this object + //---------------------------------------------------------------------- + // Arguments: copy - the object to copy + //---------------------------------------------------------------------- + // Return Value: The object + //<--------------------------------------------------------------------- + MessageHeader(const MessageHeader ©); + + //>--------------------------------------------------------------------- + // Function: ~MessageHeader() + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual ~MessageHeader(); + + //>--------------------------------------------------------------------- + // Function: Format() + // + // Purpose: Formats this message for sending + //---------------------------------------------------------------------- + // Arguments: pBuffer - the buffer to put the formatting data in + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void format(unsigned char* pData); + + //>--------------------------------------------------------------------- + // Function: GetHeaderLength() + // + // Purpose: Get the number of bytes in the header + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: number of bytes in the header + //<--------------------------------------------------------------------- + unsigned short getHeaderLength() const; + + //>--------------------------------------------------------------------- + // Function: GetMessageLength() + // + // Purpose: Get the number of bytes in the entire message, including header + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: number of bytes in the message + //<--------------------------------------------------------------------- + unsigned int getMessageLength() const; + + //>--------------------------------------------------------------------- + // Function: GetMessageId() + // + // Purpose: Get the message id + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: the message id + //<--------------------------------------------------------------------- + MessageIDs::MsgIds getMessageId(); + + //>--------------------------------------------------------------------- + // Function: Parse() + // + // Purpose: Takes an array of bytes and populates this object + //---------------------------------------------------------------------- + // Arguments: The array of bytes + //---------------------------------------------------------------------- + // Return Value: None + //<--------------------------------------------------------------------- + void parse(const unsigned char* pData); + + //>--------------------------------------------------------------------- + // Function: SetMessageLength + // + // Purpose: Sets the message length of the payload of the message (not the header) + //---------------------------------------------------------------------- + // Arguments: length - the number of bytes in the payload + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void setMessageLength(const unsigned short& length); + + //>--------------------------------------------------------------------- + // Function: ToString() + // + // Purpose: Converts this object into a string + //---------------------------------------------------------------------- + // Arguments: None + //---------------------------------------------------------------------- + // Return Value: The string form of this object + //<--------------------------------------------------------------------- + std::string toString() const; + +protected: + +private: + // do not allow + MessageHeader &operator=(const MessageHeader &rhs); + +#pragma pack(1) + struct HeaderStruct + { + unsigned int messageId; + unsigned int messageLength; + }; +#pragma pack() + + HeaderStruct m_headerStruct; +}; diff --git a/CommonLib/src/CommonLib/AutomationMessages/MessageIDs.hpp b/CommonLib/src/CommonLib/AutomationMessages/MessageIDs.hpp new file mode 100644 index 0000000..3e65ee9 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/MessageIDs.hpp @@ -0,0 +1,32 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + + +namespace MessageIDs +{ + enum MsgIds + { + TRANSFER_A_FILE_CMD = 0xCA001001, + TRANSFER_A_FOLDER_CMD = 0xCA001002, + TRANSFER_GUTS_VIDEOS_CMD = 0xCA001003, + + WAIT_FOR_COMPLETION_CMD = 0xCA002000, + + GENERIC_RSP = 0xCA002001 + }; +} diff --git a/CommonLib/src/CommonLib/AutomationMessages/TransferAFileCmdMessage.cpp b/CommonLib/src/CommonLib/AutomationMessages/TransferAFileCmdMessage.cpp new file mode 100644 index 0000000..0bb5aa3 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/TransferAFileCmdMessage.cpp @@ -0,0 +1,139 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#include +#include +#include "TransferAFileCmdMessage.hpp" +#include "LinuxProc.hpp" +#include "Exception.hpp" +#include "ErrorLog.hpp" +#include "StringUtil.hpp" +#include "GenericRspMessage.hpp" + +//------------------------------------------------------------------------ + +TransferAFileCmdMessage::TransferAFileCmdMessage(std::string fromFile, std::string toFile, bool deleteSource): + Message(MessageIDs::TRANSFER_A_FILE_CMD, "TRANSFER_A_FILE_CMD"), + m_fromFile(fromFile), + m_toFile(toFile), + m_deleteSource(static_cast(deleteSource)) +{ +} + +//------------------------------------------------------------------------ + +TransferAFileCmdMessage::TransferAFileCmdMessage(const TransferAFileCmdMessage& copy) : + Message(copy), + m_fromFile(copy.m_fromFile), + m_toFile(copy.m_toFile), + m_deleteSource(copy.m_deleteSource) +{ +} + +//------------------------------------------------------------------------ + +TransferAFileCmdMessage::~TransferAFileCmdMessage() +{ +} + +//------------------------------------------------------------------------ + +Message* TransferAFileCmdMessage::cloneSelf() +{ + TransferAFileCmdMessage* pMsg = new TransferAFileCmdMessage(*this); + return pMsg; +} + +//------------------------------------------------------------------------ + +void TransferAFileCmdMessage::executeMessage() +{ + try + { + GenericRspMessage rsp(true); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void TransferAFileCmdMessage::formatData(unsigned char* pData) +{ + // Here's how the data is formatted + // each data item is preceded by a 4-byte header which tells us the size of the data item + // 1 data item will have [header][data] + // 2 data items will have [header][data][header][data] + unsigned int dataLen = 0; + const unsigned char* pTempDataStart = pData + getHeaderLength(); + + buildByteArray(&pTempDataStart, m_fromFile.c_str(), m_fromFile.length(), dataLen); + buildByteArray(&pTempDataStart, m_toFile.c_str(), m_toFile.length(), dataLen); + buildByteArray(&pTempDataStart, reinterpret_cast(&m_deleteSource), sizeof(m_deleteSource), dataLen); + + // we want update total size of the entire message + m_header.setMessageLength(static_cast(dataLen)); + m_header.format(pData); +} + +//------------------------------------------------------------------------ + +void TransferAFileCmdMessage::parseData(const unsigned char* pData) +{ + bool selfThrown = false; + const unsigned char* pTempDataStart = pData; + const unsigned char* pTempDataEnd = pData + m_header.getMessageLength() - m_header.getHeaderLength() - 1; + + int sizeOfData = 0; + try { + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + m_fromFile = Util::Strings::ByteArrayToString(const_cast(pTempDataStart), sizeOfData); + + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + m_toFile = Util::Strings::ByteArrayToString(const_cast(pTempDataStart), sizeOfData); + + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + // make sure byte number match between what we're expected and what we receive + compareByteNumber(sizeof(m_deleteSource), sizeOfData); + memcpy(&m_deleteSource, pTempDataStart, sizeOfData); + + } catch (Exception& e) { + if (!selfThrown) + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + +//------------------------------------------------------------------------ + +std::string TransferAFileCmdMessage::toString() const +{ + std::stringstream ss; + + ss << "From file: " << m_fromFile << ". To file: " << m_toFile << ". Delete source: " << m_deleteSource; + + return ss.str(); +} diff --git a/CommonLib/src/CommonLib/AutomationMessages/TransferAFileCmdMessage.hpp b/CommonLib/src/CommonLib/AutomationMessages/TransferAFileCmdMessage.hpp new file mode 100644 index 0000000..e2a32fb --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/TransferAFileCmdMessage.hpp @@ -0,0 +1,124 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include "Message.hpp" +#include "MessageIDs.hpp" +#include "MessageHeader.hpp" + +class TransferAFileCmdMessage : public Message +{ +public: + //>--------------------------------------------------------------------------- + // Function: TransferAFileCmdMessage + // + // Purpose: constructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + TransferAFileCmdMessage(std::string fromFile = "", std::string toFile = "", bool deleteSource = false); + + //>--------------------------------------------------------------------------- + // Function: TransferAFileCmdMessage + // + // Purpose: copy constructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + TransferAFileCmdMessage(const TransferAFileCmdMessage ©); + + //>--------------------------------------------------------------------------- + // Function: ~TransferAFileCmdMessage + // + // Purpose: destructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual ~TransferAFileCmdMessage(); + + //>--------------------------------------------------------------------------- + // Function: ExecuteMessage + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void executeMessage(); + + //>--------------------------------------------------------------------------- + // Function: ToString + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual std::string toString() const; + +protected: + //>--------------------------------------------------------------------------- + // Function: CloneSelf + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual Message* cloneSelf(); + + //>--------------------------------------------------------------------------- + // Function: FormatData + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void formatData(unsigned char* pData); + + //>--------------------------------------------------------------------------- + // Function: ParseData + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void parseData(const unsigned char* pData); + +private: + // do not allow + TransferAFileCmdMessage &operator=(const TransferAFileCmdMessage &rhs); + + std::string m_fromFile; + + std::string m_toFile; + + unsigned int m_deleteSource; +}; diff --git a/CommonLib/src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.cpp b/CommonLib/src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.cpp new file mode 100644 index 0000000..828e368 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.cpp @@ -0,0 +1,173 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#include +#include +#include "TransferGutsVideosCmdMessage.hpp" +#include "LinuxProc.hpp" +#include "Exception.hpp" +#include "ErrorLog.hpp" +#include "Condition.hpp" +#include "StringUtil.hpp" +#include "GenericRspMessage.hpp" +#include "IniFile.hpp" + +//------------------------------------------------------------------------ + +TransferGutsVideosCmdMessage::TransferGutsVideosCmdMessage(std::string currentTestFolder, unsigned int transferEverything, unsigned int deleteSource): + Message(MessageIDs::TRANSFER_GUTS_VIDEOS_CMD, "TRANSFER_GUTS_VIDEOS_CMD"), + m_currentTestFolder(currentTestFolder), + m_transferEverything(transferEverything), + m_deleteSource(deleteSource) +{ +} + +//------------------------------------------------------------------------ + +TransferGutsVideosCmdMessage::TransferGutsVideosCmdMessage(const TransferGutsVideosCmdMessage& copy) : + Message(copy), + m_currentTestFolder(copy.m_currentTestFolder), + m_transferEverything(copy.m_transferEverything), + m_deleteSource(copy.m_deleteSource) +{ +} + +//------------------------------------------------------------------------ + +TransferGutsVideosCmdMessage::~TransferGutsVideosCmdMessage() +{ +} + +//------------------------------------------------------------------------ + +Message* TransferGutsVideosCmdMessage::cloneSelf() +{ + TransferGutsVideosCmdMessage* pMsg = new TransferGutsVideosCmdMessage(*this); + return pMsg; +} + +//------------------------------------------------------------------------ + +void TransferGutsVideosCmdMessage::executeMessage() +{ + try + { + Condition& commandExecutingEvent = LinuxProc::Instance().getEvent(EventNames::COMMAND_EXECUTTION_IN_PROGRESS); + Condition& completeEvent = LinuxProc::Instance().getEvent(EventNames::COMMAND_EXECUTION_COMPLETE); + + completeEvent.reset(); + if (commandExecutingEvent.timedWait(0,0)) // a command is current executing + { + completeEvent.wait(); // wait until current command finish executing + } + + // signal this command is currently executing to prevent any other command from executing + commandExecutingEvent.signal(); + + GenericRspMessage rsp(true); + + gatherInfo(); + + Condition& event = LinuxProc::Instance().getEvent(EventNames::START_FILE_TRANSFER); + event.signal(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void TransferGutsVideosCmdMessage::gatherInfo() +{ + bool selfThrown = false; + + try + { + + } + catch (Exception& e) + { + if (!selfThrown) + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void TransferGutsVideosCmdMessage::formatData(unsigned char* pData) +{ + // Here's how the data is formatted + // each data item is preceded by a 4-byte header which tells us the size of the data item + // 1 data item will have [header][data] + // 2 data items will have [header][data][header][data] + unsigned int dataLen = 0; + const unsigned char* pTempDataStart = pData + getHeaderLength(); + + buildByteArray(&pTempDataStart, m_currentTestFolder.c_str(), m_currentTestFolder.length(), dataLen); + buildByteArray(&pTempDataStart, reinterpret_cast(&m_transferEverything), sizeof(m_transferEverything), dataLen); + buildByteArray(&pTempDataStart, reinterpret_cast(&m_deleteSource), sizeof(m_deleteSource), dataLen); + + // we want update total size of the entire message + m_header.setMessageLength(static_cast(dataLen)); + m_header.format(pData); +} + +//------------------------------------------------------------------------ + +void TransferGutsVideosCmdMessage::parseData(const unsigned char* pData) +{ + bool selfThrown = false; + const unsigned char* pTempDataStart = pData; + const unsigned char* pTempDataEnd = pData + m_header.getMessageLength() - m_header.getHeaderLength() - 1; + + int sizeOfData = 0; + try { + // get the address of the 1st data item + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + m_currentTestFolder = Util::Strings::ByteArrayToString(const_cast(pTempDataStart), sizeOfData); + + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + // make sure byte number match between what we're expected and what we receive + compareByteNumber(sizeof(m_transferEverything), sizeOfData); + memcpy(&m_transferEverything, pTempDataStart, sizeOfData); + + sizeOfData = getNextDataItem(&pTempDataStart, pTempDataEnd, sizeOfData); + // make sure byte number match between what we're expected and what we receive + compareByteNumber(sizeof(m_deleteSource), sizeOfData); + memcpy(&m_deleteSource, pTempDataStart, sizeOfData); + + } catch (Exception& e) { + if (!selfThrown) + e.buildStackTrace(Q_FUNC_INFO); + + throw e; + } +} + +//------------------------------------------------------------------------ + +std::string TransferGutsVideosCmdMessage::toString() const +{ + std::stringstream ss; + + ss << "current test folder: " << m_currentTestFolder << ". transfer everything: " << m_transferEverything << ". Delete source: " << m_deleteSource; + + return ss.str(); +} diff --git a/CommonLib/src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.hpp b/CommonLib/src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.hpp new file mode 100644 index 0000000..8913e8b --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/TransferGutsVideosCmdMessage.hpp @@ -0,0 +1,142 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include "Message.hpp" +#include "MessageIDs.hpp" +#include "MessageHeader.hpp" + +class TransferGutsVideosCmdMessage : public Message +{ +public: + //>--------------------------------------------------------------------- + // Function: TransferGutsVideosCmdMessage + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + TransferGutsVideosCmdMessage(std::string currentTestFolder, unsigned int transferEverything = 0, unsigned int deleteSource = 0); + + //>--------------------------------------------------------------------- + // Function: TransferGutsVideosCmdMessage + // + // Purpose: copy constructor + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + TransferGutsVideosCmdMessage(const TransferGutsVideosCmdMessage ©); + + //>--------------------------------------------------------------------- + // Function: ~TransferGutsVideosCmdMessage + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual ~TransferGutsVideosCmdMessage(); + + //>--------------------------------------------------------------------- + // Function: ExecuteMessage + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void executeMessage(); + + //>--------------------------------------------------------------------- + // Function: ToString + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual std::string toString() const; + +protected: + //>--------------------------------------------------------------------- + // Function: CloneSelf + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual Message* cloneSelf(); + + //>--------------------------------------------------------------------- + // Function: FormatData + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void formatData(unsigned char* pData); + + //>--------------------------------------------------------------------- + // Function: ParseData + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void parseData(const unsigned char* pData); + + //>--------------------------------------------------------------------- + // Function: gatherInfo + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void gatherInfo(); + +private: + // do not allow + TransferGutsVideosCmdMessage &operator=(const TransferGutsVideosCmdMessage &rhs); + + std::string m_currentTestFolder; + unsigned int m_transferEverything; + unsigned int m_deleteSource; +}; diff --git a/CommonLib/src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.cpp b/CommonLib/src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.cpp new file mode 100644 index 0000000..4378831 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.cpp @@ -0,0 +1,96 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#include +#include +#include "WaitForLastTaskCompletionCmdMessage.hpp" +#include "Exception.hpp" +#include "Condition.hpp" +#include "LinuxProc.hpp" +#include "GenericRspMessage.hpp" + +bool WaitForLastTaskCompletionCmdMessage::s_commandCompletionSuccess = true; + +//------------------------------------------------------------------------ + +WaitForLastTaskCompletionCmdMessage::WaitForLastTaskCompletionCmdMessage(): + Message(MessageIDs::WAIT_FOR_COMPLETION_CMD, "WAIT_FOR_COMPLETION_CMD") +{ +} + +//------------------------------------------------------------------------ + +WaitForLastTaskCompletionCmdMessage::WaitForLastTaskCompletionCmdMessage(const WaitForLastTaskCompletionCmdMessage& copy) : + Message(copy) +{ +} + +//------------------------------------------------------------------------ + +WaitForLastTaskCompletionCmdMessage::~WaitForLastTaskCompletionCmdMessage() +{ +} + +//------------------------------------------------------------------------ + +Message* WaitForLastTaskCompletionCmdMessage::cloneSelf() +{ + WaitForLastTaskCompletionCmdMessage* pMsg = new WaitForLastTaskCompletionCmdMessage(*this); + return pMsg; +} + +//------------------------------------------------------------------------ + +void WaitForLastTaskCompletionCmdMessage::executeMessage() +{ + try + { + Condition& event = LinuxProc::Instance().getEvent(EventNames::COMMAND_EXECUTION_COMPLETE); + event.wait(false); + + GenericRspMessage rsp(s_commandCompletionSuccess); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void WaitForLastTaskCompletionCmdMessage::formatData(unsigned char*) +{ + // no data to send +} + +//------------------------------------------------------------------------ + +void WaitForLastTaskCompletionCmdMessage::parseData(const unsigned char*) +{ + // no data to process +} + +//------------------------------------------------------------------------ + +std::string WaitForLastTaskCompletionCmdMessage::toString() const +{ + std::stringstream ss; + + ss << ""; + + return ss.str(); +} diff --git a/CommonLib/src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.hpp b/CommonLib/src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.hpp new file mode 100644 index 0000000..ac37371 --- /dev/null +++ b/CommonLib/src/CommonLib/AutomationMessages/WaitForLastTaskCompletionCmdMessage.hpp @@ -0,0 +1,121 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include "Message.hpp" +#include "MessageIDs.hpp" +#include "MessageHeader.hpp" + +class WaitForLastTaskCompletionCmdMessage : public Message +{ +public: + //>--------------------------------------------------------------------------- + // Function: WaitForLastTaskCompletionCmdMessage + // + // Purpose: constructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + WaitForLastTaskCompletionCmdMessage(); + + //>--------------------------------------------------------------------------- + // Function: WaitForLastTaskCompletionCmdMessage + // + // Purpose: copy constructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + WaitForLastTaskCompletionCmdMessage(const WaitForLastTaskCompletionCmdMessage ©); + + //>--------------------------------------------------------------------------- + // Function: ~WaitForLastTaskCompletionCmdMessage + // + // Purpose: destructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual ~WaitForLastTaskCompletionCmdMessage(); + + //>--------------------------------------------------------------------------- + // Function: ExecuteMessage + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void executeMessage(); + + //>--------------------------------------------------------------------------- + // Function: ToString + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual std::string toString() const; + + // indicate command executed successfully or not + static bool s_commandCompletionSuccess; + +protected: + //>--------------------------------------------------------------------------- + // Function: CloneSelf + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual Message* cloneSelf(); + + //>--------------------------------------------------------------------------- + // Function: FormatData + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void formatData(unsigned char* pData); + + //>--------------------------------------------------------------------------- + // Function: ParseData + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual void parseData(const unsigned char* pData); + +private: + // do not allow + WaitForLastTaskCompletionCmdMessage &operator=(const WaitForLastTaskCompletionCmdMessage &rhs); +}; diff --git a/CommonLib/src/CommonLib/Exceptions/Exception.cpp b/CommonLib/src/CommonLib/Exceptions/Exception.cpp new file mode 100644 index 0000000..a29864e --- /dev/null +++ b/CommonLib/src/CommonLib/Exceptions/Exception.cpp @@ -0,0 +1,108 @@ +#include "Exception.hpp" +#include "StringUtil.hpp" + +#include + +#include +//------------------------------------------------------------------------ + +Exception::Exception(std::string functionName, const std::string& errorMsg, const std::string& exceptionClass) : + exception() +{ + std::string className = ""; + if (exceptionClass.length() == 0) + className = std::string(typeid(this).name()); + else + className = exceptionClass; + + // get the name of the class without the extra add-ons + QRegularExpression re("^[a-z][0-9]+([a-z].+)", QRegularExpression::CaseInsensitiveOption); + QRegularExpressionMatch match = re.match(className.c_str()); + if (match.hasMatch()) { + m_message = match.captured(1).toStdString() + ": "; + } + + m_lastFunction = functionName; + + QRegularExpression re2("([^ \\\(]+\\\().+", QRegularExpression::CaseInsensitiveOption); + QRegularExpressionMatch match2 = re2.match(functionName.c_str()); + + if (match2.hasMatch()) { + m_message += match2.captured(1).toStdString() + ") - " + errorMsg; + } + + m_fullStackTraceMessage = m_message; + m_message = errorMsg; +} + +//------------------------------------------------------------------------ + +Exception::Exception(const Exception &rhs) +{ + copy(rhs); +} + +//------------------------------------------------------------------------ + +Exception &Exception::operator=(const Exception &rhs) +{ + return copy(rhs); +} + +//------------------------------------------------------------------------ + +std::string Exception::getMessage(Message_Format msgFormat) +{ + std::string msg = m_message; + + if (msgFormat == ERROR_MESSAGE_WITH_STACKTRACE) + msg = m_fullStackTraceMessage; + + return msg; +} + +//------------------------------------------------------------------------ + +void Exception::buildStackTrace(std::string functionName, const std::string& errorMsg) +{ + if (!Util::Strings::StringsAreEqual(functionName, m_lastFunction, false)) + { + m_lastFunction = functionName; + + QRegularExpression re2("([^ \\\(]+\\\().+", QRegularExpression::CaseInsensitiveOption); + QRegularExpressionMatch match2 = re2.match(functionName.c_str()); + + if (match2.hasMatch()) { + m_fullStackTraceMessage.append(" -> "); + m_fullStackTraceMessage.append(match2.captured(1).toStdString() + ")"); + } + + if (errorMsg.length() > 0) + { + m_fullStackTraceMessage.append(" - " + errorMsg); + m_message = errorMsg; + } + } +} + +//------------------------------------------------------------------------ + +Exception &Exception::copy(const Exception &rhs) +{ + if (this == &rhs) + { + return *this; + } + + m_message = rhs.m_message; + m_fullStackTraceMessage = rhs.m_fullStackTraceMessage; + m_lastFunction = rhs.m_lastFunction; + + return *this; +} + +//------------------------------------------------------------------------ + +Exception::~Exception() throw() +{ +} diff --git a/CommonLib/src/CommonLib/Exceptions/Exception.hpp b/CommonLib/src/CommonLib/Exceptions/Exception.hpp new file mode 100644 index 0000000..9631e3c --- /dev/null +++ b/CommonLib/src/CommonLib/Exceptions/Exception.hpp @@ -0,0 +1,106 @@ +#pragma once + +#include +#include +#include + +class Exception : public std::exception +{ +public: + enum Message_Format + { + ERROR_MESSAGE_WITH_STACKTRACE, // entire stack trace + ERROR_MESSAGE_ONLY // the function that throw the exception + }; + + //>--------------------------------------------------------------------- + // Function: Exception() + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments: + // functionName - should use custom macro __FUNCTION_NAME__ + // to get the name of offending function + // errorMsg - description of what caused the exception + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + Exception(const std::string functionName, const std::string& errorMsg, const std::string& exceptionClass = ""); + + //>--------------------------------------------------------------------- + // Function: Exception() + // + // Purpose: copy constructor + // this gets called when "throw ex" executes rather than throw + // throw; throws the same exception object + // throw ex; throws a new exception + //---------------------------------------------------------------------- + // Arguments: const Exception & - reference to Exception obj + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + Exception(const Exception &rhs); + + //>--------------------------------------------------------------------- + // Function: ~Exception() + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual ~Exception() throw(); + + //>--------------------------------------------------------------------- + // Function: getMessage() + // + // Purpose: to get the message data member + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: std::string - the message string + //<--------------------------------------------------------------------- + std::string getMessage(Message_Format msgFormat= ERROR_MESSAGE_WITH_STACKTRACE); + + //>--------------------------------------------------------------------- + // Function: buildStackTrace() + // + // Purpose: Build the stack trace of function calls for debugging purposes + //---------------------------------------------------------------------- + // Arguments: + // functionName + // + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void buildStackTrace(std::string functionName, const std::string& errorMsg = ""); + +protected: + std::string m_message; + std::string m_fullStackTraceMessage; + std::string m_lastFunction; + +private: + //>--------------------------------------------------------------------- + // Function: copy() + // + // Purpose: to do the work for assignment operator and copy ctor + //---------------------------------------------------------------------- + // Arguments: const Exception &rhs - const reference to Exception obj + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + Exception ©(const Exception &rhs); + + //>--------------------------------------------------------------------- + // Function: operator&() + // + // Purpose: assignment operator + //---------------------------------------------------------------------- + // Arguments: const Exception & - reference to Exception obj + //---------------------------------------------------------------------- + // Return Value: reference to Exception obj + //<--------------------------------------------------------------------- + Exception &operator=(const Exception &rhs); +}; diff --git a/CommonLib/src/CommonLib/Exceptions/TimeoutError.cpp b/CommonLib/src/CommonLib/Exceptions/TimeoutError.cpp new file mode 100644 index 0000000..0ee4b2e --- /dev/null +++ b/CommonLib/src/CommonLib/Exceptions/TimeoutError.cpp @@ -0,0 +1,17 @@ +#include "TimeoutError.hpp" + +#include + +//------------------------------------------------------------------------ + +TimeoutError::TimeoutError(std::string functionName, const std::string& errorMsg, TIME_OUT_TYPE timeOutType) : + Exception(functionName, errorMsg, std::string(typeid(this).name())), + m_timeOutType(timeOutType) +{ +} + +//------------------------------------------------------------------------ + +TimeoutError::~TimeoutError() throw() +{ +} diff --git a/CommonLib/src/CommonLib/Exceptions/TimeoutError.hpp b/CommonLib/src/CommonLib/Exceptions/TimeoutError.hpp new file mode 100644 index 0000000..a164bcc --- /dev/null +++ b/CommonLib/src/CommonLib/Exceptions/TimeoutError.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "Exception.hpp" + +class TimeoutError : public Exception +{ +public: + enum TIME_OUT_TYPE + { + TIME_OUT_GENERIC, + TIME_OUT_SOCKET + }; + + //>--------------------------------------------------------------------- + // Function: TimeoutError() + // + // Purpose: default and non-default constructor + //---------------------------------------------------------------------- + // Arguments: std::string msg - a message to be printed + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + TimeoutError(std::string functionName, const std::string& errorMsg, TIME_OUT_TYPE timeOutType = TIME_OUT_GENERIC); + + //>--------------------------------------------------------------------- + // Function: ~TimeoutError() + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual ~TimeoutError() throw(); + + TIME_OUT_TYPE m_timeOutType; +}; diff --git a/CommonLib/src/CommonLib/Lib/Condition.cpp b/CommonLib/src/CommonLib/Lib/Condition.cpp new file mode 100644 index 0000000..7cd3fb0 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Condition.cpp @@ -0,0 +1,333 @@ +#include +#include +#include +#include "Condition.hpp" +#include "Exception.hpp" +#include "TimeoutError.hpp" + +Condition::Condition(const std::string& name) : + OSObject(name), + m_state(NOT_SIGNALED) +{ + try + { + pthread_condattr_t attr; + int ret = pthread_condattr_init(&attr); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_condattr_init returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + ret = pthread_cond_init(&m_condition, &attr); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_cond_init returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + pthread_mutexattr_t mutexAttr; + ret = pthread_mutexattr_init(&mutexAttr); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutexattr_init returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + ret = pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutexattr_settype returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + ret = pthread_mutex_init(&m_mutex, &mutexAttr); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutex_lock returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } +} + +//------------------------------------------------------------------------ + +Condition::~Condition() +{ + int ret = pthread_cond_destroy(&m_condition); + + if (ret != 0) + { + // it is being destroyed, nothing to do + } +} + +//------------------------------------------------------------------------ + +void Condition::broadcast() +{ + try + { + lockMutex(); + + int ret = pthread_cond_broadcast(&m_condition); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_cond_broadcast returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + m_state = SIGNALED; + + unlockMutex(); + } + catch (Exception& e) + { + m_state = NOT_SIGNALED; + + unlockMutex(); + + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + + +void Condition::lockMutex() +{ + int ret = pthread_mutex_lock(&m_mutex); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutex_lock returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } +} + +//------------------------------------------------------------------------ + +void Condition::reset() +{ + try + { + lockMutex(); + m_state = NOT_SIGNALED; + unlockMutex(); + } + catch (Exception& e) + { + unlockMutex(); + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void Condition::signal() +{ + try + { + lockMutex(); + + m_state = SIGNALED; + + int ret = pthread_cond_signal(&m_condition); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_cond_signal returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + unlockMutex(); + } + catch (Exception& e) + { + m_state = NOT_SIGNALED; + + unlockMutex(); + + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +bool Condition::timedWait(const unsigned long seconds, const unsigned long nanoseconds, bool shallWeReset) +{ + try + { + lockMutex(); + + // looping because pthread_cond_wait can return even if not signaled + while (m_state != SIGNALED) + { + timespec waitTime; + int status = 0; + + if( ( 0 != seconds ) || ( 0 != nanoseconds ) ) + { + status = clock_gettime(CLOCK_REALTIME , &waitTime); + + if( 0 != status ) + { + std::stringstream ss; + ss << "clock_gettime returned: " << status; + throw Exception(Q_FUNC_INFO, ss.str()); + } + else + { + waitTime.tv_nsec += nanoseconds; + + // check for more than one second's worth of nanoseconds + if( 999999999L < waitTime.tv_nsec ) + { + waitTime.tv_nsec -= 1000000000L; + waitTime.tv_sec += 1; + } + + waitTime.tv_sec += seconds; + } + } + else + { + waitTime.tv_sec = 0; + waitTime.tv_nsec = 0; + } + + status = pthread_cond_timedwait(&m_condition, &m_mutex, &waitTime); + + //@@@ + /*if (status == TIMEDOUT) + { + std::stringstream ss; + ss << "Condition::TimedWait() - pthread_cond_timedwait returned: " << status; + throw TimeoutError(ss.str()); + }*/ + if(status != 0) + { + std::stringstream ss; + ss << "pthread_cond_timedwait returned: " << status; + throw TimeoutError(Q_FUNC_INFO, ss.str()); + } + }// end while + + if (shallWeReset == true) + { + reset(); + } + + unlockMutex(); + + return true; + } + catch (TimeoutError& e) + { + if (shallWeReset == true) + { + reset(); + } + + unlockMutex(); + + return false; + } + catch (Exception& e) + { + if (shallWeReset == true) + { + reset(); + } + + unlockMutex(); + + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void Condition::unlockMutex() +{ + try + { + int ret = pthread_mutex_unlock(&m_mutex); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutex_unlock returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +void Condition::wait(bool shallWeReset) +{ + try + { + lockMutex(); + + // looping because pthread_cond_wait can return even if not signaled + while (m_state != SIGNALED) + { + int ret = pthread_cond_wait(&m_condition, &m_mutex); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_cond_wait returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + } + + if (shallWeReset == true) + { + reset(); + } + + unlockMutex(); + } + catch (Exception& e) + { + if (shallWeReset == true) + { + reset(); + } + + unlockMutex(); + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} diff --git a/CommonLib/src/CommonLib/Lib/Condition.hpp b/CommonLib/src/CommonLib/Lib/Condition.hpp new file mode 100644 index 0000000..1defe3e --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Condition.hpp @@ -0,0 +1,106 @@ +#pragma once + +#include +#include "OSObject.hpp" + +class Condition : public OSObject +{ +public: + enum State + { + NOT_SIGNALED, + SIGNALED + }; + + //>--------------------------------------------------------------------- + // Function: Condition + // + // Purpose: The constructor + //---------------------------------------------------------------------- + // Arguments: name - the name of the condition + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + Condition(const std::string& name); + + //>--------------------------------------------------------------------- + // Function: ~Condition + // + // Purpose: The destroyer + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + virtual ~Condition(); + + //>--------------------------------------------------------------------- + // Function: Broadcast + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + void broadcast(); + + //>--------------------------------------------------------------------- + // Function: Reset + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + void reset(); + + //>--------------------------------------------------------------------- + // Function: Signal + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + void signal(); + + //>--------------------------------------------------------------------- + // Function: TimedWait + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + bool timedWait(const unsigned long seconds, const unsigned long nanoseconds, bool shallWeReset = true); + + //>--------------------------------------------------------------------- + // Function: Wait + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + void wait(bool shallWeReset = true); + +private: + // dont allow + Condition(const Condition& rhs); + Condition& operator=(const Condition& rhs); + + void lockMutex(); + + void unlockMutex(); + + pthread_cond_t m_condition; + + pthread_mutex_t m_mutex; + + State m_state; +}; diff --git a/CommonLib/src/CommonLib/Lib/CustomDataTypes.hpp b/CommonLib/src/CommonLib/Lib/CustomDataTypes.hpp new file mode 100644 index 0000000..9a6a0e2 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/CustomDataTypes.hpp @@ -0,0 +1,65 @@ +//>>*************************************************************************** +// UNCLASSIFIED +// +// COPYRIGHT 2012 RAYTHEON COMPANY +// This data was developed pursuant to Contract Number HQ0276-10-C-0005 +// with the US Government. The US Government's rights in and to this +// copyrighted data are as specified in DFAR 252.227-7014 +// which was made part of the above contract. +// +// Distribution Statement: D -Distribution authorized to the DoD and DoD +// contractors only based on Critical Technology Requirements, May 2001. +// Other requests shall be referred to PEO THEATER AIR DEFENSE (PMS 422). +// WARNING - This document contains technical data whose export is restricted +// by the Arms Export Control Act (Title 22, U.S.C. Sec. 2751 et seq.) or Export +// Administration Act of 1979, as amended, Title 50, U.S.C., App. 2401, et seq. +// Violations of these laws are subject to severe criminal penalties. Disseminate +// per the provisions of OPNAVINST 5510.161. + +// Destruction Notice: - For unclassified, limited distribution information, +// destroy by any method that will prevent disclosure of contents or +// reconstruction of the document. Classified information, destroy in +// accordance with DoD-5220.22-M or OPNAVINST 5510.1h. +// +// +// NAME +// @(#) $Workfile: util.hpp $ - +// +// DESCRIPTION +// +// $Revision: 0 $ +// +// REVISION HISTORY +// $Log: /SM3/src/app/common/lib/Util.hpp $ +//<<*************************************************************************** + +#pragma once + +#include + +#include +#include +#include +//------------------------------------------------------------------------ + +namespace CustomDataTypes +{ + // by default, key search for std::map is case-sensitive + // this struct makes it so any key search for std::map is case-insensitive + // example: std::map + struct ci_less + { + struct nocase_compare + { + bool operator()(const unsigned char& c1, const unsigned char& c2) const + { + return tolower(c1) < tolower(c2); + } + }; + + bool operator()(const std::string& s1, const std::string& s2) const + { + return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), nocase_compare()); + } + }; +} diff --git a/CommonLib/src/CommonLib/Lib/ErrorLog.cpp b/CommonLib/src/CommonLib/Lib/ErrorLog.cpp new file mode 100644 index 0000000..67d28cf --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/ErrorLog.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +#include "ErrorLog.hpp" +#include "Exception.hpp" +#include "LockMutex.hpp" +#include "Timestamp.hpp" + +//----------------------------------------------------------------------------- + +ErrorLog &ErrorLog::Instance(const std::string& filename) +{ + try + { + static ErrorLog fileAndConsoleWriter(filename); + return fileAndConsoleWriter; + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +ErrorLog::ErrorLog(const std::string& filename): + m_mutex("ErrorLog - Mutex") +{ + try + { + m_outlog.open(filename.c_str(), std::ios_base::app); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +ErrorLog::~ErrorLog() +{ + m_outlog.close(); +} + +//----------------------------------------------------------------------------- + +void ErrorLog::log(const std::string& msg, const bool putToConsole, ErrorLog::LogLevel level) +{ + try + { + LockMutex lock(&m_mutex); + + // build the string to log + std::stringstream msgToLogSs; + + if (level == INFO) + { + msgToLogSs << "INFO, " << msg + "\n"; + } + else + { + msgToLogSs << "ERROR, " << msg + "\n"; + } + + // log out data to console + if (putToConsole == true) + { + std::cout << msgToLogSs.str(); + } + + m_outlog << Timestamp::GetCurrentDateTimeString(Timestamp::DateTimeFormat::YYYYMMDD) << "_" << Timestamp::GetCurrentDateTimeString(Timestamp::DateTimeFormat::HHMMSSMM, "_") + "_" + msgToLogSs.str(); + + m_outlog.flush(); + } + catch (...) + { + // if we cant log, just continue on + } +} + + + + diff --git a/CommonLib/src/CommonLib/Lib/ErrorLog.hpp b/CommonLib/src/CommonLib/Lib/ErrorLog.hpp new file mode 100644 index 0000000..f7bd6b4 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/ErrorLog.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include +#include + +#include "Mutex.hpp" + +class ErrorLog +{ +public: + enum LogLevel + { + INFO, + ERROR + }; + + static ErrorLog &Instance(const std::string& filename = ""); + + ~ErrorLog(); + + void log(const std::string& msg, const bool putToConsole = true, LogLevel logLevel = ERROR); + +private: + // do not allow + ErrorLog(const ErrorLog& rhs); + ErrorLog& operator=(const ErrorLog& rhs); + ErrorLog(const std::string& filename); + + std::string m_filename; + std::ofstream m_outlog; + + Mutex m_mutex; +}; + + + diff --git a/CommonLib/src/CommonLib/Lib/EventNames.cpp b/CommonLib/src/CommonLib/Lib/EventNames.cpp new file mode 100644 index 0000000..77d2b2c --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/EventNames.cpp @@ -0,0 +1,42 @@ +#include + +#include "EventNames.hpp" +#include "Exception.hpp" + +EventNames& EventNames::Instance() +{ + static EventNames names; + return names; +} + +//------------------------------------------------------------------------ + +EventNames::~EventNames() +{ +} + +//------------------------------------------------------------------------ + +EventNames::EventNames(): + m_nameMap() +{ + m_nameMap[COMMAND_EXECUTTION_IN_PROGRESS] = "COMMAND_EXECUTTION_IN_PROGRESS"; + m_nameMap[COMMAND_EXECUTION_COMPLETE] = "COMMAND_EXECUTION_COMPLETE"; + m_nameMap[START_FILE_TRANSFER] = "START_FILE_TRANSFER"; + m_nameMap[GLOBAL_QUIT] = "GLOBAL_QUIT"; +} + +//------------------------------------------------------------------------ + +std::string EventNames::operator[](EventNames::Names name) +{ + std::map::const_iterator it = m_nameMap.find(name); + if (it == m_nameMap.end()) + { + std::stringstream ss; + ss << "EventNames::operator[](): value " << name << "not found"; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + return it->second; +} diff --git a/CommonLib/src/CommonLib/Lib/EventNames.hpp b/CommonLib/src/CommonLib/Lib/EventNames.hpp new file mode 100644 index 0000000..a6aca37 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/EventNames.hpp @@ -0,0 +1,74 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include +#include + +class EventNames +{ +public: + enum Names + { + COMMAND_EXECUTTION_IN_PROGRESS, + COMMAND_EXECUTION_COMPLETE, + START_FILE_TRANSFER, + GLOBAL_QUIT, + NUM_EVENTS + }; + + //>--------------------------------------------------------------------------- + // Function: Instance + // + // Purpose: singleton + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + static EventNames& Instance(); + + //>--------------------------------------------------------------------------- + // Function: ~EventNames + // + // Purpose: destructor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual ~EventNames(); + + //>--------------------------------------------------------------------------- + // Function: operator[] + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + std::string operator[](EventNames::Names name); + +private: + // do not allow + EventNames(const EventNames &rhs); + EventNames &operator=(const EventNames &rhs); + EventNames(); + + std::map m_nameMap; +}; diff --git a/CommonLib/src/CommonLib/Lib/IniFile.cpp b/CommonLib/src/CommonLib/Lib/IniFile.cpp new file mode 100644 index 0000000..b7f33d7 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/IniFile.cpp @@ -0,0 +1,442 @@ +#include +#include + +#include "IniFile.hpp" +#include "Exception.hpp" +#include "StringUtil.hpp" + +// INI-file delimiters +static const char COMMENT_CHAR = '#'; +static const char KEY_VALUE_DELIMITER = '='; +static const char SECTION_HDR_START_CHAR = '['; +static const char SECTION_HDR_END_CHAR = ']'; + +//------------------------------------------------------------------------ + +IniFile::IniFile(const std::string& fileName) +{ + try + { + readFile(fileName); + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +IniFile::~IniFile() +{ +} + +//------------------------------------------------------------------------ + +void IniFile::addKeyValuePair(const std::string& section, + const std::string& key, + const std::string& value) +{ + configMap[section].insert(KeyValuePair(key, value)); +} + +//------------------------------------------------------------------------ + +bool IniFile::getBool(const std::string& section , + const std::string& key) const +{ + try + { + std::stringstream ss; + + ss.str(Util::Strings::ToLower(getString(section , key))); + ss.setf(std::ios_base::boolalpha); + bool tmp; + ss >> tmp; + + return tmp; + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +double IniFile::getDouble(const std::string& section, + const std::string& key) const +{ + try + { + std::stringstream ss; + + ss.str(getString(section , key)); + double tmp; + ss >> tmp; + + return tmp; + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +void IniFile::getKeys(const std::string& section, + std::vector& keys) const +{ + try + { + keys.clear(); // empty list + + // make sure section exists + SectionIter sectionIt = configMap.find(section); + + if (sectionIt == configMap.end()) + { + std::ostringstream oss; + + oss << "section '" << section.c_str() << "' does not exist"; + + throw Exception(Q_FUNC_INFO, oss.str()); + } + + // fill up vector with keys + for (KeyValueIter keyValueIt = sectionIt->second.begin(); + keyValueIt != sectionIt->second.end(); + ++keyValueIt) + { + keys.push_back(keyValueIt->first); + } + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +void IniFile::getKeyValue(const std::string& line, + std::string& key, + std::string& value) const +{ + try + { + size_t equalPos; + + // everything to left of delimiter is key + equalPos = line.find(KEY_VALUE_DELIMITER); + key = line.substr(0 , equalPos); + + // everything to right of delimiter is value (except possible comment) + value = line.substr(equalPos + 1); + + size_t commentPos; + commentPos = value.find(COMMENT_CHAR); + + if (commentPos != std::string::npos) + { + value = value.substr(0 , commentPos); + } + + // trim leading/trailing spaces on key and value + key = Util::Strings::TrimSpaces(key); + value = Util::Strings::TrimSpaces(value); + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +IniFile::LineType IniFile::getLineType(const std::string& line) const +{ + try + { + // trim leading/trailing spaces + std::string tempLine = Util::Strings::TrimSpaces(line); + + // blank line? + if (tempLine.length() == 0) + { + return BLANK_LINE; + } + + // commment? + if (tempLine[0] == COMMENT_CHAR) + { + return COMMENT; + } + + // section header? + if (tempLine[0] == SECTION_HDR_START_CHAR) + { + // check for end of section header + if (tempLine.find(SECTION_HDR_END_CHAR) != std::string::npos) + { + return SECTION_HEADER; + } + else // starts out as a section header, but no end char + { + return BAD_LINE; + } + } + + // Assume everything else is a key/value pair. + // Previously checked for "=" and returned BAD_LINE if it didn't exist. + // However, this did not allow for keys with no values. + return KEY_VALUE_PAIR; + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +int IniFile::getInt(const std::string& section, const std::string& key) const +{ + try + { + std::stringstream ss; + + ss.str(getString(section , key)); + int tmp = 0; + ss >> tmp; + + return tmp; + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +unsigned int IniFile::getUint(const std::string& section, + const std::string& key, + bool isHexString) const +{ + try + { + std::stringstream ss; + + unsigned int tmp = 0; + + if (isHexString == false) + { + std::string iniVal = getString(section , key); + ss << iniVal; + ss >> tmp; + } + else + { + std::string iniVal = getString(section , key); + ss << std::hex << iniVal; + ss >> tmp; + } + + return tmp; + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +std::string IniFile::getString(const std::string& section, + const std::string& key) const +{ + try + { + // make sure section exists + SectionIter sectionIt = configMap.find(section); + + if (sectionIt == configMap.end()) + { + std::ostringstream oss; + + std::stringstream ss; + ss << "IniFile::getString "; + throw Exception(Q_FUNC_INFO, oss.str()); + } + + // make sure key exists + KeyValueIter keyValueIt = sectionIt->second.find(key); + + if (keyValueIt == sectionIt->second.end()) + { + std::ostringstream oss; + + oss << "key '" << key.c_str() + << "' does not exist"; + + throw Exception(Q_FUNC_INFO, oss.str()); + } + + return keyValueIt->second; + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ + +bool IniFile::isSection(const std::string& section) const +{ + return configMap.find(section) != configMap.end(); +} + +//------------------------------------------------------------------------ + +void IniFile::readFile(const std::string& fileName) +{ + try + { + // open file + std::ifstream cfgStream(fileName.c_str()); + + if (cfgStream.fail()) + { + std::string s = "could not open file: " + fileName; + throw Exception(Q_FUNC_INFO, s); + } + + // read line-by-line + // - skip comments, blank lines + // - save last section name + // - add key/value pairs as ecnountered + std::string section; + bool bHaveSection = false; + + while (!cfgStream.eof()) + { + std::string line = ""; + + std::getline(cfgStream, line); + + if(line != "") + { + char lastChar = line[line.length() - 1]; + + if(lastChar == '\r') + { + line = line.substr(0, line.length() - 1); + } + } + + LineType lineType = getLineType(line); + + switch (lineType) + { + case BLANK_LINE: + case COMMENT: + // do nothing + break; + + case SECTION_HEADER: + { + // save section + size_t first = line.find_first_of('['); + size_t last = line.find_first_of(']'); + section = line.substr(first + 1 , last - first - 1); + + bHaveSection = true; + } + break; + + case KEY_VALUE_PAIR: + if (bHaveSection) + { + // add to map + std::string key; + std::string value; + getKeyValue(line , key , value); + + addKeyValuePair(section, key, value); + } + else + { + throw Exception(Q_FUNC_INFO, "key/value pair before section"); + } + + break; + + case BAD_LINE: + std::string errMsg; + + errMsg = "bad line: '" + line + "'"; + + throw Exception(Q_FUNC_INFO, errMsg.c_str()); + } + } + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown error"); + } +} + +//------------------------------------------------------------------------ diff --git a/CommonLib/src/CommonLib/Lib/IniFile.hpp b/CommonLib/src/CommonLib/Lib/IniFile.hpp new file mode 100644 index 0000000..725a9b7 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/IniFile.hpp @@ -0,0 +1,242 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include "CustomDataTypes.hpp" + +#include +#include +#include + +class IniFile +{ +public: + //>--------------------------------------------------------------------------- + // Function: IniFile + // + // Purpose: Constructor + //---------------------------------------------------------------------------- + // Arguments:fileName - name of INI-format file + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + IniFile(const std::string& fileName); + + //>--------------------------------------------------------------------------- + // Function: ~IniFile + // + // Purpose: destructor + //---------------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + virtual ~IniFile(); + + //>--------------------------------------------------------------------------- + // Function: getBool + // + // Purpose: return value of a given section/key interpreted as a boolean + //---------------------------------------------------------------------------- + // Arguments: + // section - section containing desired key/value pair + // key - key in given section + //---------------------------------------------------------------------------- + // Return Value: + // value string of given section/key interpreted as a boolean + //---------------------------------------------------------------------------- + bool getBool(const std::string& section, const std::string& key) const; + + //>--------------------------------------------------------------------------- + // Function: getDouble + // + // Purpose: return value of a given section/key interpreted as a double + //---------------------------------------------------------------------------- + // Arguments: + // section - section containing desired key/value pair + // key - key in given section + //---------------------------------------------------------------------------- + // Return Value: + // value string of given section/key interpreted as a double + //---------------------------------------------------------------------------- + double getDouble(const std::string& section, const std::string& key) const; + + //>--------------------------------------------------------------------------- + // Function: getKeys + // + // Purpose: return list of keys in a given section + //---------------------------------------------------------------------------- + // Arguments: + // section - section from which to return list of keys + // keys - list of keys in given section + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + void getKeys(const std::string& section, + std::vector& keys) const; + + //>--------------------------------------------------------------------------- + // Function: GetInt + // + // Purpose: return value of a given section/key interpreted as a int + //---------------------------------------------------------------------------- + // Arguments: + // section - section containing desired key/value pair + // key - key in given section + //---------------------------------------------------------------------------- + // Return Value: the value as an int + //---------------------------------------------------------------------------- + int getInt(const std::string& section, const std::string& key) const; + + //>--------------------------------------------------------------------------- + // Function: getLong + // + // Purpose: return value of a given section/key interpreted as a long + //---------------------------------------------------------------------------- + // Arguments: + // section - section containing desired key/value pair + // key - key in given section + //---------------------------------------------------------------------------- + // Return Value: + // value string of given section/key interpreted as a long + //---------------------------------------------------------------------------- + unsigned int getUint(const std::string& section, const std::string& key, bool isHexString) const; + + //>--------------------------------------------------------------------------- + // Function: getString + // + // Purpose: return value of a given section/key interpreted as a string + //---------------------------------------------------------------------------- + // Arguments: + // section - section containing desired key/value pair + // key - key in given section + //---------------------------------------------------------------------------- + // Return Value: + // value string of given section/key interpreted as a string + //---------------------------------------------------------------------------- + std::string getString(const std::string& section, + const std::string& key) const; + + //>--------------------------------------------------------------------------- + // Function: isSection + // + // Purpose: return whether given string is a section + //---------------------------------------------------------------------------- + // Arguments: + // str - string for which to return whether it is a section + //---------------------------------------------------------------------------- + // Return Value: + // whether specified string is a section(true) or not(false) + //---------------------------------------------------------------------------- + bool isSection(const std::string& str) const; + +protected: + //>--------------------------------------------------------------------------- + // Function: addKeyValuePair + // + // Purpose: + // adds a key/value pair to a section + //---------------------------------------------------------------------------- + // Arguments: + // section - section to which to add key/value pair + // key - key of key/value pair to add + // value - value of key/value pair to add + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + void addKeyValuePair(const std::string& section, + const std::string& key, + const std::string& value); + +private: + // do not allow + IniFile(); + IniFile(const IniFile ©); + IniFile &operator=(const IniFile &rhs); + + // types of line allowed in INI-format file + enum LineType + { + COMMENT, + KEY_VALUE_PAIR, + SECTION_HEADER, + BLANK_LINE, + BAD_LINE + }; + + // internally-used data structures + typedef std::map KeyValueMap; + typedef std::pair KeyValuePair; + typedef std::map SectionMap; + typedef KeyValueMap::const_iterator KeyValueIter; + typedef SectionMap::const_iterator SectionIter; + + SectionMap configMap; // map containing sections and key/value pairs + + //>--------------------------------------------------------------------------- + // Function: getKeyValue + // + // Purpose: + // returns key/value pair(strings) from a string. + // + // Key/value pairs are delimited by an equal sign, "=". If no equal sign + // is found, then value is an empty string. Spaces are trimmed from both + // ends of the key and value strings. + //---------------------------------------------------------------------------- + // Arguments: + // line - string containing a key/value pair + // key - returned key string + // value - return value string. may be empty if no equal sign found in + // line. + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + void getKeyValue(const std::string& line, + std::string& key, + std::string& value) const; + + //>--------------------------------------------------------------------------- + // Function: getLineType + // + // Purpose: + // returns type of line in passed string + //---------------------------------------------------------------------------- + // Arguments: + // line - string containing line read from file + //---------------------------------------------------------------------------- + // Return Value: + // type of line (see LineType enum) of passed string + //---------------------------------------------------------------------------- + LineType getLineType(const std::string& line) const; + + //>--------------------------------------------------------------------------- + // Function: readFile + // + // Purpose: + // reads file and repeatedly calls ConfigData::addKeyValuePair to store + // sections and key/value pairs. Throws FileErr exception if could not + // open file. + // + // Throws IniFileErr exception if problem with data in file. + //---------------------------------------------------------------------------- + // Arguments: + // fileName - name of INI-format file to read/parse + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + void readFile(const std::string& fileName); +}; diff --git a/CommonLib/src/CommonLib/Lib/LockMutex.cpp b/CommonLib/src/CommonLib/Lib/LockMutex.cpp new file mode 100644 index 0000000..6681f0e --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/LockMutex.cpp @@ -0,0 +1,43 @@ +#include "LockMutex.hpp" +#include "Exception.hpp" +#include "ErrorLog.hpp" +#include "Mutex.hpp" +#include "Proc.hpp" + +LockMutex::LockMutex(Mutex* pMutex) : + m_pMutex(pMutex) +{ + try + { + m_pMutex->lock(); + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } + catch(...) + { + throw Exception(Q_FUNC_INFO, "caught unknown"); + } +} + +//------------------------------------------------------------------------ + +LockMutex::~LockMutex() +{ + try + { + m_pMutex->unlock(); + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + ErrorLog::Instance().log(e.getMessage(), true); + } + catch (...) + { + ErrorLog::Instance().log("LockMutex::~LockMutex()", true); + } + m_pMutex = 0; +} diff --git a/CommonLib/src/CommonLib/Lib/LockMutex.hpp b/CommonLib/src/CommonLib/Lib/LockMutex.hpp new file mode 100644 index 0000000..0089836 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/LockMutex.hpp @@ -0,0 +1,53 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +class Mutex; + +class LockMutex +{ +public: + //>--------------------------------------------------------------------- + // Function: LockMutex() + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments:pMutex, pointer to a mutex + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + explicit LockMutex(Mutex* pMutex); + + //>--------------------------------------------------------------------- + // Function: ~LockMutex() + // + // Purpose: destroyer + //---------------------------------------------------------------------- + // Arguments: none + //-------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + ~LockMutex(); + +private: + // do not allow + LockMutex(); + LockMutex(const LockMutex ©); + LockMutex &operator=(const LockMutex &rhs); + + Mutex* m_pMutex; // do not delete +}; diff --git a/CommonLib/src/CommonLib/Lib/Mutex.cpp b/CommonLib/src/CommonLib/Lib/Mutex.cpp new file mode 100644 index 0000000..ae3b685 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Mutex.cpp @@ -0,0 +1,99 @@ +#include + +#include "Mutex.hpp" +#include "Exception.hpp" + +Mutex::Mutex(const std::string& name) : + OSObject(name) +{ + try + { + // set up mutex to be of the recursive type + + pthread_mutexattr_t mutexAttr; + int ret = pthread_mutexattr_init(&mutexAttr); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutexattr_init returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + ret = pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutexattr_settype returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + + ret = pthread_mutex_init(&m_mutex, &mutexAttr); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutex_lock returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + } + catch (Exception &e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } +} + +//------------------------------------------------------------------------ + +Mutex::~Mutex() +{ + pthread_mutex_unlock(&m_mutex); + + pthread_mutex_destroy(&m_mutex); +} + +//------------------------------------------------------------------------ + +void Mutex::lock() +{ + try + { + int ret = pthread_mutex_lock(&m_mutex); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutex_lock returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + } + catch (Exception &e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } +} + +//------------------------------------------------------------------------ + +void Mutex::unlock() +{ + try + { + int ret = pthread_mutex_unlock(&m_mutex); + + if (ret != 0) + { + std::stringstream ss; + ss << "pthread_mutex_unlock returned: " << ret; + throw Exception(Q_FUNC_INFO, ss.str()); + } + } + catch (Exception &e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } +} diff --git a/CommonLib/src/CommonLib/Lib/Mutex.hpp b/CommonLib/src/CommonLib/Lib/Mutex.hpp new file mode 100644 index 0000000..d5983fc --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Mutex.hpp @@ -0,0 +1,87 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include +#include "OSObject.hpp" + +class Mutex : OSObject +{ +public: + //>--------------------------------------------------------------------------- + // Function: Mutex + // + // Purpose: Constructor + //---------------------------------------------------------------------------- + // Arguments: name - the name of this mutex + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + Mutex(const std::string& name); + + //>--------------------------------------------------------------------------- + // Function: ~Mutex + // + // Purpose: Destroyer + //---------------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + virtual ~Mutex(); + + //>--------------------------------------------------------------------------- + // Function: GetMutex + // + // Purpose: Get the mutex + //---------------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + //pthread_mutex_t GetMutex() { return m_mutex;}; + + //>--------------------------------------------------------------------------- + // Function: Lock + // + // Purpose: lock the mutex + //---------------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + void lock(); + + //>--------------------------------------------------------------------------- + // Function: Unlock + // + // Purpose: unlock the mutex + //---------------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + void unlock(); + +private: + // not allowed + Mutex(const Mutex& rhs); + Mutex& operator=(const Mutex& rhs); + + pthread_mutex_t m_mutex; +}; + diff --git a/CommonLib/src/CommonLib/Lib/OSObject.cpp b/CommonLib/src/CommonLib/Lib/OSObject.cpp new file mode 100644 index 0000000..87bbce2 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/OSObject.cpp @@ -0,0 +1,20 @@ +#include +#include "OSObject.hpp" + +OSObject::OSObject(const std::string &name) : + m_name(name), + m_handle(0) +{ +} + +//------------------------------------------------------------------------ + +OSObject::~OSObject() +{ + if (m_handle) + { + //@@@ + //::CloseHandle(handle_); + } + m_handle = 0; +} diff --git a/CommonLib/src/CommonLib/Lib/OSObject.hpp b/CommonLib/src/CommonLib/Lib/OSObject.hpp new file mode 100644 index 0000000..913553f --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/OSObject.hpp @@ -0,0 +1,89 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include + +class OSObject +{ +public: + typedef void* HANDLE; + + //>--------------------------------------------------------------------- + // Function: OSObject() + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments: name - the name of this object + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + explicit OSObject(const std::string &name); + + //>--------------------------------------------------------------------- + // Function: ~OSObject() + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual ~OSObject(); + + //>--------------------------------------------------------------------------- + // Function: GetName + // + // Purpose: Retrieve the name + //---------------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------------- + // Return Value: the name + //---------------------------------------------------------------------------- + std::string getName() const { return m_name; } + + //>--------------------------------------------------------------------------- + // Function: GetHandle + // + // Purpose: Retrieve the handle + //---------------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------------- + // Return Value: the handle + //---------------------------------------------------------------------------- + HANDLE getHandle() const { return m_handle; } + + //>--------------------------------------------------------------------------- + // Function: SetHandle + // + // Purpose: sets the handle + //---------------------------------------------------------------------------- + // Arguments: handle - the handle + //---------------------------------------------------------------------------- + // Return Value: none + //---------------------------------------------------------------------------- + void setHandle(HANDLE handle) { m_handle = handle; } + +private: + // do not allow + OSObject(const OSObject ©); + OSObject &operator=(const OSObject &rhs); + + std::string m_name; + HANDLE m_handle; +}; + diff --git a/CommonLib/src/CommonLib/Lib/Timestamp.cpp b/CommonLib/src/CommonLib/Lib/Timestamp.cpp new file mode 100644 index 0000000..63215fc --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Timestamp.cpp @@ -0,0 +1,153 @@ +#include +#include +#include + +#include "sys/time.h" + +#include "Timestamp.hpp" +#include "Exception.hpp" + +//------------------------------------------------------------------------ + +Timestamp::Timestamp() : + m_currentTime(0), + m_timeString(), + m_startTime(0) +{ + timeBuff_[0] = '\0'; +} + +//------------------------------------------------------------------------ + +Timestamp::~Timestamp() +{ +} + +//------------------------------------------------------------------------ + +void Timestamp::startTiming() +{ + m_startTime = GetCurrentTimeMs(); +} + +//------------------------------------------------------------------------ + +unsigned int Timestamp::getElapsedTimeSecs() +{ + unsigned int currentTime = GetCurrentTimeMs(); + return (currentTime - m_startTime) / 1000; +} + +//------------------------------------------------------------------------ + +unsigned int Timestamp::getElapsedTimeMs() +{ + unsigned int currentTime = GetCurrentTimeMs(); + unsigned int delta = currentTime - m_startTime; + return delta; +} + +//------------------------------------------------------------------------ + +std::string Timestamp::getElapsedTimeString() +{ + double d = getElapsedTimeSecs(); + + std::stringstream ss; + ss << d; + + return ss.str(); +} + +//----------------------------------------------------------------------------- + +std::string Timestamp::GetCurrentDateTimeString(Timestamp::DateTimeFormat format, std::string separator) +{ + std::stringstream ss; + + struct timeval curTime; + gettimeofday(&curTime, 0); + int millisecs = curTime.tv_usec / 1000; + + struct tm* myTm = localtime(&curTime.tv_sec); + + try + { + switch (format) + { + case Timestamp::DateTimeFormat::YYYYMM: + ss << std::setfill('0') << "20" << std::setw(2)<< myTm->tm_year%100 + << separator << std::setfill('0') << std::setw(2) << myTm->tm_mon+1; + break; + case Timestamp::DateTimeFormat::YYYYMMDD: + ss << std::setfill('0') << "20" << std::setw(2)<< myTm->tm_year%100 + << separator << std::setfill('0') << std::setw(2) << myTm->tm_mon+1 + << separator << std::setfill('0') << std::setw(2) << myTm->tm_mday; + break; + case Timestamp::DateTimeFormat::MMDDYYYY: + ss << std::setfill('0') << std::setfill('0') << std::setw(2) << myTm->tm_mon+1 + << separator << std::setfill('0') << std::setw(2) << myTm->tm_mday + << separator << "20" << std::setw(2)<< myTm->tm_year%100; + break; + case Timestamp::DateTimeFormat::HHMMSS: + case Timestamp::DateTimeFormat::HHMMSSMM: + ss << std::setfill('0') << std::setw(2) << myTm->tm_hour << separator + << std::setfill('0') << std::setw(2) << myTm->tm_min << separator + << std::setfill('0') << std::setw(2) << myTm->tm_sec; + } + + if (format == Timestamp::DateTimeFormat::HHMMSSMM) + ss << "." << std::setfill('0') << std::setw(3) << millisecs; + + return ss.str(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +unsigned int Timestamp::GetCurrentTimeNs() +{ + const unsigned long NS_PER_SEC = 1000000000; + struct timespec curTime; + clock_gettime(CLOCK_REALTIME, &curTime); + + unsigned long curTimeNs = (curTime.tv_sec * NS_PER_SEC) + curTime.tv_nsec; + + return curTimeNs; +} + +//----------------------------------------------------------------------------- + +unsigned int Timestamp::GetCurrentTimeUs() +{ + const unsigned int NS_PER_SEC = 1000000000; + struct timespec curTime; + clock_gettime(CLOCK_REALTIME, &curTime); + + unsigned int curTimeNs = (curTime.tv_sec * NS_PER_SEC) + curTime.tv_nsec; + + return curTimeNs/1000; +} + +//----------------------------------------------------------------------------- + +unsigned int Timestamp::GetCurrentTimeMs() +{ + const unsigned long NS_PER_SEC = 1000000000; + struct timespec curTime; + clock_gettime(CLOCK_REALTIME, &curTime); + + unsigned long curTimeNs = (curTime.tv_sec * NS_PER_SEC) + curTime.tv_nsec; + + return curTimeNs/1000000; +} + +//----------------------------------------------------------------------------- + + + diff --git a/CommonLib/src/CommonLib/Lib/Timestamp.hpp b/CommonLib/src/CommonLib/Lib/Timestamp.hpp new file mode 100644 index 0000000..878c060 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Timestamp.hpp @@ -0,0 +1,185 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include +#include + +class Timestamp +{ +public: + enum class DateTimeFormat + { + YYYYMM, + YYYYMMDD, + MMDDYYYY, + HHMMSS, + HHMMSSMM + }; + + enum class TimeResolution + { + SECS, + MILLISECS + }; + + //>--------------------------------------------------------------------- + // Function: Timestamp() + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + Timestamp(); + + //>--------------------------------------------------------------------- + // Function: ~Timestamp() + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + ~Timestamp(); + + //>--------------------------------------------------------------------- + // Function: getLength() + // + // Purpose: to return the length of the time string + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: int, the length + //<--------------------------------------------------------------------- + int getLength() const { return CTIME_LENGTH_; } + + //>--------------------------------------------------------------------- + // Function: startTiming() + // + // Purpose: to start the timing to get some elapsed time + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void startTiming(); + + //>--------------------------------------------------------------------- + // Function: getElapsedTime() + // + // Purpose: to get the amount of elapsed time since the call to + // startTiming(). + // Note: clock returns the number of clock ticks of elapsed + // processor time. The returned value is the product of the amount + // of time that has elapsed since the start of a process and the + // value of the CLOCKS_PER_SEC constant. If the amount of elapsed + // time is unavailable, the function returns -1, cast as a + // clock_t. + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: double, seconds of elapsed time + //<--------------------------------------------------------------------- + unsigned int getElapsedTimeSecs(); + + //>--------------------------------------------------------------------- + // Function: GetElapsedTimeMs() + // + // Purpose: to get the amount of elapsed time since the call to + // startTiming(). + // Note: clock returns the number of clock ticks of elapsed + // processor time. The returned value is the product of the amount + // of time that has elapsed since the start of a process and the + // value of the CLOCKS_PER_SEC constant. If the amount of elapsed + // time is unavailable, the function returns -1, cast as a + // clock_t. + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: double, seconds of elapsed time + //<--------------------------------------------------------------------- + unsigned int getElapsedTimeMs(); + + //>--------------------------------------------------------------------- + // Function: getElapsedTimeString() + // + // Purpose: to do the same as getElapsedTime() but return std::string + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: std::string, seconds of elapsed time + //<--------------------------------------------------------------------- + std::string getElapsedTimeString(); + + //>--------------------------------------------------------------------- + // Function: GetTime + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + static std::string GetCurrentDateTimeString(Timestamp::DateTimeFormat format, std::string separator = ""); + + //>--------------------------------------------------------------------- + // Function: GetCurrentTimeNs + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + static unsigned int GetCurrentTimeNs(); + + //>--------------------------------------------------------------------- + // Function: GetCurrentTimeUs + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + static unsigned int GetCurrentTimeUs(); + + //>--------------------------------------------------------------------- + // Function: GetCurrentTimeMs + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + static unsigned int GetCurrentTimeMs(); + +private: + // do not allow + Timestamp(const Timestamp &rhs); + Timestamp &operator=(const Timestamp &rhs); + + enum { CTIME_LENGTH_ = 26 }; + char timeBuff_[CTIME_LENGTH_]; + time_t m_currentTime; + std::string m_timeString; + unsigned int m_startTime; +}; diff --git a/CommonLib/src/CommonLib/Lib/Util/DateTimeUtil.cpp b/CommonLib/src/CommonLib/Lib/Util/DateTimeUtil.cpp new file mode 100644 index 0000000..5f2465e --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Util/DateTimeUtil.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "DateTimeUtil.hpp" +#include "Exception.hpp" +#include "Timestamp.hpp" + +//----------------------------------------------------------------------------- + +void Util::DateTime::Sleep(unsigned int msToSleep) +{ + const int MICO_TO_MS_CONVERSION = 1000; + + usleep(msToSleep * MICO_TO_MS_CONVERSION); +} + +//----------------------------------------------------------------------------- + +std::string Util::DateTime::DescribeTimeElapsed(int secs) +{ + std::stringstream description; + int secsTemp = secs; + int mins = 0; + int hours = 0; + int days = 0; + + if (secs >= 60) + { + mins = secs / 60; + secsTemp = secs % 60; + } + + if (mins >= 60) + hours = mins / 60; + + if (hours >= 24) + days = hours / 24; + + if (days > 0) + description << days << " d "; + + if (hours > 0) + description << hours << " h "; + + if (mins > 0) + description << mins << " m "; + + if (secsTemp > 0) + description << secsTemp << " s"; + + return description.str(); +} + diff --git a/CommonLib/src/CommonLib/Lib/Util/DateTimeUtil.hpp b/CommonLib/src/CommonLib/Lib/Util/DateTimeUtil.hpp new file mode 100644 index 0000000..9b933e1 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Util/DateTimeUtil.hpp @@ -0,0 +1,36 @@ +#ifndef DATE_TIME_UTIL_H +#define DATE_TIME_UTIL_H + +#include +#include + +namespace Util +{ + namespace DateTime + { + + //>--------------------------------------------------------------------- + // Function: Sleep + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + void Sleep(unsigned int msToSleep); + + //>--------------------------------------------------------------------- + // Function: DescribeTimeElapsed + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string DescribeTimeElapsed(int secs); + } +} + +#endif diff --git a/CommonLib/src/CommonLib/Lib/Util/FileSystemUtil.cpp b/CommonLib/src/CommonLib/Lib/Util/FileSystemUtil.cpp new file mode 100644 index 0000000..482e33b --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Util/FileSystemUtil.cpp @@ -0,0 +1,423 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "FileSystemUtil.hpp" +#include "StringUtil.hpp" +#include "Exception.hpp" +#include "Timestamp.hpp" + +//----------------------------------------------------------------------------- + +void Util::FileSystem::ChangeFilePermissions(const std::string& file, const unsigned int& permissions) +{ + try + { + int ret = chmod(file.c_str(), permissions); + + if (ret != 0) + { + std::stringstream ss; + ss << "Chmod returned " << ret << " for file " << file; + throw Exception(Q_FUNC_INFO,ss.str()); + } + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +void Util::FileSystem::CopyDirectory(const std::string& fromName, const std::string& toName) +{ + try + { + // recursive copy file/directory and retain permissions + std::stringstream cmdSs; + cmdSs << "cp -r -p " << fromName << " " << toName; + int ret = system(cmdSs.str().c_str()); + + if (ret != 0) + { + std::stringstream ss; + ss << "Could not copy: " << fromName << " to " << toName; + throw Exception(Q_FUNC_INFO,ss.str()); + } + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +void Util::FileSystem::CopyFile(const std::string& fromName, const std::string& toName) +{ + try + { + // copy the file and retain permissions in the new file + std::stringstream cmdCopySs; + cmdCopySs << "cp -p " << fromName << " " << toName; + int ret = system(cmdCopySs.str().c_str()); + + if (ret != 0) + { + std::stringstream ss; + ss << "Could not copy: " << fromName << " to " << toName; + throw Exception(Q_FUNC_INFO,ss.str()); + } + + // remove the original + std::stringstream cmdRemoveSs; + cmdRemoveSs << "rm " << fromName; + ret = system(cmdRemoveSs.str().c_str()); + + if (ret != 0) + { + std::stringstream ss; + ss << "Could not remove: " << fromName; + throw Exception(Q_FUNC_INFO,ss.str()); + } + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +void Util::FileSystem::CreateDirectory(const std::string& directory) +{ + // if we saved a file, open up the permissions + const std::string FILE_PERMISSION_STR = "0777"; + + try + { + struct stat statBuf; + + if (stat(directory.c_str(), &statBuf) != -1) + { + if (S_ISDIR(statBuf.st_mode)) + { + // directory is already there + return; + } + } + + QRegularExpression re("^(.+)(/[^/]+/?)$", QRegularExpression::CaseInsensitiveOption); + QRegularExpressionMatch match = re.match(directory.c_str()); + if (match.hasMatch()) { + QString parentDir = match.captured(1); + + if (parentDir.length() > 0) + { + QDir dir(parentDir); + if (!dir.exists()) + CreateDirectory(parentDir.toStdString()); + } + } + + int ret = mkdir(directory.c_str(), S_IWOTH); + + if (ret != 0) + { + std::stringstream ss; + ss << "Can't create directory " << directory; + ss << ". mkdir() returned: " << ret; + throw Exception (Q_FUNC_INFO, ss.str()); + } + + // change permissions + long permission = strtol(FILE_PERMISSION_STR.c_str(), 0, 8); + ChangeFilePermissions(directory, permission); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::FileSystem::GetExePathAndFilename() +{ + try + { + std::string strPath = ""; + char path[PATH_MAX] = { 0 }; + int nchar = readlink("/proc/self/exe", path, sizeof(path)); + + if (nchar > 0) + strPath = std::string(path); + + return strPath; + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +int Util::FileSystem::IsFile(const char* path) +{ + try + { + struct stat pathStat; + stat(path, &pathStat); + return S_ISREG(pathStat.st_mode); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +bool Util::FileSystem::FileExists(std::string filePath) +{ + bool fileExists = false; + + QFileInfo qFileInfo(filePath.c_str()); + if (qFileInfo.exists()) + { + fileExists = true; + } + + return fileExists; +} + +//----------------------------------------------------------------------------- + +int Util::FileSystem::IsDirectory(const char* path) +{ + try + { + struct stat pathStat; + stat(path, &pathStat); + return S_ISDIR(pathStat.st_mode); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +bool Util::FileSystem::DirExists(std::string dir) +{ + bool dirExists = false; + + QDir qDir(dir.c_str()); + + if (qDir.exists()) + { + dirExists = true; + } + + return dirExists; +} + +//----------------------------------------------------------------------------- + +std::string Util::FileSystem::ExtractDirectory(std::string filePath) +{ + try + { + std::string dir = ""; + + QFileInfo qFileInfo(filePath.c_str()); + QDir qDir(qFileInfo.dir()); + + return qDir.absolutePath().toStdString(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::FileSystem::ExtractFilename(std::string filePath) +{ + try + { + std::string filename = ""; + + QFileInfo qFileInfo(filePath.c_str()); + QString qFilename(qFileInfo.fileName()); + + return qFilename.toStdString(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::FileSystem::AddSlashToPath(const std::string &path) +{ + char ch = '/'; + char nativeSeparator = '/'; + std::string tempPath = path; + + ch = QDir::toNativeSeparators(path.c_str()).toStdString().back(); + nativeSeparator = QDir::toNativeSeparators("/").toStdString().back(); + tempPath = QDir::toNativeSeparators(path.c_str()).toStdString(); + + if (ch != nativeSeparator) + tempPath = tempPath + nativeSeparator; + + return tempPath; +} + +//----------------------------------------------------------------------------- + +std::string Util::FileSystem::RemoveSlashInFrontOfPath(const std::string &path) +{ + char ch = '/'; + char nativeSeparator = '/'; + std::string tempPath = path; + + ch = QDir::toNativeSeparators(path.c_str()).toStdString().front(); + nativeSeparator = QDir::toNativeSeparators("\\").toStdString().back(); + tempPath = QDir::toNativeSeparators(path.c_str()).toStdString(); + + if (ch == nativeSeparator) + tempPath = tempPath.substr(1,path.length()-1); + + return tempPath; +} + +//----------------------------------------------------------------------------- + +std::string Util::FileSystem::BuildPath(const std::string &left, const std::string &right) +{ + std::string path; + + path = AddSlashToPath(left); + + if (right.length() > 0) + path += RemoveSlashInFrontOfPath(right); + + return path; +} + +//----------------------------------------------------------------------------- + +QStringList Util::FileSystem::GetListOfFilesInADirectory(QDir dir, bool recursive) +{ + QStringList fileList; + QDirIterator* iterator = 0; + + if (recursive) + iterator = new QDirIterator(dir.absolutePath(), QDirIterator::Subdirectories); + else + iterator = new QDirIterator(dir.absolutePath()); + + while (iterator->hasNext()) { + QFile file(iterator->next()); + + if ( file.open( QIODevice::ReadOnly ) ) + fileList.append(file.fileName()); + } + + if (iterator != 0) + delete iterator; + + return fileList; +} + +//----------------------------------------------------------------------------- + +std::string Util::FileSystem::DescribeFileSize(int64_t fileSizeInBytes) +{ + int64_t oneKb = 1024; + int64_t oneMb = oneKb * oneKb; + int64_t oneGb = oneMb * oneKb; + int64_t oneTb = oneGb * oneKb; + + std::stringstream description; + + double multiples = 0.0; + + if (fileSizeInBytes < oneKb) + { + description << fileSizeInBytes << " B"; + } + else if (fileSizeInBytes >= oneKb && fileSizeInBytes < oneMb) + { + multiples = (double)fileSizeInBytes / (double)oneKb; + description << Util::Strings::DoubleToString(multiples, 2, true) + " KB"; + } + else if (fileSizeInBytes >= oneMb && fileSizeInBytes < oneGb) + { + multiples = (double)fileSizeInBytes / (double)oneMb; + description << Util::Strings::DoubleToString(multiples, 2, true) + " MB"; + } + else if (fileSizeInBytes >= oneGb && fileSizeInBytes < oneTb) + { + multiples = (double)fileSizeInBytes / (double)oneGb; + description << Util::Strings::DoubleToString(multiples, 2, true) + " GB"; + } + else + { + multiples = (double)fileSizeInBytes / (double)oneTb; + description << Util::Strings::DoubleToString(multiples, 2, true) + " TB"; + } + return description.str(); +} + +//----------------------------------------------------------------------------- + +void Util::FileSystem::ChangeFileDateTime(std::string file, QDateTime desireModifiedDate, QDateTime desiredAccessedDate) +{ + utimbuf utimeBuf; + time_t modifiedTime; + time_t accessedTime; + + modifiedTime = desireModifiedDate.toMSecsSinceEpoch() / 1000; + accessedTime = desiredAccessedDate.toMSecsSinceEpoch() / 1000; + + utimeBuf.actime = accessedTime; + utimeBuf.modtime = modifiedTime; + + utime(file.c_str(), &utimeBuf); +} diff --git a/CommonLib/src/CommonLib/Lib/Util/FileSystemUtil.hpp b/CommonLib/src/CommonLib/Lib/Util/FileSystemUtil.hpp new file mode 100644 index 0000000..b6824a8 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Util/FileSystemUtil.hpp @@ -0,0 +1,208 @@ +#ifndef FILE_SYSTEM_UTIL_H +#define FILE_SYSTEM_UTIL_H + +#include +#include + +#include +#include +#include +#include +#include + +namespace Util +{ + namespace FileSystem + { + //>--------------------------------------------------------------------- + // Function: ChangeFilePermissions + // + // Purpose: change the permissions on an existing file + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void ChangeFilePermissions(const std::string& file, const unsigned int& permissions); + + //>--------------------------------------------------------------------- + // Function: CopyDirectory + // + // Purpose: recursivly copy a directory + //---------------------------------------------------------------------- + // Arguments: fromName - + // toName - + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void CopyDirectory(const std::string& fromName, const std::string& toName); + + //>--------------------------------------------------------------------- + // Function: CopyFile1 + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: fromName - + // toName - + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void CopyFile(const std::string& fromName, const std::string& toName); + + //>--------------------------------------------------------------------- + // Function: CreateDirectory + // + // Purpose: creates a directory + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + void CreateDirectory(const std::string& directory); + + //>--------------------------------------------------------------------- + // Function: getExePathAndFilename + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string GetExePathAndFilename(); + + //>--------------------------------------------------------------------- + // Function: isFile + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + int IsFile(const char* path); + + //>--------------------------------------------------------------------- + // Function: fileExists + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + bool FileExists(std::string filePath); + + //>--------------------------------------------------------------------- + // Function: isDirectory + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + int IsDirectory(const char* path); + + //>--------------------------------------------------------------------- + // Function: dirExists + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + bool DirExists(std::string dir); + + //>--------------------------------------------------------------------- + // Function: extractDirectory + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ExtractDirectory(std::string filePath); + + //>--------------------------------------------------------------------- + // Function: extractFilename + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ExtractFilename(std::string filePath); + + //>--------------------------------------------------------------------- + // Function: AddBackSlashToPath + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string AddSlashToPath(const std::string &path); + + //>--------------------------------------------------------------------- + // Function: RemoveSlashInFrontOfPath + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string RemoveSlashInFrontOfPath(const std::string &path); + + //>--------------------------------------------------------------------- + // Function: BuildPath + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string BuildPath(const std::string &left, const std::string &right = ""); + + //>--------------------------------------------------------------------- + // Function: getListOfFilesInADirectory + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + QStringList GetListOfFilesInADirectory(QDir dir, bool recursive); + + //>--------------------------------------------------------------------- + // Function: DescribeFileSize + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string DescribeFileSize(int64_t fileSizeInBytes); + + //>--------------------------------------------------------------------- + // Function: ChangeFileDateTime + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + void ChangeFileDateTime(std::string file, QDateTime desireModifiedDate, QDateTime desiredAccessedDate); + } +} + +#endif diff --git a/CommonLib/src/CommonLib/Lib/Util/MiscUtil.cpp b/CommonLib/src/CommonLib/Lib/Util/MiscUtil.cpp new file mode 100644 index 0000000..8339d38 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Util/MiscUtil.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "MiscUtil.hpp" +#include "StringUtil.hpp" +#include "Exception.hpp" +#include "Timestamp.hpp" + +//----------------------------------------------------------------------------- + +std::string Util::Misc::GetUuidString() +{ + static int uid = 0; + uid++; + + std::stringstream ss; + ss << uid; + + return ss.str(); +} + +//----------------------------------------------------------------------------- + +int Util::Misc::GetNumDecimalPlaces(double num) +{ + std::stringstream ss; + ss << num; + std::string fractionalStr = ""; + size_t index = ss.str().find("."); + if (index != std::string::npos) + fractionalStr = ss.str().substr(index+1); + return fractionalStr.length(); +} + +//----------------------------------------------------------------------------- + +std::map Util::Misc::GetUsersOfRunningProcess(const std::string& processName) +{ + std::map users; + + try + { + const int BUF_SIZE = 512; + char buf[BUF_SIZE] = {"\0"}; + std::string command = "pidof " + processName; + FILE *cmdPipe = popen(command.c_str(), "r"); + // run the command to get the process id of the process name + fgets(buf, BUF_SIZE, cmdPipe); + pclose(cmdPipe); + std::string val(buf); + val = val.substr(0, val.length()-1); + std::vector uidVec = Util::Strings::StringSplit(val, " ", false); + + for (std::vector::iterator it = uidVec.begin(); it != uidVec.end(); it++) + { + command = "ps -o uname= -p " + *it; + cmdPipe = popen(command.c_str(), "r"); + // get the user id of the process id + fgets(buf,BUF_SIZE,cmdPipe); + pclose(cmdPipe); + val = std::string(buf); + val = val.substr(0, val.length()-1); + users[*it]=val; + } + return users; + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::Misc::GetComputerName() +{ + char hostname[_SC_HOST_NAME_MAX]; + gethostname(hostname, _SC_HOST_NAME_MAX); + + return std::string(hostname); +} + +//----------------------------------------------------------------------------- + +std::string Util::Misc::ParseErrorStringForDisplay(const std::string& errMsg) +{ + // want everything to the left of -> + std::size_t pos = errMsg.find("->"); + + std::string userMsg = errMsg.substr (0, pos); + + return userMsg; +} + +//----------------------------------------------------------------------------- + + diff --git a/CommonLib/src/CommonLib/Lib/Util/MiscUtil.hpp b/CommonLib/src/CommonLib/Lib/Util/MiscUtil.hpp new file mode 100644 index 0000000..97d2d2a --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Util/MiscUtil.hpp @@ -0,0 +1,70 @@ +#ifndef MISC_UTIL_H +#define MISC_UTIL_H + +#include +#include +#include + +namespace Util +{ + namespace Misc + { + //>--------------------------------------------------------------------- + // Function: GetUuidString + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string GetUuidString(); + + //>--------------------------------------------------------------------- + // Function: GetNumDecimalPlaces + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + int GetNumDecimalPlaces(double num); + + //>--------------------------------------------------------------------- + // Function: GetUsersOfRunningProcess + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::map GetUsersOfRunningProcess(const std::string& processName); + + //>--------------------------------------------------------------------- + // Function: GetComputerName + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string GetComputerName(); + + //>--------------------------------------------------------------------- + // Function: ParseErrorStringForDisplay + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ParseErrorStringForDisplay(const std::string& errMsg); + + } +} + +#endif diff --git a/CommonLib/src/CommonLib/Lib/Util/StringUtil.cpp b/CommonLib/src/CommonLib/Lib/Util/StringUtil.cpp new file mode 100644 index 0000000..8c34cbc --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Util/StringUtil.cpp @@ -0,0 +1,378 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include "StringUtil.hpp" +#include "Exception.hpp" +#include "Timestamp.hpp" +#include "MiscUtil.hpp" + +//----------------------------------------------------------------------------- + +std::vector Util::Strings::StringSplit(const std::string& s, const std::string& delim, const bool keepEmpty) +{ + std::vector result; + if (delim.empty()) + { + result.push_back(s); + } + else + { + std::string::const_iterator substart = s.begin(), subend; + while (true) + { + subend = std::search(substart, s.end(), delim.begin(), delim.end()); + std::string temp(substart, subend); + if (keepEmpty || !temp.empty()) + { + result.push_back(temp); + } + if (subend == s.end()) + { + break; + } + substart = subend + delim.size(); + } + } + return result; +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::ToLower(const std::string& s) +{ + try + { + std::string lower(s); + for(size_t i = 0; i < s.length(); i++) + { + lower[i] = tolower(lower[i]); + } + + return lower; + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::ToUpper(const std::string& s) +{ + try + { + std::string upper(s); + for(size_t i = 0; i < s.length(); i++) + { + upper[i] = toupper(upper[i]); + } + + return upper; + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::TrimSpaces(const std::string& s) +{ + try + { + size_t firstNonSpace = s.find_first_not_of(' '); + size_t lastNonSpace = s.find_last_not_of(' '); + + // if no leading space, then start at beginning of string + if (firstNonSpace == std::string::npos) + { + firstNonSpace = 0; + } + + return s.substr(firstNonSpace, lastNonSpace - firstNonSpace + 1); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +bool Util::Strings::StrToInt(std::string string, int &value) +{ + bool status = true; + value = 0; + + TrimSpaces(string); + // CConvert string type to char array + const char* str = (char*) string.c_str(); + // CConvert string to integer type + int fieldCount = sscanf(str, "%d", &value); + + if ( fieldCount < 1 ) + status = false; + + return status; +} + +//----------------------------------------------------------------------------- + +double Util::Strings::ToDouble(const std::string& string) +{ + try + { + std::stringstream ss; + ss << string; + + double temp = 0.0; + + ss >> temp; + + return temp; + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::ToString(const int& val) +{ + try + { + std::stringstream ss; + ss << val; + return ss.str(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::ToString(const double& val) +{ + try + { + std::stringstream ss; + ss << val; + return ss.str(); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +std::string Util::Strings::GetSubstringDelimited(std::string s, std::string delimiter, int index) +{ + try + { + std::vector row; + + row = StringSplit(s, delimiter, false); + + if ((int)row.size() > index) + return row[index]; + else + return ""; + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//------------------------------------------------------------------------ + +bool Util::Strings::IsNumeric(const std::string& s) +{ + try + { + int sizeOfString = static_cast(s.length()); + int iteration = 0; + bool isNumeric = true; + + if (sizeOfString == 0) + isNumeric = false; + + while (iteration < sizeOfString) + { + if (!isdigit(s[iteration])) + { + isNumeric = false; + break; + } + + iteration++; + } + + return isNumeric; + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +bool Util::Strings::StringsAreEqual(std::string str1, std::string str2, bool caseSensitive) +{ + bool stringsAreEqual = false; + + QString pattern = "^" + QRegularExpression::escape(QString(str1.c_str())) + "$"; + + QRegularExpression::PatternOption regexOption = QRegularExpression::NoPatternOption; + + if (!caseSensitive) + regexOption = QRegularExpression::CaseInsensitiveOption; + + + QRegularExpression re(pattern, regexOption); + QRegularExpressionMatch match = re.match(str2.c_str()); + if (match.hasMatch()) + stringsAreEqual = true; + + return stringsAreEqual; + +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::DoubleToString(double num, int numDecimalPlaces, bool roundUp) +{ + //std::setprecision(2); + std::stringstream ss; + + if (Util::Misc::GetNumDecimalPlaces(num) >= numDecimalPlaces && roundUp) + { + ss << std::fixed << std::setprecision(numDecimalPlaces) << num; + } + else + ss << num; + + std::vector numVec = StringSplit(ss.str(), ".", false); + + // if the fractional part contains only 0's, we just want to display whole number + if (numVec.size() == 2) + { + int num2 = 0; + + if ( StrToInt(numVec[1], num2) ) + { + if (num2 == 0) + { + ss.str(""); + ss << numVec[0]; + } + else if (!roundUp) + { + ss.str(""); + + if (static_cast(numVec[1].length()) < numDecimalPlaces) + numDecimalPlaces = numVec[1].length(); + + ss << numVec[0] << "." << numVec[1].substr(0,numDecimalPlaces); + } + } + } + + return ss.str(); +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::ShortenStringToSize(std::string str, int desiredStrLen) +{ + std::string shortenString = str; + + if (str.length() > static_cast(desiredStrLen) && str.length() >= 5) + { + int leftCharCount = std::ceil((desiredStrLen - 3.0) / 2.0); + int rightCharCount = desiredStrLen - 3 - leftCharCount; + shortenString = str.substr(0, leftCharCount) + "..." + str.substr(str.length()-rightCharCount, rightCharCount); + } + return shortenString; +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::ByteArrayToString(unsigned char* byteArray, unsigned int numBytes) +{ + unsigned char* pTempBuf = new unsigned char[numBytes+1]; + + memcpy(pTempBuf, byteArray, numBytes); + pTempBuf[numBytes] = '\0'; + + std::string str(reinterpret_cast(pTempBuf)); + + delete[] pTempBuf; + + return str; +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::ByteArrayToHexString(unsigned char* byteArray, unsigned int numBytes) +{ + try + { + QByteArray buffer(reinterpret_cast(byteArray), numBytes); + return ByteArrayToHexString(buffer); + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} + +//----------------------------------------------------------------------------- + +std::string Util::Strings::ByteArrayToHexString(QByteArray& byteArray) +{ + std::string str = ""; + + str = QString(byteArray.toHex(' ')).toStdString(); + + return str; +} + +//----------------------------------------------------------------------------- + diff --git a/CommonLib/src/CommonLib/Lib/Util/StringUtil.hpp b/CommonLib/src/CommonLib/Lib/Util/StringUtil.hpp new file mode 100644 index 0000000..5718a05 --- /dev/null +++ b/CommonLib/src/CommonLib/Lib/Util/StringUtil.hpp @@ -0,0 +1,191 @@ +#ifndef STRING_UTIL_H +#define STRING_UTIL_H + +#include +#include + +#include + +namespace Util +{ + namespace Strings + { + //>--------------------------------------------------------------------- + // Function: Split + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::vector StringSplit(const std::string& s, const std::string& delim, const bool keepEmpty = false); + + //>--------------------------------------------------------------------- + // Function: ToDouble + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + double ToDouble(const std::string& string); + + //>--------------------------------------------------------------------- + // Function: ToLower + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ToLower(const std::string& s); + + //>--------------------------------------------------------------------- + // Function: ToString + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ToString(const int& val); + + //>--------------------------------------------------------------------- + // Function: ToString + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ToString(const double& val); + + //>--------------------------------------------------------------------- + // Function: ToUpper + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ToUpper(const std::string& s); + + //>--------------------------------------------------------------------- + // Function: TrimSpaces + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string TrimSpaces(const std::string& s); + + //>--------------------------------------------------------------------- + // Function: StrToInt + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + bool StrToInt(std::string string, int &value); + + //>--------------------------------------------------------------------- + // Function: GetSubstringDelimited + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string GetSubstringDelimited(std::string s, std::string delimiter, int index); + + //>--------------------------------------------------------------------- + // Function: IsNumeric + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + bool IsNumeric(const std::string& s); + + //>--------------------------------------------------------------------- + // Function: StringsAreEqual + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + bool StringsAreEqual(std::string str1, std::string str2, bool caseSensitive = false); + + //>--------------------------------------------------------------------- + // Function: DoubleToString + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string DoubleToString(double num, int numDecimalPlaces, bool roundUp); + + //>--------------------------------------------------------------------- + // Function: ShortenStringToSize + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ShortenStringToSize(std::string str, int desiredStrLen); + + //>--------------------------------------------------------------------- + // Function: ByteArrayToHexString + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ByteArrayToHexString(unsigned char* byteArray, unsigned int numBytes); + + //>--------------------------------------------------------------------- + // Function: ArrayToHexString + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ByteArrayToHexString(QByteArray& byteArray); + + //>--------------------------------------------------------------------- + // Function: ByteArrayToString + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: + //<--------------------------------------------------------------------- + std::string ByteArrayToString(unsigned char* byteArray, unsigned int numBytes); + } +} + +#endif diff --git a/CommonLib/src/CommonLib/Proc/Proc.cpp b/CommonLib/src/CommonLib/Proc/Proc.cpp new file mode 100644 index 0000000..bfcc187 --- /dev/null +++ b/CommonLib/src/CommonLib/Proc/Proc.cpp @@ -0,0 +1,111 @@ +#include +#include "Proc.hpp" +#include "IniFile.hpp" +#include "Exception.hpp" +#include "Timestamp.hpp" +#include "Condition.hpp" + +#include + +//----------------------------------------------------------------------------- + +Proc::Proc(): + m_eventMap() +{ + // Every project must create a singleton LinuxProc class that inherits Proc() + // All single classes must be instantiated in LinuxProc constructor + + // all singleton classes aka [class]::instance() need to be instantiated here as they create static variables + // they all should also be instantiated in one thread, preferable the main thread. Instantiating them in multiple + // threads will make it hard for us to synchronize their destructions, as static variables are destroyed in the order + // they were created per thread + + // all static variables are destroyed in the reverse order they were created per thread + // so any static variables that are dependent upon need to be instantiated first so they will be destroyed last + // LinuxProc and Proc are the last static variables to be instantiated and the first static variable to be destroyed + // as it needs to coordinate the destruction of all variables created on the heap + try { + initEvents(); + + } catch (Exception& e) { + + throw; + } +} + +//----------------------------------------------------------------------------- + +Proc::~Proc() +{ + // delete events + std::map::iterator eventIt; + for (eventIt = m_eventMap.begin(); eventIt != m_eventMap.end(); ++eventIt) + { + //std::string name = eventIt->second->GetName(); + delete eventIt->second; + eventIt->second = 0; + } +} + +//----------------------------------------------------------------------------- + +void Proc::initEvents() +{ + try + { + for (int i = 0; i < EventNames::NUM_EVENTS; i++) + { + EventNames::Names eventName = static_cast(i); + addEvent(eventName); + } + } + catch (Exception &e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } +} + +//---------------------------------------------------------------------------- + +void Proc::addEvent(const EventNames::Names& event) +{ + try + { + EventNames& eventNames = EventNames::Instance(); + std::string eventName = eventNames[event]; + m_eventMap[eventName] = new Condition(eventName); + } + catch (Exception &e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } +} + +//---------------------------------------------------------------------------- + +Condition& Proc::getEvent(const EventNames::Names& name) const +{ + try + { + std::map::const_iterator i = m_eventMap.find(EventNames::Instance()[name]); + + if (i == m_eventMap.end()) + { + std::stringstream oss; + oss << "couldn't find event: " + << EventNames::Instance()[name]; + throw Exception(Q_FUNC_INFO,oss.str()); + } + else + { + return *(i->second); + } + } + catch (Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw e; + } +} diff --git a/CommonLib/src/CommonLib/Proc/Proc.hpp b/CommonLib/src/CommonLib/Proc/Proc.hpp new file mode 100644 index 0000000..47c83c1 --- /dev/null +++ b/CommonLib/src/CommonLib/Proc/Proc.hpp @@ -0,0 +1,74 @@ +#ifndef PROC_H +#define PROC_H + +#include "EventNames.hpp" + +class Message; +class Condition; + +class Proc +{ + +public: + //>--------------------------------------------------------------------------- + // Function: ~Proc + // + // Purpose: Destroyer + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + virtual ~Proc(); + + //>--------------------------------------------------------------------- + // Function: GetEvent + // + // Purpose: Get a condition (ie: Event) + //---------------------------------------------------------------------- + // Arguments: name - the name of the condition + //---------------------------------------------------------------------- + // Return Value: a referance to the condition + //<--------------------------------------------------------------------- + Condition& getEvent(const EventNames::Names& name) const; + +protected: + //>--------------------------------------------------------------------------- + // Function: Proc + // + // Purpose: Ctor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + Proc(); + + //>--------------------------------------------------------------------- + // Function: AddEvent() + // + // Purpose: Add an event to this process + //---------------------------------------------------------------------- + // Arguments: + // event - the name of the event + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void addEvent(const EventNames::Names& event); + + //>--------------------------------------------------------------------- + // Function: InitEvents() + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void initEvents(); + +private: + std::map m_eventMap; +}; + +#endif diff --git a/CommonLib/src/CommonLib/Threading/Thread.cpp b/CommonLib/src/CommonLib/Threading/Thread.cpp new file mode 100644 index 0000000..c1a76a6 --- /dev/null +++ b/CommonLib/src/CommonLib/Threading/Thread.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include + +#include + +#include "Thread.hpp" +#include "Exception.hpp" + +//------------------------------------------------------------------------ + +Thread::Thread(const std::string &name, unsigned int stackSize) : + OSObject(name), + m_stackSize(stackSize), + m_id(0), + m_resumeCondition(name + "_RESUME_EVENT") +{ +} + +//------------------------------------------------------------------------ + +Thread::~Thread() +{ +} + +//------------------------------------------------------------------------ + +void* Thread::StartRoutine(void* pArg) +{ + // use this when we bomb + std::ostringstream oss; + oss << "Thread::startRoutine(): "; + + // this code below makes use of RTTI through dynamic_cast<>. For + // more information, see "The C++ Programming Language" 3th ed, by + // B. Stroustrup, section 15.4. + Thread *pBase = static_cast(pArg); + Thread *pObj = dynamic_cast(pBase); + + // hang out here until caller tells us to start + pBase->m_resumeCondition.wait(); + + if (pObj != 0) + { + try + { + (*pObj)(); // run the thread + } + catch (Exception& e) + { + oss << e.getMessage(); + // handle situation where thread is deleted before we get here + if (pObj) + { + oss << ", thread name = " << pObj->getName() << ", ID = " + << pObj->getID(); + } + oss << std::endl; + } + catch (std::exception& e) + { + oss << "std::exception: " + << e.what() + << " , thread name = " + << pObj->getName(); + } + catch (...) + { + oss << "pObj->operator()(), caught (...)," + << " thread name = " + << pObj->getName(); + } + } + else + { + oss << "Thread::startRoutine(): problem w/ dymanic cast" + << ", thread name = " + << pObj->getName(); + throw Exception(Q_FUNC_INFO,oss.str()); + } + + return 0; +} + +//------------------------------------------------------------------------ + +void Thread::create() +{ + try + { + pthread_attr_t attr; + pthread_attr_init(&attr); + + pthread_t handle = 0; + + int res = pthread_create(&handle, &attr, &StartRoutine, this); + + if (res != 0) + { + std::stringstream ss; + ss << "pthread_create returned: " << res; + throw Exception(Q_FUNC_INFO,ss.str()); + } + + if (handle == 0) + { + std::stringstream ss; + ss << "pthread_create returned null handle"; + throw Exception(Q_FUNC_INFO,ss.str()); + } + + setHandle((void*)handle); + } + catch(Exception& e) + { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } + catch(...) + { + throw Exception(Q_FUNC_INFO,"Thread::Create() - unknown err"); + } +} + +//------------------------------------------------------------------------ + +void Thread::resume() +{ + m_resumeCondition.signal(); +} diff --git a/CommonLib/src/CommonLib/Threading/Thread.hpp b/CommonLib/src/CommonLib/Threading/Thread.hpp new file mode 100644 index 0000000..047dffe --- /dev/null +++ b/CommonLib/src/CommonLib/Threading/Thread.hpp @@ -0,0 +1,162 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include +#include + +#include "OSObject.hpp" +#include "Condition.hpp" + +class Thread : public OSObject +{ +public: + enum { DEFAULT_STACK_SIZE = 4096 }; + + //>--------------------------------------------------------------------- + // Function: Thread() + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments:name - the name of this thread + // stackSize - + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + explicit Thread(const std::string &name, unsigned int stackSize = DEFAULT_STACK_SIZE); + + //>--------------------------------------------------------------------- + // Function: ~Thread() + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual ~Thread(); + + //>--------------------------------------------------------------------- + // Function: operator()() + // + // Purpose: where all the action is in the derived classes + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void operator()() = 0; + + //>--------------------------------------------------------------------- + // Function: create() + // + // Purpose: to make call the system thread creation routine + //---------------------------------------------------------------------- + // Arguments: LPTHREAD_START_ROUTINE func, pointer to start function + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void create(); + + //>--------------------------------------------------------------------- + // Function: getID() + // + // Purpose: to get the ID of the thread + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: unsigned int, the ID + //<--------------------------------------------------------------------- + unsigned int getID() const { return m_id; } + + //>--------------------------------------------------------------------- + // Function: resume() + // + // Purpose: to use the same call to resume MFC and non-MFC threads + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void resume(); + + //>--------------------------------------------------------------------- + // Function: startRoutine() + // + // Purpose: to call the run() method of this class. It uses RTTI to + // determine what the derived type is and calls correct method. + //---------------------------------------------------------------------- + // Arguments: LPVOID pArg, 'this' pointer from create() method + //---------------------------------------------------------------------- + // Return Value: DWORD WINAPI, return zero for now + //<--------------------------------------------------------------------- + static void* StartRoutine(void* pArg); + + //>--------------------------------------------------------------------- + // Function: StartThread() + // + // Purpose: Commands the thread to run + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void startThread() = 0; + + //>--------------------------------------------------------------------- + // Function: SuspendThread() + // + // Purpose: Commands the thread to pause its execution + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void suspendThread() = 0; + + //>--------------------------------------------------------------------- + // Function: Quit() + // + // Purpose: Command the thread to exit + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + virtual void quit() = 0; + + //>--------------------------------------------------------------------- + // Function: WaitForThreadToExit() + // + // Purpose: + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void waitForThreadToExit() { pthread_join((pthread_t)getHandle(), 0); }; + +private: + // do not allow + Thread(); + Thread(const Thread ©); + Thread &operator=(const Thread &rhs); + + unsigned int m_stackSize; + unsigned int m_id; + Condition m_resumeCondition; +}; diff --git a/CommonLib/src/Config/ConfigFileManager.cpp b/CommonLib/src/Config/ConfigFileManager.cpp new file mode 100644 index 0000000..fde23f1 --- /dev/null +++ b/CommonLib/src/Config/ConfigFileManager.cpp @@ -0,0 +1,69 @@ +#include "ConfigFileManager.hpp" +#include "LinuxProc.hpp" +#include "IniFile.hpp" +#include "Exception.hpp" +#include "StringUtil.hpp" + +#include + +//----------------------------------------------------------------------------- + +ConfigFileManager& ConfigFileManager::Instance() +{ + static ConfigFileManager confgMgr; + return confgMgr; +} + +//------------------------------------------------------------------------ + +ConfigFileManager::ConfigFileManager() +{ +} + +//------------------------------------------------------------------------ + +ConfigFileManager::~ConfigFileManager() +{ +} + +//------------------------------------------------------------------------ + +void ConfigFileManager::parseLogInfo(std::string sectionName) +{ + try { + std::vector iniKeys; + LinuxProc::Instance().getConfig().getKeys(sectionName, iniKeys); + + std::vector::iterator it; + for (it = iniKeys.begin(); it != iniKeys.end(); it++) + { + QRegularExpression re("^(test_data\\d+)[^\\d].+", QRegularExpression::CaseInsensitiveOption); + QRegularExpressionMatch match = re.match(std::string(*it).c_str()); + if (match.hasMatch()) { + std::string uniqueKey = match.captured(1).toStdString(); + if (m_testDataMap.find(uniqueKey) == m_testDataMap.end()) + { + std::string key = uniqueKey + "_path"; + + TestDataInfo testData; + testData.path = LinuxProc::Instance().getConfig().getString(sectionName, key); + key = uniqueKey + "_FILE_REGEX_PATTERNS"; + testData.fileRegexPatterns = LinuxProc::Instance().getConfig().getString(sectionName, key); + key = uniqueKey + "_TRANSFER_TYPE"; + if (Util::Strings::StringsAreEqual(LinuxProc::Instance().getConfig().getString(sectionName, key), "move")) + { + testData.transferType = FileTransferType::MOVE; + } + key = uniqueKey + "_PERFORM_CLEAN_UP"; + testData.performCleanUp = LinuxProc::Instance().getConfig().getBool(sectionName, key); + + m_testDataMap[uniqueKey] = testData; + } + } + } + + } catch (Exception& e) { + e.buildStackTrace(Q_FUNC_INFO); + throw; + } +} diff --git a/CommonLib/src/Config/ConfigFileManager.hpp b/CommonLib/src/Config/ConfigFileManager.hpp new file mode 100644 index 0000000..4b0f0b3 --- /dev/null +++ b/CommonLib/src/Config/ConfigFileManager.hpp @@ -0,0 +1,122 @@ +// UNCLASSIFIED +/*------------------------------------------------------------------------- +RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION +PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS +AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO +UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO +RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS +CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS +OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON +COMPANY. + +THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S. +GOVERNMENT. + +UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY. +-------------------------------------------------------------------------*/ +#pragma once + +#include "CustomDataTypes.hpp" + +#include +#include +#include + +class ConfigFileManager +{ +public: + enum class FileTransferType + { + COPY, + MOVE + }; + + struct TestDataInfo + { + std::string path; + std::string fileRegexPatterns; + FileTransferType transferType; + bool performCleanUp; + + TestDataInfo(): + path(), + fileRegexPatterns(), + transferType(FileTransferType::COPY), + performCleanUp(false) + { + } + }; + + static ConfigFileManager& Instance(); + + //>--------------------------------------------------------------------- + // Function: ~ConfigFileManager() + // + // Purpose: destructor + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + ~ConfigFileManager(); + + //>--------------------------------------------------------------------- + // Function: parseLogInfo() + // + // Purpose: + // + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + void parseLogInfo(std::string sectionName); + + //>--------------------------------------------------------------------- + // Function: getLogMap() + // + // Purpose: + // + //---------------------------------------------------------------------- + // Arguments: none + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + std::map getTestDataMap(){return m_testDataMap;} + +private: + //>--------------------------------------------------------------------- + // Function: ConfigFileManager() + // + // Purpose: constructor + //---------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + ConfigFileManager(); + + //>--------------------------------------------------------------------- + // Function: ConfigFileManager() + // + // Purpose: copy constructor + //---------------------------------------------------------------------- + // Arguments: const ConfigFileManager & - reference to ConfigFileManager obj + //---------------------------------------------------------------------- + // Return Value: none + //<--------------------------------------------------------------------- + ConfigFileManager(const ConfigFileManager &rhs); + + //>--------------------------------------------------------------------- + // Function: operator&() + // + // Purpose: assignment operator + //---------------------------------------------------------------------- + // Arguments: const ConfigFileManager & - reference to ConfigFileManager obj + //---------------------------------------------------------------------- + // Return Value: reference to ConfigFileManager obj + //<--------------------------------------------------------------------- + ConfigFileManager &operator=(const ConfigFileManager &); + + std::map m_testDataMap; +}; diff --git a/CommonLib/src/Config/config.ini b/CommonLib/src/Config/config.ini new file mode 100644 index 0000000..0646d82 --- /dev/null +++ b/CommonLib/src/Config/config.ini @@ -0,0 +1,68 @@ +[GENERAL] +APP_SINGLETON_LOCAL_PORT = 44453 + +CMD_LOCAL_PORT = 44454 + +SHOW_MAIN_WINDOW = false + +# true - log everything for debugging purposes +# false - minimum debugging +VERBOSE_LOGGING = TRUE + +BASE_TEST_DATA_PATH = /duc_file_server/TestData/ + +[SWDEV_COMPUTER] +COMPUTER_NAME_REGEX_PATTERN = -swdev$ +#COMPUTER_NAME_REGEX_PATTERN = -6700k$ +SHOW_MAIN_WINDOW = true +RUN_TELEMETRY = true +#TELEMETRY_APP_PATH = /net/netapp01/vol/vol1/home/dle/Devshed/Development/Linux/Telemetry/tlm +TELEMETRY_APP_PATH = /duc_file_server/Programming/C_CPP/UdpServer/UdpServer/Debug/UdpServer + +# If more than one host is moving data to main repository, only one computer should create the test folder while all other must wait +# until the folder is created +WAIT_TIME_BEFORE_CREATING_TEST_DATA_FOLDER = 3 + +TEST_DATA1_PATH = /duc_file_server/Programming/QTCreator/HWIL_Assistant/HWIL_Assistant/build_x64_debug/Output_Test +TEST_DATA1_FILE_REGEX_PATTERNS = _log\.txt +TEST_DATA1_TRANSFER_TYPE = move +TEST_DATA1_PERFORM_CLEAN_UP = true + +#TEST_DATA2_PATH = /duc_file_server/Programming/QTCreator/HWIL_Assistant/HWIL_Assistant/build_x64_debug/Output_Test +#TEST_DATA2_FILE_REGEX_PATTERN = ^log* +#TEST_DATA2_TRANSFER_TYPE = move +#TEST_DATA2_PERFORM_CLEAN_UP = true + +[SIXDOF_COMPUTER] +#COMPUTER_NAME_REGEX_PATTERN = -6dof$ +COMPUTER_NAME_REGEX_PATTERN = -6700k$ +SHOW_MAIN_WINDOW = true + +RUN_HOSTBUS = true +HOSTBUS_APP_PATH = /duc_file_server/Programming/C_CPP/UdpServer/UdpServer/Debug/UdpServer + +RUN_SIXDOF = true +SIXDOF_APP_PATH = /duc_file_server/Programming/C_CPP/UdpClient/UdpClient/Debug/UdpClient + +# If more than one host is moving data to main repository, only one computer should create the test folder while all other must wait +# until the folder is created +WAIT_TIME_BEFORE_CREATING_TEST_DATA_FOLDER = 4 + +TEST_DATA1_PATH = /duc_file_server/Programming/QTCreator/HWIL_Assistant/HWIL_Assistant/build_x64_debug/Output_Test +TEST_DATA1_FILE_REGEX_PATTERNS = _log\.txt +TEST_DATA1_TRANSFER_TYPE = move +TEST_DATA1_PERFORM_CLEAN_UP = true + +#TEST_DATA2_PATH = /duc_file_server/Programming/QTCreator/HWIL_Assistant/HWIL_Assistant/build_x64_debug/Output_Test +#TEST_DATA2_FILE_REGEX_PATTERN = ^log* +#TEST_DATA2_TRANSFER_TYPE = move +#TEST_DATA2_PERFORM_CLEAN_UP = true + +[VIDEO_COMPUTER] +#COMPUTER_NAME_REGEX_PATTERN = -vcs$ +COMPUTER_NAME_REGEX_PATTERN = -6700k$ +SHOW_MAIN_WINDOW = true + +# If more than one host is moving data to main repository, only one computer should create the test folder while all other must wait +# until the folder is created +WAIT_TIME_BEFORE_CREATING_TEST_DATA_FOLDER = 5 diff --git a/CommonLib/src/LinuxProc.cpp b/CommonLib/src/LinuxProc.cpp new file mode 100644 index 0000000..18f3483 --- /dev/null +++ b/CommonLib/src/LinuxProc.cpp @@ -0,0 +1,61 @@ +#include "LinuxProc.hpp" +#include "Exception.hpp" +#include "IniFile.hpp" +#include "Timestamp.hpp" +#include "ErrorLog.hpp" +#include "FileSystemUtil.hpp" + +#include + +//----------------------------------------------------------------------------- + +LinuxProc& LinuxProc::Instance() +{ + static LinuxProc proc; + return proc; +} + +//----------------------------------------------------------------------------- + +LinuxProc::LinuxProc(): + Proc() +{ + // all singleton classes aka [class]::instance() need to be instantiated here as they create static variables + // they all should also be instantiated in one thread, preferable the main thread. Instantiating them in multiple + // threads will make it hard for us to synchronize their destructions, as static variables are destroyed in the order + // they were created per thread + + // all static variables are destroyed in the reverse order they were created per thread + // so any static variables that are dependent upon need to be instantiated first so they will be destroyed last + // LinuxProc is the last static variables to be instantiated and the first static variable to be destroyed + // as it needs to coordinate the destruction of all variables created on the heap + try { + std::string exePath = Util::FileSystem::ExtractDirectory(Util::FileSystem::GetExePathAndFilename().c_str()); + + std::string configFilePath = Util::FileSystem::BuildPath(exePath,"config.ini"); + + m_iniFile = new IniFile(configFilePath); + + std::string logPath = Util::FileSystem::BuildPath(exePath,"Output"); + + Util::FileSystem::CreateDirectory(logPath); + + Timestamp time; + std::stringstream ss; + + ss << time.GetCurrentDateTimeString(Timestamp::DateTimeFormat::YYYYMMDD) << "_" << Timestamp::GetCurrentDateTimeString(Timestamp::DateTimeFormat::HHMMSS, "_") << "_log.txt"; + + ErrorLog::Instance(Util::FileSystem::BuildPath(logPath,ss.str())); + + } catch (Exception& e) { + e.buildStackTrace(__PRETTY_FUNCTION__); + throw; + } +} + +//----------------------------------------------------------------------------- + +LinuxProc::~LinuxProc() +{ + +} diff --git a/CommonLib/src/LinuxProc.hpp b/CommonLib/src/LinuxProc.hpp new file mode 100644 index 0000000..ff38fc4 --- /dev/null +++ b/CommonLib/src/LinuxProc.hpp @@ -0,0 +1,60 @@ +#ifndef LINUXPROC_H +#define LINUXPROC_H + +#include "Proc.hpp" + +class IniFile; + +class LinuxProc : public Proc +{ + +public: + //>--------------------------------------------------------------------------- + // Function: instance + // + // Purpose: singleton + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + static LinuxProc& Instance(); + + //>--------------------------------------------------------------------------- + // Function: getConfig + // + // Purpose: + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + IniFile& getConfig(){return *m_iniFile;}; + + //>--------------------------------------------------------------------------- + // Function: ~Proc + // + // Purpose: Destroyer + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + ~LinuxProc(); + +private: + //>--------------------------------------------------------------------------- + // Function: Proc + // + // Purpose: Ctor + //---------------------------------------------------------------------------- + // Arguments: + //---------------------------------------------------------------------------- + // Return Value: + //---------------------------------------------------------------------------- + LinuxProc(); + + IniFile* m_iniFile; +}; + +#endif diff --git a/CommonLib/src/UI/mainwindow.cpp b/CommonLib/src/UI/mainwindow.cpp new file mode 100644 index 0000000..94378d6 --- /dev/null +++ b/CommonLib/src/UI/mainwindow.cpp @@ -0,0 +1,15 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + diff --git a/CommonLib/src/UI/mainwindow.h b/CommonLib/src/UI/mainwindow.h new file mode 100644 index 0000000..07b4dd5 --- /dev/null +++ b/CommonLib/src/UI/mainwindow.h @@ -0,0 +1,21 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; +#endif // MAINWINDOW_H diff --git a/CommonLib/src/UI/mainwindow.ui b/CommonLib/src/UI/mainwindow.ui new file mode 100644 index 0000000..b232854 --- /dev/null +++ b/CommonLib/src/UI/mainwindow.ui @@ -0,0 +1,22 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + + + diff --git a/CommonLib/src/main.cpp b/CommonLib/src/main.cpp new file mode 100644 index 0000000..ba9cb26 --- /dev/null +++ b/CommonLib/src/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +}