/*
 * SocketConnection.cpp
 *
 *  Created on: Jun 13, 2018
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */

#include "InputSocketHandler.h"


using Poco::Net::StreamSocket;
using Poco::Net::TCPServerConnection;
using flippR_driver::input::InputDriver;
using flippR_driver::input::EventHandler;
using flippR_driver::input::Event;

namespace flippR_driver
{
namespace networking
{
namespace input
{

InputSocketHandler::InputSocketHandler(StreamSocket streamSocket, std::shared_ptr<InputDriver> inputDriver) :
  TCPServerConnection(streamSocket),
  EventHandler(inputDriver)
{
}

void InputSocketHandler::run()
{
  //todo should ne closed when connection is closed
 // while(this->socket().poll(100, Poco::Net::Socket::SelectMode::SELECT_ERROR))
  while(true)
  {
    Event event = this->queue.pop();

    if(event.name == "END")
    {
      return;
    }

    std::string str = event.getJsonString() +  std::string(1, 0x02 ) ;


    this->socket().sendBytes(str.c_str(), str.size());
  }
}

void InputSocketHandler::handle(Event &event)
{
  this->queue.push(event);
}


}
}
}
