QXRD  0.11.16
qxrdsimpleserver.cpp
Go to the documentation of this file.
1 #include "qxrddebug.h"
2 #include "qxrdsimpleserver.h"
4 #include <QTextStream>
5 #include <QDateTime>
6 #include <QThread>
7 #include "qxrdexperiment.h"
8 #include <QScriptValueIterator>
9 
11  QTcpServer(NULL),
12  m_RunSimpleServer(saver, this,"runSimpleServer", 1, "Run Simple Socket Server?"),
13  m_SimpleServerPort(saver, this,"simpleServerPort", 1234, "Port for Simple Socket Server"),
14  m_Experiment(doc),
15  m_Name(name)
16 {
18  printf("QxrdSimpleServer::QxrdSimpleServer(%p)\n", this);
19  }
20 
21  connect(prop_RunSimpleServer(), &QcepIntProperty::valueChanged, this, &QxrdSimpleServer::runModeChanged);
22  connect(prop_SimpleServerPort(), &QcepIntProperty::valueChanged, this, &QxrdSimpleServer::serverPortChanged);
23 
24  connect(this, &QTcpServer::newConnection, this, &QxrdSimpleServer::openNewConnection);
25 }
26 
28 {
30  printf("QxrdSimpleServer::~QxrdSimpleServer(%p)\n", this);
31  }
32 
33  stopServer();
34 }
35 
36 void QxrdSimpleServer::readSettings(QSettings *settings, QString section)
37 {
38  QcepProperty::readSettings(this, settings, section);
39 
41 }
42 
43 void QxrdSimpleServer::writeSettings(QSettings *settings, QString section)
44 {
45  QcepProperty::writeSettings(this, settings, section);
46 }
47 
49 {
50  if (QThread::currentThread() != thread()) {
51  QMetaObject::invokeMethod(this, "runModeChanged");
52  } else {
53  if (get_RunSimpleServer()) {
54  startServer(QHostAddress::Any, get_SimpleServerPort());
55  } else {
56  stopServer();
57  }
58  }
59 }
60 
62 {
63  if (QThread::currentThread() != thread()) {
64  QMetaObject::invokeMethod(this, "serverPortChanged");
65  } else {
66  stopServer();
67 
68  if (get_RunSimpleServer()) {
69  startServer(QHostAddress::Any, get_SimpleServerPort());
70  }
71  }
72 }
73 
74 void QxrdSimpleServer::printMessage(QString msg, QDateTime ts)
75 {
77 
78  if (exp) {
79  exp->printMessage(msg, ts);
80  }
81 }
82 
84 {
86 
87  if (exp) {
88  exp->criticalMessage(msg);
89  }
90 }
91 
92 void QxrdSimpleServer::startServer(QHostAddress addr, int port)
93 {
94  if (qcepDebug(DEBUG_SERVER)) {
95  printMessage(tr("Starting simple server on address %1, port %2")
96  .arg(addr.toString()).arg(port));
97  }
98 
99  setMaxPendingConnections(1);
100 
101  if (isListening()) {
102  close();
103  }
104 
105  if (!listen(addr, port)) {
106  criticalMessage(tr("Failed to bind to address %1 port %2\nIs there another copy of qxrd running already?").arg(addr.toString()).arg(port));
107  }
108 }
109 
111 {
112  if (isListening()) {
113  close();
114  }
115 }
116 
118 {
119  m_Socket = nextPendingConnection();
120 
121  connect(m_Socket, &QIODevice::readyRead, this, &QxrdSimpleServer::clientRead);
122 
123  if (qcepDebug(DEBUG_SERVER)) {
124  printMessage(tr("New connection from %1").arg(m_Socket->peerAddress().toString()) );
125  }
126 }
127 
129 {
130  if (qcepDebug(DEBUG_SERVER)) {
131  printMessage("Client closed connection");
132  }
133 }
134 
136 {
137  QTextStream ts( m_Socket );
138 
139  while ( m_Socket->canReadLine() ) {
140  QString str = ts.readLine();
141 
142  if (qcepDebug(DEBUG_SERVER)) {
143  printMessage(tr("Command: %1 received").arg(str));
144  }
145 
146  emit executeCommand(str);
147  }
148 }
149 
150 void QxrdSimpleServer::finishedCommand(QScriptValue result)
151 {
152  if (qcepDebug(DEBUG_SERVER)) {
153  printMessage(tr("Result: %1").arg(result.toString()));
154  }
155 
156  if (m_Socket && (m_Socket->isWritable())) {
157  m_Socket -> write(qPrintable(QxrdScriptEngine::convertToString(result)+"\n"));
158  }
159 }
160 
162 {
163  close();
164 
165  thread()->exit();
166 }
QSharedPointer< QxrdExperiment > QxrdExperimentPtr
static void readSettings(QObject *object, QSettings *settings, QString section)
qint64 qcepDebug(int cond)
Definition: qcepdebug.cpp:26
void finishedCommand(QScriptValue result)
QWeakPointer< QxrdExperiment > QxrdExperimentWPtr
void executeCommand(QString cmd)
QTcpSocket * m_Socket
static QString convertToString(QScriptValue result)
static void writeSettings(QObject *object, QSettings *settings, QString section)
QxrdSimpleServer(QcepSettingsSaverWPtr saver, QxrdExperimentWPtr doc, QString name)
void criticalMessage(QString msg)
virtual ~QxrdSimpleServer()
void printMessage(QString msg, QDateTime ts=QDateTime::currentDateTime())
#define str(s)
void startServer(QHostAddress addr, int port)
virtual void writeSettings(QSettings *settings, QString section)
void valueChanged(int val, int index)
QxrdExperimentWPtr m_Experiment
QWeakPointer< QcepSettingsSaver > QcepSettingsSaverWPtr
virtual void readSettings(QSettings *settings, QString section)