Subversion Repositories HomeAutomation

Rev

Rev 1596 | 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 "Network.h"
  22.  
  23. #include <vector>
  24.  
  25. #include <boost/algorithm/string.hpp>
  26. #include <boost/lexical_cast.hpp>
  27.  
  28. #include "net/Manager.h"
  29.  
  30. namespace atom {
  31. namespace can {
  32.  
  33. Network::Network(std::string address): Subscriber(false), LOG("can::Network")
  34. {
  35.     this->address_ = address;
  36.     this->client_id_ = 0;
  37.        
  38.     net::Manager::Instance()->ConnectSlots(net::Client::SignalOnNewState::slot_type(&Network::SlotOnNewState, this, _1, _2, _3).track(this->tracker_),
  39.                                            net::Client::SignalOnNewData::slot_type(&Network::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
  40.    
  41.     // Examples of address
  42.     // udp:192.168.1.250:1100
  43.     // serial:/dev/ttyUSB0:38400
  44.    
  45.     std::vector<std::string> parts;
  46.     boost::algorithm::split(parts, address, boost::is_any_of(":"), boost::algorithm::token_compress_off);
  47.    
  48.     if (parts.size() < 3)
  49.     {
  50.         LOG.Error("Malformed address string: " + address);
  51.         return;
  52.     }
  53.    
  54.     boost::algorithm::to_lower(parts[0]);
  55.    
  56.     if (parts[0] == "udp")
  57.     {
  58.         this->protocol_ = net::PROTOCOL_UDP;
  59.     }
  60.     else if (parts[0] == "serial")
  61.     {
  62.         this->protocol_ = net::PROTOCOL_SERIAL;
  63.     }
  64.     else
  65.     {
  66.         LOG.Error("Unknown protocol, only support udp and serial, got " + parts[0]);
  67.         return;
  68.     }
  69.    
  70.     this->address_ = parts[1];
  71.     this->port_or_baud_ = boost::lexical_cast<unsigned int>(parts[2]);
  72.    
  73.     try
  74.     {
  75.         this->client_id_ = net::Manager::Instance()->Connect(this->protocol_, this->address_, this->port_or_baud_);
  76.         LOG.Info("Connected to CAN network at " + address);
  77.        
  78.         // TODO: Send ping
  79.     }
  80.     catch (std::exception e)
  81.     {
  82.         LOG.Error(e.what());
  83.     }
  84. }
  85.  
  86. Network::~Network()
  87. {
  88.     net::Manager::Instance()->Disconnect(this->client_id_);
  89. }
  90.  
  91. void Network::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, net::Buffer data)
  92. {
  93.     if (client_id == this->client_id_)
  94.     {
  95.         this->io_service_.post(boost::bind(&Network::SlotOnNewDataHandler, this, client_id, server_id, data));
  96.     }
  97. }
  98.  
  99. void Network::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  100. {
  101.     if (client_id == this->client_id_)
  102.     {
  103.         this->io_service_.post(boost::bind(&Network::SlotOnNewStateHandler, this, client_id, server_id, client_state));
  104.     }
  105. }
  106.  
  107. void Network::SlotOnMessageHandler(broker::Message::Pointer message)
  108. {
  109.     // TODO: Send to network
  110. }
  111.  
  112. void Network::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, net::Buffer data)
  113. {
  114.     // TODO: Send to broker
  115. }
  116.  
  117. void Network::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  118. {
  119.     if (client_state == net::CLIENT_STATE_DISCONNECTED)
  120.     {
  121.         LOG.Warning("Got disconnected from CAN network, will try to reconnect...");
  122.      
  123.         this->client_id_ = 0;
  124.        
  125.         try
  126.         {
  127.             this->client_id_ = net::Manager::Instance()->Connect(this->protocol_, this->address_, this->port_or_baud_);
  128.             LOG.Info("Connected to CAN network again.");
  129.         }
  130.         catch (std::exception e)
  131.         {
  132.             LOG.Error(e.what());
  133.         }
  134.     }
  135.     else
  136.     {
  137.         LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
  138.     }
  139. }
  140.    
  141. }; // namespace can
  142. }; // namespace atom
  143.