From 27a91083a60df5f2f269b80a2bd35e7d658f6770 Mon Sep 17 00:00:00 2001 From: jwillmartin Date: Mon, 29 Jan 2024 17:41:34 -0500 Subject: [PATCH] modified: src/tmx/TmxApi/tmx/j2735_messages/RoadSideAlertMessage.hpp modified: src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp modified: src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp --- .../j2735_messages/RoadSideAlertMessage.hpp | 16 ++++- src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp | 65 +++++++++++++------ .../RSAPlugin/src/include/RsaPlugin.hpp | 17 +++-- 3 files changed, 68 insertions(+), 30 deletions(-) diff --git a/src/tmx/TmxApi/tmx/j2735_messages/RoadSideAlertMessage.hpp b/src/tmx/TmxApi/tmx/j2735_messages/RoadSideAlertMessage.hpp index d57408c4f..21230f739 100644 --- a/src/tmx/TmxApi/tmx/j2735_messages/RoadSideAlertMessage.hpp +++ b/src/tmx/TmxApi/tmx/j2735_messages/RoadSideAlertMessage.hpp @@ -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(std::shared_ptr message) { + return 1; +} + +TMX_J2735_NAMESPACE_END(j2735) +TMX_J2735_NAMESPACE_END(messages) +TMX_J2735_NAMESPACE_END(tmx) + +#endif /* TMX_J2735_MESSAGES_ROADSIDEALERTMESSAGE_HPP_ */ \ No newline at end of file diff --git a/src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp b/src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp index 83f489fa4..9fb9fe0e9 100644 --- a/src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp +++ b/src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp @@ -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 //========================================================================== @@ -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(this, &RsaPlugin::HandleRoadSideAlertMessage); + // Subscribe to all messages specified by the filters above. SubscribeToMessages(); } void RsaPlugin::RsaRequestHandler(QHttpEngine::Socket *socket) { - auto router = QSharedPointer::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) { @@ -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); } } @@ -61,17 +75,25 @@ int RsaPlugin::StartWebService() QSharedPointer handler(new OpenAPI::OAIApiRequestHandler()); handler = QSharedPointer (new OpenAPI::OAIApiRequestHandler()); + auto router = QSharedPointer::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(); } @@ -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 lock(_cfgLock); + lock_guard lock(_cfgLock); - GetConfigValue("WebServiceIP",webip); - GetConfigValue("WebServicePort",webport); - GetConfigValue("Instance", instance); - - std::thread webthread(&RsaPlugin::StartWebService,this); - webthread.detach(); // wait for the thread to finish + GetConfigValue("WebServiceIP", webip); + GetConfigValue("WebServicePort", webport); } void RsaPlugin::OnConfigChanged(const char *key, const char *value) @@ -112,9 +129,17 @@ 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 @@ -122,17 +147,17 @@ void RsaPlugin::BroadcastRsa(char * rsaJson) RsaMessage rsamessage; RsaEncodedMessage rsaENC; tmx::message_container_type container; - std::unique_ptr msg; + unique_ptr msg; try { - std::stringstream ss; + stringstream ss; ss << rsaJson; container.load(ss); rsamessage.set_contents(container.get_storage().get_tree()); - const std::string rsaString(rsaJson); + const string rsaString(rsaJson); rsaENC.encode_j2735_message(rsamessage); @@ -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; } @@ -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) diff --git a/src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp b/src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp index cdd51a5cf..89994d75b 100644 --- a/src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp +++ b/src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp @@ -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 @@ -57,13 +57,9 @@ class RsaPlugin: public PluginClient { public: RsaPlugin(std::string); - RsaPlugin(); virtual ~RsaPlugin(); int Main(); - uint16_t webport; - std::string webip; - protected: void UpdateConfigSettings(); @@ -71,9 +67,10 @@ class RsaPlugin: public PluginClient 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); @@ -81,8 +78,10 @@ class RsaPlugin: public PluginClient tmx::utils::UdpClient *_signSimClient = NULL; J2735MessageFactory factory; + uint16_t webport; + string webip; }; -std::mutex _cfgLock; +mutex _cfgLock; -}; +}