wrote socket handler
This commit is contained in:
@@ -19,6 +19,8 @@ class Event {
|
||||
public:
|
||||
Event(uint8_t address, int priority, std::string name);
|
||||
|
||||
std::string getJsonString();
|
||||
|
||||
friend bool operator==(const Event &left, const Event &right);
|
||||
|
||||
friend bool operator<(const Event &left, const Event &right) {
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
*/
|
||||
#include "input/Event.h"
|
||||
|
||||
#include <Poco/JSON/Object.h>
|
||||
|
||||
#include "utility/config.h"
|
||||
|
||||
namespace flippR_driver
|
||||
@@ -19,6 +21,17 @@ Event::Event(uint8_t address, int priority, std::string name) :
|
||||
CLOG_IF(VLOG_IS_ON(0), INFO, INPUT_LOGGER) << "Created event: " << name << ", address: " << address;
|
||||
}
|
||||
|
||||
std::string Event::getJsonString()
|
||||
{
|
||||
Poco::JSON::Object json;
|
||||
json.set("name", this->name);
|
||||
json.set("address", this->address);
|
||||
json.set("priority", this->priority);
|
||||
|
||||
std::stringstream stringstream;
|
||||
json.stringify(stringstream);
|
||||
}
|
||||
|
||||
bool operator==(const Event& left, const Event& right)
|
||||
{
|
||||
return left.name == right.name;
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
/*
|
||||
* SocketConnection.cpp
|
||||
*
|
||||
* Created on: Jun 13, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#include "SocketConnection.h"
|
||||
|
||||
|
||||
using Poco::Net::StreamSocket;
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* SocketConnection.h
|
||||
*
|
||||
* Created on: Jun 13, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#ifndef FLIPPR_CODE_INPUTSOCKETCONNECTION_H
|
||||
#define FLIPPR_CODE_INPUTSOCKETCONNECTION_H
|
||||
|
||||
|
||||
#include <Poco/Net/TCPServerConnection.h>
|
||||
|
||||
#include "Event.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace utility
|
||||
{
|
||||
namespace networking
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
class SocketConnection : public Poco::Net::TCPServerConnection
|
||||
{
|
||||
public:
|
||||
SocketConnection(Poco::Net::StreamSocket streamSocket);
|
||||
|
||||
void run();
|
||||
void sendEvent(flippR_driver::input::Event event);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif //FLIPPR_CODE_INPUTSOCKETCONNECTION_H
|
||||
@@ -1,12 +1,18 @@
|
||||
//
|
||||
// Created by rhetenor on 27.11.18.
|
||||
//
|
||||
/*
|
||||
* SocketConnection.cpp
|
||||
*
|
||||
* Created on: Jun 13, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#include "SocketHandler.h"
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
#include "json/json.hpp"
|
||||
using Poco::Net::StreamSocket;
|
||||
using Poco::Net::TCPServerConnection;
|
||||
using flippR_driver::input::InputDriver;
|
||||
using flippR_driver::input::detail::EventHandler;
|
||||
using flippR_driver::input::Event;
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
@@ -16,34 +22,38 @@ namespace networking
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
using namespace nlohmann;
|
||||
|
||||
InputSocketHandler::InputSocketHandler ()
|
||||
{}
|
||||
|
||||
void InputSocketHandler::handle (flippR_driver::input::Event &event)
|
||||
SocketHandler::SocketHandler(StreamSocket streamSocket, std::shared_ptr<InputDriver> inputDriver) :
|
||||
TCPServerConnection(streamSocket),
|
||||
EventHandler(inputDriver)
|
||||
{
|
||||
json event_serialization = serialize_event (event);
|
||||
|
||||
// write_to_socket(event_serialization);
|
||||
}
|
||||
|
||||
//json InputSocketHandler::serialize_event(input::Event &event)
|
||||
//{
|
||||
// json serialized_event = json("event");
|
||||
//
|
||||
// serialized_event["name"] = event.name;
|
||||
//
|
||||
// std::time_t activation_time = std::chrono::system_clock::to_time_t(event.last_activation);
|
||||
// boost::posix_time::ptime posix_time = boost::posix_time::from_time_t(activation_time);
|
||||
// serialized_event["activation_time"] = boost::posix_time::to_simple_string(posix_time); // todo learn to write time right
|
||||
//
|
||||
// serialized_event["priority"] = event.priority;
|
||||
//
|
||||
// return serialized_event;
|
||||
//}
|
||||
void SocketHandler::run()
|
||||
{
|
||||
StreamSocket& streamSocket = socket();
|
||||
|
||||
while(true)
|
||||
{
|
||||
Event event = this->queue.pop();
|
||||
|
||||
if(event.name == "END")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::string str = event.getJsonString();
|
||||
|
||||
streamSocket.sendBytes(str.c_str(), str.length());
|
||||
}
|
||||
}
|
||||
|
||||
void SocketHandler::handle(Event &event)
|
||||
{
|
||||
this->queue.push(event);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,20 @@
|
||||
//
|
||||
// Created by rhetenor on 27.11.18.
|
||||
//
|
||||
/*
|
||||
* SocketConnection.h
|
||||
*
|
||||
* Created on: Jun 13, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#ifndef FLIPPR_DRIVER_INPUTSOCKETHANDLER_H
|
||||
#define FLIPPR_DRIVER_INPUTSOCKETHANDLER_H
|
||||
#ifndef FLIPPR_CODE_INPUTSOCKETCONNECTION_H
|
||||
#define FLIPPR_CODE_INPUTSOCKETCONNECTION_H
|
||||
|
||||
#include "input/EventHandler.h"
|
||||
|
||||
#include <Poco/JSON/JSON.h>
|
||||
#include <Poco/Net/TCPServerConnection.h>
|
||||
|
||||
#include <string>
|
||||
#include "BlockingQueue.hpp"
|
||||
#include "InputDriver.h"
|
||||
#include "detail/EventHandler.h"
|
||||
#include "Event.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
@@ -19,16 +24,16 @@ namespace networking
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
class InputSocketHandler : public flippR_driver::input::EventHandler
|
||||
class SocketHandler : public Poco::Net::TCPServerConnection, flippR_driver::input::detail::EventHandler
|
||||
{
|
||||
public:
|
||||
InputSocketHandler ();
|
||||
SocketHandler(Poco::Net::StreamSocket streamSocket, std::shared_ptr<flippR_driver::input::InputDriver> input_driver);
|
||||
|
||||
void handle (flippR_driver::input::Event &event) override;
|
||||
void run() override;
|
||||
void handle(flippR_driver::input::Event &event) override;
|
||||
|
||||
private:
|
||||
std::string serialize_event (flippR_driver::input::Event &event);
|
||||
|
||||
flippR_driver::utility::BlockingQueue<flippR_driver::input::Event> queue;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -36,4 +41,4 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
#endif //FLIPPR_DRIVER_INPUTSOCKETHANDLER_H
|
||||
#endif //FLIPPR_CODE_INPUTSOCKETCONNECTION_H
|
||||
|
||||
Reference in New Issue
Block a user