File: dsingleapplication.cpp

package info (click to toggle)
texstudio 2.3%2Bdebian-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 20,992 kB
  • sloc: cpp: 61,734; ansic: 4,300; xml: 726; sh: 125; makefile: 25
file content (292 lines) | stat: -rw-r--r-- 8,479 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*******************************************************************************

  DSingleApplication is basically imitating QtSingleApplication commercial class

  The implementation is though quite different from what is described in
  trolltech documetation for QtSingleApplication. DSingleApplication uses
  tcp sockets to test/open a port in a range and then sed a message to that port
  and expects a correct answer, if it's correct then the app is running and
  we can talk to it.

  Messages sent are in text and start with APP_ID+":", unles message has this three
  bytes it is descarded. Each text message is prepended with int32 value of
  it's size.

  Author: Dima Fedorov Levit <dimin@dimin.net> <http://www.dimin.net/>
  Copyright (C) BioImage Informatics <www.bioimage.ucsb.edu>

  Licence: GPL

  History:
    02/08/2007 17:14 - First creation

  ver: 1
*******************************************************************************/

#include "dsingleapplication.h"

#include <QtCore>
#include <QtGui>
#include <QtNetwork>


DSingleApplication::DSingleApplication(const QString &id, bool initialize) {
	other_instance_running = false;
	app_id = id;
	port = d_unique_port_start;

	tcpServer = new DTalker(app_id, this);
	tcpSocket = NULL;

	if (initialize) init();
}

DSingleApplication::~DSingleApplication() {
	if (tcpServer != NULL) delete tcpServer;
	if (tcpSocket != NULL) delete tcpSocket;
}

QString DSingleApplication::id() const {
	return app_id;
}

void DSingleApplication::initialize() {
	init();
}

bool DSingleApplication::isRunning() const {
	// may require some checks here
	return other_instance_running;
}

void DSingleApplication::init() {

	// start at d_unique_port_start and go until find a free port or a port
	// that answers correctly
	other_instance_running = false;

	DPortChecker checker(app_id, this);

	// first go over the range of ports and check for other instance
	// if not, then listen on the forst port available
	DPortList ports;
	
	port = d_unique_port_start;
	while (port <= d_unique_port_finish) {

		// here check if the stuff running on port is our instance if not procede
		DPortChecker::PortStatus port_status = checker.check(port);

		if (port_status == DPortChecker::us) {
			other_instance_running = true;
			// here we have to connect to other instance to send messages
			tcpSocket = checker.transferSocketOwnership();
			return;
		}

		DPortInfo pi(port, port_status == DPortChecker::free);
		ports << pi;
		++port;
	} // while

	port = ports.firstFreePort();

	// other instance is not running in the range and there's available port
	if (port == -1) return;

	// this port is free and current instance is in listening mode
	bool listening = tcpServer->listen(QHostAddress::LocalHost, port);
	if (listening)
		connect(tcpServer, SIGNAL(messageReceived(const QString &)), this, SLOT(onClientMessage(const QString &)));

}

bool DSingleApplication::sendMessage(const QString &message) {
	if (!other_instance_running) return false;
	if (!tcpSocket) return false;
	if (tcpSocket->state() != QAbstractSocket::ConnectedState) return false;

	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(out.version());   // set to the current Qt version

	QString msg = app_id + ":" + message;
	out << (quint32) msg.size();
	out << msg;

	tcpSocket->write(block);
	tcpSocket->flush();
	tcpSocket->waitForBytesWritten(d_timeout_try_write);

	return true;
}

void DSingleApplication::onClientMessage(const QString & message) {
	emit messageReceived(message);
}


//******************************************************************************
// DPortChecker
// This class is used to check specific port if it has an instance of this app
//******************************************************************************

DPortChecker::DPortChecker(const QString &id,  QObject *parent)
		: QObject(parent) {
	tcpSocket = NULL;
	app_id = id;
	tcpSocket = new QTcpSocket();
	tcpServer = new QTcpServer();
}

DPortChecker::~DPortChecker() {
	if (tcpSocket) delete tcpSocket;
	if (tcpServer) delete tcpServer;
}

