Skip to content

Commit

Permalink
modified: src/tmx/TmxApi/tmx/j2735_messages/RoadSideAlertMessage.hpp
Browse files Browse the repository at this point in the history
	modified:   src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp
	modified:   src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp
  • Loading branch information
jwillmartin committed Jan 29, 2024
1 parent 2084ae1 commit 27a9108
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
16 changes: 15 additions & 1 deletion src/tmx/TmxApi/tmx/j2735_messages/RoadSideAlertMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,18 @@ TMX_J2735_DECLARE(Rsa, RoadSideAlert, api::roadSideAlert_D, api::MSGSUBTYPE_ROAD
TMX_J2735_DECLARE(Rsa, RoadSideAlert, api::roadSideAlert, api::MSGSUBTYPE_ROADSIDEALERT_STRING)
#endif

#endif /* TMX_J2735_MESSAGES_ROADSIDEALERTMESSAGE_HPP_ */
// Specialize the unique key function
TMX_J2735_NAMESPACE_START(tmx)
TMX_J2735_NAMESPACE_START(messages)
TMX_J2735_NAMESPACE_START(j2735)

template <>
inline int get_j2735_message_key<tmx::messages::RsaMessage>(std::shared_ptr<RoadSideAlert> message) {
return 1;
}

TMX_J2735_NAMESPACE_END(j2735)
TMX_J2735_NAMESPACE_END(messages)
TMX_J2735_NAMESPACE_END(tmx)

#endif /* TMX_J2735_MESSAGES_ROADSIDEALERTMESSAGE_HPP_ */
65 changes: 45 additions & 20 deletions src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Name : RsaPlugin.cpp
// Author : FHWA Saxton Transportation Operations Laboratory
// Version :
// Copyright : Copyright (c) 2019 FHWA Saxton Transportation Operations Laboratory. All rights reserved.
// Copyright : Copyright (c) 2023 FHWA Saxton Transportation Operations Laboratory. All rights reserved.
// Description : Rsa Plugin
//==========================================================================

Expand All @@ -20,13 +20,22 @@ RsaPlugin::RsaPlugin(string name): PluginClient(name)
// The log level can be changed from the default here.
FILELog::ReportingLevel() = FILELog::FromString("DEBUG");

AddMessageFilter<RsaMessage>(this, &RsaPlugin::HandleRoadSideAlertMessage);

// Subscribe to all messages specified by the filters above.
SubscribeToMessages();
}

void RsaPlugin::RsaRequestHandler(QHttpEngine::Socket *socket)
{
auto router = QSharedPointer<OpenAPI::OAIApiRouter>::create();
if(socket->bytesAvailable() == 0)
{
PLOG(logERROR) << "RSA Plugin does not receive web service request content!" << endl;
writeResponse(QHttpEngine::Socket::BadRequest, socket);
return;
}

// should read from the websocket and parse
QString st;
while(socket->bytesAvailable()>0)
{
Expand All @@ -36,12 +45,17 @@ void RsaPlugin::RsaRequestHandler(QHttpEngine::Socket *socket)

char* rsaMsgdef = array.data();
// Catch parse exceptions

stringstream ss;
ss << rsaMsgdef;
PLOG(logDEBUG) << "Received from webservice: " << ss.str() << endl;

try {
BroadcastRsa(rsaMsgdef);
writeResponse(QHttpEngine::Socket::Created, socket);
}
catch(const J2735Exception &e) {
PLOG(logERROR) << "Error parsing file: " << e.what() << std::endl;
catch (TmxException &ex) {
PLOG(logERROR) << "Failed to encode message : " << ex.what();
writeResponse(QHttpEngine::Socket::BadRequest, socket);
}
}
Expand All @@ -61,17 +75,25 @@ int RsaPlugin::StartWebService()
QSharedPointer<OpenAPI::OAIApiRequestHandler> handler(new OpenAPI::OAIApiRequestHandler());
handler = QSharedPointer<OpenAPI::OAIApiRequestHandler> (new OpenAPI::OAIApiRequestHandler());

auto router = QSharedPointer<OpenAPI::OAIApiRouter>::create();
router->setUpRoutes();

QObject::connect(handler.data(), &OpenAPI::OAIApiRequestHandler::requestReceived, [&](QHttpEngine::Socket *socket) {

this->RsaRequestHandler(socket);
});

QObject::connect(handler.data(), &OpenAPI::OAIApiRequestHandler::requestReceived, [&](QHttpEngine::Socket *socket) {
router->processRequest(socket);
});

QHttpEngine::Server server(handler.data());

if (!server.listen(address, port)) {
qCritical("Unable to listen on the specified port.");
qCritical("RsaPlugin:: Unable to listen on the specified port.");
return 1;
}
PLOG(logINFO)<<"RsaPlugin:: Started web service";
return a.exec();

}
Expand All @@ -86,17 +108,12 @@ void RsaPlugin::UpdateConfigSettings()
{
// Configuration settings are retrieved from the API using the GetConfigValue template class.
// This method does NOT execute in the main thread, so variables must be protected
// (e.g. using std::atomic, std::mutex, etc.).
// (e.g. using atomic, mutex, etc.).

int instance;
std::lock_guard<mutex> lock(_cfgLock);
lock_guard<mutex> lock(_cfgLock);

GetConfigValue<string>("WebServiceIP",webip);
GetConfigValue<uint16_t>("WebServicePort",webport);
GetConfigValue<int>("Instance", instance);

std::thread webthread(&RsaPlugin::StartWebService,this);
webthread.detach(); // wait for the thread to finish
GetConfigValue<string>("WebServiceIP", webip);
GetConfigValue<uint16_t>("WebServicePort", webport);
}

void RsaPlugin::OnConfigChanged(const char *key, const char *value)
Expand All @@ -112,27 +129,35 @@ void RsaPlugin::OnStateChange(IvpPluginState state)
if (state == IvpPluginState_registered)
{
UpdateConfigSettings();
// Start webservice needs to occur after the first updateConfigSettings call to acquire port and ip configurations.
// Also needs to be called from Main thread to work.
thread webthread(&RsaPlugin::StartWebService, this);
webthread.detach(); // wait for the thread to finish
}
}

