From 2b9ffff7ecb9c29ba8bc0a9e70eb3b8a4063787d Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 11 Apr 2024 09:34:09 -0400 Subject: [PATCH 1/5] fix uzip --- .../CARMACloudPlugin/src/CARMACloudPlugin.cpp | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp index 4bd2a567a..bb78b2fa4 100644 --- a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp +++ b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp @@ -205,31 +205,28 @@ string CARMACloudPlugin::updateTags(string str,string tagout, string tagin) void CARMACloudPlugin::CARMAResponseHandler(QHttpEngine::Socket *socket) { - QString st; + QByteArray st; while(socket->bytesAvailable()>0) { auto readBytes = socket->readAll(); - if (socket->headers().keys().contains(CONTENT_ENCODING_KEY) && std::string(socket->headers().constFind(CONTENT_ENCODING_KEY).value().data()) == CONTENT_ENCODING_VALUE) - { - //readBytes is compressed in gzip format - st.append(UncompressBytes(readBytes)); - }else{ - st.append(readBytes); - } + st.append(readBytes); } - QByteArray array = st.toLocal8Bit(); - - char* _cloudUpdate = array.data(); // would be the cloud update packet, needs parsing - - - string tcm = _cloudUpdate; - PLOG(logINFO) << "Received TCM from cloud" << tcm << std::endl; - if(tcm.length() == 0) + if(st.size() == 0) { - PLOG(logERROR) << "Received TCM length is zero, and skipped." << std::endl; + PLOG(logERROR) << "Received TCM is empty, and skipped." << std::endl; return; } + PLOG(logINFO) << "Received TCM bytes size: " << st.size()<< std::endl; + + string tcm = ""; + bool isCompressed = socket->headers().keys().contains(CONTENT_ENCODING_KEY) && std::string(socket->headers().constFind(CONTENT_ENCODING_KEY).value().data()) == CONTENT_ENCODING_VALUE; + if (isCompressed) + { + tcm = UncompressBytes(st).data(); + }else{ + tcm = st.data(); + } //Transform carma-cloud TCM XML to J2735 compatible TCM XML by updating tags tcm=updateTags(tcm,"",""); @@ -237,9 +234,14 @@ void CARMACloudPlugin::CARMAResponseHandler(QHttpEngine::Socket *socket) tcm=updateTags(tcm,"TrafficControlParams","params"); tcm=updateTags(tcm,"TrafficControlGeometry","geometry"); tcm=updateTags(tcm,"TrafficControlPackage","package"); - - //List of tcm in string format - std::list tcm_sl = FilterTCMs(tcm); + + std::list tcm_sl = {}; + if (isCompressed) + { + tcm_sl = FilterTCMs(tcm); + }else{ + tcm_sl.push_back(tcm); + } for(const auto tcm_s: tcm_sl) { @@ -247,7 +249,6 @@ void CARMACloudPlugin::CARMAResponseHandler(QHttpEngine::Socket *socket) tsm5EncodedMessage tsm5ENC; tmx::message_container_type container; - std::stringstream ss; ss << tcm_s; @@ -376,7 +377,7 @@ void CARMACloudPlugin::TCMAckCheckAndRebroadcastTCM() } else { - PLOG(logDEBUG) << "NO TCMs to broadcast." << std::endl; + PLOG(logDEBUG4) << "NO TCMs to broadcast." << std::endl; _tcm_broadcast_times->clear(); _tcm_broadcast_starting_time->clear(); } @@ -604,13 +605,13 @@ void CARMACloudPlugin::ConvertString2Pair(std::pair &str_pair, co QByteArray CARMACloudPlugin::UncompressBytes(const QByteArray compressedBytes) const { z_stream strm; - strm.zalloc = nullptr;//Refer to zlib docs (https://zlib.net/zlib_how.html) - strm.zfree = nullptr; - strm.opaque = nullptr; + strm.zalloc = Z_NULL;//Refer to zlib docs (https://zlib.net/zlib_how.html) + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; strm.avail_in = compressedBytes.size(); - strm.next_in = (Byte *)compressedBytes.data(); + strm.next_in = (Byte *)compressedBytes.data(); //checking input z_stream to see if there is any error, eg: invalid data etc. - auto err = inflateInit2(&strm, MAX_WBITS + 16); // gzip input + auto err = inflateInit2(&strm, MAX_WBITS+32); // gzip input https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib QByteArray outBuf; //MAX numbers of bytes stored in a buffer const int BUFFER_SIZE = 4092; @@ -623,11 +624,10 @@ QByteArray CARMACloudPlugin::UncompressBytes(const QByteArray compressedBytes) c char buffer[BUFFER_SIZE] = {0}; strm.avail_out = BUFFER_SIZE; strm.next_out = (Byte *)buffer; - //Uncompress finished - isDone = inflate(&strm, Z_FINISH); - outBuf.append(buffer); - } while (Z_STREAM_END != isDone); //Reach the end of stream to be uncompressed - }else{ + isDone = inflate(&strm, Z_NO_FLUSH); + outBuf.append(buffer, BUFFER_SIZE - strm.avail_out); + } while (Z_STREAM_END != isDone); // Reach the end of stream to be uncompressed + }else{ PLOG(logWARNING) << "Error initalize stream. Err code = " << err << std::endl; } //Finished decompress data stream From 0e1ee6502a6591769a59e4c08d88262d89aa3d52 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 11 Apr 2024 10:53:04 -0400 Subject: [PATCH 2/5] variable format --- src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp index bb78b2fa4..2c9b518f7 100644 --- a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp +++ b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp @@ -219,7 +219,7 @@ void CARMACloudPlugin::CARMAResponseHandler(QHttpEngine::Socket *socket) } PLOG(logINFO) << "Received TCM bytes size: " << st.size()<< std::endl; - string tcm = ""; + std::string tcm = ""; bool isCompressed = socket->headers().keys().contains(CONTENT_ENCODING_KEY) && std::string(socket->headers().constFind(CONTENT_ENCODING_KEY).value().data()) == CONTENT_ENCODING_VALUE; if (isCompressed) { From 003134480d09e481fe4de07503585b67eea399cb Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 11 Apr 2024 13:28:12 -0400 Subject: [PATCH 3/5] indenit --- src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp index 2c9b518f7..ba05e9c85 100644 --- a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp +++ b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp @@ -625,7 +625,7 @@ QByteArray CARMACloudPlugin::UncompressBytes(const QByteArray compressedBytes) c strm.avail_out = BUFFER_SIZE; strm.next_out = (Byte *)buffer; isDone = inflate(&strm, Z_NO_FLUSH); - outBuf.append(buffer, BUFFER_SIZE - strm.avail_out); + outBuf.append(buffer, BUFFER_SIZE - strm.avail_out); } while (Z_STREAM_END != isDone); // Reach the end of stream to be uncompressed }else{ PLOG(logWARNING) << "Error initalize stream. Err code = " << err << std::endl; From 7bdd702484d6e82c03ff597b57532411f2de038a Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Thu, 11 Apr 2024 14:33:51 -0400 Subject: [PATCH 4/5] address comments --- .../CARMACloudPlugin/src/CARMACloudPlugin.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp index ba05e9c85..377905cb9 100644 --- a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp +++ b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp @@ -208,6 +208,7 @@ void CARMACloudPlugin::CARMAResponseHandler(QHttpEngine::Socket *socket) QByteArray st; while(socket->bytesAvailable()>0) { + PLOG(logDEBUG) << "Bytes available." << std::endl; auto readBytes = socket->readAll(); st.append(readBytes); } @@ -223,7 +224,12 @@ void CARMACloudPlugin::CARMAResponseHandler(QHttpEngine::Socket *socket) bool isCompressed = socket->headers().keys().contains(CONTENT_ENCODING_KEY) && std::string(socket->headers().constFind(CONTENT_ENCODING_KEY).value().data()) == CONTENT_ENCODING_VALUE; if (isCompressed) { - tcm = UncompressBytes(st).data(); + QByteArray tcmBytes = UncompressBytes(st); + if(tcmBytes.size() == 0) + { + return; + } + tcm = tcmBytes.data(); }else{ tcm = st.data(); } @@ -235,15 +241,15 @@ void CARMACloudPlugin::CARMAResponseHandler(QHttpEngine::Socket *socket) tcm=updateTags(tcm,"TrafficControlGeometry","geometry"); tcm=updateTags(tcm,"TrafficControlPackage","package"); - std::list tcm_sl = {}; + std::list tcmSL = {}; if (isCompressed) { - tcm_sl = FilterTCMs(tcm); + tcmSL = FilterTCMs(tcm); }else{ - tcm_sl.push_back(tcm); + tcmSL.push_back(tcm); } - for(const auto tcm_s: tcm_sl) + for(const auto tcm_s: tcmSL) { tsm5Message tsm5message; tsm5EncodedMessage tsm5ENC; @@ -628,7 +634,7 @@ QByteArray CARMACloudPlugin::UncompressBytes(const QByteArray compressedBytes) c outBuf.append(buffer, BUFFER_SIZE - strm.avail_out); } while (Z_STREAM_END != isDone); // Reach the end of stream to be uncompressed }else{ - PLOG(logWARNING) << "Error initalize stream. Err code = " << err << std::endl; + PLOG(logERROR) << "Error initalize stream. Err code = " << err << std::endl; } //Finished decompress data stream inflateEnd(&strm); From 2b04189125c6f78bbb5fa527c8666e02e3199d6e Mon Sep 17 00:00:00 2001 From: dan-du-car Date: Thu, 11 Apr 2024 19:59:04 +0000 Subject: [PATCH 5/5] add log --- src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp index 377905cb9..651384e09 100644 --- a/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp +++ b/src/v2i-hub/CARMACloudPlugin/src/CARMACloudPlugin.cpp @@ -240,6 +240,7 @@ void CARMACloudPlugin::CARMAResponseHandler(QHttpEngine::Socket *socket) tcm=updateTags(tcm,"TrafficControlParams","params"); tcm=updateTags(tcm,"TrafficControlGeometry","geometry"); tcm=updateTags(tcm,"TrafficControlPackage","package"); + PLOG(logDEBUG2) << "Received TCM: " << tcm << std::endl; std::list tcmSL = {}; if (isCompressed)