From 694d9136159bbc0a54efa6076f6272b8f5d252e5 Mon Sep 17 00:00:00 2001
From: Hoop77 <p.badenhoop@gmx.de>
Date: Thu, 22 Mar 2018 16:50:05 +0100
Subject: [PATCH] logging node finished and pc side implemented

---
 modules/catkin_ws/src/PC/CMakeLists.txt       | 15 +++++
 modules/catkin_ws/src/PC/include/Logging.h    | 31 ++++++++++
 modules/catkin_ws/src/PC/src/Logging.cpp      | 58 +++++++++++++++++++
 modules/catkin_ws/src/PC/src/main.cpp         |  8 +++
 .../src/car/include/logging/Logging.h         |  7 +++
 .../catkin_ws/src/car/src/logging/Logging.cpp | 25 +++++++-
 6 files changed, 142 insertions(+), 2 deletions(-)
 create mode 100644 modules/catkin_ws/src/PC/CMakeLists.txt
 create mode 100644 modules/catkin_ws/src/PC/include/Logging.h
 create mode 100644 modules/catkin_ws/src/PC/src/Logging.cpp
 create mode 100644 modules/catkin_ws/src/PC/src/main.cpp

diff --git a/modules/catkin_ws/src/PC/CMakeLists.txt b/modules/catkin_ws/src/PC/CMakeLists.txt
new file mode 100644
index 00000000..226a1708
--- /dev/null
+++ b/modules/catkin_ws/src/PC/CMakeLists.txt
@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 3.9)
+project(PC)
+
+set(CMAKE_CXX_STANDARD 14)
+
+find_package(Boost REQUIRED COMPONENTS regex system)
+
+set(SOURCE_FILES
+        src/main.cpp
+        src/Logging.cpp
+        include/Logging.h)
+
+add_executable(PC ${SOURCE_FILES})
+
+target_link_libraries(PC ${Boost_LIBRARIES})
\ No newline at end of file
diff --git a/modules/catkin_ws/src/PC/include/Logging.h b/modules/catkin_ws/src/PC/include/Logging.h
new file mode 100644
index 00000000..edfcc572
--- /dev/null
+++ b/modules/catkin_ws/src/PC/include/Logging.h
@@ -0,0 +1,31 @@
+//
+// Created by philipp on 22.03.18.
+//
+
+#ifndef PC_LOGGING_H
+#define PC_LOGGING_H
+
+#include <boost/asio.hpp>
+
+namespace pc
+{
+
+class Logging
+{
+public:
+    explicit Logging(std::uint16_t port)
+        : port(port)
+    {}
+
+    void start();
+    void stop();
+
+private:
+    std::uint16_t port;
+    std::atomic<bool> running{false};
+    boost::asio::io_service ioService;
+};
+
+}
+
+#endif //PC_LOGGING_H
diff --git a/modules/catkin_ws/src/PC/src/Logging.cpp b/modules/catkin_ws/src/PC/src/Logging.cpp
new file mode 100644
index 00000000..8c5153e8
--- /dev/null
+++ b/modules/catkin_ws/src/PC/src/Logging.cpp
@@ -0,0 +1,58 @@
+//
+// Created by philipp on 22.03.18.
+//
+
+#include <iostream>
+#include "../include/Logging.h"
+
+namespace pc
+{
+
+void Logging::start()
+{
+    using boost::asio::ip::tcp;
+
+    if (running)
+        return;
+
+    running = true;
+    tcp::endpoint endpoint{tcp::v4(), port};
+    tcp::acceptor acceptor{ioService, endpoint};
+
+    while (running)
+    {
+        try
+        {
+            tcp::iostream stream;
+            boost::system::error_code ec;
+            acceptor.accept(*stream.rdbuf(), ec);
+            if (ec)
+                continue;
+
+            while (running)
+            {
+                std::string msg;
+                stream >> msg;
+                std::cout << msg << std::endl;
+                if (stream.error())
+                {
+                    if (stream.error() != boost::asio::error::eof)
+                        std::cerr << "LOGGING ERROR: " << stream.error().message() << "\n";
+
+                    break;
+                }
+            }
+        }
+        catch (const std::exception & e)
+        {
+            std::cerr << "LOGGING ERROR: " << e.what() << "\n";
+        }
+    }
+}
+
+void Logging::stop()
+{
+    running = false;
+}
+
+}
\ No newline at end of file
diff --git a/modules/catkin_ws/src/PC/src/main.cpp b/modules/catkin_ws/src/PC/src/main.cpp
new file mode 100644
index 00000000..3594c9a7
--- /dev/null
+++ b/modules/catkin_ws/src/PC/src/main.cpp
@@ -0,0 +1,8 @@
+#include "../include/Logging.h"
+
+int main()
+{
+    pc::Logging logging{10207};
+    logging.start();
+    return 0;
+}
\ No newline at end of file
diff --git a/modules/catkin_ws/src/car/include/logging/Logging.h b/modules/catkin_ws/src/car/include/logging/Logging.h
index 0c0f969c..a6d7e7f0 100644
--- a/modules/catkin_ws/src/car/include/logging/Logging.h
+++ b/modules/catkin_ws/src/car/include/logging/Logging.h
@@ -8,6 +8,8 @@
 #include <nodelet/nodelet.h>
 #include <ros/ros.h>
 
