Subversion Repositories HomeAutomation

Rev

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

  1. /***************************************************************************
  2.  *   Copyright (C) December 27, 2008 by Mattias Runge                             *
  3.  *   mattias@runge.se                                                      *
  4.  *   candebug.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 "candebug.h"
  23.  
  24. CanDebug* CanDebug::myInstance = NULL;
  25.  
  26. CanDebug& CanDebug::getInstance()
  27. {
  28.     if (myInstance == NULL)
  29.     {
  30.         myInstance = new CanDebug();
  31.     }
  32.  
  33.     return *myInstance;
  34. }
  35.  
  36. void CanDebug::deleteInstance()
  37. {
  38.     if (myInstance != NULL)
  39.     {
  40.         delete myInstance;
  41.         myInstance = NULL;
  42.     }
  43. }
  44.  
  45. CanDebug::CanDebug()
  46. {
  47.     Thread<CanDebug>();
  48. }
  49.  
  50. CanDebug::~CanDebug()
  51. {
  52.     stop();
  53.  
  54.     map<int, AsyncSocket*>::iterator iter;
  55.  
  56.     for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
  57.     {
  58.         if (iter->second->isConnected())
  59.         {
  60.             iter->second->stop();
  61.         }
  62.  
  63.         delete iter->second;
  64.     }
  65. }
  66.  
  67. void CanDebug::run()
  68. {
  69.     SyslogStream &slog = SyslogStream::getInstance();
  70.  
  71.     string port = Settings::get("CanDebugPort");
  72.  
  73.     try
  74.     {
  75.         mySocket.setPort(stoi(port));
  76.  
  77.         mySocket.startListen();
  78.  
  79.         slog << "CanDebug is listening for connections on port " + port + ".\n";
  80.  
  81.         while (true)
  82.         {
  83.             AsyncSocket *newSocket = new AsyncSocket();
  84.  
  85.             if (mySocket.accept(newSocket))
  86.             {
  87.                 newSocket->sendDataDirect("Welcome to Atom CanDebug\n");
  88.  
  89.                 slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n";
  90.                 myClientSockets[newSocket->getSocket()] = newSocket;
  91.  
  92.                
  93.             }
  94.         }
  95.     }
  96.     catch (SocketException* e)
  97.     {
  98.         slog << "Exception was caught:" + e->getDescription() + ".\n";
  99.     }
  100. }
  101.  
  102. void CanDebug::sendData(string data)
  103. {
  104.     SyslogStream &slog = SyslogStream::getInstance();
  105.  
  106.     map<int, AsyncSocket*>::iterator iter;
  107.  
  108.     for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
  109.     {
  110.         if (iter->second->isConnected())
  111.         {
  112.             //slog << "CanDebug sent data to client " + itos(iter->second->getSocket()) + ".\n";
  113.             iter->second->sendDataDirect(data);
  114.         }
  115.         else
  116.         {
  117.             slog << "CanDebug client " + itos(iter->second->getSocket()) + " has disconnected.\n";
  118.             delete iter->second;
  119.             myClientSockets.erase(iter);
  120.         }
  121.     }
  122. }
  123.  
  124. void CanDebug::sendCanMessage(CanMessage canMessage)
  125. {
  126.     if (!canMessage.isHeartbeat() && myClientSockets.size() > 0)
  127.     {
  128.         string debugData = "";
  129.  
  130.         if (canMessage.getDirectionFlag() == "To_Owner")
  131.         {
  132.             debugData += "RX";
  133.         }
  134.         else if (canMessage.getDirectionFlag() == "From_Owner")
  135.         {
  136.             debugData += "TX";
  137.         }
  138.         else
  139.         {
  140.             debugData += "??";
  141.         }
  142.  
  143.         debugData += " " + canMessage.getClassName();
  144.         debugData += "_" + canMessage.getModuleName();
  145.         debugData += ":" + itos(canMessage.getModuleId());
  146.         debugData += " CMD=" + canMessage.getCommandName() + " ";
  147.  
  148.         map<string, CanVariable> vars = canMessage.getData();
  149.  
  150.         map<string, CanVariable>::iterator iter;
  151.  
  152.         for (iter = vars.begin(); iter != vars.end(); iter++)
  153.         {
  154.             debugData += " " + iter->second.getName() + "=" + iter->second.getValue();
  155.         }
  156.  
  157.         sendData("[" + niceTime() + "] " + debugData + "\n");
  158.     }
  159. }
  160.