-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrcdaq.cc
2786 lines (2176 loc) · 57.4 KB
/
rcdaq.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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#define WRITEPRDF
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <pthread.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netpacket/packet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <vector>
#include <map>
#include <queue>
#include "getopt.h"
#include "daq_device.h"
#include "daqBuffer.h"
#include "eloghandler.h"
#include "TriggerHandler.h"
#include "rcdaq.h"
#include "rcdaq_rpc.h"
#include "md5.h"
#ifdef HAVE_MOSQUITTO_H
#include "MQTTConnection.h"
#endif
int open_file_on_server(const int run_number);
int server_open_Connection();
int open_serverSocket(const char * host_name, const int port);
int server_send_beginrun_sequence(const char * filename, const int runnumber, int fd);
int server_send_rollover_sequence(const char * filename, int fd);
int server_send_endrun_sequence(int fd);
int server_send_close_sequence(int fd);
void * mg_server (void *arg);
int mg_end();
int request_mg_update(const int what);
pthread_mutex_t WriteSem;
pthread_mutex_t WriteProtectSem;
pthread_mutex_t MonitoringRequestSem;
pthread_mutex_t SendSem;
pthread_mutex_t SendProtectSem;
pthread_mutex_t FdManagementSem;
char pidfilename[128];
char daq_event_env_string[128];
// those are the "todo" definitions. DAQ can be woken up by a trigger
// and read something, or by a command and change its status.
int servernumber = 0;
#define DAQ_TRIGGER 0x01
#define DAQ_COMMAND 0x02
#define DAQ_SPECIAL 0x04
// now there are a few commands which the DAQ process obeys.
#define COMMAND_INIT 1
#define COMMAND_BEGIN 2
#define COMMAND_END 3
#define COMMAND_FINISH 4
#define COMMAND_OPENP 5
// now there are a few actions for the DAQ process
// when DAQ-triggered
#define DAQ_INIT 1
#define DAQ_READ 2
using namespace std;
static std::map<string,string> RunTypes;
static std::string TheFileRule = "rcdaq-%08d-%04d.evt";
static std::string TheRunType = " ";
static std::string CurrentFilename = "";
static std::string PreviousFilename = "";
static std::string TheRunnumberfile;
static int RunnumberfileIsSet = 0;
static std::string TheRunnumberApp;
static int RunnumberAppIsSet = 0;
static std::string MyName = "";
static int daq_open_flag = 0; //no files written unless asked for
static int daq_server_flag = 0; //no server access
static int daq_server_port = 0; // invalid port
static std::string daq_server_name = ""; // our server, if any
static int file_is_open = 0;
static int server_is_open = 0;
static int current_filesequence = 0;
static int outfile_fd;
static int md5_enabled =1;
static int md5_allow_turnoff = 0;
#ifdef HAVE_MOSQUITTO_H
MQTTConnection *mqtt = 0;
std::string mqtt_host;
int mqtt_port = 0;
#endif
static md5_state_t md5state;
static int RunControlMode = 0;
static int CurrentEventType = 0;
static ElogHandler *ElogH =0;
static TriggerHandler *TriggerH =0;
typedef std::vector<daq_device *> devicevector;
typedef devicevector::iterator deviceiterator;
#define MAXEVENTID 32
static int Eventsize[MAXEVENTID];
// int Daq_Status;
// #define DAQ_RUNNING 0x01
// #define DAQ_READING 0x02
// #define DAQ_ENDREQUESTED 0x04
// #define DAQ_PROTOCOL 0x10
// #define DAQ_BEGININPROGRESS 0x20
// the original bit-wise status word manipulation wasn't particular thread-safe.
// upgrading to individual variables (and we ditch "DAQ_READING")
int DAQ_RUNNING = 0;
int DAQ_ENDREQUESTED = 0;
int DAQ_BEGININPROGRESS =0;
static daqBuffer Buffer1;
static daqBuffer Buffer2;
int Trigger_Todo;
int Command_Todo;
int TheRun = 0;
time_t StartTime = 0;
int Buffer_number;
int Event_number;
int Event_number_at_last_open = 0;
int Event_number_at_last_write = 0;
int Event_number_at_last_end = 0;
int update_delta;
static int TriggerControl = 0;
int ThePort=8899;
int TheServerFD = 0;
daqBuffer *fillBuffer, *transportBuffer;
pthread_t ThreadId;
pthread_t ThreadMon;
pthread_t ThreadEvt;
pthread_t ThreadWeb = 0;
int *thread_arg;
int end_thread = 0;
pthread_mutex_t M_cout;
struct sockaddr_in si_mine;
#define MONITORINGPORT 9930
devicevector DeviceList;
int NumberWritten = 0;
unsigned long long BytesInThisRun = 0;
unsigned long long BytesInThisFile = 0;
unsigned long long RolloverLimit = 0;
unsigned long long run_volume, max_volume;
int max_events;
time_t last_speed_time = 0;
int last_event_nr = 0;
static time_t last_volume_time = 0;
double last_volume = 0;
int EvtId = 0;
int max_seconds = 0;
int verbose = 0;
int runnumber=1;
int packetid = 1001;
int max_buffers=0;
int adaptivebuffering = 15;
int last_bufferwritetime = 0;
int persistentErrorCondition = 0;
std::string MyHostName;
std::string shortHostName;
char *obtain_pidfilename()
{
return pidfilename;
}
int registerTriggerHandler ( TriggerHandler *th)
{
// we already have one
if ( TriggerH ) return -1;
TriggerH = th;
return 0;
}
int clearTriggerHandler ()
{
TriggerH = 0;
return 0;
}
void sig_handler(int i)
{
if (verbose)
{
pthread_mutex_lock(&M_cout);
cout << "**interrupt " << endl;
pthread_mutex_unlock(&M_cout);
}
TriggerControl = 0;
}
int reset_deadtime()
{
if ( TriggerH) TriggerH->rearm();
return 0;
}
int enable_trigger()
{
TriggerControl=1;
if ( TriggerH) TriggerH->enable();
int status = pthread_create(&ThreadEvt, NULL,
EventLoop,
(void *) 0);
if (status )
{
cout << "error in event thread create " << status << endl;
exit(0);
}
else
{
if ( TriggerH) TriggerH->rearm();
}
return 0;
}
int disable_trigger()
{
TriggerControl=0; // this makes the trigger process terminate
if ( TriggerH) TriggerH->disable();
return 0;
}
int daq_setmaxevents (const int n, std::ostream& os)
{
max_events =n;
return 0;
}
int daq_setmaxvolume (const int n_mb, std::ostream& os)
{
unsigned long long x = n_mb;
max_volume =x * 1024 *1024;
return 0;
}
int daq_setRunControlMode(const int flag, std::ostream& os)
{
if ( DAQ_RUNNING )
{
os << "Run is active" << endl;
return -1;
}
if (flag) RunControlMode = 1;
else RunControlMode = 0;
return 0;
}
int daq_setrolloverlimit (const int n_gb, std::ostream& os)
{
if ( DAQ_RUNNING )
{
os << "Run is active" << endl;
return -1;
}
RolloverLimit = n_gb;
return 0;
}
int daq_setmaxbuffersize (const int n_kb, std::ostream& os)
{
if ( DAQ_RUNNING )
{
os << "Run is active" << endl;
return -1;
}
Buffer1.setMaxSize(n_kb*1024);
Buffer2.setMaxSize(n_kb*1024);
return 0;
}
int daq_setadaptivebuffering (const int usecs, std::ostream& os)
{
adaptivebuffering = usecs;
return 0;
}
// this call was added to allow chaging the format after the
// server was started. Before this was a compile-time option.
int daq_setEventFormat(const int f, std::ostream& os )
{
if ( daq_running() )
{
os << "Run is active" << endl;
return -1;
}
if (DeviceList.size())
{
os << "Cannot switch format after devices are defined" << endl;
return -1;
}
int status = Buffer1.setEventFormat(f);
status |= Buffer2.setEventFormat(f);
return status;
}
// this is a method for the devices to obtain
// the info which format we are writing
int daq_getEventFormat()
{
return Buffer1.getEventFormat();
}
int daq_getRunControlMode(std::ostream& os)
{
os << RunControlMode << endl;
return 0;
}
// elog server setup
int daq_set_eloghandler( const char *host, const int port, const char *logname)
{
if ( ElogH) delete ElogH;
ElogH = new ElogHandler (host, port, logname );
setenv ( "DAQ_ELOGHOST", host , 1);
char str [128];
sprintf(str, "%d", port);
setenv ( "DAQ_ELOGPORT", str , 1);
setenv ( "DAQ_ELOGLOGBOOK", logname , 1);
return 0;
}
int readn (int fd, char *ptr, const int nbytes)
{
// in the interest of having just one "writen" call,
// we determine if the file descriptor we got is a socket
// (then we use send() ) or a file ( then we use write() ).
struct stat statbuf;
fstat(fd, &statbuf);
int fd_is_socket = 0;
if ( S_ISSOCK(statbuf.st_mode) ) fd_is_socket = 1;
int nread, nleft;
nleft = nbytes;
while ( nleft>0 )
{
if ( fd_is_socket) nread = recv (fd, ptr, nleft, MSG_NOSIGNAL);
else nread = read (fd, ptr, nleft);
if ( nread <= 0 )
{
return nread;
}
nleft -= nread;
ptr += nread;
}
return (nbytes-nleft);
}
int writen (int fd, char *ptr, const int nbytes)
{
// in the interest of having just one "writen" call,
// we determine if the file descriptor we got is a socket
// (then we use send() ) or a file ( then we use write() ).
struct stat statbuf;
fstat(fd, &statbuf);
int fd_is_socket = 0;
if ( S_ISSOCK(statbuf.st_mode) ) fd_is_socket = 1;
int nleft, nwritten;
nleft = nbytes;
while ( nleft>0 )
{
nwritten = 0;
if ( fd_is_socket) nwritten = send (fd, ptr, nleft, MSG_NOSIGNAL);
else nwritten = write (fd, ptr, nleft);
if ( nwritten <0 )
{
perror ("writen");
return 0;
}
nleft -= nwritten;
ptr += nwritten;
}
return (nbytes-nleft);
}
//-------------------------------------------------------------
int open_file(const int run_number, int *fd)
{
if ( file_is_open) return -1;
static char d[1024];
sprintf( d, TheFileRule.c_str(),
run_number, current_filesequence);
// test if the file exists, do not overwrite
int ifd = open(d, O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE ,
S_IRWXU | S_IROTH | S_IRGRP );
if (ifd < 0)
{
pthread_mutex_lock(&M_cout);
cout << " error opening file " << d << endl;
perror ( d);
pthread_mutex_unlock(&M_cout);
*fd = 0;
return -1;
}
*fd = ifd;
CurrentFilename = d;
PreviousFilename = CurrentFilename;
md5_init(&md5state);
file_is_open =1;
// this is tricky. If we open the very first file, we latch in this event number.
// but if we need to roll over, we find that out when the current buffer is written,
// and the event nr is what's in this buffer, not the last written buffer.
// only for the real start this is right:
if (current_filesequence == 0)
{
Event_number_at_last_open = Event_number;
Event_number_at_last_write = Event_number;
}
daq_generate_json (0); // generate the "new file" report
return 0;
}
int open_file_on_server(const int run_number)
{
if ( file_is_open) return -1;
int status = server_open_Connection();
if (status) return -1;
static char d[1024];
sprintf( d, TheFileRule.c_str(),
run_number, current_filesequence);
status = server_send_beginrun_sequence(d, run_number, TheServerFD);
if ( status)
{
return -1;
}
CurrentFilename = d;
PreviousFilename = CurrentFilename;
file_is_open =1;
return 0;
}
// this function can be called sirectly from daq_end_interactive,
// or we can make a thread for the "immediate" option
void *daq_end_thread (void *arg)
{
std::ostream *os = (std::ostream *) arg;
// with an operator-induced daq_end, we make the event loop terminate
// and wait for it to be done.
disable_trigger();
// it is possible that we call daq_end before we ever started a run
if (ThreadEvt) pthread_join(ThreadEvt, NULL);
daq_end(*os);
DAQ_ENDREQUESTED = 0;
return 0;
}
void *shutdown_thread (void *arg)
{
unsigned long *t_args = (unsigned long *) arg;
pthread_mutex_lock(&M_cout);
cout << "shutting down... " << t_args[0] << " " << t_args[1] << endl;
int pid_fd = t_args[2];
TriggerControl = 0;
if ( TriggerH) delete TriggerH;
pthread_mutex_unlock(&M_cout);
// unregister out service
svc_unregister ( t_args[0], t_args[1]);
flock(pid_fd, LOCK_UN);
unlink(pidfilename);
sleep(2);
exit(0);
}
// this thread is watching for incoming requests for
// monitoring data
std::queue<int> fd_queue; // this queue holds the monitoring requests
void *monitorRequestwatcher_thread (void *arg)
{
struct sockaddr_in server_addr;
int sockfd;
if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0 )
{
pthread_mutex_lock(&M_cout);
cout << "cannot create socket" << endl;
pthread_mutex_unlock(&M_cout);
}
bzero( (char*)&server_addr, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(MONITORINGPORT + servernumber);
int status = bind(sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr));
if (status < 0)
{
perror("bind");
}
pthread_mutex_lock(&M_cout);
cout << "Listening for monitoring requests on port " << MONITORINGPORT + servernumber << endl;
pthread_mutex_unlock(&M_cout);
listen(sockfd, 16);
int dd_fd;
struct sockaddr_in out;
while (sockfd > 0)
{
socklen_t len = sizeof(out);
dd_fd = accept(sockfd, (struct sockaddr *) &out, &len);
if ( dd_fd < 0 )
{
pthread_mutex_lock(&M_cout);
cout << "error in accept socket" << endl;
pthread_mutex_unlock(&M_cout);
}
else
{
char *host = new char[64];
getnameinfo((struct sockaddr *) &out, sizeof(struct sockaddr_in), host, 64,
NULL, 0, NI_NOFQDN);
// time_t x = time(0);
// pthread_mutex_lock(&M_cout);
// cout << ctime(&x) << "new request for monitoring connection accepted from " << host << endl;
// pthread_mutex_unlock(&M_cout);
pthread_mutex_lock(&FdManagementSem);
fd_queue.push(dd_fd);
pthread_mutex_unlock(&FdManagementSem);
//kick off "the sender of monitoring data" thread
pthread_mutex_unlock(&MonitoringRequestSem);
}
}
return 0;
}
void handler ( int sig)
{
//pthread_mutex_lock(&M_cout);
// cout << " in handler..." << endl;
//pthread_mutex_unlock(&M_cout);
pthread_mutex_unlock(&SendProtectSem);
}
void *sendMonitorData( void *arg)
{
int fd;
int status;
while (1)
{
// we wait here until a monitoring request comes
//pthread_mutex_lock(&MonitoringRequestSem);
// when we get here, there are requestst. Now wait for a buffer
// to be available:
// pthread_mutex_lock(&M_cout);
// cout << " locking SendSem " << endl;
// pthread_mutex_unlock(&M_cout);
pthread_mutex_lock( &SendSem);
// Now we go through all requests in the queue
while ( !fd_queue.empty() )
{
// now we are manipulating the queue. To do that, we need to
// lock-protect this:
pthread_mutex_lock(&FdManagementSem);
fd = fd_queue.front();
fd_queue.pop();
pthread_mutex_unlock(&FdManagementSem);
int max_length =0;
// we always wait for a controlword which tells us what to do next.
if ( (status = readn (fd, (char *) &max_length, 4) ) <= 0)
{
max_length = 0;
}
else
{
max_length = ntohl(max_length);
status = transportBuffer->sendData(fd, max_length);
int reply;
if ( ! status && ( status = readn (fd, (char *) &reply, 4) ) <= 0)
{
// pthread_mutex_lock(&M_cout);
// time_t x = time(0);
// cout << ctime(&x) << " connection was broken for fd " << fd << endl;
// pthread_mutex_unlock(&M_cout);
}
else
{
// reply = ntohl(reply);
// pthread_mutex_lock(&M_cout);
// cout << " reply = " << reply << endl;
// pthread_mutex_unlock(&M_cout);
}
}
close(fd);
}
// pthread_mutex_lock(&M_cout);
// cout << " unlocking SendProtectSem " << endl;
// pthread_mutex_unlock(&M_cout);
pthread_mutex_unlock(&SendProtectSem);
if ( end_thread) pthread_exit(0);
}
return 0;
}
void *writebuffers ( void * arg)
{
while(1)
{
pthread_mutex_lock( &WriteSem); // we wait for an unlock
last_bufferwritetime = time(0);
if ( daq_open_flag && outfile_fd)
{
unsigned int bytecount = transportBuffer->writeout(outfile_fd);
NumberWritten++;
BytesInThisRun += bytecount;
BytesInThisFile += bytecount;
}
else if ( daq_server_flag && TheServerFD)
{
unsigned int bytecount = transportBuffer->sendout(TheServerFD);
NumberWritten++;
BytesInThisRun += bytecount;
BytesInThisFile += bytecount;
}
if ( end_thread) pthread_exit(0);
pthread_mutex_unlock(&WriteProtectSem);
}
return 0;
}
int switch_buffer()
{
// pthread_mutex_lock(&M_cout);
// cout << __LINE__ << " " << __FILE__ << " switching buffer" << endl;
// pthread_mutex_unlock(&M_cout);
daqBuffer *spare;
pthread_mutex_lock(&WriteProtectSem);
pthread_mutex_lock(&SendProtectSem);
fillBuffer->addEoB();
//switch buffers
spare = transportBuffer;
transportBuffer = fillBuffer;
fillBuffer = spare;
//+++
fillBuffer->prepare_next(++Buffer_number, TheRun);
// let's see if we need to roll over
if ( daq_open_flag && RolloverLimit)
{
unsigned int blength = transportBuffer->getLength();
if ( blength + BytesInThisFile > RolloverLimit * 1024 * 1024 * 1024)
{
if ( daq_server_flag)
{
current_filesequence++;
static char d[1024];
sprintf( d, TheFileRule.c_str(),
TheRun, current_filesequence);
cout << __FILE__ << " " << __LINE__ << " rolling over " << d << endl;
int status = server_send_rollover_sequence(d,TheServerFD);
CurrentFilename = d;
}
else // not server
{
close(outfile_fd);
file_is_open = 0;
current_filesequence++;
daq_generate_json(1);
Event_number_at_last_open = Event_number_at_last_write;
int status = open_file ( TheRun, &outfile_fd);
if (status)
{
cout << MyHostName << "Could not open output file - Run " << TheRun << " file sequence " << current_filesequence<< endl;
}
}
// cout << MyHostName << " -- Rolling output file over at "
// << transportBuffer->getLength() + BytesInThisFile
// << " sequence: " << current_filesequence
// << " limit: " << RolloverLimit
// << " now: " << CurrentFilename
// << endl;
BytesInThisFile = 0;
}
}
Event_number_at_last_write = Event_number;
pthread_mutex_unlock(&WriteSem);
pthread_mutex_unlock(&SendSem);
return 0;
}
int daq_set_runnumberfile(const char *file, const int flag)
{
if ( flag)
{
TheRunnumberfile.clear();
RunnumberfileIsSet = 0;
return 0;
}
TheRunnumberfile = file;
RunnumberfileIsSet = 1;
FILE *fp = fopen(TheRunnumberfile.c_str(), "r");
int r = 0;
if (fp)
{
int status = fscanf(fp, "%d", &r);
if ( status != 1) r = 0;
if ( ! TheRun )
{
TheRun = r;
}
fclose(fp);
}
return 0;
}
int daq_write_runnumberfile(const int run)
{
if ( !RunnumberfileIsSet ) return 1;
if ( RunControlMode ) return 1;
FILE *fp = fopen(TheRunnumberfile.c_str(), "w");
if (fp )
{
fprintf(fp, "%d\n", run);
fclose(fp);
}
return 0;
}
int daq_set_runnumberApp(const char *file, const int flag)
{
if ( flag)
{
TheRunnumberApp.clear();
RunnumberAppIsSet = 0;
return 0;
}
TheRunnumberApp = file;
RunnumberAppIsSet = 1;
return 0;
}
int getRunNumberFromApp()
{
if ( ! RunnumberAppIsSet) return -1;
FILE *fp = popen(TheRunnumberApp.c_str(),"r");
if (fp == NULL)
{
std::cerr << "error running the runnumber app" << std::endl;
return -1;
}
char in[64];
int len = fread(in, 1, 64, fp);
pclose(fp);
std::stringstream s (in);
int run;
if (! (s >> run) )
{
return -1;
}
return run;
}
int daq_set_filerule(const char *rule , std::ostream& os)
{
if ( DAQ_RUNNING )
{
os << MyHostName << "Run is active" << endl;;
return -1;
}
TheFileRule = rule;
return 0;
}
int daq_set_name(const char *name)
{
MyName = name;
if (servernumber)
{
MyName = MyName + ":" + to_string(servernumber);
}
request_mg_update (MG_REQUEST_NAME);
return 0;
}
#ifdef HAVE_MOSQUITTO_H
int daq_set_mqtt_host(const char * host, const int port, std::ostream& os)
{
std::cout << __FILE__ << " " << __LINE__ << " mqtt host " << host << " port " << port << endl;
if (mqtt) delete mqtt;