Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

/***************************************************************************
 *   Copyright (C) December 27, 2008 by Mattias Runge                             *
 *   mattias@runge.se                                                      *
 *   candebug.cpp                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "candebug.h"

CanDebug* CanDebug::myInstance = NULL;

CanDebug& CanDebug::getInstance()
{
    if (myInstance == NULL)
    {
        myInstance = new CanDebug();
    }

    return *myInstance;
}

void CanDebug::deleteInstance()
{
    if (myInstance != NULL)
    {
        delete myInstance;
        myInstance = NULL;
    }
}

CanDebug::CanDebug()
{
    Thread<CanDebug>();
}

CanDebug::~CanDebug()
{
    map<int, AsyncSocket*>::iterator iter;

    for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
    {
        if (iter->second->isConnected())
        {
            iter->second->stop();
        }

        delete iter->second;
    }

    stop();
}

void CanDebug::run()
{
    SyslogStream &slog = SyslogStream::getInstance();

    string port = Settings::get("CanDebugPort");

    try
    {
        mySocket.setPort(stoi(port));

        mySocket.startListen();

        slog << "CanDebug is listening for connections on port " + port + ".\n";

        while (true)
        {
            AsyncSocket *newSocket = new AsyncSocket();

            if (mySocket.accept(newSocket))
            {
                newSocket->sendDataDirect("Welcome to Atom CanDebug\n");

                slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n";
                myClientSockets[newSocket->getSocket()] = newSocket;

                
            }
        }
    }
    catch (SocketException* e)
    {
        slog << "Exception was caught:" + e->getDescription() + ".\n";
    }
}

void CanDebug::sendData(string data)
{
    SyslogStream &slog = SyslogStream::getInstance();

    map<int, AsyncSocket*>::iterator iter;

    for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
    {
        if (iter->second->isConnected())
        {
            //slog << "CanDebug sent data to client " + itos(iter->second->getSocket()) + ".\n";
            iter->second->sendDataDirect(data);
        }
        else
        {
            slog << "CanDebug client " + itos(iter->second->getSocket()) + " has disconnected.\n";
            delete iter->second;
            myClientSockets.erase(iter);
        }
    }
}

void CanDebug::sendCanMessage(CanMessage canMessage)
{
    if (!canMessage.isHeartbeat())
    {
        string debugData = "";

        if (canMessage.getDirectionFlag() == "To_Owner")
        {
            debugData += "RX";
        }
        else if (canMessage.getDirectionFlag() == "From_Owner")
        {
            debugData += "TX";
        }
        else
        {
            debugData += "??";
        }

        debugData += " " + canMessage.getClassName();
        debugData += "_" + canMessage.getModuleName();
        debugData += ":" + itos(canMessage.getModuleId());
        debugData += " CMD=" + canMessage.getCommandName() + " ";

        map<string, CanVariable> vars = canMessage.getData();

        map<string, CanVariable>::iterator iter;

        for (iter = vars.begin(); iter != vars.end(); iter++)
        {
            debugData += " " + iter->second.getName() + "=" + iter->second.getValue();
        }

        sendData("[" + niceTime() + "] " + debugData + "\n");
    }
}