Subversion Repositories HomeAutomation

Rev

Rev 1596 | Rev 1598 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  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 "Monitor.h"
  22.  
  23. #include <boost/lexical_cast.hpp>
  24. #include <boost/algorithm/string/trim.hpp>
  25.  
  26. #include "net/Manager.h"
  27.  
  28. #include "Message.h"
  29.  
  30. namespace atom {
  31. namespace can {
  32.    
  33. Monitor::Monitor(unsigned int port): Subscriber(port), LOG("can::Monitor")
  34. {
  35.     net::Manager::Instance()->ConnectSlots(net::Client::SignalOnNewState::slot_type(&Monitor::SlotOnNewState, this, _1, _2, _3).track(this->tracker_),
  36.                                            net::Client::SignalOnNewData::slot_type(&Monitor::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
  37.    
  38.     try
  39.     {
  40.         this->server_id_ = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, port);
  41.         LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port));
  42.         LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_));
  43.     }
  44.     catch (std::exception e)
  45.     {
  46.         LOG.Error(e.what());
  47.     }
  48. }
  49.  
  50. Monitor::~Monitor()
  51. {
  52.     net::Manager::Instance()->StopServer(this->server_id_);
  53. }
  54.  
  55.  
  56. void Monitor::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
  57. {
  58.     if (server_id == this->server_id_)
  59.     {
  60.         type::Byteset temp_buffer = data;
  61.         this->io_service_.post(boost::bind(&Monitor::SlotOnNewDataHandler, this, client_id, server_id, temp_buffer));
  62.     }
  63. }
  64.  
  65. void Monitor::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  66. {
  67.     if (server_id == this->server_id_)
  68.     {
  69.         this->io_service_.post(boost::bind(&Monitor::SlotOnNewStateHandler, this, client_id, server_id, client_state));
  70.     }
  71. }
  72.  
  73. void Monitor::SlotOnMessageHandler(broker::Message::Pointer message)
  74. {
  75.     if (message->GetType() == broker::Message::CAN_MESSAGE)
  76.     {
  77.         Message* payload = static_cast<Message*>(message->GetPayload().get());
  78.        
  79.         std::string line = "";
  80.        
  81.         if (payload->GetClassName() == "nmt")
  82.         {
  83.             line += "NMT";
  84.         }
  85.         else
  86.         {
  87.             if (payload->GetDirectionName() == "To_Owner")
  88.             {
  89.                 line += "RX";
  90.             }
  91.             else if (payload->GetDirectionName() == "From_Owner")
  92.             {
  93.                 line += "TX";
  94.             }
  95.             else
  96.             {
  97.                 line += "??";
  98.             }
  99.            
  100.             line += " " + payload->GetClassName();
  101.             line += "_" + payload->GetModuleName();
  102.             line += ":" + boost::lexical_cast<std::string>(payload->GetId());
  103.         }
  104.        
  105.         line += " CMD=" + payload->GetCommandName() + " ";
  106.        
  107.         Message::VariableList variables = payload->GetVariables();
  108.        
  109.         for (Message::VariableList::iterator it = variables.begin(); it != variables.end(); it++)
  110.         {
  111.             line += " " + it->first + "=" + it->second;
  112.         }
  113.        
  114.         line += "\n";
  115.        
  116.         net::Manager::Instance()->SendToAll(this->server_id_, type::Byteset(line));
  117.     }
  118. }
  119.  
  120. void Monitor::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
  121. {
  122.     std::string str = data.ToCharString();
  123.    
  124.     boost::algorithm::trim_right_if(str, boost::is_any_of("\r\n"));
  125.    
  126.     LOG.Debug("Received: \"" + str + "\" from client " + boost::lexical_cast<std::string>(client_id) + " on server " + boost::lexical_cast<std::string>(server_id));
  127.    
  128.     if (str == "q" || str == "quit")
  129.     {
  130.         LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has requested to be disconnected.");
  131.         net::Manager::Instance()->Disconnect(client_id);
  132.     }
  133. }
  134.  
  135. void Monitor::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  136. {
  137.     if (client_state == net::CLIENT_STATE_DISCONNECTED)
  138.     {
  139.         LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected.");
  140.     }
  141.     else if (client_state == net::CLIENT_STATE_CONNECTED)
  142.     {
  143.         LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has connected.");
  144.         net::Manager::Instance()->SendTo(client_id, type::Byteset("Welcome to Atom CAN monitoring\n"));
  145.     }
  146.     else
  147.     {
  148.         LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
  149.     }
  150. }
  151.    
  152. }; // namespace can
  153. }; // namespace atom
  154.