MIDAS
Loading...
Searching...
No Matches
tmfe_example_everything.cxx
Go to the documentation of this file.
1/*******************************************************************\
2
3 Name: tmfe_example_everything.cxx
4 Created by: K.Olchanski
5
6 Contents: Example Front end to demonstrate all functions of TMFE class
7
8\********************************************************************/
9
10#undef NDEBUG // midas required assert() to be always enabled
11
12#include <stdio.h>
13#include <math.h> // M_PI
14
15#include "tmfe.h"
16
18 public TMFeEquipment
19{
20public:
21 EqEverything(const char* eqname, const char* eqfilename) // ctor
22 : TMFeEquipment(eqname, eqfilename)
23 {
24 printf("EqEverything::ctor!\n");
25
26 // configure the equipment here:
27
28 //fEqConfReadConfigFromOdb = false;
33 fEqConfEnablePoll = true; // enable polled equipment
34 //fEqConfPollSleepSec = 0; // to create a "100% CPU busy" polling loop, set poll sleep time to zero
35 }
36
37 ~EqEverything() // dtor
38 {
39 printf("EqEverything::dtor!\n");
40 }
41
43 {
44 printf("EqEverything::HandleUsage!\n");
45 }
46
47 TMFeResult HandleInit(const std::vector<std::string>& args)
48 {
49 printf("EqEverything::HandleInit!\n");
51 fEqConfReadOnlyWhenRunning = false; // overwrite ODB Common RO_RUNNING to false
52 fEqConfWriteEventsToOdb = true; // overwrite ODB Common RO_ODB to true
53 EqSetStatus("Started...", "white");
54 //EqStartPollThread();
55 return TMFeOk();
56 }
57
58 TMFeResult HandleRpc(const char* cmd, const char* args, std::string& response)
59 {
60 fMfe->Msg(MINFO, "HandleRpc", "RPC cmd [%s], args [%s]", cmd, args);
61
62 // RPC handler
63
64 char tmp[256];
65 time_t now = time(NULL);
66 snprintf(tmp, sizeof(tmp), "{ \"current_time\" : [ %d, \"%s\"] }", (int)now, ctime(&now));
67
68 response = tmp;
69
70 return TMFeOk();
71 }
72
73 TMFeResult HandleBinaryRpc(const char* cmd, const char* args, std::vector<char>& response)
74 {
75 fMfe->Msg(MINFO, "HandleBinaryRpc", "Binary RPC cmd [%s], args [%s]", cmd, args);
76
77 // RPC handler
78
79 response.resize(8*64);
80
81 uint64_t* p64 = (uint64_t*)response.data();
82 uint64_t one = 1;
83
84 for (size_t i=0; i<response.size()/sizeof(p64[0]); i++) {
85 *p64++ = (one<<i);
86 }
87
88 return TMFeOk();
89 }
90
91
93 {
94 fMfe->Msg(MINFO, "HandleBeginRun", "Begin run %d!", run_number);
95 EqSetStatus("Running", "#00FF00");
96 return TMFeOk();
97 }
98
100 {
101 fMfe->Msg(MINFO, "HandleEndRun", "End run %d!", run_number);
102 EqSetStatus("Stopped", "#FFFFFF");
103 return TMFeOk();
104 }
105
107 {
108 fMfe->Msg(MINFO, "HandlePauseRun", "Pause run %d!", run_number);
109 EqSetStatus("Paused", "#FFFF00");
110 return TMFeOk();
111 }
112
114 {
115 fMfe->Msg(MINFO, "HandleResumeRun", "Resume run %d!", run_number);
116 EqSetStatus("Running", "#00FF00");
117 return TMFeOk();
118 }
119
121 {
122 fMfe->Msg(MINFO, "HandleStartAbortRun", "Begin run %d aborted!", run_number);
123 EqSetStatus("Stopped", "#FFFFFF");
124 return TMFeOk();
125 }
126
127 void SendData(double dvalue)
128 {
129 char buf[1024];
130 ComposeEvent(buf, sizeof(buf));
131 BkInit(buf, sizeof(buf));
132
133 double* ptr = (double*)BkOpen(buf, "data", TID_DOUBLE);
134 *ptr++ = dvalue;
135 BkClose(buf, ptr);
136
137 EqSendEvent(buf);
138 }
139
141 {
142 printf("EqEverything::HandlePeriodic!\n");
143 double t = TMFE::GetTime();
144 double data = 100.0*sin(0*M_PI/2.0+M_PI*t/40);
145 SendData(data);
146 char status_buf[256];
147 snprintf(status_buf, sizeof(status_buf), "value %.1f", data);
148 EqSetStatus(status_buf, "#00FF00");
149 }
150
152 {
153 //printf("EqEverything::HandlePoll!\n");
154
155 if (!fMfe->fStateRunning) // only poll when running
156 return false;
157
158 double r = drand48();
159 if (r > 0.999) {
160 // return successful poll rarely
161 printf("EqEverything::HandlePoll!\n");
162 return true;
163 }
164
165 return false;
166 }
167
169 {
170 printf("EqEverything::HandlePollRead!\n");
171
172 char buf[1024];
173
174 ComposeEvent(buf, sizeof(buf));
175 BkInit(buf, sizeof(buf));
176
177 uint32_t* ptr = (uint32_t*)BkOpen(buf, "poll", TID_UINT32);
178 for (int i=0; i<16; i++) {
179 *ptr++ = lrand48();
180 }
181 BkClose(buf, ptr);
182
183 EqSendEvent(buf, false); // do not write polled data to ODB and history
184 }
185};
186
187// example frontend
188
190{
191public:
192 FeEverything() // ctor
193 {
194 printf("FeEverything::ctor!\n");
195 FeSetName("tmfe_example_everything");
196 FeAddEquipment(new EqEverything("tmfe_example_everything", __FILE__));
197 }
198
200 {
201 printf("FeEverything::HandleUsage!\n");
202 };
203
204 TMFeResult HandleArguments(const std::vector<std::string>& args)
205 {
206 printf("FeEverything::HandleArguments!\n");
207 return TMFeOk();
208 };
209
210 TMFeResult HandleFrontendInit(const std::vector<std::string>& args)
211 {
212 printf("FeEverything::HandleFrontendInit!\n");
213 return TMFeOk();
214 };
215
216 TMFeResult HandleFrontendReady(const std::vector<std::string>& args)
217 {
218 printf("FeEverything::HandleFrontendReady!\n");
219 //FeStartPeriodicThread();
220 //fMfe->StartRpcThread();
221 return TMFeOk();
222 };
223
225 {
226 printf("FeEverything::HandleFrontendExit!\n");
227 };
228};
229
230// boilerplate main function
231
232int main(int argc, char* argv[])
233{
234 FeEverything fe_everything;
235 return fe_everything.FeMain(argc, argv);
236}
237
238/* emacs
239 * Local Variables:
240 * tab-width: 8
241 * c-basic-offset: 3
242 * indent-tabs-mode: nil
243 * End:
244 */
TMFeResult HandleInit(const std::vector< std::string > &args)
EqEverything(const char *eqname, const char *eqfilename)
TMFeResult HandleRpc(const char *cmd, const char *args, std::string &response)
TMFeResult HandleBeginRun(int run_number)
TMFeResult HandleResumeRun(int run_number)
void SendData(double dvalue)
TMFeResult HandlePauseRun(int run_number)
TMFeResult HandleEndRun(int run_number)
TMFeResult HandleStartAbortRun(int run_number)
TMFeResult HandleBinaryRpc(const char *cmd, const char *args, std::vector< char > &response)
TMFeResult HandleArguments(const std::vector< std::string > &args)
TMFeResult HandleFrontendInit(const std::vector< std::string > &args)
TMFeResult HandleFrontendReady(const std::vector< std::string > &args)
bool fStateRunning
run state is running or paused
Definition tmfe.h:404
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
void RegisterTransitionStartAbort()
Definition tmfe.cxx:1562
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
TMFE * fMfe
Definition tmfe.h:208
TMFeResult EqSetStatus(const char *status, const char *color)
Definition tmfe.cxx:2272
bool fEqConfWriteEventsToOdb
Definition tmfe.h:198
uint16_t fEqConfEventID
Definition tmfe.h:172
int fEqConfPeriodMilliSec
Definition tmfe.h:179
TMFeResult BkClose(char *pevent, void *ptr) const
Definition tmfe.cxx:2265
int fEqConfLogHistory
Definition tmfe.h:182
TMFeResult ComposeEvent(char *pevent, size_t size) const
Definition tmfe.cxx:2052
TMFeResult EqSendEvent(const char *pevent, bool write_to_odb=true)
Definition tmfe.cxx:2063
bool fEqConfEnablePoll
Definition tmfe.h:167
bool fEqConfReadOnlyWhenRunning
Definition tmfe.h:197
void FeSetName(const char *program_name)
Definition tmfe.cxx:1704
TMFeResult FeAddEquipment(TMFeEquipment *eq)
Definition tmfe.cxx:1657
int FeMain(int argc, char *argv[])
Definition tmfe.cxx:2336
#define TID_DOUBLE
Definition midas.h:343
#define MINFO
Definition midas.h:560
#define TID_UINT32
Definition midas.h:337
int main()
Definition hwtest.cxx:23
INT run_number[2]
Definition mana.cxx:246
void * data
Definition mana.cxx:268
INT i
Definition mdump.cxx:32
char response[10000]
Definition melog.cxx:90
#define ctime
Definition msystem.h:271
TMFeResult TMFeOk()
Definition tmfe.h:106