Skip to content

Commit 8bef044

Browse files
committed
IPs now return as a JSON array, echo data now tries to format as JSON
1 parent 2b06a06 commit 8bef044

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

driver_files/src/Driver/VRDriver.cpp

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include <thread>
2222
#include <vector>
2323

24+
// -----------------------
25+
#include <nlohmann/json.hpp>
26+
using json = nlohmann::json;
27+
// -----------------------
2428

2529
// Report a failure
2630
void fail(boost::system::error_code ec, char const* what) {
@@ -637,11 +641,56 @@ class session : public boost::asio::coroutine,
637641

638642
std::string reply_echo = get_data_echo();
639643
unlock_data_echo();
644+
645+
std::string reply_echo_copy = reply_echo;
646+
647+
reply_echo = reply_echo + std::string(" ");
648+
649+
650+
std::size_t n = reply_echo.length();
651+
std::string escaped;
652+
escaped.reserve(n * 2); // pessimistic preallocation
653+
654+
for (std::size_t i = 0; i < n; ++i) {
655+
if (reply_echo[i] == '\\' && reply_echo[i + 1] == '\"') {
656+
657+
}
658+
else {
659+
escaped += reply_echo[i];
660+
}
661+
}
662+
663+
reply_echo = escaped;
664+
665+
666+
667+
size_t endpos = reply_echo.find_last_not_of(" \t");
668+
size_t startpos = reply_echo.find_first_not_of(" \t");
669+
if (std::string::npos != endpos)
670+
{
671+
reply_echo = reply_echo.substr(0, endpos + 1);
672+
reply_echo = reply_echo.substr(startpos);
673+
}
674+
else {
675+
reply_echo.erase(std::remove(std::begin(reply_echo), std::end(reply_echo), ' '), std::end(reply_echo));
676+
}
677+
678+
679+
680+
681+
682+
683+
if (reply_echo.length() > 0 && json::accept(reply_echo)) {
684+
//If valid JSON we use the modified string we made, otherwise use the original string
685+
}else{
686+
reply_echo = std::string("\"") + reply_echo_copy + std::string("\"");
687+
}
688+
640689

641690
std::string local_ip_str = get_ip_echo();
642691
unlock_ip_echo();
643692

644-
std::string reply_message = "{\"vr_trackers\": [" + reply_data + "], \"echo\": \"" + reply_echo + "\", \"ip\": \"" + local_ip_str + "\"" + "}";
693+
std::string reply_message = "{\"vr_trackers\": [" + reply_data + "], \"echo\": " + reply_echo + ", \"ip\": [" + local_ip_str + "]" + "}";
645694

646695

647696
// std::string reply_message = "{\"vr_trackers\": [" + reply_data + "], \"echo\": \"" + reply_echo + "\"" + "}";
@@ -780,7 +829,7 @@ int SaveLocalIP()
780829

781830

782831
std::string current_ip(inet_ntoa(addr));
783-
output_string = output_string + current_ip;
832+
output_string = output_string + std::string("\"") + current_ip + std::string("\"");
784833
}
785834

786835
set_ip_echo(output_string);
@@ -823,10 +872,6 @@ std::string GetExePath()
823872
#include <cassert>
824873

825874
int multithreadServer_ws() {
826-
// set_ip_echo(getLocalIP());
827-
// unlock_ip_echo();
828-
SaveLocalIP();
829-
830875
// Code for the websocket server
831876
boost::asio::io_context ioc{ 1 };
832877
auto const address = boost::asio::ip::make_address("0.0.0.0");
@@ -868,14 +913,15 @@ void startServer() {
868913
static bool running = FALSE;
869914
if (!running) {
870915
running = TRUE;
916+
917+
// set_ip_echo(getLocalIP());
918+
// unlock_ip_echo();
919+
SaveLocalIP();
920+
871921
boost::thread* thread = new boost::thread(&multithreadServer_ws);
872922
boost::thread* thread_web = new boost::thread(&multithreadServer_web);
873923
}
874924
}
875-
// -----------------------
876-
#include <nlohmann/json.hpp>
877-
using json = nlohmann::json;
878-
// -----------------------
879925

880926

881927

0 commit comments

Comments
 (0)