Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2012  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Forward.h"
  22.  
  23. #include <boost/lexical_cast.hpp>
  24. #include <boost/algorithm/string/trim.hpp>
  25.  
  26. #include "net/Manager.h"
  27. #include "broker/Manager.h"
  28. #include "common/Byteset.h"
  29.  
  30. #include "Message.h"
  31.  
  32. namespace atom {
  33. namespace can {
  34.  
  35. Forward::Forward(unsigned int port): broker::Subscriber(port), LOG("can::Forward")
  36. {
  37.     try
  38.     {
  39.         this->server_id_ = net::Manager::Instance()->StartServer(net::TRANSPORT_PROTOCOL_TCP, port);
  40.         LOG.Info("Started TCP CAN forward server on port " + boost::lexical_cast<std::string>(port) + ".");
  41.         LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_) + ".");
  42.     }
  43.     catch (std::runtime_error& e)
  44.     {
  45.         LOG.Error(e.what());
  46.     }
  47. }
  48.  
  49. Forward::~Forward()
  50. {
  51.     net::Manager::Instance()->StopServer(this->server_id_);
  52.     this->server_id_ = 0;
  53. }
  54.  
  55.  
  56. void Forward::SlotOnMessageHandler(broker::Message::Pointer message)
  57. {
  58.     if (message->GetType() == broker::Message::CAN_RAW_BYTES)
  59.     {
  60.       net::Manager::Instance()->SendToAll(this->server_id_, message->GetRawData());
  61.     }
  62. }
  63.  
  64. void Forward::SlotOnNewDataHandler(net::SocketId id, common::Byteset data)
  65. {
  66.     if (std::find(this->clients_.begin(), this->clients_.end(), id) == this->clients_.end())
  67.     {
  68.       return;
  69.     }
  70.  
  71.   broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_RAW_BYTES, data, this)));
  72. }
  73.  
  74. void Forward::SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id)
  75. {
  76.   if (server_id == this->server_id_)
  77.   {
  78.     this->clients_.insert(id);
  79.   }
  80. }
  81.  
  82. void Forward::SlotOnNewStateHandler(net::SocketId id, net::ClientState client_state)
  83. {
  84.     if (std::find(this->clients_.begin(), this->clients_.end(), id) == this->clients_.end())
  85.     {
  86.       return;
  87.     }
  88.  
  89.     if (client_state == net::CLIENT_STATE_DISCONNECTED)
  90.     {
  91.         LOG.Debug("Client " + boost::lexical_cast<std::string>(id) + " has disconnected.");
  92.         this->clients_.erase(id);
  93.     }
  94.     else if (client_state == net::CLIENT_STATE_CONNECTED)
  95.     {
  96.         LOG.Debug("Client " + boost::lexical_cast<std::string>(id) + " has connected.");
  97.     }
  98.     else
  99.     {
  100.         LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
  101.     }
  102. }
  103.  
  104. }; // namespace can
  105. }; // namespace atom
  106.