Files
HwilClient/HWIL_Client/HWIL_Client/source/HwilClient.cpp
2025-03-13 18:34:37 -07:00

221 lines
5.2 KiB
C++

//>>********************************************************************
// UNCLASSIFIED
//
// COPYRIGHT 2020 RAYTHEON COMPANY
//
// DESCRIPTION
// Class to be exported from this DLL for use in other assemblies
//
// $Revision: 1 $
//
// REVISION HISTORY
//
// 1 4/23/2020 Duc Le
// - initial checkin
//<<********************************************************************
#include "HwilClient.hpp"
#include "HwilUdpSocket.hpp"
#include "Exception.hpp"
#include "AutomationMsgParser.hpp"
#include "Message.hpp"
#include "GenericRspMessage.hpp"
#include "KwScenarioStarted.hpp"
#include "KwScenarioStopped.hpp"
#include "WaitForLastTaskCompletionCmdMessage.hpp"
#include "WinProc.hpp"
#include "IniFile.hpp"
#include "Timestamp.hpp"
#include "FileSystemUtil.hpp"
#include <sstream>
#include <iomanip>
//-----------------------------------------------------------------------------
HwilClient* HwilClient::instance()
{
static HwilClient* pClient = 0;
try
{
if (pClient == 0)
{
pClient = new HwilClient();
}
}
catch (Exception& ex)
{
ex.buildStackTrace(__FUNCTION_NAME__);
throw ex;
}
return pClient;
}
//-----------------------------------------------------------------------------
HwilClient::HwilClient():
pUdpSocket_(0)
{
try
{
pUdpSocket_ = new HwilUdpSocket();
}
catch (Exception& ex)
{
ex.buildStackTrace(__FUNCTION_NAME__);
throw ex;
}
}
//-----------------------------------------------------------------------------
HwilClient::~HwilClient()
{
if (pUdpSocket_ != 0)
delete pUdpSocket_;
}
//-----------------------------------------------------------------------------
void HwilClient::startingScenario(Constants::RemoteHosts remoteHost)
{
try
{
KwScenarioStarted cmd;
GenericRspMessage rsp(false);
SendCommandGetResponse(remoteHost, &cmd, &rsp);
if (!rsp.GetSuccessfulFlag())
{
throw Exception(__FUNCTION_NAME__, "Command failed to execute");
}
}
catch (Exception & ex)
{
ex.buildStackTrace(__FUNCTION_NAME__);
throw ex;
}
}
//-----------------------------------------------------------------------------
void HwilClient::endingScenario(Constants::RemoteHosts remoteHost)
{
try
{
bool createFolderSuccess = false;
Timestamp ts;
std::string basePath = WinProc::Instance().getConfig().getString("GENERAL", "BASE_TEST_DATA_PATH");
std::string relPath = "";
int runCount = 1;
std::stringstream testFolder;
std::string path = "";
std::string date;
std::string time;
ts.GetCurrentDateTimeString(date, time, Timestamp::DateFormat::YYYYMM, Timestamp::TimeFormat::HHMMSS, "_");
while (1)
{
testFolder.clear();
testFolder.str("");
testFolder << "run_" << std::setfill('0') << std::setw(3) << runCount++;
relPath = Util::FileSystem::BuildPath(date, testFolder.str());
path = Util::FileSystem::BuildPath(basePath, relPath);
if (!Util::FileSystem::IsDirectory(path))
{
createFolderSuccess = Util::FileSystem::CreateFolder(path.c_str());
break;
}
}
if (createFolderSuccess)
{
KwScenarioStopped cmd(relPath);
GenericRspMessage rsp(false);
SendCommandGetResponse(remoteHost, &cmd, &rsp);
if (!rsp.GetSuccessfulFlag())
{
throw Exception(__FUNCTION_NAME__, "Command failed to execute");
}
}
}
catch (Exception& ex)
{
ex.buildStackTrace(__FUNCTION_NAME__);
throw ex;
}
}
//-----------------------------------------------------------------------------
void HwilClient::waitForCompletion(Constants::RemoteHosts remoteHost, unsigned long timeoutMs)
{
try
{
WaitForLastTaskCompletionCmdMessage cmd;
GenericRspMessage rsp(false);
SendCommandGetResponse(remoteHost, &cmd, &rsp, timeoutMs);
if (!rsp.GetSuccessfulFlag())
{
throw Exception(__FUNCTION_NAME__, "Command failed to execute");
}
}
catch (Exception & ex)
{
ex.buildStackTrace(__FUNCTION_NAME__);
throw ex;
}
}
//-----------------------------------------------------------------------------
void HwilClient::SendCommandGetResponse(Constants::RemoteHosts remoteHost, Message* commandMsg, Message* rspMsg, unsigned long timeoutInMs)
{
std::stringstream ss;
try
{
// just need a buffer big enough for outgoing commands
const unsigned int BUF_SIZE = 1024;
unsigned char data[BUF_SIZE];
commandMsg->Format(data);
pUdpSocket_->write(remoteHost, data, commandMsg->GetEntireMessageLength());
unsigned int numBytesRead = 0;
unsigned char* data2;
pUdpSocket_->read(&data2, numBytesRead, timeoutInMs);
unsigned int msgId = 0;
AutomationMsgParser::instance().parseMsg(data2, numBytesRead, &msgId);
if (msgId != rspMsg->GetMessageID())
{
ss << "Expected MSG ID: " << rspMsg << ". Received MSG ID: " << msgId;
throw Exception(__FUNCTION_NAME__, ss.str());
}
rspMsg->Parse(data2);
}
catch (Exception & ex)
{
ex.buildStackTrace(__FUNCTION_NAME__);
throw ex;
}
}