Subversion Repositories HomeAutomation

Rev

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

  1. /***************************************************************************
  2.  *   Copyright (C) November 27, 2008 by Mattias Runge                             *
  3.  *   mattias@runge.se                                                      *
  4.  *   cannetchannel.cpp                                            *
  5.  *                                                                         *
  6.  *   This program is free software; you can redistribute it and/or modify  *
  7.  *   it under the terms of the GNU General Public License as published by  *
  8.  *   the Free Software Foundation; either version 2 of the License, or     *
  9.  *   (at your option) any later version.                                   *
  10.  *                                                                         *
  11.  *   This program is distributed in the hope that it will be useful,       *
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  14.  *   GNU General Public License for more details.                          *
  15.  *                                                                         *
  16.  *   You should have received a copy of the GNU General Public License     *
  17.  *   along with this program; if not, write to the                         *
  18.  *   Free Software Foundation, Inc.,                                       *
  19.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  20.  ***************************************************************************/
  21.  
  22. #include "cannetmanager.h"
  23.  
  24.  
  25. #include "cannetchannel.h"
  26.  
  27. CanNetChannel::CanNetChannel()
  28. {
  29. }
  30.  
  31. CanNetChannel::~CanNetChannel()
  32. {
  33. }
  34.  
  35. void CanNetChannel::sendString(string data)
  36. {
  37.     SyslogStream &slog = SyslogStream::getInstance();
  38.  
  39.     try
  40.     {
  41.         myClientSocket << data;
  42.     }
  43.     catch (SocketException& e)
  44.     {
  45.         slog << "SocketException was caught:\n";
  46.         slog << e.getDescription() << "\n";
  47.     }
  48. }
  49.  
  50. void CanNetChannel::run()
  51. {
  52.     CanNetManager &canMan = CanNetManager::getInstance();
  53.     SyslogStream &slog = SyslogStream::getInstance();
  54.  
  55.     try
  56.     {
  57.         string address = Settings::get("CanNetAddress");
  58.         string port = Settings::get("CanNetPort");
  59.  
  60.         slog << "Trying to connect to canDaemon on " << address << ":" << port << "...\n";
  61.  
  62.         myClientSocket.start(address, atoi(port.c_str()));
  63.  
  64.         slog << "Connected to canDaemon.\n";
  65.  
  66.         string data;
  67.  
  68.         while (1)
  69.         {
  70.             myClientSocket >> data;
  71.  
  72.             try
  73.             {
  74.                 CanMessage canMessage;
  75.                 canMessage.setRaw(data);
  76.  
  77.                 if (canMessage.isUnknown())
  78.                 {
  79.                     slog << "Received unknown message. Skipping...\n";
  80.                 }
  81.                 else
  82.                 {
  83.                     canMan.addToInQueue(canMessage);
  84.                 }
  85.             }
  86.             catch (CanMessageException& e)
  87.             {
  88.                 slog << "CanMessageException was caught:\n";
  89.                 slog << e.getDescription() << "\n";
  90.             }
  91.  
  92.             //myClientSocket << "TEST\n";
  93.  
  94.             data = "";
  95.         }
  96.     }
  97.     catch (SocketException& e)
  98.     {
  99.         slog << "SocketException was caught:\n";
  100.         slog << e.getDescription() << "\n";
  101.     }
  102. }
  103.  
  104.