QTfrontend/tcpBase.cpp
changeset 185 3b777d7008d9
parent 184 f97a7a3dc8f6
child 186 7157b34a24d6
equal deleted inserted replaced
184:f97a7a3dc8f6 185:3b777d7008d9
    20 
    20 
    21 #include <QMessageBox>
    21 #include <QMessageBox>
    22 #include <QList>
    22 #include <QList>
    23 
    23 
    24 #include <QImage>
    24 #include <QImage>
    25 #include <QTimer>
       
    26 
    25 
    27 #include "hwconsts.h"
    26 #include "hwconsts.h"
    28 
    27 
    29 QList<TCPBase*> srvsList;
    28 QList<TCPBase*> srvsList;
       
    29 int TCPBase::isIPCServerStarted=0;
       
    30 QTcpServer* TCPBase::IPCServer(0);
    30 
    31 
    31 TCPBase::TCPBase(bool demoMode) :
    32 TCPBase::TCPBase(bool demoMode) :
    32   m_isDemoMode(demoMode)
    33   m_isDemoMode(demoMode),
       
    34   IPCSocket(0)
    33 {
    35 {
    34   IPCServer = new QTcpServer(this);
    36   if(!isIPCServerStarted++) {
       
    37     IPCServer = new QTcpServer(this);
       
    38     IPCServer->setMaxPendingConnections(1);
       
    39     if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
       
    40       QMessageBox::critical(0, tr("Error"),
       
    41 			    tr("Unable to start the server: %1.")
       
    42 			    .arg(IPCServer->errorString()));
       
    43     }
       
    44   }
    35   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    45   connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
    36   IPCServer->setMaxPendingConnections(1);
       
    37 }
    46 }
    38 
    47 
    39 void TCPBase::NewConnection()
    48 void TCPBase::NewConnection()
    40 {
    49 {
       
    50   if(IPCSocket) {
       
    51     // connection should be already finished
       
    52     return;
       
    53   }
    41   QTcpSocket * client = IPCServer->nextPendingConnection();
    54   QTcpSocket * client = IPCServer->nextPendingConnection();
    42   if(!IPCSocket) {
    55   if(!client) return;
    43     IPCServer->close();
    56   IPCSocket = client;
    44     IPCSocket = client;
    57   connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    45     connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
    58   connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    46     connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
    59   SendToClientFirst();
    47     SendToClientFirst();
       
    48   } else {
       
    49     qWarning("2nd IPC client?!");
       
    50     client->disconnectFromHost();
       
    51   }
       
    52 }
    60 }
    53 
    61 
    54 void TCPBase::RealStart()
    62 void TCPBase::RealStart()
    55 {
    63 {
    56   IPCSocket = 0;
    64   IPCSocket = 0;
    57   if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
    65   
    58     QMessageBox::critical(0, tr("Error"),
       
    59 			  tr("Unable to start the server: %1.")
       
    60 			  .arg(IPCServer->errorString()));
       
    61   }
       
    62 
       
    63   QProcess * process;
    66   QProcess * process;
    64   process = new QProcess;
    67   process = new QProcess;
    65   connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
    68   connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
    66   QStringList arguments=setArguments();
    69   QStringList arguments=setArguments();
    67   process->start(bindir->absolutePath() + "/hwengine", arguments);
    70   process->start(bindir->absolutePath() + "/hwengine", arguments);
    68 }
    71 }
    69 
    72 
    70 void TCPBase::ClientDisconnect()
    73 void TCPBase::ClientDisconnect()
    71 {
    74 {
    72   IPCSocket->close();
    75   IPCSocket->close();
    73   IPCServer->close();
    76   //IPCServer->close();
    74 
    77 
    75   onClientDisconnect();
    78   onClientDisconnect();
    76 
    79 
    77   readbuffer.clear();
    80   readbuffer.clear();
    78 
    81 
    96 void TCPBase::tcpServerReady()
    99 void TCPBase::tcpServerReady()
    97 {
   100 {
    98   disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
   101   disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
    99   srvsList.pop_front();
   102   srvsList.pop_front();
   100 
   103 
   101   QTimer::singleShot(150, this, SLOT(RealStart()));
   104   RealStart();
   102 }
   105 }
   103 
   106 
   104 void TCPBase::Start()
   107 void TCPBase::Start()
   105 {
   108 {
   106   if(srvsList.isEmpty()) {
   109   if(srvsList.isEmpty()) {
   108   } else {
   111   } else {
   109     connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   112     connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
   110     srvsList.push_back(this);
   113     srvsList.push_back(this);
   111     return;
   114     return;
   112   }
   115   }
       
   116   
   113   RealStart();
   117   RealStart();
   114 }
   118 }
   115 
   119 
   116 void TCPBase::onClientRead()
   120 void TCPBase::onClientRead()
   117 {
   121 {