MIDAS
Loading...
Searching...
No Matches
TMidasOnline.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: TMidasOnline.cxx
4 Created by: Konstantin Olchanski - TRIUMF
5
6 Contents: C++ MIDAS analyzer
7
8 $Id$
9
10 $Log: TMidasOnline.cxx,v $
11 Revision 1.3 2006/08/07 09:19:51 alpha
12 RAH - changes made to the connection routine (by KO)
13
14 Revision 1.2 2006/06/05 19:22:22 alpha
15 KO- added functions to read values from ODB
16
17 Revision 1.1 2006/05/25 05:58:05 alpha
18 First commit
19
20
21\********************************************************************/
22
23#include "TMidasOnline.h"
24
25#include <string>
26#include <assert.h>
27#include "midas.h"
28#include "msystem.h"
29#include "hardware.h"
30#include "ybos.h"
31
32
33TMidasOnline::TMidasOnline() // ctor
34{
35 fDB = 0;
36 fStartHandler = 0;
37 fStopHandler = 0;
38 fPauseHandler = 0;
41 fEventHandler = 0;
42}
43
44TMidasOnline::~TMidasOnline() // dtor
45{
46 disconnect();
47}
48
49TMidasOnline* TMidasOnline::instance()
50{
51 if (!gfMidas)
52 gfMidas = new TMidasOnline();
53
54 return gfMidas;
55}
56
57int TMidasOnline::connect(const char*hostname,const char*exptname,const char*progname)
58{
59 int status;
60
63
64 if (hostname)
65 strlcpy(xhostname,hostname,sizeof(xhostname));
66 else
67 xhostname[0] = 0;
68
69 if (exptname)
71 else
72 xexptname[0] = 0;
73
74 /* get default from environment */
76 assert(status == CM_SUCCESS);
77
78 fHostname = xhostname;
79 fExptname = xexptname;
80
81 printf("TMidasOnline::connect: Connecting to experiment \"%s\" on host \"%s\"\n", fExptname.c_str(), fHostname.c_str());
82
83 //int watchdog = DEFAULT_WATCHDOG_TIMEOUT;
84 int watchdog = 60*1000;
85
86 status = cm_connect_experiment1((char*)fHostname.c_str(), (char*)fExptname.c_str(), (char*)progname, NULL, DEFAULT_ODB_SIZE, watchdog);
87
88 if (status == CM_UNDEF_EXP)
89 {
90 printf("Error: experiment \"%s\" not defined.\n", fExptname.c_str());
91 return -1;
92 }
93 else if (status != CM_SUCCESS)
94 {
95 printf("Error %d connecting to MIDAS.\n", status);
96 return -1;
97 }
98
100 assert(status == CM_SUCCESS);
101
102 cm_set_watchdog_params(true, 60*1000);
103
104 return 0;
105}
106
107int TMidasOnline::disconnect()
108{
109 if (fDB)
110 {
111 printf("TMidasOnline::disconnect: Disconnecting from experiment \"%s\" on host \"%s\"\n", fExptname.c_str(), fHostname.c_str());
113 fDB = 0;
114 }
115
116 return 0;
117}
118
119void TMidasOnline::registerTransitions()
120{
125}
126
127void TMidasOnline::setTransitionHandlers(TransitionHandler start,TransitionHandler stop,TransitionHandler pause,TransitionHandler resume)
128{
129 fStartHandler = start;
133}
134
135bool TMidasOnline::checkTransitions()
136{
137 int transition, run_number, trans_time;
138
139 int status = cm_query_transition(&transition, &run_number, &trans_time);
140 if (status != CM_SUCCESS)
141 return false;
142
143 //printf("cm_query_transition: status %d, tr %d, run %d, time %d\n",status,transition,run_number,trans_time);
144
145 if (transition == TR_START)
146 {
147 if (fStartHandler)
148 (*fStartHandler)(transition,run_number,trans_time);
149 return true;
150 }
151 else if (transition == TR_STOP)
152 {
153 if (fStopHandler)
154 (*fStopHandler)(transition,run_number,trans_time);
155 return true;
156
157 }
158 else if (transition == TR_PAUSE)
159 {
160 if (fPauseHandler)
161 (*fPauseHandler)(transition,run_number,trans_time);
162 return true;
163
164 }
165 else if (transition == TR_RESUME)
166 {
167 if (fResumeHandler)
168 (*fResumeHandler)(transition,run_number,trans_time);
169 return true;
170 }
171
172 return false;
173}
174
175bool TMidasOnline::poll(int mdelay)
176{
177 //printf("poll!\n");
178
179 if (checkTransitions())
180 return true;
181
182 int status = cm_yield(mdelay);
183 if (status == RPC_SHUTDOWN || status == SS_ABORT)
184 {
185 printf("TMidasOnline: poll: cm_yield(%d) status %d, shutting down.\n",mdelay,status);
186 disconnect();
187 return false;
188 }
189
190 return true;
191}
192
193void TMidasOnline::setEventHandler(EventHandler handler)
194{
195 fEventHandler = handler;
196}
197
198static void eventCallback(HNDLE buffer_handle, HNDLE request_id, EVENT_HEADER* pheader, void* pevent)
199{
200#if 0
201 printf("eventCallback: buffer %d, request %d, pheader %p (event_id: %d, trigger mask: 0x%x, serial: %d, time: %d, size: %d), pevent %p\n",
202 buffer_handle,
203 request_id,
204 pheader,
205 pheader->event_id,
206 pheader->trigger_mask,
207 pheader->serial_number,
208 pheader->time_stamp,
209 pheader->data_size,
210 pevent);
211#endif
212
213 if (TMidasOnline::instance()->fEventHandler)
214 TMidasOnline::instance()->fEventHandler(pheader,pevent,pheader->data_size);
215}
216
217int TMidasOnline::eventRequest(const char* bufferName,int eventId,int triggerMask,int samplingType)
218{
219 int status;
220 EventRequest* r = new EventRequest();
221
222 r->fNext = NULL;
223 r->fBufferName = bufferName;
224 r->fEventId = eventId;
225 r->fTriggerMask = triggerMask;
226 r->fSamplingType = samplingType;
227
228
229 /*---- open event buffer ---------------------------------------*/
230 status = bm_open_buffer((char*)bufferName, EVENT_BUFFER_SIZE, &r->fBufferHandle);
231 if (status != SUCCESS)
232 {
233 printf("TMidasOnline::eventRequest: Cannot find data buffer \"%s\", bm_open_buffer() error %d\n", bufferName, status);
234 return -1;
235 }
236
237 /* set the default buffer cache size */
238 status = bm_set_cache_size(r->fBufferHandle, 100000, 0);
239 assert(status == BM_SUCCESS);
240
241 status = bm_request_event(r->fBufferHandle, r->fEventId, r->fTriggerMask, r->fSamplingType, &r->fRequestId, eventCallback);
242 assert(status == BM_SUCCESS);
243
244 printf("Event request: buffer \"%s\" (%d), event id 0x%x, trigger mask 0x%x, sample %d, request id: %d\n",bufferName,r->fBufferHandle,r->fEventId,r->fTriggerMask,r->fSamplingType,r->fRequestId);
245
246 r->fNext = fEventRequests;
247 fEventRequests = r;
248
249 return r->fRequestId;
250};
251
252void TMidasOnline::deleteEventRequest(int requestId)
253{
254 for (EventRequest* r = fEventRequests; r != NULL; r = r->fNext)
255 if (r->fRequestId == requestId)
256 {
257 int status = bm_delete_request(r->fRequestId);
258 assert(status == BM_SUCCESS);
259
260 r->fBufferHandle = -1;
261 r->fRequestId = -1;
262 }
263}
264
265int TMidasOnline::odbReadInt(const char*name,int index,int defaultValue)
266{
267 int value = defaultValue;
268 if (odbReadAny(name,index,TID_INT,&value) == 0)
269 return value;
270 else
271 return defaultValue;
272};
273
274uint32_t TMidasOnline::odbReadUint32(const char*name,int index,uint32_t defaultValue)
275{
278 return value;
279 else
280 return defaultValue;
281};
282
283bool TMidasOnline::odbReadBool(const char*name,int index,bool defaultValue)
284{
285 bool value = defaultValue;
287 return value;
288 else
289 return defaultValue;
290};
291
292int TMidasOnline::odbReadAny(const char*name,int index,int tid,void* value)
293{
294 int status;
295 int size = rpc_tid_size(tid);
296 HNDLE hdir = 0;
297 HNDLE hkey;
298
299 status = db_find_key (fDB, hdir, (char*)name, &hkey);
300 if (status == SUCCESS)
301 {
302 status = db_get_data_index(fDB, hkey, value, &size, index, tid);
303 if (status != SUCCESS)
304 {
305 cm_msg(MERROR, "TMidasOnline", "Cannot read \'%s\'[%d] of type %d from odb, db_get_data_index() status %d", name, index, tid, status);
306 return -1;
307 }
308
309 return 0;
310 }
311 else if (status == DB_NO_KEY)
312 {
313 cm_msg(MINFO, "TMidasOnline", "Creating \'%s\'[%d] of type %d", name, index, tid);
314
315 status = db_create_key(fDB, hdir, (char*)name, tid);
316 if (status != SUCCESS)
317 {
318 cm_msg (MERROR, "TMidasOnline", "Cannot create \'%s\' of type %d, db_create_key() status %d", name, tid, status);
319 return -1;
320 }
321
322 status = db_find_key (fDB, hdir, (char*)name, &hkey);
323 if (status != SUCCESS)
324 {
325 cm_msg(MERROR, "TMidasOnline", "Cannot create \'%s\', db_find_key() status %d", name, status);
326 return -1;
327 }
328
329 status = db_set_data_index(fDB, hkey, value, size, index, tid);
330 if (status != SUCCESS)
331 {
332 cm_msg(MERROR, "TMidasOnline", "Cannot write \'%s\'[%d] of type %d to odb, db_set_data_index() status %d", name, index, tid, status);
333 return -1;
334 }
335
336 return 0;
337 }
338 else
339 {
340 cm_msg(MERROR, "TMidasOnline", "Cannot read \'%s\'[%d] from odb, db_find_key() status %d", name, index, status);
341 return -1;
342 }
343};
344
345TMidasOnline* TMidasOnline::gfMidas = NULL;
346
347//end
static void eventCallback(HNDLE buffer_handle, HNDLE request_id, EVENT_HEADER *pheader, void *pevent)
INT transition(INT run_number, char *error)
Definition consume.cxx:35
INT bm_open_buffer(const char *buffer_name, INT buffer_size, INT *buffer_handle)
Definition midas.cxx:6717
INT bm_delete_request(INT request_id)
Definition midas.cxx:8584
INT bm_request_event(HNDLE buffer_handle, short int event_id, short int trigger_mask, INT sampling_type, HNDLE *request_id, EVENT_HANDLER *func)
Definition midas.cxx:8465
INT bm_set_cache_size(INT buffer_handle, size_t read_size, size_t write_size)
Definition midas.cxx:8140
INT cm_register_transition(INT transition, INT(*func)(INT, char *), INT sequence_number)
Definition midas.cxx:3593
INT cm_yield(INT millisec)
Definition midas.cxx:5642
INT cm_get_experiment_database(HNDLE *hDB, HNDLE *hKeyClient)
Definition midas.cxx:3011
INT cm_connect_experiment1(const char *host_name, const char *default_exp_name, const char *client_name, void(*func)(char *), INT odb_size, DWORD watchdog_timeout)
Definition midas.cxx:2297
INT cm_disconnect_experiment(void)
Definition midas.cxx:2846
INT cm_get_environment(char *host_name, int host_name_size, char *exp_name, int exp_name_size)
Definition midas.cxx:2134
INT cm_set_watchdog_params(BOOL call_watchdog, DWORD timeout)
Definition midas.cxx:3283
#define CM_SUCCESS
Definition midas.h:582
#define CM_UNDEF_EXP
Definition midas.h:586
#define BM_SUCCESS
Definition midas.h:605
#define DB_NO_KEY
Definition midas.h:642
#define SS_ABORT
Definition midas.h:677
#define RPC_SHUTDOWN
Definition midas.h:707
#define SUCCESS
Definition mcstd.h:54
#define TR_RESUME
Definition midas.h:408
#define TR_PAUSE
Definition midas.h:407
#define TID_BOOL
Definition midas.h:340
#define TR_START
Definition midas.h:405
#define MINFO
Definition midas.h:560
#define MERROR
Definition midas.h:559
#define TID_INT
Definition midas.h:338
#define TR_STOP
Definition midas.h:406
#define TID_DWORD
Definition midas.h:336
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
Definition midas.cxx:915
INT db_get_data_index(HNDLE hDB, HNDLE hKey, void *data, INT *buf_size, INT idx, DWORD type)
Definition odb.cxx:6893
INT db_create_key(HNDLE hDB, HNDLE hKey, const char *key_name, DWORD type)
Definition odb.cxx:3308
INT db_set_data_index(HNDLE hDB, HNDLE hKey, const void *data, INT data_size, INT idx, DWORD type)
Definition odb.cxx:7648
INT db_find_key(HNDLE hDB, HNDLE hKey, const char *key_name, HNDLE *subhKey)
Definition odb.cxx:4079
int cm_query_transition(int *transition, int *run_number, int *trans_time)
Definition midas.cxx:14120
INT rpc_tid_size(INT id)
Definition midas.cxx:11757
INT run_number[2]
Definition mana.cxx:246
INT index
Definition mana.cxx:271
INT HNDLE
Definition midas.h:132
#define HOST_NAME_LENGTH
Definition midas.h:273
#define DEFAULT_ODB_SIZE
Definition midas.h:270
#define NAME_LENGTH
Definition midas.h:272
#define name(x)
Definition midas_macro.h:24
double value[100]
Definition odbhist.cxx:42
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
short int event_id
Definition midas.h:852
DWORD data_size
Definition midas.h:856
DWORD serial_number
Definition midas.h:854
DWORD time_stamp
Definition midas.h:855
short int trigger_mask
Definition midas.h:853