MIDAS
Loading...
Searching...
No Matches
tmfe_example_multithread.cxx
Go to the documentation of this file.
1//
2// tmfe_example_multithread.cxx
3//
4// Example tmfe multithreaded 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#include <math.h> // M_PI
13
14#include "midas.h"
15#include "tmfe.h"
16
17class Myfe :
18 public TMFeEquipment
19{
20public:
21 Myfe(const char* eqname, const char* eqfilename) // ctor
23 {
24 }
25
26 ~Myfe() // dtor
27 {
28 }
29
30 void SendData(double dvalue)
31 {
32 char buf[1024];
33 ComposeEvent(buf, sizeof(buf));
34 BkInit(buf, sizeof(buf));
35
36 double* ptr = (double*)BkOpen(buf, "test", TID_DOUBLE);
37 *ptr++ = dvalue;
38 BkClose(buf, ptr);
39
40 EqSendEvent(buf);
41 }
42
43 TMFeResult HandleRpc(const char* cmd, const char* args, std::string& response)
44 {
45 fMfe->Msg(MINFO, "HandleRpc", "Thread %s, RPC cmd [%s], args [%s]", TMFE::GetThreadId().c_str(), cmd, args);
46 return TMFeOk();
47 }
48
50 {
51 fMfe->Msg(MINFO, "HandleBeginRun", "Thread %s, Begin run %d!", TMFE::GetThreadId().c_str(), run_number);
52 EqSetStatus("Running", "#00FF00");
53 return TMFeOk();
54 }
55
57 {
58 fMfe->Msg(MINFO, "HandleEndRun", "Thread %s, End run %d!", TMFE::GetThreadId().c_str(), run_number);
59 EqSetStatus("Stopped", "#00FF00");
60 return TMFeOk();
61 }
62
63 //TMFeResult HandleStartAbortRun(int run_number)
64 //{
65 // fMfe->Msg(MINFO, "HandleStartAbortRun", "Begin run %d aborted!", run_number);
66 // EqSetStatus("Stopped", "#00FF00");
67 // return TMFeOk();
68 //}
69
71 {
72 printf("Thread %s, periodic!\n", TMFE::GetThreadId().c_str());
73 double t = TMFE::GetTime();
74 double data = 100.0*sin(M_PI/2.0+M_PI*t/60);
76 fOdbEqVariables->WD("data", data);
77 char status_buf[256];
78 sprintf(status_buf, "value %.1f", data);
79 EqSetStatus(status_buf, "#00FF00");
80 }
81};
82
83static void usage()
84{
85 fprintf(stderr, "Usage: tmfe_example_mt ...\n");
86 exit(1);
87}
88
89int main(int argc, char* argv[])
90{
93
95
96 std::vector<std::string> eq_args;
97
98 //std::string name = "";
99 //
100 //if (argc == 2) {
101 // name = argv[1];
102 //} else {
103 // usage(); // DOES NOT RETURN
104 //}
105
107
108 TMFeResult result = mfe->Connect("tmfe_example_mt", __FILE__);
109 if (result.error_flag) {
110 fprintf(stderr, "Cannot connect to MIDAS, error \"%s\", bye.\n", result.error_message.c_str());
111 return 1;
112 }
113
114 //mfe->SetWatchdogSec(0);
115
116 TMFeEquipment* eq = new Myfe("tmfe_example_mt", __FILE__);
117 eq->fEqConfEventID = 1;
118 eq->fEqConfLogHistory = 1;
119 eq->fEqConfPeriodMilliSec = 1000; // milliseconds
120 eq->fEqConfBuffer = "SYSTEM";
121
122 eq->EqInit(eq_args);
123 eq->EqSetStatus("Starting...", "white");
124
125 mfe->AddRpcHandler(eq);
126
127 //mfe->SetTransitionSequenceStart(910);
128 //mfe->SetTransitionSequenceStop(90);
129
130 //mfe->DeregisterTransitionPause();
131 //mfe->DeregisterTransitionResume();
132
133 //mfe->RegisterTransitionStartAbort();
134
135 printf("Main thread is %s\n", TMFE::GetThreadId().c_str());
136
137 mfe->StartRpcThread();
138 //mfe->StartPeriodicThread();
139
140 eq->EqSetStatus("Started...", "white");
141
142 while (!mfe->fShutdownRequested) {
143 ::sleep(1);
144 }
145
146 mfe->Disconnect();
147
148 return 0;
149}
150
151/* emacs
152 * Local Variables:
153 * tab-width: 8
154 * c-basic-offset: 3
155 * indent-tabs-mode: nil
156 * End:
157 */
TMFeResult HandleEndRun(int run_number)
TMFE * fMfe
TMFeResult HandleBeginRun(int run_number)
void SendData(double dvalue)
Myfe(const char *eqname, const char *eqfilename)
TMFeResult HandleRpc(const char *cmd, const char *args, std::string &response)
Definition tmfe.h:381
static std::string GetThreadId()
return identification of this thread
Definition tmfe.cxx:1140
static double GetTime()
return current time in seconds, with micro-second precision
Definition tmfe.cxx:1011
void Msg(int message_type, const char *filename, int line, const char *routine, const char *format,...) MATTRPRINTF(6
Definition tmfe.cxx:991
static TMFE * Instance()
Definition tmfe.cxx:57
void * BkOpen(char *pevent, const char *bank_name, int bank_type) const
Definition tmfe.cxx:2201
TMFeResult BkInit(char *pevent, size_t size) const
Definition tmfe.cxx:2195
TMFeResult EqSetStatus(const char *status, const char *color)
Definition tmfe.cxx:2215
MVOdb * fOdbEqVariables
ODB Equipment/EQNAME/Variables.
Definition tmfe.h:214
TMFeResult BkClose(char *pevent, void *ptr) const
Definition tmfe.cxx:2208
TMFeResult ComposeEvent(char *pevent, size_t size) const
Definition tmfe.cxx:1995
TMFeResult EqSendEvent(const char *pevent, bool write_to_odb=true)
Definition tmfe.cxx:2006
bool error_flag
Definition tmfe.h:89
std::string error_message
Definition tmfe.h:91
#define TID_DOUBLE
Definition midas.h:343
#define MINFO
Definition midas.h:560
int main()
Definition hwtest.cxx:23
INT run_number[2]
Definition mana.cxx:246
void * data
Definition mana.cxx:268
std::vector< FMT_ID > eq
Definition mdump.cxx:55
char response[10000]
Definition melog.cxx:90
#define sleep(ms)
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
TMFeResult TMFeOk()
Definition tmfe.h:106
static void usage()