File: server_monitor.cpp

package info (click to toggle)
lyx 2.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 96,552 kB
  • sloc: cpp: 388,556; python: 19,985; ansic: 9,725; sh: 5,696; makefile: 3,907; pascal: 1,388; objc: 985; perl: 319; yacc: 289; tcl: 163; xml: 23; sed: 16
file content (375 lines) | stat: -rw-r--r-- 10,513 bytes parent folder | download | duplicates (3)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
 * \file server_monitor.cpp
 * This file is part of LyX, the document processor.
 * Licence details can be found in the file COPYING.
 *
 * \author Enrico Forestieri
 *
 * Full author contact details are available in file CREDITS.
 *
 * This program sends commands to a running instance of LyX and
 * receives information back from LyX.
 *
 * Build instructions:
 * 1) Run moc or moc-qt4 on server_monitor.h to produce moc_server_monitor.cpp:
 *    moc-qt4 server_monitor.h -o moc_server_monitor.cpp
 * 2) If the QtGui.pc file is not in the pkg-config search path, find the
 *    directory where it is located (e.g., use the command `locate QtGui.pc')
 *    and set the environment variable PKG_CONFIG_PATH to this directory.
 *    For example:
 *      export PKG_CONFIG_PATH=/path/to/directory    (if using bash)
 *      setenv PKG_CONFIG_PATH /path/to/directory    (if using tcsh)
 *    If the command `pkg-config --modversion QtGui' does not complain and
 *    prints the Qt version, you don't need to set PKG_CONFIG_PATH.
 * 3) Compile using the following command:
 *    g++ server_monitor.cpp -o monitor -I. `pkg-config --cflags --libs QtGui`
 *
 * Alternatively, you can create a Makefile with qmake and then build
 * the executable by running make (or nmake, if you use msvc):
 *    qmake
 *    make
 *
 * Usage:
 * 1) Set the LyXserver pipe path in the LyX preferences (on *nix you can use
 *    any path, for example ~/.lyx/lyxpipe, whereas on Windows the path has
 *    to start with `\\.\pipe\', for example you can use \\.\pipe\lyxpipe).
 * 2) Quit and restart LyX.
 * 3) Launch this program, adjust the pipe name to match that one used in LyX,
 *    push the button labeled "Open pipes" and then try issuing some commands.
 */

#include <QApplication>
#include <QtGui>
#include <QtDebug>

#include "server_monitor.h"

LyXServerMonitor::LyXServerMonitor()
	: pipein(-1), pipeout(-1), thread_exit(false), lyx_listen(false)
{
	createGridGroupBox();
	createCmdsGroupBox();

	char const * const home = getenv("HOME");
	QString const pipeName = (home && home[0]) ?
	    QString::fromUtf8(home) + "/.lyx/lyxpipe" : "\\\\.\\pipe\\lyxpipe";

	pipeNameLE->setText(pipeName);
	clientNameLE->setText("monitor");
	submitCommandPB->setDisabled(true);
	closePipesPB->setDisabled(true);

	connect(openPipesPB, SIGNAL(clicked()), this, SLOT(openPipes()));
	connect(closePipesPB, SIGNAL(clicked()), this, SLOT(closePipes()));
	connect(submitCommandPB, SIGNAL(clicked()), this, SLOT(submitCommand()));
	connect(donePB, SIGNAL(clicked()), this, SLOT(reject()));

	QVBoxLayout * mainLayout = new QVBoxLayout;
	mainLayout->addWidget(gridGB);
	mainLayout->addWidget(horizontalGB);
	setLayout(mainLayout);

	setWindowTitle("LyX Server Monitor");
}


LyXServerMonitor::~LyXServerMonitor()
{
	if (pipein != -1)
		closePipes();
}


void LyXServerMonitor::createGridGroupBox()
{
	gridGB = new QGroupBox;
	QGridLayout * layout = new QGridLayout;

	labels[0] = new QLabel("Pipe name");
	pipeNameLE = new QLineEdit;
	layout->addWidget(labels[0], 0, 0, Qt::AlignRight);
	layout->addWidget(pipeNameLE, 0, 1);

	labels[1] = new QLabel("Command");
	commandLE = new QLineEdit;
	layout->addWidget(labels[1], 1, 0, Qt::AlignRight);
	layout->addWidget(commandLE, 1, 1);

	labels[2] = new QLabel("Client name");
	clientNameLE = new QLineEdit;
	layout->addWidget(labels[2], 0, 2, Qt::AlignRight);
	layout->addWidget(clientNameLE, 0, 3);

	labels[3] = new QLabel("Argument");
	argumentLE = new QLineEdit;
	layout->addWidget(labels[3], 1, 2, Qt::AlignRight);
	layout->addWidget(argumentLE, 1, 3);

	labels[4] = new QLabel("Info");
	infoLB = new QLabel;
	infoLB->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	infoLB->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
	layout->addWidget(labels[4], 2, 0, Qt::AlignRight);
	layout->addWidget(infoLB, 2, 1, 1, 3);

	labels[5] = new QLabel("Notify");
	notifyLB = new QLabel;
	notifyLB->setFrameStyle(QFrame::Panel | QFrame::Sunken);
	notifyLB->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
	layout->addWidget(labels[5], 3, 0, Qt::AlignRight);
	layout->addWidget(notifyLB, 3, 1, 1, 3);

	layout->setColumnMinimumWidth(1, 200);
	layout->setColumnMinimumWidth(3, 200);
	gridGB->setLayout(layout);
}


void LyXServerMonitor::createCmdsGroupBox()
{
	horizontalGB = new QGroupBox;
	QHBoxLayout * layout = new QHBoxLayout;

	openPipesPB = new QPushButton("&Open pipes");
	layout->addWidget(openPipesPB);

	closePipesPB = new QPushButton("C&lose pipes");
	layout->addWidget(closePipesPB);

	submitCommandPB = new QPushButton("&Submit Command");
	layout->addWidget(submitCommandPB);

	donePB = new QPushButton("&Done");
	layout->addWidget(donePB);

	horizontalGB->setLayout(layout);
}


void LyXServerMonitor::readPipe()
{
	int n;
	errno = 0;
	bool notified = false;

	while ((n = ::read(pipeout, pipedata, BUFSIZE - 1)) && !thread_exit) {
		if (n > 0) {
			pipedata[n] = 0;
			QString const fromLyX =
				QString::fromUtf8(pipedata).trimmed();
			qWarning() << "monitor: Coming: " << fromLyX;
			if (fromLyX.startsWith("LYXSRV:")) {
				if (fromLyX.contains("bye")) {
					qWarning() << "monitor: LyX has closed "
						      "connection!";
					pipethread->emitNotice(fromLyX);
					notified = true;
					break;
				}
				if (fromLyX.contains("hello")) {
					lyx_listen = true;
					qWarning() << "monitor: "
						      "LyX is listening!";
					submitCommandPB->setDisabled(false);
				}
			}
			if (fromLyX[0] == QLatin1Char('I'))
				pipethread->emitInfo(fromLyX);
			else
				pipethread->emitNotice(fromLyX);
#ifdef _WIN32
			// On Windows, we have to close and reopen
			// the pipe after each use.
			::close(pipeout);
			pipeout = ::open(
				outPipeName().toLocal8Bit().constData(),
				O_RDONLY);
			if (pipeout < 0) {
				perror("monitor");
				pipethread->emitNotice("An error occurred, "
							"closing pipes");
				notified = true;
				break;
			}
#endif
		} else if (n < 0) {
#ifdef __CYGWIN__
			if (errno == ECOMM) {
				// When talking to a native Windows version of
				// LyX, the second time we try to use the pipe,
				// read() fails with ECOMM. In this case, we
				// have to simply close and reopen it.
				::close(pipeout);
				pipeout = ::open(
					outPipeName().toLocal8Bit().constData(),
					O_RDONLY);
				if (pipeout >= 0)
					continue;
			}
#endif
			perror("monitor");
			pipethread->emitNotice("An error occurred, closing pipes");
			notified = true;
			break;
		} else
			break;
	}

	if (!notified) {
		if (thread_exit) {
			qWarning() << "monitor: Closing pipes";
			pipethread->emitNotice("Closing pipes");
		} else {
			qWarning() << "monitor: LyX has closed connection!";
			pipethread->emitNotice("LyX has closed connection!");
		}
	}
	QEvent * event = new QEvent(QEvent::User);
	QCoreApplication::postEvent(this, event);
	lyx_listen = false;
	if (!thread_exit)
		pipethread->emitClosing();
}


bool LyXServerMonitor::event(QEvent * e)
{
	if (e->type() == QEvent::User) {
		pipethread->wait();
		thread_exit = false;
		delete pipethread;
		return true;
	}
	return QDialog::event(e);
}


void LyXServerMonitor::showInfo(QString const & msg)
{
	infoLB->setText(msg);
	notifyLB->clear();
}


void LyXServerMonitor::showNotice(QString const & msg)
{
	infoLB->clear();
	notifyLB->setText(msg);
}


void LyXServerMonitor::openPipes()
{
	if (pipein == -1) {
		qWarning() << "monitor: Opening pipes " << inPipeName()
			   << " and " << outPipeName();
		pipein = ::open(inPipeName().toLocal8Bit().constData(),
				O_WRONLY);
		pipeout = ::open(outPipeName().toLocal8Bit().constData(),
				 O_RDONLY);
		if (pipein < 0 || pipeout < 0) {
			qWarning() << "monitor: Could not open the pipes";
			infoLB->clear();
			notifyLB->setText("Could not open the pipes");
			if (pipein >= 0 || pipeout >= 0)
				closePipes();
			return;
		}
		pipethread = new ReadPipe(this);
		pipethread->start();
		if (!pipethread->isRunning()) {
			qWarning() << "monitor: Could not create pipe thread";
			infoLB->clear();
			notifyLB->setText("Could not create pipe thread");
			closePipes();
			return;
		}
		connect(pipethread, SIGNAL(info(QString const &)),
			this, SLOT(showInfo(QString const &)));
		connect(pipethread, SIGNAL(notice(QString const &)),
			this, SLOT(showNotice(QString const &)));
		connect(pipethread, SIGNAL(closing()),
			this, SLOT(closePipes()));
		openPipesPB->setDisabled(true);
		closePipesPB->setDisabled(false);
		// greet LyX
		QString const clientname = clientNameLE->text();
		snprintf(buffer, BUFSIZE - 1,
			"LYXSRV:%s:hello\n", clientname.toUtf8().constData());
		buffer[BUFSIZE - 1] = '\0';
		::write(pipein, buffer, strlen(buffer));
	} else
		qWarning() << "monitor: Pipes already opened, close them first\n";
}


void LyXServerMonitor::closePipes()
{
	if (pipein == -1 && pipeout == -1) {
		qWarning() << "monitor: Pipes are not opened";
		return;
	}

	if (pipein >= 0) {
		if (lyx_listen) {
			lyx_listen = false;
			QString const clientname = clientNameLE->text();
			if (pipethread->isRunning()) {
				thread_exit = true;
				// The thread, currently blocked on the read()
				// call, will be waked up by the reply from
				// LyX and will exit.
				snprintf(buffer, BUFSIZE - 1,
					"LYXCMD:%s:message:Client '%s' is leaving\n",
					clientname.toUtf8().constData(),
					clientname.toUtf8().constData());
				buffer[BUFSIZE - 1] = '\0';
				::write(pipein, buffer, strlen(buffer));
			}
			// Say goodbye
			snprintf(buffer, BUFSIZE - 1, "LYXSRV:%s:bye\n",
				 clientname.toUtf8().constData());
			buffer[BUFSIZE - 1] = '\0';
			::write(pipein, buffer, strlen(buffer));
			pipethread->wait();
			thread_exit = false;
		}
		::close(pipein);
	}

	if (pipeout >= 0)
		::close(pipeout);
	pipein = pipeout = -1;
	submitCommandPB->setDisabled(true);
	openPipesPB->setDisabled(false);
	closePipesPB->setDisabled(true);
}


void LyXServerMonitor::submitCommand()
{
	if (pipein >= 0) {
		QString const command = commandLE->text();
		QString const argument = argumentLE->text();
		QString const clientname = clientNameLE->text();
		snprintf(buffer, BUFSIZE - 2, "LYXCMD:%s:%s:%s",
			 clientname.toUtf8().constData(),
			 command.toUtf8().constData(),
			 argument.toUtf8().constData());
		buffer[BUFSIZE - 1] = '\0';
		qWarning() << "monitor: Sending: " << buffer;
		strcat(buffer, "\n");
		::write(pipein, buffer, strlen(buffer));
	} else
		qWarning() << "monitor: Pipe is not opened";
}


int main(int argc, char * argv[])
{
	QApplication app(argc, argv);
	LyXServerMonitor dialog;
	return dialog.exec();
}

#include "moc_server_monitor.cpp"