forked from sPHENIX-Collaboration/rcdaq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcdaq_server.cc
855 lines (676 loc) · 21.1 KB
/
rcdaq_server.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
#include "rcdaq_rpc.h"
#include "rcdaq_actions.h"
#include "parseargument.h"
#include "rcdaq.h"
#include "daq_device.h"
#include "rcdaq_plugin.h"
#include "all.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <rpc/pmap_clnt.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "pthread.h"
#include "signal.h"
#include <vector>
#include <set>
#include <iostream>
using namespace std;
static pthread_mutex_t M_output;
std::vector<RCDAQPlugin *> pluginlist;
static unsigned long my_servernumber = 0;
void rcdaq_1(struct svc_req *rqstp, register SVCXPRT *transp);
//-------------------------------------------------------------
int server_setup(int argc, char **argv)
{
return 0;
}
//-------------------------------------------------------------
void plugin_register(RCDAQPlugin * p )
{
pluginlist.push_back(p);
}
void plugin_unregister(RCDAQPlugin * p )
{
// do nothing for now
// we need to implement this once we add unloading of plugins
// as a feature (don't see why at this point)
}
int daq_load_plugin( const char *sharedlib, std::ostream& os)
{
void * v = dlopen(sharedlib, RTLD_GLOBAL | RTLD_NOW | RTLD_NOLOAD);
if (v)
{
std::cout << "Plugin "
<< sharedlib << " already loaded" << std::endl;
os << "Plugin "
<< sharedlib << " already loaded" << std::endl;
return 0;
}
void * voidpointer = dlopen(sharedlib, RTLD_GLOBAL | RTLD_NOW);
if (!voidpointer)
{
std::cout << "Loading of the plugin "
<< sharedlib << " failed: " << dlerror() << std::endl;
os << "Loading of the plugin "
<< sharedlib << " failed: " << dlerror() << std::endl;
return -1;
}
os << "Plugin " << sharedlib << " successfully loaded" << std::endl;
cout << "Plugin " << sharedlib << " successfully loaded" << std::endl;
return 0;
}
//-------------------------------------------------------------
// for certain flags we add some info to the status output
// with this routine. It is called from daq_status.
int daq_status_plugin (const int flag, std::ostream& os )
{
// in case we do have plugins, list them
// if not, we just say "no plugins loded"
if ( pluginlist.size() )
{
os << " List of loaded Plugins:" << endl;
}
else
{
os << " No Plugins loaded" << endl;
}
std::vector<RCDAQPlugin *>::iterator it;
for ( it=pluginlist.begin(); it != pluginlist.end(); ++it)
{
os << " ";
(*it)->identify(os, flag);
}
return 0;
}
//-------------------------------------------------------------
shortResult * r_create_device_1_svc(deviceblock *db, struct svc_req *rqstp)
{
static shortResult result, error;
static char e_string[512];
static char r_string[512];
error.str = e_string;
result.str = r_string;
strcpy ( r_string, " ");
if ( daq_running() )
{
strcpy(e_string, "Run is active");
error.content = 1;
error.what = 0;
error.status = -1;
return &error;
}
strcpy(e_string, "Device needs at least 2 parameters");
error.content = 1;
error.what = 0;
error.status = -1;
result.status = 0;
result.what = 0;
result.content= 0;
int eventtype;
int subid;
if ( db->npar < 3)
{
strcpy(r_string, "Device needs at least 2 parameters");
return &error;
}
strcpy(r_string, "Wrong number of parameters");
// and we decode the event type and subid
eventtype = get_value ( db->argv1); // event type
subid = get_value ( db->argv2); // subevent id
// now we will see what device we are supposed to set up.
// we first check if it is one of our built-in ones, such as
// device_random, or device_file or so.
if ( strcasecmp(db->argv0,"device_random") == 0 )
{
// this happens to be the most complex contructor part
// so far since there are a few variants, 2-6 parameters
switch ( db->npar)
{
case 3:
add_readoutdevice ( new daq_device_random( eventtype,
subid ));
break;
case 4:
add_readoutdevice ( new daq_device_random( eventtype,
subid,
get_value ( db->argv3)));
break;
case 5:
add_readoutdevice ( new daq_device_random( eventtype,
subid,
get_value ( db->argv3),
get_value ( db->argv4)));
break;
case 6:
add_readoutdevice ( new daq_device_random( eventtype,
subid,
get_value ( db->argv3),
get_value ( db->argv4),
get_value ( db->argv5)));
break;
case 7:
add_readoutdevice ( new daq_device_random( eventtype,
subid,
get_value ( db->argv3),
get_value ( db->argv4),
get_value ( db->argv5),
get_value ( db->argv6)));
break;
default:
return &error;
break;
}
return &result;
}
if ( strcasecmp(db->argv0,"device_deadtime") == 0 )
{
// this happens to be the most complex contructor part
// so far since there are a few variants, 2-6 parameters
switch ( db->npar)
{
case 3:
add_readoutdevice ( new daq_device_deadtime( eventtype,
subid ));
break;
case 4: // plus number of ticks
add_readoutdevice ( new daq_device_deadtime( eventtype,
subid,
get_value ( db->argv3)));
break;
case 5: // plus number of words
add_readoutdevice ( new daq_device_deadtime( eventtype,
subid,
get_value ( db->argv3),
get_value ( db->argv4)));
break;
case 6: // plus trigger flag
add_readoutdevice ( new daq_device_deadtime( eventtype,
subid,
get_value ( db->argv3),
get_value ( db->argv4),
get_value ( db->argv5)));
break;
default:
return &error;
break;
}
return &result;
}
else if ( strcasecmp(db->argv0,"device_file") == 0 )
{
if ( db->npar < 4) return &error;
if ( db->npar >= 5)
{
// we give the size in kbytes but we want it in words
int s = 1024*(get_value(db->argv4)+3)/4;
if ( s < 4*1024) s = 4*1024; // this is the default size
add_readoutdevice ( new daq_device_file( eventtype,
subid,
db->argv3,
0, // no delete
s ) );
return &result;
}
else
{
add_readoutdevice ( new daq_device_file( eventtype,
subid,
db->argv3));
return &result;
}
}
// --------------------------------------------------------------------------
// although this logic is very similar to device_file, since the consequences
// of a misplaced parameter are so severe, we make a new device
else if ( strcasecmp(db->argv0,"device_file_delete") == 0 )
{
if ( db->npar < 4) return &error;
if ( db->npar >= 5)
{
// we give the size in kbytes but we want it in words
int s = 1024*(get_value(db->argv4)+3)/4;
if ( s < 4*1024) s = 4*1024; // this is the default size
add_readoutdevice ( new daq_device_file( eventtype,
subid,
db->argv3,
1, // we add the delete flag
s ) );
return &result;
}
else
{
add_readoutdevice ( new daq_device_file( eventtype,
subid,
db->argv3,
1) );
return &result;
}
}
// --------------------------------------------------------------------------
else if ( strcasecmp(db->argv0,"device_filenumbers") == 0 )
{
if ( db->npar < 4) return &error;
if ( db->npar >= 5)
{
int s = 1024*(get_value(db->argv4)+3)/4;
if ( s < 256) s = 256; // this is the default size
add_readoutdevice ( new daq_device_filenumbers( eventtype,
subid,
db->argv3,
0, // no delete
s));
return &result;
}
else
{
add_readoutdevice ( new daq_device_filenumbers( eventtype,
subid,
db->argv3));
return &result;
}
}
// --------------------------------------------------------------------------
else if ( strcasecmp(db->argv0,"device_filenumbers_delete") == 0 )
{
if ( db->npar < 4) return &error;
if ( db->npar >= 5)
{
int s = 1024*(get_value(db->argv4)+3)/4;
if ( s < 256) s = 256; // this is the default size
add_readoutdevice ( new daq_device_filenumbers( eventtype,
subid,
db->argv3,
1, // we add the delete flag
s));
return &result;
}
else
{
add_readoutdevice ( new daq_device_filenumbers( eventtype,
subid,
db->argv3,
1) );
return &result;
}
}
else if ( strcasecmp(db->argv0,"device_command") == 0 )
{
if ( db->npar < 4) return &error;
if ( db->npar >= 5)
{
int s = (get_value(db->argv4)+3)/4;
if ( s < 1280) s = 1280; // this is the default size
add_readoutdevice ( new daq_device_file( eventtype,
subid,
db->argv3,
get_value(db->argv4) ) );
return &result;
}
else
{
add_readoutdevice ( new daq_device_command( eventtype,
subid,
db->argv3));
return &result;
}
}
else if ( strcasecmp(db->argv0,"device_rtclock") == 0 )
{
add_readoutdevice ( new daq_device_rtclock( eventtype,
subid));
return &result;
}
// nada, it was none of the built-in ones if we arrive here.
// we now go through through the list of plugins and see if any one of
// our plugins can make the device.
// there are three possibilities:
// 1) the plugin can make the device, all done. In that case, return = 0
// 2) the plugin can make the device but the parameters are wrong, return = 1
// 3) the plugin dosn not knwo about tha device, we keep on going... return = 2
// we keep doing that until we either find a plugin that knows the device or we run
// out of plugins
std::vector<RCDAQPlugin *>::iterator it;
int status;
for ( it=pluginlist.begin(); it != pluginlist.end(); ++it)
{
status = (*it)->create_device(db);
// in both of the following cases we are done here:
if (status == 0) return &result; // sucessfully created
else if ( status == 1) return &error; // it's my device but wrong params
// in all other cases we continue and try the next plugin
}
result.content=1;
strcpy(r_string, "Unknown device");
return &result;
}
//-------------------------------------------------------------
shortResult * r_action_1_svc(actionblock *ab, struct svc_req *rqstp)
{
static shortResult result;
static std::ostringstream outputstream;
static unsigned int currentmaxresultlength = 10*2048;
static char *resultstring = new char[currentmaxresultlength+1];
// to avoid a race condition with the asynchronous "end requested" feature,
// wait here...
daq_wait_for_actual_end();
if ( outputstream.str().size() > currentmaxresultlength )
{
currentmaxresultlength = outputstream.str().size();
std::cout << __LINE__<< " " << __FILE__
<< " *** warning: extended result string to " << currentmaxresultlength << std::endl;
delete [] resultstring;
resultstring = new char[currentmaxresultlength+1];
}
pthread_mutex_lock(&M_output);
result.str=resultstring;
strcpy(resultstring," ");
result.content = 0;
result.what = 0;
result.status = 0;
outputstream.str("");
switch ( ab->action)
{
case DAQ_BEGIN:
result.status = daq_begin ( ab->ipar[0], outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_END:
result.status = daq_end_interactive(outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_END_IMMEDIATE:
result.status = daq_end_immediate(outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_RUNNUMBERFILE:
daq_set_runnumberfile(ab->spar);
break;
case DAQ_SETFILERULE:
daq_set_filerule(ab->spar);
break;
case DAQ_SETNAME:
daq_set_name(ab->spar);
break;
case DAQ_SETRUNTYPE:
result.status = daq_setruntype(ab->spar,outputstream);
if ( result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_GETRUNTYPE:
result.status = daq_getruntype(ab->ipar[0], outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_DEFINERUNTYPE:
daq_define_runtype(ab->spar, ab->spar2);
break;
case DAQ_LISTRUNTYPES:
result.status = daq_list_runtypes(ab->ipar[0], outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_GETNAME:
result.status = daq_get_name(outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_OPEN:
result.status = daq_open(outputstream);
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_SET_SERVER:
result.status = daq_set_server(ab->spar, ab->ipar[0], outputstream);
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_CLOSE:
result.status = daq_close(outputstream);
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_FAKETRIGGER:
result.status = daq_fake_trigger (ab->ipar[0], ab->ipar[1]);
break;
case DAQ_LISTREADLIST:
result.status = daq_list_readlist(outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_CLEARREADLIST:
result.status = daq_clear_readlist(outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_STATUS:
result.status = daq_status(ab->ipar[0], outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_SETMAXEVENTS:
result.status = daq_setmaxevents ( ab->ipar[0], outputstream);
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_SETMAXVOLUME:
result.status = daq_setmaxvolume ( ab->ipar[0], outputstream);
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_SETMAXBUFFERSIZE:
result.status = daq_setmaxbuffersize ( ab->ipar[0], outputstream);
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_SETADAPTIVEBUFFER:
result.status = daq_setadaptivebuffering ( ab->ipar[0], outputstream);
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_ELOG:
daq_set_eloghandler( ab->spar, ab->ipar[0], ab->spar2);
break;
case DAQ_LOAD:
result.status = daq_load_plugin( ab->spar, outputstream );
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_WEBCONTROL:
result.status = daq_webcontrol ( ab->ipar[0], outputstream);
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
case DAQ_GETLASTFILENAME:
result.status = daq_getlastfilename ( outputstream);
if ( result.status )
{
pthread_mutex_unlock(&M_output);
result.content = 0;
return &result;
}
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
pthread_mutex_unlock(&M_output);
return &result;
break;
default:
strcpy(resultstring, "Unknown action");
result.content = 1;
result.status = 1;
break;
}
pthread_mutex_unlock(&M_output);
return &result;
}
shortResult * r_shutdown_1_svc(void *x, struct svc_req *rqstp)
{
static shortResult result;
static std::ostringstream outputstream;
static char resultstring[256];
pthread_mutex_lock(&M_output);
result.content = 0;
result.what = 0;
result.status = 0;
outputstream.str("");
result.str = (char *) outputstream.str().c_str();
result.status = daq_shutdown ( my_servernumber, RCDAQ_VERS, outputstream);
cout << "daq_shutdown status = " << result.status << endl;
if (result.status)
{
outputstream.str().copy(resultstring,outputstream.str().size());
resultstring[outputstream.str().size()] = 0;
result.str = resultstring;
result.content = 1;
}
pthread_mutex_unlock(&M_output);
return &result;
}
//-------------------------------------------------------------
int
main (int argc, char **argv)
{
int i;
pthread_mutex_init(&M_output, 0);
rcdaq_init( M_output);
server_setup(argc, argv);
int servernumber = 0;
if ( argc > 1)
{
servernumber = get_value(argv[1]);
}
std::cout << "Server number is " << servernumber << std::endl;
my_servernumber = RCDAQ+servernumber; // remember who we are for later
register SVCXPRT *transp;
pmap_unset (my_servernumber, RCDAQ_VERS);
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (transp == NULL) {
fprintf (stderr, "%s", "cannot create tcp service.");
exit(1);
}
if (!svc_register(transp, my_servernumber, RCDAQ_VERS, rcdaq_1, IPPROTO_TCP)) {
fprintf (stderr, "%s", "unable to register (RCDAQ+servernumber, RCDAQ_VERS, tcp).");
exit(1);
}
char hostname[1024];
gethostname(hostname, 1024);
daq_set_name(hostname);
svc_run ();
fprintf (stderr, "%s", "svc_run returned");
exit (1);
/* NOTREACHED */
}