DPortChecker::PortStatus DPortChecker::check(int port) {
	if (tcpSocket == NULL) 
		return DPortChecker::free; //broken if this happens

	// if port is occupied, listening = false
	bool listening = tcpServer->listen(QHostAddress::LocalHost, port);
	tcpServer->close(); // close listening, but do not destroy

	if (listening)
		return DPortChecker::free;

	tcpSocket->connectToHost(QHostAddress(QHostAddress::LocalHost), port);
	if (!tcpSocket->waitForConnected(d_timeout_try_connect)) {
		tcpSocket->abort();
		return DPortChecker::others;
	}

	
	if (!tcpSocket->waitForReadyRead(d_timeout_try_read)) {
		tcpSocket->abort();
		return DPortChecker::others;
	}

	// now compare received bytes with app_id
	QDataStream in(tcpSocket);
	in.setVersion(in.version());   // set to the current Qt version

	if (tcpSocket->bytesAvailable() > 0) {
		QString msgString;
		in >> msgString;
		if (msgString.size() <= 1) {
			tcpSocket->abort();
			return DPortChecker::others;
		}
		int s = qMin(msgString.size(), app_id.size());
		if (QString::compare(msgString.left(s), app_id.left(s)) == 0)
			return DPortChecker::us;
	}
	return DPortChecker::others;
}

QTcpSocket* DPortChecker::transferSocketOwnership() {
	QTcpSocket *tmp = tcpSocket;
	tcpSocket = NULL;
	return tmp;
}

//******************************************************************************
// DTalker
// This is a server responsible to talking to incoming connections
//******************************************************************************

DTalker::DTalker(const QString &id, QObject *parent)
		: QTcpServer(parent), app_id(id) {

}

void DTalker::incomingConnection(int socketDescriptor) {

	DListner *listner = new DListner(app_id, socketDescriptor, this);
	connect(listner, SIGNAL(messageReceived(const QString &)), this, SLOT(onClientMessage(const QString &)));
	connect(listner, SIGNAL(finished()), listner, SLOT(deleteLater()));
	listner->start();
}

void DTalker::onClientMessage(const QString & message) {
	emit messageReceived(message);
}


//******************************************************************************
// DListner
// This thread is used to communicate with connected client
//******************************************************************************

DListner::DListner(const QString &id, int socketDescriptor, QObject *parent)
		: QThread(parent),  socketDescriptor(socketDescriptor), app_id(id) {
	blockSize = 0;
}

DListner::~DListner() {

}

void DListner::run() {
	QTcpSocket tcpSocket;
	if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
		return;
	}

	// send app_id to client
	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(out.version());   // set to the current Qt version instead
	out << app_id;
	tcpSocket.write(block);
	//waitForBytesWritten ( int msecs )

	while (1) {
		if (tcpSocket.state() != QAbstractSocket::ConnectedState) return;
		tcpSocket.waitForReadyRead(-1);
		read(&tcpSocket);
	}
}

void DListner::read(QTcpSocket *tcpSocket) {
	if (tcpSocket == NULL) return;
	if (tcpSocket->state() != QAbstractSocket::ConnectedState) return;

	QDataStream in(tcpSocket);
	in.setVersion(in.version());   // set to the current Qt version instead

	if (blockSize == 0) {
		if (tcpSocket->bytesAvailable() < (int)sizeof(quint32)) return;
		in >> blockSize;
	}
	if (tcpSocket->bytesAvailable() < blockSize) return;
	QString msgString;
	in >> msgString;

	// if header matches, trim and emit
	QString magic = app_id + ":";
	if (QString::compare(msgString.left(magic.size()), magic) == 0)
		emit messageReceived(msgString.remove(0, magic.size()));

	blockSize = 0;
	if (tcpSocket->bytesAvailable() > 0) read(tcpSocket);
}

//******************************************************************************
// DPortList
//******************************************************************************

int DPortList::firstFreePort() {
	DPortList::iterator it = this->begin();
	while (it < this->end()) {
		if (it->free == true) return it->port;
		++it;
	}
	return -1;
}

bool DPortList::freePortAvailable() {
	int p = firstFreePort();
	return p != -1;
}