qpidctrl  0.7.1
qpidserversession.cpp
Go to the documentation of this file.
1 #include <QHostAddress>
2 #include "qpidserversession.h"
3 #include "qpiddebug.h"
4 #include "qpidcontroller.h"
5 #include "qpidserver.h"
6 
8  QObject(),
9  m_Server(server),
10  m_Controller(ctrl),
11  m_Socket(socket)
12 {
13  connect(m_Socket.data(), SIGNAL(readyRead()), this, SLOT(clientRead()));
14 
15  if (qcepDebug(DEBUG_SERVER)) {
16  printMessage(tr("New connection from %1").arg(m_Socket->peerAddress().toString()) );
17  }
18 
19  connect(m_Socket.data(), SIGNAL(disconnected()), this, SLOT(connectionClosed()));
20 // connect(m_Socket.data(), SIGNAL(disconnected()), m_Socket.data(), SLOT(deleteLater()));
21 }
22 
23 void QPidServerSession::printMessage(QString msg, QDateTime ts)
24 {
26 
27  if (ctl) {
28  ctl->printMessage(msg, ts);
29  }
30 }
31 
33 {
34  QTextStream ts( m_Socket.data() );
35 
36  while ( m_Socket->canReadLine() ) {
37  QString str = ts.readLine();
38 
39  if (qcepDebug(DEBUG_SERVER)) {
40  printMessage(tr("Command: %1 received").arg(str));
41  }
42 
44 
45  if (ctl) {
46  INVOKE_CHECK(
47  QMetaObject::invokeMethod(ctl.data(), "executeServerCommand",
49  Q_ARG(QString, str)));
50  }
51  }
52 }
53 
54 void QPidServerSession::finishedCommand(QScriptValue result)
55 {
56  if (qcepDebug(DEBUG_SERVER)) {
57  printMessage(tr("Result: %1").arg(result.toString()));
58  }
59 
60  if (m_Socket && (m_Socket->isWritable())) {
61  m_Socket -> write(qPrintable(result.toString()+"\n"));
62  }
63 }
64 
66 {
67  if (qcepDebug(DEBUG_SERVER)) {
68  printMessage("Client closed connection");
69  }
70 
71  QPidServerPtr server(m_Server);
72 
73  if (server) {
74  server->deleteSession(QPidServerSessionPtr(this));
75  }
76 }
77 
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
QWeakPointer< QPidController > QPidControllerWPtr
QWeakPointer< QPidServerSession > QPidServerSessionWPtr
QWeakPointer< QPidServer > QPidServerWPtr
Definition: qpidserver-ptr.h:9
QSharedPointer< QPidController > QPidControllerPtr
QPidControllerWPtr m_Controller
QSharedPointer< QPidServerSession > QPidServerSessionPtr
QPidServerSession(QPidServerWPtr server, QPidControllerWPtr ctrl, QTcpSocketPtr socket)
QPidServerWPtr m_Server
QSharedPointer< QTcpSocket > QTcpSocketPtr
void finishedCommand(QScriptValue result)
QSharedPointer< QPidServer > QPidServerPtr
Definition: qpidserver-ptr.h:6
QTcpSocketPtr m_Socket