void RsaPlugin::HandleRoadSideAlertMessage(RsaMessage &msg, routeable_message &routeableMsg)
{
PLOG(logDEBUG)<<"HandleRoadSideAlertMessage";
}

void RsaPlugin::BroadcastRsa(char * rsaJson)
{ //overloaded

RsaMessage rsamessage;
RsaEncodedMessage rsaENC;
tmx::message_container_type container;
std::unique_ptr<RsaEncodedMessage> msg;
unique_ptr<RsaEncodedMessage> msg;

try
{
std::stringstream ss;
stringstream ss;
ss << rsaJson;

container.load<XML>(ss);
rsamessage.set_contents(container.get_storage().get_tree());

const std::string rsaString(rsaJson);
const string rsaString(rsaJson);

rsaENC.encode_j2735_message(rsamessage);

Expand All @@ -152,9 +177,9 @@ void RsaPlugin::BroadcastRsa(char * rsaJson)

PLOG(logINFO) << " RSA Plugin :: Broadcast RSA:: " << rsaENC.get_payload_str();
}
catch(const std::exception& e)
catch(const exception& e)
{
PLOG(logWARNING) << "Error: " << e.what() << " broadcasting RSA for xml: " << rsaJson << std::endl;
PLOG(logWARNING) << "Error: " << e.what() << " broadcasting RSA for xml: " << rsaJson << endl;
}


Expand All @@ -176,7 +201,7 @@ void RsaPlugin::writeResponse(int responseCode , QHttpEngine::Socket *socket) {

int RsaPlugin::Main()
{
PLOG(logINFO) << "Starting plugin.";
PLOG(logINFO) << "RsaPlugin:: Starting plugin.\n";

uint msCount = 0;
while (_plugin->state != IvpPluginState_error)
Expand Down
17 changes: 8 additions & 9 deletions src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Name : RsaPlugin.cpp
// Author : FHWA Saxton Transportation Operations Laboratory
// Version :
// Copyright : Copyright (c) 2019 FHWA Saxton Transportation Operations Laboratory. All rights reserved.
// Copyright : Copyright (c) 2023 FHWA Saxton Transportation Operations Laboratory. All rights reserved.
// Description : RSA Plugin
//==========================================================================
#pragma once
Expand Down Expand Up @@ -57,32 +57,31 @@ class RsaPlugin: public PluginClient
{
public:
RsaPlugin(std::string);
RsaPlugin();
virtual ~RsaPlugin();
int Main();

uint16_t webport;
std::string webip;

protected:
void UpdateConfigSettings();

// Virtual method overrides.
void OnConfigChanged(const char *key, const char *value);
void OnStateChange(IvpPluginState state);

void HandleRoadSideAlertMessage(RsaMessage &msg, routeable_message &routeableMsg);
void BroadcastRsa(char *rsaJson);
int StartWebService();


int StartWebService();
void RsaRequestHandler(QHttpEngine::Socket *socket);
void writeResponse(int responseCode , QHttpEngine::Socket *socket);

private:
tmx::utils::UdpClient *_signSimClient = NULL;
J2735MessageFactory factory;

uint16_t webport;
string webip;

};
std::mutex _cfgLock;
mutex _cfgLock;

};
}

0 comments on commit 27a9108

Please sign in to comment.