MIDAS
Loading...
Searching...
No Matches
fetest_tmfe_thread.cxx
Go to the documentation of this file.
1//
2// fetest_tmfe_thread.cxx
3//
4// Frontend for test and example of multithreaded tmfe c++ frontend
5//
6
7#include <stdio.h>
8#include <signal.h> // SIGPIPE
9#include <assert.h> // assert()
10#include <stdlib.h> // malloc()
11#include <unistd.h> // sleep()
12
13#include "midas.h"
14#include "tmfe.h"
15
16class Myfe :
19{
20public:
21 TMFE* fMfe;
23
24 int fEventSize;
25 char* fEventBuf;
26
27 Myfe(TMFE* mfe, TMFeEquipment* eq) // ctor
28 {
29 fMfe = mfe;
30 fEq = eq;
31 fEventSize = 0;
32 fEventBuf = NULL;
33 }
34
35 ~Myfe() // dtor
36 {
37 if (fEventBuf) {
38 free(fEventBuf);
39 fEventBuf = NULL;
40 }
41 }
42
43 void Init()
44 {
45 fEventSize = 100;
46 fEq->fOdbEqSettings->RI("event_size", &fEventSize, true);
47 if (fEventBuf) {
48 free(fEventBuf);
49 }
50 fEventBuf = (char*)malloc(fEventSize);
51 }
52
53 void SendEvent(double dvalue)
54 {
57
58 double* ptr = (double*)fEq->BkOpen(fEventBuf, "test", TID_DOUBLE);
59 *ptr = dvalue;
60 fEq->BkClose(fEventBuf, ptr+1);
61
63 }
64
65 std::string HandleRpc(const char* cmd, const char* args)
66 {
67 fMfe->Msg(MINFO, "HandleRpc", "Thread %s, RPC cmd [%s], args [%s]", TMFE::GetThreadId().c_str(), cmd, args);
68 return "OK";
69 }
70
72 {
73 fMfe->Msg(MINFO, "HandleBeginRun", "Thread %s, Begin run!", TMFE::GetThreadId().c_str());
74 fEq->SetStatus("Running", "#00FF00");
75 }
76
78 {
79 fMfe->Msg(MINFO, "HandleEndRun", "Thread %s, End run!", TMFE::GetThreadId().c_str());
80 fEq->SetStatus("Stopped", "#00FF00");
81 }
82
84 {
85 printf("Thread %s, periodic!\n", TMFE::GetThreadId().c_str());
86 //char buf[256];
87 //sprintf(buf, "buffered %d (max %d), dropped %d, unknown %d, max flushed %d", gUdpPacketBufSize, fMaxBuffered, fCountDroppedPackets, fCountUnknownPackets, fMaxFlushed);
88 //fEq->SetStatus(buf, "#00FF00");
89 //fEq->WriteStatistics();
90 }
91};
92
93static void usage()
94{
95 fprintf(stderr, "Usage: fetest_tmfe_thread ...\n");
96 exit(1);
97}
98
99int main(int argc, char* argv[])
100{
101 setbuf(stdout, NULL);
102 setbuf(stderr, NULL);
103
104 signal(SIGPIPE, SIG_IGN);
105
106 std::string name = "";
107
108 if (argc == 2) {
109 name = argv[1];
110 } else {
111 usage(); // DOES NOT RETURN
112 }
113
114 TMFE* mfe = TMFE::Instance();
115
116 TMFeError err = mfe->Connect("fetest_tmfe_thread", __FILE__);
117 if (err.error) {
118 printf("Cannot connect, bye.\n");
119 return 1;
120 }
121
122 //mfe->SetWatchdogSec(0);
123
124 TMFeCommon *common = new TMFeCommon();
125 common->EventID = 1;
126 common->LogHistory = 1;
127 //common->Buffer = "SYSTEM";
128
129 TMFeEquipment* eq = new TMFeEquipment(mfe, "tmfe", common);
130 eq->Init();
131 eq->SetStatus("Starting...", "white");
132 eq->ZeroStatistics();
133 eq->WriteStatistics();
134
135 mfe->RegisterEquipment(eq);
136
137 Myfe* myfe = new Myfe(mfe, eq);
138
139 mfe->RegisterRpcHandler(myfe);
140
141 //mfe->SetTransitionSequenceStart(910);
142 //mfe->SetTransitionSequenceStop(90);
143 //mfe->DeregisterTransitionPause();
144 //mfe->DeregisterTransitionResume();
145
146 myfe->Init();
147
148 mfe->RegisterPeriodicHandler(eq, myfe);
149
150 printf("Main thread is %s\n", TMFE::GetThreadId().c_str());
151
152 mfe->StartRpcThread();
153 mfe->StartPeriodicThread();
154
155 eq->SetStatus("Started...", "white");
156
157 while (!mfe->fShutdownRequested) {
158 ::sleep(1);
159 }
160
161 mfe->Disconnect();
162
163 return 0;
164}
165
166/* emacs
167 * Local Variables:
168 * tab-width: 8
169 * c-basic-offset: 3
170 * indent-tabs-mode: nil
171 * End:
172 */
Myfe(TMFE *mfe, TMFeEquipment *eq)
TMFeEquipment * fEq
void SendEvent(double dvalue)
char * fEventBuf
TMFE * fMfe
int fEventSize
void HandleBeginRun()
void HandlePeriodic()
std::string HandleRpc(const char *cmd, const char *args)
void HandleEndRun()
Definition tmfe.h:381
void RegisterRpcHandler(TMFeRpcHandlerInterface *handler)
RPC handlers are executed from the RPC thread, if started.
static std::string GetThreadId()
return identification of this thread
Definition tmfe.cxx:1140
void StartPeriodicThread()
void Msg(int message_type, const char *filename, int line, const char *routine, const char *format,...) MATTRPRINTF(6
Definition tmfe.cxx:991
void StartRpcThread()
Definition tmfe.cxx:903
TMFeResult Connect(const char *progname=NULL, const char *hostname=NULL, const char *exptname=NULL)
Definition tmfe.cxx:65
static TMFE * Instance()
Definition tmfe.cxx:57
std::atomic_bool fShutdownRequested
shutdown was requested by Ctrl-C or by RPC command
Definition tmfe.h:398
void RegisterPeriodicHandler(TMFeEquipment *eq, TMFePeriodicHandlerInterface *handler)
periodic handlers are executed from the periodic thread, if started
TMFeResult Disconnect()
Definition tmfe.cxx:154
TMFeError RegisterEquipment(TMFeEquipment *eq)
uint16_t EventID
Definition tmfe_rev0.h:105
int LogHistory
Definition tmfe_rev0.h:116
void * BkOpen(char *pevent, const char *bank_name, int bank_type) const
Definition tmfe.cxx:2258
TMFeResult BkInit(char *pevent, size_t size) const
Definition tmfe.cxx:2252
MVOdb * fOdbEqSettings
ODB Equipment/EQNAME/Settings.
Definition tmfe.h:213
TMFeResult BkClose(char *pevent, void *ptr) const
Definition tmfe.cxx:2265
TMFeResult ComposeEvent(char *pevent, size_t size) const
Definition tmfe.cxx:2052
TMFeError SendEvent(const char *pevent)
TMFeError SetStatus(const char *status, const char *color)
int error
Definition tmfe_rev0.h:87
static void usage()
#define TID_DOUBLE
Definition midas.h:343
#define MINFO
Definition midas.h:560
int main()
Definition hwtest.cxx:23
std::vector< FMT_ID > eq
Definition mdump.cxx:55
#define sleep(ms)
#define name(x)
Definition midas_macro.h:24