From 0204bedcaf23e5809031581388c089272665a067 Mon Sep 17 00:00:00 2001
From: Hoop77 <p.badenhoop@gmx.de>
Date: Tue, 8 May 2018 19:14:15 +0200
Subject: [PATCH] pc builds now

---
 modules/catkin_ws/src/PC/include/Logger.h     | 26 ++++++++++--
 modules/catkin_ws/src/PC/src/Logger.cpp       | 40 ++++++++++++-------
 modules/catkin_ws/src/PC/src/main.cpp         |  6 ++-
 .../src/car/include/logging/Logging.h         |  1 +
 .../catkin_ws/src/car/src/logging/Logging.cpp |  5 ++-
 5 files changed, 57 insertions(+), 21 deletions(-)

diff --git a/modules/catkin_ws/src/PC/include/Logger.h b/modules/catkin_ws/src/PC/include/Logger.h
index 863493d7..a78e9897 100644
--- a/modules/catkin_ws/src/PC/include/Logger.h
+++ b/modules/catkin_ws/src/PC/include/Logger.h
@@ -12,7 +12,9 @@
 namespace pc
 {
 
-class Logger : public std::enable_shared_from_this<Logger>
+class Logger
+    : public std::enable_shared_from_this<Logger>
+      , private networking::Busyable
 {
 private:
     struct PrivateTag
@@ -35,15 +37,31 @@ public:
     static Ptr create(networking::Networking & net, const std::string & host)
     { return std::make_shared<Logger>(PrivateTag{}, net, host); }
 
-    Logger(PrivateTag, networking::Networking & net, const std::string & host)
+    Logger(PrivateTag, networking::Networking & net, const std::string & host);
+
+    void start();
+
+    void stop();
 
 private:
+    struct AsyncState
+    {
+        using Ptr = std::shared_ptr<AsyncState>;
+
+        AsyncState(Logger::Ptr self)
+            : lock(*self), self(self)
+        {}
+
+        networking::BusyLock lock;
+        Logger::Ptr self;
+    };
+
     networking::Networking & net;
     std::string host;
-    LoggingClient::Ptr loggingClient;
+    LoggingClient::Ptr client;
     networking::time::Timer::Ptr timer;
 
-    void log();
+    void log(AsyncState::Ptr state);
 };
 
 }
diff --git a/modules/catkin_ws/src/PC/src/Logger.cpp b/modules/catkin_ws/src/PC/src/Logger.cpp
index 893a4bbb..e8cb7d91 100644
--- a/modules/catkin_ws/src/PC/src/Logger.cpp
+++ b/modules/catkin_ws/src/PC/src/Logger.cpp
@@ -3,32 +3,44 @@
 //
 
 #include "../include/Logger.h"
+#include <iostream>
 
 namespace pc
 {
 
-void Logger::log()
+Logger::Logger(Logger::PrivateTag, networking::Networking & net, const std::string & host)
+    : net(net), host(host)
+{
+    client = LoggingClient::create(net, MAX_MESSAGE_SIZE);
+    timer = networking::time::Timer::create(net);
+}
+
+void Logger::start()
+{
+    auto self = shared_from_this();
+    auto state = std::make_shared<AsyncState>(self);
+    log(state);
+}
+
+void Logger::stop()
+{
+    timer->stop();
+    client->stop();
+}
+
+void Logger::log(AsyncState::Ptr state)
 {
     using namespace std::chrono_literals;
     std::string emptyRequest;
-    auto self = shared_from_this();
-    loggingClient->asyncCall(
+    client->asyncCall(
         emptyRequest, host, PORT, 10s,
-        [self](const auto & error, auto & responseMessage)
+        [state](const auto & error, auto & responseMessage)
         {
             std::cout << responseMessage;
-            self->timer->startTimeout(1ms, [self]
-            { self->log; });
+            state->self->timer->startTimeout(1ms, [state]
+            { state->self->log(state); });
         });
 }
 
-Logger::Logger(Logger::PrivateTag, networking::Networking & net, const std::string & host)
-    : net(net), host(host)
-{
-    loggingClient = LoggingClient::create(net, MAX_MESSAGE_SIZE);
-    timer = networking::time::Timer::create(net);
-    log();
-}
-
 }
 
diff --git a/modules/catkin_ws/src/PC/src/main.cpp b/modules/catkin_ws/src/PC/src/main.cpp
index a559f51b..4ca09fdc 100644
--- a/modules/catkin_ws/src/PC/src/main.cpp
+++ b/modules/catkin_ws/src/PC/src/main.cpp
@@ -7,8 +7,9 @@ int main()
     using namespace std::chrono_literals;
     float speed = 0.0f;
     networking::Networking net;
+    const std::string host{"10.5.37.195"};
 
-    pc2car::CommandSender commandSender{net, "10.5.37.195"};
+    pc2car::CommandSender commandSender{net, host};
     auto commandTimer = networking::time::Timer::create(net);
     commandTimer->startPeriodicTimeout(
         1s, 
@@ -18,7 +19,8 @@ int main()
             speed += 1.0f; 
         });
 
-    auto logger = pc::Logger::create(net, "10.5.37.195");
+    auto logger = pc::Logger::create(net, host);
+    logger->start();
 
     for (;;);
     return 0;
diff --git a/modules/catkin_ws/src/car/include/logging/Logging.h b/modules/catkin_ws/src/car/include/logging/Logging.h
index 25ef4c0f..93aa686c 100644
--- a/modules/catkin_ws/src/car/include/logging/Logging.h
+++ b/modules/catkin_ws/src/car/include/logging/Logging.h
@@ -29,6 +29,7 @@ class Logging : public nodelet::Nodelet
 public:
     static constexpr std::size_t PORT{10207};
     static constexpr std::size_t MAX_MESSAGE_SIZE{1024};
+    static constexpr std::size_t MAX_BUFFER_SIZE{0x100000}; // 1MB
 
     void onInit() override;
 
diff --git a/modules/catkin_ws/src/car/src/logging/Logging.cpp b/modules/catkin_ws/src/car/src/logging/Logging.cpp
index 9684e6f2..128b4ab5 100644
--- a/modules/catkin_ws/src/car/src/logging/Logging.cpp
+++ b/modules/catkin_ws/src/car/src/logging/Logging.cpp
@@ -14,15 +14,18 @@ namespace car
 
 constexpr std::size_t Logging::PORT;
 constexpr std::size_t Logging::MAX_MESSAGE_SIZE;
+constexpr std::size_t Logging::MAX_BUFFER_SIZE;
 
 Logging::Logging(ros::NodeHandle & nh, std::string & name)
     : nh_(nh)
       , name_(name)
+      , loggingBuffer(MAX_BUFFER_SIZE)
       , loggingStream(&loggingBuffer)
 {}
 
 Logging::Logging()
-    : loggingStream(&loggingBuffer)
+    : loggingBuffer(MAX_BUFFER_SIZE)
+      , loggingStream(&loggingBuffer)
 {}
 
 Logging::~Logging()
-- 
GitLab