/*
 * SmartHome server: Plug-in tcpserver
 *
 * Copyright (C) 2010 Daniel Pisi <dan.pisi@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 */

/**
 * @file shtcpserver.h
 * @brief Allows other applications to interact with the system via TCP/IP connection
 * @author Daniel Pisi
 */

#ifndef SHTCPSERVER_H
#define SHTCPSERVER_H

#include <QtCore/QCoreApplication>
#include <QtPlugin>
#include <QtNetwork>
#include <QObject>
#include "shobjecthelper.h"
#include "shmessage.h"
#include "shbase.h"

// -----------------------------------------------------------------------------

/**
 * \class ShTcpServer
 * \brief Allows other applications to interact with the system via TCP/IP connection
 */
class ShTcpServer : public ShObjectHelper
{
    Q_OBJECT
    Q_INTERFACES(ShObject)

public:
    ShTcpServer() { mBufLen = 0; };
    virtual ~ShTcpServer() {};
    virtual bool init(QString param);
    virtual void updatePins(const QList<ShPin> &pins) {};
    virtual void processMessage(ShMessagePtr message);

private slots:
    void newClientConnected();
    void dataAvailable();

private:
    void processPacket(const QString &cmd);

    QTcpServer *mTcpServer;
    QTcpSocket *mClientConnection;

    char mBuffer[1024];
    qint64 mBufLen;
};
// -----------------------------------------------------------------------------

#endif // SHTCPSERVER_H
