# shtcpserver.cpp

Go to the documentation of this file.

    00001 /*
    00002  * SmartHome server: Plug-in tcpserver
    00003  *
    00004  * Copyright (C) 2010 Daniel Pisi <dan.pisi@gmail.com>
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it
    00007  * under the terms of version 2 of the GNU General Public License
    00008  * as published by the Free Software Foundation.
    00009  *
    00010  */
    00011
    00018 #include "shtcpserver.h"
    00019
    00020 // -----------------------------------------------------------------------------
    00021
    00027 bool ShTcpServer::init(QString param)
    00028 {
    00029     setIdApi(param.toUpper().contains("FORMAT=ID"));
    00030
    00031     mClientConnection = NULL;
    00032     mTcpServer = new QTcpServer(this);
    00033
    00034     qDebug("Starting TCP server");
    00035     if (!mTcpServer->listen(QHostAddress::LocalHost, 12345))
    00036     {
    00037         qCritical(tr("Unable to start the TCP server: %1.").arg(mTcpServer->errorString()).toLatin1());
    00038         return false;
    00039     }
    00040
    00041     connect(mTcpServer, SIGNAL(newConnection()), this, SLOT(newClientConnected()));
    00042     return true;
    00043 }
    00044 // -----------------------------------------------------------------------------
    00045
    00050 void ShTcpServer::processMessage(ShMessagePtr message)
    00051 {
    00052     if (mClientConnection != NULL)
    00053     {
    00054         mClientConnection->write(
    00055                 QString("%1;%2;%3\n").arg(message->signal(),
    00056                                           message->msgTypeStr(),
    00057                                           message->value().toString()).toUtf8());
    00058     }
    00059 }
    00060 // -----------------------------------------------------------------------------
    00061
    00066 void ShTcpServer::processPacket(const QString &cmd)
    00067 {
    00068     // pin;command;parameter
    00069     QRegExp rx("([a-zA-Z0-9_-]*);([a-zA-Z0-9_-]*);(.*)");
    00070
    00071     qDebug(QString("processCommand(%1)").arg(cmd).toLatin1());
    00072
    00073     if (rx.exactMatch(cmd))
    00074     {
    00075         ShMessagePtr message = ShMessagePtr(new ShMessage);
    00076         message->setMsgType(rx.cap(2));
    00077         message->setSignal(rx.cap(1));
    00078         message->setValue(rx.cap(3));
    00079         message->setRequireAck(true);
    00080         dispatchMessage(message);
    00081     }
    00082 }
    00083 // -----------------------------------------------------------------------------
    00084
    00088 void ShTcpServer::newClientConnected()
    00089 {
    00090     mClientConnection = mTcpServer->nextPendingConnection();
    00091
    00092     connect(mClientConnection, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
    00093
    00094     qDebug("New TCP connection initialized");
    00095 }
    00096 // -----------------------------------------------------------------------------
    00097
    00101 void ShTcpServer::dataAvailable()
    00102 {
    00103     qint64 n;
    00104
    00105     if (mClientConnection)
    00106     {
    00107         while ((n = mClientConnection->readLine(mBuffer+mBufLen, sizeof(mBuffer)-mBufLen)) > 0)
    00108         {
    00109             mBufLen += n;
    00110             if (mBuffer[mBufLen-1] == '\n')
    00111             {
    00112                 mBuffer[mBufLen-1] = 0;
    00113                 processPacket(mBuffer);
    00114                 mBufLen = 0;
    00115             }
    00116         }
    00117         if (n < 0)
    00118         {
    00119             qCritical((tr("Unable to read TCP socket: %1.").arg(mClientConnection->errorString())).toLatin1());
    00120             mClientConnection->disconnectFromHost();
    00121             mBufLen = 0;
    00122         }
    00123     }
    00124 }
    00125 // -----------------------------------------------------------------------------
    00126
    00127 Q_EXPORT_PLUGIN2(tcpserver, ShTcpServer);

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends