-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathq6refpersys.cc
859 lines (775 loc) · 29.1 KB
/
q6refpersys.cc
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
// file misc-basile/q6refpersys.cc
// SPDX-License-Identifier: GPL-3.0-or-later
/***
© Copyright 2024 by Basile Starynkevitch
program released under GNU General Public License v3+
This is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
This is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
This q6refpersys program is an opensource Qt6 application (Qt is a
graphical user toolkit for Linux; see https://www.qt.io/product/qt6
...) It is the interface to the RefPerSys inference engine on
http://refpersys.org/ and communicates with the refpersys process
using some JSONRPC protocol on named fifos. In contrast to
refpersys itself, the q6refpersys process is short lived.
****/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#define UNUSED __attribute__((unused))
extern "C" const char myqr_git_id[];
extern "C" char myqr_host_name[64];
#ifndef GITID
#error GITID should be defined in compilation command
#endif
#include <QApplication>
#include <QProcess>
#include <QCommandLineParser>
#include <QDebug>
#include <QMainWindow>
#include <QMenuBar>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QLineEdit>
#include <QSizePolicy>
#include <QLabel>
#include <QSocketNotifier>
//#include <QJsonValue>
#include <QtCore/QtCoreVersion>
//#include <QJsonValue>
#include <QtCore/qglobal.h>
// from jsoncpp (see https://github.com/open-source-parsers/jsoncpp)
#include "json/value.h"
#include "json/reader.h"
#include "json/writer.h"
#include <iostream>
#include <sstream>
#include <functional>
#include <mutex>
#include <deque>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
extern "C" char myqr_host_name[];
extern "C" char* myqr_progname;
extern "C" bool myqr_debug;
extern "C" std::string myqr_jsonrpc; // the FIFO prefix
extern "C" int myqr_jsonrpc_cmd_fd; /// written by RefPerSys, read by q6refpersys
extern "C" QSocketNotifier* myqr_notifier_jsonrpc_cmd;
extern "C" std::recursive_mutex myqr_mtx_jsonrpc_cmd;
extern "C" std::stringstream myqr_stream_jsonrpc_cmd;
extern "C" Json::CharReader* myqr_jsonrpc_reader;
extern "C" Json::CharReaderBuilder myqr_jsoncpp_reader_builder;
extern "C" QSocketNotifier* myqr_notifier_jsonrpc_out;
extern "C" int myqr_jsonrpc_out_fd; /// read by RefPerSys, written by q6refpersys
extern "C" std::recursive_mutex myqr_mtx_jsonrpc_out;
extern "C" std::deque<Json::Value> myqr_deque_jsonrpc_out;
extern "C" std::stringstream myqr_stream_jsonrpc_out;
extern "C" std::map<int,std::function<void(const Json::Value&res)>> myqr_jsonrpc_out_procmap;
extern "C" Json::StreamWriterBuilder myqr_jsoncpp_writer_builder;
extern "C" pid_t myqr_refpersys_pid;
extern "C" std::string myqr_json2str(const Json::Value&jv);
/// process the JSON recieved from refpersys
extern "C" void myqr_process_jsonrpc_from_refpersys(const Json::Value&js);
/// do a remote procedure call to RefPerSys using our JSONRPC variant
extern "C" void myqr_call_jsonrpc_to_refpersys
(const std::string& method,
const Json::Value& args,
const std::function<void(const Json::Value&res)>& resfun);
#define MYQR_FATALOUT_AT_BIS(Fil,Lin,Out) do { \
std::ostringstream outs##Lin; \
outs##Lin << Out << std::flush; \
qFatal("%s:%d: %s\n[git %s@%s] on %s", \
Fil, Lin, outs##Lin.str().c_str(), \
myqr_git_id, __DATE__" " __TIME__, \
myqr_host_name); \
abort(); \
} while(0)
#define MYQR_FATALOUT_AT(Fil,Lin,Out) \
MYQR_FATALOUT_AT_BIS(Fil,Lin,Out)
#define MYQR_FATALOUT(Out) MYQR_FATALOUT_AT(__FILE__,__LINE__,Out)
#define MYQR_DEBUGOUT_AT_BIS(Fil,Lin,Out) do { \
if (myqr_debug) \
std::clog << Fil << ":" << Lin << " " \
<< Out << std::endl; \
} while(0)
#define MYQR_DEBUGOUT_AT(Fil,Lin,Out) \
MYQR_DEBUGOUT_AT_BIS(Fil,Lin,Out)
#define MYQR_DEBUGOUT(Out) MYQR_DEBUGOUT_AT(__FILE__,__LINE__,Out)
extern "C" QApplication *myqr_app;
extern "C" {
class MyqrMainWindow;
class MyqrDisplayWindow;
};
////////////////////////////////////////////////////////////////
class MyqrMainWindow : public QMainWindow
{
Q_OBJECT;
//// the menubar
QMenuBar* _mainwin_menubar;
QMenu* _mainwin_appmenu;
QAction* _mainwin_aboutact;
QAction* _mainwin_aboutqtact;
QMenu* _mainwin_editmenu;
QAction* _mainwin_copyact;
QAction* _mainwin_pasteact;
/// the central widget is a vertical group box
QGroupBox* _mainwin_centralgroup;
QLabel*_mainwin_toplabel;
QLineEdit*_mainwin_cmdline;
QTextEdit*_mainwin_textoutput;
private slots:
void about();
void aboutQt();
public:
static MyqrMainWindow*the_instance;
explicit MyqrMainWindow(QWidget*parent = nullptr);
virtual ~MyqrMainWindow();
static constexpr int minimal_width = 512;
static constexpr int minimal_height = 128;
static constexpr int maximal_width = 2048;
static constexpr int maximal_height = 1024;
}; // end MyqrMainWindow
MyqrMainWindow*MyqrMainWindow::the_instance;
////////////////////////////////////////////////////////////////
class MyqrDisplayWindow : public QMainWindow
{
Q_OBJECT;
private slots:
void about();
public:
explicit MyqrDisplayWindow(QWidget*parent = nullptr);
virtual ~MyqrDisplayWindow();
}; // end MyqrDisplayWindow
////////////////////////////////////////////////////////////////
extern "C" QProcess*myqr_refpersys_process;
//=============================================================
std::ostream& operator << (std::ostream&out, const QList<QString>&qslist)
{
int nbl=0;
for (const QString&qs: qslist)
{
if (nbl++ > 0)
out << ' ';
std::string s = qs.toStdString();
bool needquotes=false;
for (char c: s)
{
if (!isalnum(c)&& c!='_') needquotes=true;
}
if (needquotes)
out << "'";
for (QChar qc : qs)
{
switch (qc.unicode())
{
case '\\':
out << "\\\\";
break;
case '\'':
out << "\\'";
break;
case '\"':
out << "\\\"";
break;
case '\r':
out << "\\r";
break;
case '\n':
out << "\\n";
break;
case '\t':
out << "\\t";
break;
case '\v':
out << "\\v";
break;
case '\f':
out << "\\f";
break;
case ' ':
out << " ";
break;
default:
out << QString(qc).toStdString();
break;
}
}
if (needquotes)
out << "'";
}
return out;
}// end operator << (std::ostream&out, const QList<QString>&qslist)
////////////////////////////////////////////////////////////////
MyqrMainWindow::MyqrMainWindow(QWidget*parent)
: QMainWindow(parent),
_mainwin_menubar(nullptr),
_mainwin_appmenu(nullptr),
_mainwin_aboutact(nullptr),
_mainwin_aboutqtact(nullptr),
_mainwin_editmenu(nullptr),
_mainwin_copyact(nullptr),
_mainwin_pasteact(nullptr),
_mainwin_centralgroup(nullptr),
_mainwin_toplabel(nullptr),
_mainwin_cmdline(nullptr),
_mainwin_textoutput(nullptr)
{
if (the_instance != nullptr)
MYQR_FATALOUT("duplicate MyqrMainWndow @" << (void*)the_instance
<< " and this@" << (void*)this);
_mainwin_menubar = menuBar();
_mainwin_appmenu =_mainwin_menubar-> addMenu("App");
_mainwin_aboutact = _mainwin_appmenu->addAction("About");
QObject::connect(_mainwin_aboutact,&QAction::triggered,this,&MyqrMainWindow::about);
_mainwin_aboutqtact = _mainwin_appmenu->addAction("About Qt");
QObject::connect(_mainwin_aboutqtact,&QAction::triggered,this,&MyqrMainWindow::aboutQt);
_mainwin_editmenu =_mainwin_menubar-> addMenu("Edit");
_mainwin_copyact = _mainwin_editmenu->addAction("Copy");
_mainwin_pasteact = _mainwin_editmenu->addAction("Paste");
_mainwin_centralgroup = new QGroupBox(this);
{
QVBoxLayout *vbox = new QVBoxLayout;
_mainwin_centralgroup->setLayout(vbox);
_mainwin_toplabel = new QLabel("q6refpersys");
vbox->addWidget(_mainwin_toplabel);
_mainwin_cmdline = new QLineEdit(_mainwin_centralgroup);
_mainwin_cmdline->setFixedWidth(this->width()-16);
vbox->addWidget(_mainwin_cmdline);
_mainwin_textoutput = new QTextEdit();
_mainwin_textoutput->setReadOnly(true);
vbox->addWidget(_mainwin_textoutput);
}
setCentralWidget(_mainwin_centralgroup);
the_instance = this;
MYQR_DEBUGOUT("MyqrMainWndow the_instance@" << (void*)the_instance
<< " parent@" << (void*)parent);
setMinimumWidth(minimal_width);
setMinimumHeight(minimal_height);
setMaximumWidth(maximal_width);
setMaximumHeight(maximal_height);
qDebug() << "incomplete MyqrMainWindow constructor "
<< __FILE__ ":" << (__LINE__-1);
#warning incomplete MyqrMainWindow constructor
} // end MyqrMainWindow constructor
MyqrDisplayWindow::MyqrDisplayWindow(QWidget*parent)
: QMainWindow(parent)
{
} // end MyqrDisplayWindow constructor
void
MyqrDisplayWindow::about()
{
qDebug() << " unimplemented MyqrDisplayWindow::about";
} // end MyqrDisplayWindow::about
MyqrMainWindow::~MyqrMainWindow()
{
if (the_instance != this)
MYQR_FATALOUT("corruption in MyqrMainWndow the_instance@" << (void*)the_instance
<< " this@" << (void*)this);
the_instance = nullptr;
} // end MyqrMainWindow destructor
MyqrDisplayWindow::~MyqrDisplayWindow()
{
} // end MyqrDisplayWindow destructor
void
MyqrMainWindow::aboutQt()
{
MYQR_DEBUGOUT("unimplemented MyqrMainWindow::aboutQt");
#warning unimplemented MyqrMainWindow::aboutQt
} // end MyqrDisplayWindow::aboutQt
void
MyqrMainWindow::about()
{
MYQR_DEBUGOUT("unimplemented MyqrMainWindow::about");
#warning unimplemented MyqrMainWindow::about
} // end MyqrDisplayWindow::aboutQt
void myqr_create_windows(const QString& geom);
void
myqr_create_windows(const QString& geom)
{
MYQR_DEBUGOUT("incomplete myqr_create_windows geometry "
<< geom.toStdString() << ";");
int w=0, h=0;
const char*geomcstr = geom.toStdString().c_str();
if (geomcstr != nullptr)
{
int p= -1;
MYQR_DEBUGOUT("myqr_create_windows geomcstr='" << geomcstr << "'");
if (sscanf(geomcstr, " %dx%d %n", &w, &h, &p) >= 2)
{
MYQR_DEBUGOUT("scanned w=" << w << " h=" << h
<< " geomcstr='" << geomcstr << "' p=" << p);
if (w > MyqrMainWindow::maximal_width)
w= MyqrMainWindow::maximal_width;
if (h > MyqrMainWindow::maximal_height)
h=MyqrMainWindow::maximal_height;
}
}
if (w< MyqrMainWindow::minimal_width)
w=MyqrMainWindow::minimal_width;
if (h< MyqrMainWindow::minimal_height)
h= MyqrMainWindow::minimal_height;
MYQR_DEBUGOUT("myqr_create_windows normalized w=" << w << ", h=" << h
<< " from geomcstr='" << geomcstr << "'");
auto mainwin = new MyqrMainWindow(nullptr);
mainwin->resize(w,h);
mainwin->show();
MYQR_DEBUGOUT("myqr_create_windows incomplete mainwin@" << (void*)mainwin);
#warning incomplete myqr_create_windows
} // end myqr_create_windows
/// This function gets called when some bytes could be read on the
/// file descriptor (FIFO) from refpersys to GUI.
void
myqr_readable_jsonrpc_cmd(void)
{
MYQR_DEBUGOUT("myqr_readable_jsonrpc_cmd start myqr_jsonrpc_cmd_fd="
<< myqr_jsonrpc_cmd_fd);
/*** NOTE:
We actually expect that most JSON objects sent by refpersys to
this GUI process are short, typically a few dozen bytes
each. And each JSON message is terminated by a formfeed (which is
invalid in JSON, in C "\f") or a double newline, in C "\n\n" ...
In rare cases refpersys might send a large JSON (over a
kilobyte), but we expect this to be not frequent. In other
words, there is a lot of string copying happening here, to ease
coding.
***/
const std::lock_guard<std::recursive_mutex> lock(myqr_mtx_jsonrpc_cmd);
constexpr unsigned jrbufsize= 2048;
char buf [jrbufsize+4];
memset (buf, 0, sizeof(buf));
char errbuf[64];
memset (errbuf, 0, sizeof(errbuf));
errno = 0;
ssize_t rdcnt = read(myqr_jsonrpc_cmd_fd, buf, jrbufsize);
int rderr = errno;
MYQR_DEBUGOUT("myqr_readable_jsonrpc_cmd read JSONRPC cmdfd#" <<
myqr_readable_jsonrpc_cmd << " recieved "
<< rdcnt << " bytes in buffer of " << jrbufsize);
if (rdcnt < 0 && rderr > 0)
{
strerror_r(rderr, errbuf, sizeof(errbuf));
assert(errbuf[0] != (char)0);
};
MYQR_DEBUGOUT("myqr_readable_jsonrpc_cmd got rdcnt=" << rdcnt
<< (rderr?" : ":".")
<< (rderr?errbuf:""));
if (rdcnt<0)
{
if (rderr == EWOULDBLOCK || rderr == EAGAIN || rderr == EINTR)
return;
MYQR_FATALOUT("myqr_readable_jsonrpc_cmd read fd#"
<< myqr_jsonrpc_cmd_fd << " failed: " << errbuf);
}
else if (rdcnt==0)
{
MYQR_DEBUGOUT("myqr_readable_jsonrpc_cmd got EOF on fd#"
<< myqr_jsonrpc_cmd_fd);
myqr_jsonrpc_cmd_fd = -1;
exit(EXIT_SUCCESS);
}
buf[rdcnt] = (char)0;
MYQR_DEBUGOUT("myqr_readable_jsonrpc_cmd incomplete read " << rdcnt
<< " bytes:" << std::endl << buf);
size_t oldcmdlen = myqr_stream_jsonrpc_cmd.tellp();
MYQR_DEBUGOUT("myqr_readable_jsonrpc_cmd oldcmdlen:" << oldcmdlen);
myqr_stream_jsonrpc_cmd.write(buf, rdcnt);
char*begm = buf;
while(begm && *begm)
{
char*ff = strchr(begm, '\f');
char*nn = strstr(begm, "\n\n");
char*eom = nullptr;
if (!ff && !nn)
{
/// the JSON message is incomplete since not ended by formfeed
/// or double-newline
return;
};
if (ff != nullptr && nn != nullptr)
{
eom = (ff < nn)?ff:nn;
}
else if (ff != nullptr)
eom = ff+1;
else if (nn != nullptr)
eom = nn+2;
else
{
/// this should never happen
MYQR_FATALOUT("myqr_readable_jsonrpc_cmd bug ff=" << ff << " nn=" << nn);
};
int deltaeom = eom - begm;
assert(deltaeom > 0);
std::string jsonstr;
if (oldcmdlen>0)
{
jsonstr.assign(myqr_stream_jsonrpc_cmd.str());
jsonstr.append(begm, deltaeom);
oldcmdlen=0;
}
else
jsonstr.assign(begm, deltaeom);
begm = eom+1;
MYQR_DEBUGOUT("myqr_readable_jsonrpc_cmd jsonstr=" << jsonstr);
Json::Value jv;
std::string errmsg;
bool okparse = //
myqr_jsonrpc_reader->parse(jsonstr.c_str(), &jsonstr.back(),
&jv, &errmsg);
if (!okparse)
MYQR_FATALOUT("myqr_readable_jsonrpc_cmd failed to parse:"
<< std::endl << jsonstr
<< std::endl << "error:" << errmsg);
MYQR_DEBUGOUT("myqr_readable_jsonrpc_cmd parsed json:"
<< std::endl << jv.asString() << std::endl);
myqr_process_jsonrpc_from_refpersys(jv);
};
} // end myqr_readable_jsonrpc_cmd
void
myqr_writable_jsonrpc_out(void)
{
MYQR_DEBUGOUT("myqr_writable_jsonrpc_out unimplemented myqr_jsonrpc_out_fd="
<< myqr_jsonrpc_out_fd);
#warning unimplemented myqr_writable_jsonrpc_out
} // end myqr_writable_jsonrpc_out
/// a JSONRPC communication happens with RefPerSys
void
myqr_have_jsonrpc(const std::string&jsonrpc)
{
MYQR_DEBUGOUT("myqr_have_jsonrpc incomplete " << jsonrpc);
std::string jsonrpc_cmd = jsonrpc+".cmd"; /// written by RefPerSys, read by q6refpersys
std::string jsonrpc_out = jsonrpc+".out"; /// read by RefPerSys, written by q6refpersys
if (access(jsonrpc_cmd.c_str(), F_OK))
{
if (mkfifo(jsonrpc_cmd.c_str(), 0660)<0)
MYQR_FATALOUT("failed to create command JSONRPC fifo " << jsonrpc_cmd << ":" << strerror(errno));
else
MYQR_DEBUGOUT("myqr_have_jsonrpc created command fifo " << jsonrpc_cmd);
};
if (access(jsonrpc_out.c_str(), F_OK))
{
if (mkfifo(jsonrpc_out.c_str(), 0660)<0)
MYQR_FATALOUT("failed to create output JSONRPC fifo " << jsonrpc_out << ":" << strerror(errno));
else
MYQR_DEBUGOUT("myqr_have_jsonrpc created output fifo " << jsonrpc_out);
};
myqr_jsonrpc_cmd_fd = open(jsonrpc_cmd.c_str(), 0440 | O_CLOEXEC | O_NONBLOCK);
if (myqr_jsonrpc_cmd_fd<0)
MYQR_FATALOUT("failed to open command JSONRPC " << jsonrpc_cmd << " for reading:" << strerror(errno));
else
MYQR_DEBUGOUT("myqr_have_jsonrpc cmd fd#" << myqr_jsonrpc_cmd_fd);
myqr_notifier_jsonrpc_cmd = new QSocketNotifier(myqr_jsonrpc_cmd_fd, QSocketNotifier::Read);
QObject::connect(myqr_notifier_jsonrpc_cmd,&QSocketNotifier::activated,
myqr_readable_jsonrpc_cmd);
myqr_jsonrpc_out_fd = open(jsonrpc_out.c_str(), 0660 | O_CLOEXEC | O_NONBLOCK);
if (myqr_jsonrpc_out_fd<0)
MYQR_FATALOUT("failed to open output JSONRPC " << jsonrpc_out << " for writing:" << strerror(errno));
else
MYQR_DEBUGOUT("myqr_have_jsonrpc out fd#" << myqr_jsonrpc_out_fd);
myqr_notifier_jsonrpc_out = new QSocketNotifier(myqr_jsonrpc_out_fd, QSocketNotifier::Write);
QObject::connect(myqr_notifier_jsonrpc_out,&QSocketNotifier::activated,myqr_writable_jsonrpc_out);
if (setenv("REFPERSYS_JSONRPC", jsonrpc.c_str(), /*overwrite:*/(int)true))
{
MYQR_FATALOUT("failed to setenv REFPERSYS_JSONRPC to " << jsonrpc
<< " :" << strerror(errno));
}
else
{
MYQR_DEBUGOUT("myqr_have_jsonrpc did setenv REFPERSYS_JSONRPC to "
<< jsonrpc.c_str());
};
MYQR_DEBUGOUT("myqr_have_jsonrpc installed cmd: " << jsonrpc_cmd << " fd#" << myqr_jsonrpc_cmd_fd
<< " out: " << jsonrpc_out
<< " fd#" << myqr_jsonrpc_out_fd);
MYQR_DEBUGOUT("myqr_have_jsonrpc ending jsonrpc:" << jsonrpc
<< " cmd.fd#" << myqr_jsonrpc_cmd_fd << " out.fd#" << myqr_jsonrpc_out_fd);
} // end myqr_have_jsonrpc
void
myqr_start_refpersys(const std::string& refpersysprog,
QStringList&arglist)
{
std::string prog= refpersysprog;
qint64 pid= 0;
MYQR_DEBUGOUT("starting myqr_start_refpersys " << refpersysprog
<< " " << arglist << " from " << myqr_progname
<< " on " << myqr_host_name
<< " pid " << (int)getpid() << " git " << myqr_git_id);
if (refpersysprog.empty())
{
prog = "refpersys";
}
else if (refpersysprog[0] == '-')
{
prog = "refpersys";
arglist.prepend(QString(refpersysprog.c_str()));
};
MYQR_DEBUGOUT("myqr_start_refpersys prog=" << prog << " arglist=" << arglist
<< "before process creation from pid " << (int)getpid());
myqr_refpersys_process = new QProcess();
std::string progname;
if (prog.find('/') == std::string::npos)
{
const char* path = getenv("PATH");
const char*pc = nullptr;
const char*colon = nullptr;
const char*nextpc = nullptr;
MYQR_DEBUGOUT("myqr_start_refpersys PATH=" << path
<< " prog=" << prog);
for (pc = path; pc && *pc; pc = nextpc)
{
colon = strchr(pc, ':');
nextpc = colon?(colon+1):nullptr;
std::string dir(pc, colon?(colon-pc):strlen(pc));
std::string exepath = dir + "/" + prog;
MYQR_DEBUGOUT("myqr_start_refpersys pc="
<< pc << " dir=" << dir << " exepath=" << exepath);
if (!access(exepath.c_str(), F_OK|X_OK))
{
progname = exepath;
MYQR_DEBUGOUT("myqr_start_refpersys progname=" << progname);
nextpc = nullptr;
break;
}
};
if (progname.empty())
MYQR_FATALOUT("failed to find program " << prog
<< " in PATH=" << path);
}
else
{
MYQR_DEBUGOUT("myqr_start_refpersys prog has slash " << prog);
progname = prog;
};
myqr_refpersys_process->setProgram(QString(progname.c_str()));
myqr_refpersys_process->setArguments(arglist);
MYQR_DEBUGOUT("myqr_start_refpersys before starting " << progname
<< " with arguments " << arglist
<< " git " << myqr_git_id
<< " myqr_refpersys_process@" << (void*)myqr_refpersys_process);
errno = 0;
if (!myqr_refpersys_process->startDetached(&pid))
{
int e = errno;
MYQR_FATALOUT("failed to start refpersys program: " << prog
<< " from " << myqr_progname << " pid:" << getpid()
<< " errno:" << strerror(e));
};
MYQR_DEBUGOUT("myqr_start_refpersys started " << prog
<< " with arguments " << arglist
<< " as pid " << pid);
myqr_refpersys_pid = (pid_t) pid;
usleep (320*1024); /// hopefully let the refpersys process run a little bit...
} // end myqr_start_refpersys
/// process the JSON recieved from refpersys
void
myqr_process_jsonrpc_from_refpersys(const Json::Value&js)
{
MYQR_DEBUGOUT("myqr_process_jsonrpc_from_refpersys got JSON" << std::endl
<< myqr_json2str(js));
#warning myqr_process_jsonrpc_from_refpersys unimplemented
MYQR_FATALOUT("unimplemented myqr_process_jsonrpc_from_refpersys"
<< std::endl
<< myqr_json2str(js));
} // end myqr_process_jsonrpc_from_refpersys
/// do a remote procedure call to RefPerSys using our JSONRPC variant
void
myqr_call_jsonrpc_to_refpersys
(const std::string& method,
const Json::Value& args,
const std::function<void(const Json::Value&res)>& resfun)
{
Json::Value jresult;
static int count;
{
std::lock_guard<std::recursive_mutex> lock(myqr_mtx_jsonrpc_out);
Json::Value jreq(Json::objectValue);
jreq["jsonrpc"] = "2.0";
jreq["method"] = method;
jreq["params"] = args;
jreq["id"] = ++count;
myqr_deque_jsonrpc_out.push_back(jreq);
myqr_jsonrpc_out_procmap.insert_or_assign(count, resfun);
MYQR_DEBUGOUT("myqr_call_jsonrpc_to_refpersys jreq:"
<< myqr_json2str(jreq));
}
usleep(500);
{
std::lock_guard<std::recursive_mutex> lock(myqr_mtx_jsonrpc_out);
if (!myqr_deque_jsonrpc_out.empty())
{
Json::Value joldreq = myqr_deque_jsonrpc_out.front();
myqr_deque_jsonrpc_out.pop_front();
myqr_stream_jsonrpc_out << myqr_json2str(joldreq)
<< "\n\f" << std::flush;
}
std::string outs = myqr_stream_jsonrpc_out.str();
size_t outslen = outs.size();
errno = 0;
ssize_t wcnt = write(myqr_jsonrpc_out_fd, outs.c_str(), outslen);
MYQR_DEBUGOUT("myqr_call_jsonrpc_to_refpersys outslen:" << outslen
<< " wcnt:" << wcnt << " outfd#" << myqr_jsonrpc_out_fd
<< " errno:" << strerror(errno) << " pid:" << (int)getpid());
if (wcnt>0) // https://stackoverflow.com/a/4546562
{
if ((long)wcnt<(long)outslen)
{
outs.erase(0, wcnt);
MYQR_DEBUGOUT("myqr_call_jsonrpc_to_refpersys outs becomes: '"
<< outs << "'");
myqr_stream_jsonrpc_out.str(outs);
}
else // https://stackoverflow.com/a/20792
{
MYQR_DEBUGOUT("myqr_call_jsonrpc_to_refpersys outs cleared");
myqr_stream_jsonrpc_out.str("");
}
}
#warning incomplete myqr_call_jsonrpc_to_refpersys
}
} // end myqr_call_jsonrpc_to_refpersys
std::string
myqr_json2str(const Json::Value&jv)
{
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = " ";
auto str = Json::writeString(builder, jv);
return str;
} // end myqr_json2str
int
main(int argc, char **argv)
{
myqr_progname = argv[0];
for (int i=1; i<argc; i++)
{
if (!strcmp(argv[i], "-D") || !strcmp(argv[i], "--debug"))
{
qDebug().setVerbosity(QDebug::DefaultVerbosity);
myqr_debug = true;
}
}
gethostname(myqr_host_name, sizeof(myqr_host_name)-1);
MYQR_DEBUGOUT("starting " << myqr_progname << " on " << myqr_host_name
<< " git " << myqr_git_id << " pid " << (int)getpid()
<< " argc=" << argc
<< " dynamic qVersion=" << qVersion());
myqr_jsoncpp_reader_builder["collectComments"] = false;
myqr_jsoncpp_reader_builder["rejectDupKeys"] = true;
myqr_jsoncpp_writer_builder["commentStyle"] = "None";
myqr_jsoncpp_writer_builder["indentation"] = "";
myqr_jsonrpc_reader = myqr_jsoncpp_reader_builder.newCharReader();
QCoreApplication::setApplicationName("q6refpersys");
QCoreApplication::setApplicationVersion(QString("version ") + myqr_git_id
+ " " __DATE__ "@" __TIME__);
QApplication the_app(argc, argv);
MYQR_DEBUGOUT("the_app@" << (void*)&the_app << " argc:" << argc);
QCommandLineParser cli_parser;
cli_parser.addVersionOption();
cli_parser.addHelpOption();
QCommandLineOption debug_opt(QStringList() << "D" << "debug",
"show debugging messages");
cli_parser.addOption(debug_opt);
QCommandLineOption jsonrpc_opt{{"J", "jsonrpc"},
"Use $JSONRPC.out and $JSONRPC.cmd fifos.\n"
"Also sets the REFPERSYS_JSONRPC environment variable to $JSONRPC.", "JSONRPC"};
cli_parser.addOption(jsonrpc_opt);
QCommandLineOption geometry_opt{{"G", "geometry"},
"Main window geometry is W*H,\n... e.g. --geometry 400x650", "WxH"};
cli_parser.addOption(geometry_opt);
QCommandLineOption refpersys_opt{"start-refpersys",
"Start the given $REFPERSYS, defaulted to refpersys",
"REFPERSYS", QString("refpersys")};
cli_parser.addOption(refpersys_opt);
cli_parser.process(the_app);
MYQR_DEBUGOUT("main cli_parser@" << (void*)&cli_parser);
QStringList args = cli_parser.positionalArguments();
MYQR_DEBUGOUT("main args:" << args);
myqr_app = &the_app;
QString geomstr = cli_parser.value(geometry_opt);
MYQR_DEBUGOUT("main geomstr:" << geomstr.toStdString());
MYQR_DEBUGOUT("main debug:" << cli_parser.value(debug_opt).toStdString());
MYQR_DEBUGOUT("main startrefpersys:" << cli_parser.value(refpersys_opt).toStdString()
<< (cli_parser.isSet(refpersys_opt)?" is set":" is not set"));
myqr_create_windows(geomstr);
if (cli_parser.isSet(jsonrpc_opt))
myqr_have_jsonrpc(cli_parser.value(jsonrpc_opt).toStdString());
if (cli_parser.isSet(refpersys_opt))
{
if (cli_parser.isSet(jsonrpc_opt))
args += cli_parser.value(jsonrpc_opt);
myqr_start_refpersys(cli_parser.value(refpersys_opt).toStdString(), args);
};
///
MYQR_DEBUGOUT("main jsonrpc_opt:" << cli_parser.value(jsonrpc_opt).toStdString()
<< " refpersys_opt: " << cli_parser.value(refpersys_opt).toStdString()
<< " jsonrpc:" << myqr_jsonrpc << " pid:" << myqr_refpersys_pid);
if (cli_parser.isSet(jsonrpc_opt) && cli_parser.isSet(refpersys_opt))
{
Json::Value jargs(Json::objectValue);
jargs["gitid"] = myqr_git_id;
jargs["runtime_Qt"] = qVersion();
jargs["compile_Qt"] = QTCORE_VERSION_STR;
MYQR_DEBUGOUT("myqr_have_jsonrpc jargs is " << myqr_json2str(jargs));
MYQR_DEBUGOUT("myqr_have_jsonrpc jargs=" << jargs << " call _VERSION" << " refpersys_pid:" << myqr_refpersys_pid);
myqr_call_jsonrpc_to_refpersys("_VERSION", jargs,
[&] (const Json::Value&jres)
{
MYQR_DEBUGOUT("myqr_have_jsonrpc _VERSION got " << myqr_json2str(jres)
<< " with jargs " << myqr_json2str(jargs));
});
}
MYQR_DEBUGOUT("main before exec");
int execret = myqr_app->exec();
MYQR_DEBUGOUT("main after exec execret=" << execret);
if (myqr_refpersys_pid>0)
{
MYQR_DEBUGOUT("main kill with SIGTERM refpersys pid#" << myqr_refpersys_pid);
errno = 0;
if (kill(myqr_refpersys_pid, SIGTERM)<0)
{
MYQR_FATALOUT("failed to TERM refpersys pid#" << myqr_refpersys_pid
<< ":" << strerror(errno));
}
}
myqr_app = nullptr;
MYQR_DEBUGOUT("main returns execret=" << execret);
return execret;
} // end main
const char myqr_git_id[] = GITID;
char* myqr_progname;
char myqr_host_name[sizeof(myqr_host_name)];
QApplication *myqr_app;
bool myqr_debug;
std::string myqr_jsonrpc;
Json::CharReaderBuilder myqr_jsoncpp_reader_builder;
Json::StreamWriterBuilder myqr_jsoncpp_writer_builder;
Json::CharReader* myqr_jsonrpc_reader;
int myqr_jsonrpc_cmd_fd = -1;
int myqr_jsonrpc_out_fd = -1;
std::recursive_mutex myqr_mtx_jsonrpc_cmd;
std::stringstream myqr_stream_jsonrpc_cmd;
QProcess*myqr_refpersys_process;
QSocketNotifier* myqr_notifier_jsonrpc_cmd;
QSocketNotifier* myqr_notifier_jsonrpc_out;
std::recursive_mutex myqr_mtx_jsonrpc_out;
std::deque<Json::Value> myqr_deque_jsonrpc_out;
std::stringstream myqr_stream_jsonrpc_out;
std::map<int,std::function<void(const Json::Value&res)>> myqr_jsonrpc_out_procmap;
pid_t myqr_refpersys_pid;
#include "_q6refpersys-moc.cc"
/****************
** for Emacs...
** Local Variables: ;;
** compile-command: "make q6refpersys" ;;
** End: ;;
**
****************/