+#include <boost/asio.hpp>
+
 #include "car/camDataMsg.h"
 #include "car/ccDataMsg.h"
 #include "car/environmentDataMsg.h"
@@ -35,6 +37,9 @@ private:
     ros::NodeHandle nh_;
     std::string name_;
 
+    std::string host{"127.0.0.1"};
+    std::string port{"10207"};
+
     ros::Subscriber camData;
     ros::Subscriber ccData;
     ros::Subscriber environmentData;
@@ -45,6 +50,8 @@ private:
     ros::Subscriber stmData;
     ros::Subscriber ussData;
 
+    void logMessage(const std::string & str);
+
     void camDataCallback(const camDataMsg::ConstPtr & inMsg);
     void ccDataCallback(const ccDataMsg::ConstPtr & inMsg);
     void environmentDataCallback(const environmentDataMsg::ConstPtr & inMsg);
diff --git a/modules/catkin_ws/src/car/src/logging/Logging.cpp b/modules/catkin_ws/src/car/src/logging/Logging.cpp
index 688eacfd..cef2c2ff 100644
--- a/modules/catkin_ws/src/car/src/logging/Logging.cpp
+++ b/modules/catkin_ws/src/car/src/logging/Logging.cpp
@@ -7,8 +7,7 @@
 
 #include "logging/Logging.h"
 
-PLUGINLIB_EXPORT_CLASS(car::Logging, nodelet::Nodelet
-);
+PLUGINLIB_EXPORT_CLASS(car::Logging, nodelet::Nodelet);
 
 namespace car
 {
@@ -40,6 +39,27 @@ void Logging::onInit()
     NODELET_INFO("Logging::onInit -- END");
 }
 
+void Logging::logMessage(const std::string & msg)
+{
+    try
+    {
+        using boost::asio::ip::tcp;
+        tcp::iostream stream;
+        stream.expires_from_now(boost::posix_time::seconds(10));
+        stream.connect(host, port);
+        if (stream.error())
+        {
+            NODELET_ERROR_STREAM("Logging: Could not connect to PC!\n" << stream.error().message());
+            return;
+        }
+        stream << msg;
+    }
+    catch (const std::exception & e)
+    {
+        NODELET_ERROR_STREAM("Logging: Could not connect to PC!\n" << e.what());
+    }
+}
+
 void Logging::ccDataCallback(const ccDataMsg::ConstPtr & inMsg)
 {
     std::cout << "Logging received new cc data ( ).\n";
@@ -53,6 +73,7 @@ void Logging::camDataCallback(const camDataMsg::ConstPtr & inMsg)
 void Logging::environmentDataCallback(const environmentDataMsg::ConstPtr & inMsg)
 {
     std::cout << "Logging received new environment data ( ).\n";
+    logMessage(std::string{"Environment [distance]: "} + std::to_string(inMsg->distance));
 }
 
 void Logging::laneDataCallback(const laneDataMsg::ConstPtr & inMsg)
-- 
GitLab