37 assert(!
"TMidasOnline::~TMidasOnline(): destruction of the TMidasOnline singleton is not permitted!");
52 char xhostname[HOST_NAME_LENGTH];
53 char xexptname[NAME_LENGTH];
56 status = cm_get_environment(xhostname,
sizeof(xhostname), xexptname,
sizeof(xexptname));
57 assert(status == CM_SUCCESS);
60 strlcpy(xhostname,hostname,
sizeof(xhostname));
63 strlcpy(xexptname,exptname,
sizeof(xexptname));
68 fprintf(stderr,
"TMidasOnline::connect: Connecting to experiment \"%s\" on host \"%s\"\n",
fExptname.c_str(),
fHostname.c_str());
71 int watchdog = 60*1000;
73 status = cm_connect_experiment1((
char*)
fHostname.c_str(), (
char*)
fExptname.c_str(), (
char*)progname, NULL, DEFAULT_ODB_SIZE, watchdog);
75 if (status == CM_UNDEF_EXP)
77 fprintf(stderr,
"TMidasOnline::connect: Error: experiment \"%s\" not defined.\n",
fExptname.c_str());
80 else if (status != CM_SUCCESS)
82 fprintf(stderr,
"TMidasOnline::connect: Cannot connect to MIDAS, status %d.\n", status);
86 status = cm_get_experiment_database(&
fDB, NULL);
87 assert(status == CM_SUCCESS);
89 cm_set_watchdog_params(
true, 60*1000);
98 fprintf(stderr,
"TMidasOnline::disconnect: Disconnecting from experiment \"%s\" on host \"%s\"\n",
fExptname.c_str(),
fHostname.c_str());
99 cm_disconnect_experiment();
108 cm_register_transition(TR_START, NULL, 300);
109 cm_register_transition(TR_PAUSE, NULL, 700);
110 cm_register_transition(TR_RESUME, NULL, 300);
111 cm_register_transition(TR_STOP, NULL, 700);
124 int transition, run_number, trans_time;
126 int status = cm_query_transition(&transition, &run_number, &trans_time);
127 if (status != CM_SUCCESS)
132 for (
unsigned i=0; i<
fHandlers.size(); i++)
133 fHandlers[i]->Transition(transition, run_number, trans_time);
135 if (transition == TR_START)
138 (*fStartHandler)(transition,run_number,trans_time);
141 else if (transition == TR_STOP)
144 (*fStopHandler)(transition,run_number,trans_time);
148 else if (transition == TR_PAUSE)
151 (*fPauseHandler)(transition,run_number,trans_time);
155 else if (transition == TR_RESUME)
158 (*fResumeHandler)(transition,run_number,trans_time);
172 int status = cm_yield(mdelay);
173 if (status == RPC_SHUTDOWN || status == SS_ABORT)
175 fprintf(stderr,
"TMidasOnline::poll: cm_yield(%d) status %d, shutting down.\n",mdelay,status);
190 int status = ss_suspend(mdelay, MSG_BM);
191 if (status == SS_SUCCESS)
193 if (status == SS_TIMEOUT)
196 if (status == SS_SERVER_RECV) {
202 fprintf(stderr,
"TMidasOnline::sleep(): Unexpected ss_suspend() status %d\n", status);
204 if (status == RPC_SHUTDOWN || status == SS_ABORT)
206 fprintf(stderr,
"TMidasOnline::poll: cm_yield(%d) status %d, shutting down.\n",mdelay,status);
220static void eventCallback(HNDLE buffer_handle, HNDLE request_id, EVENT_HEADER* pheader,
void* pevent)
223 printf(
"eventCallback: buffer %d, request %d, pheader %p (event_id: %d, trigger mask: 0x%x, serial: %d, time: %d, size: %d), pevent %p\n",
228 pheader->trigger_mask,
229 pheader->serial_number,
237 for (
unsigned i=0; i<midas->
fHandlers.size(); i++)
238 midas->
fHandlers[i]->Event(pheader,
sizeof(EVENT_HEADER) + pheader->data_size);
252 fprintf(stderr,
"TMidasOnline::receiveEvent: Cannot find request %d\n", requestId);
272 int status = bm_receive_event(r->
fBufferHandle, pevent, &size, flag);
274 if (status == BM_ASYNC_RETURN)
279 if (status != BM_SUCCESS)
281 fprintf(stderr,
"TMidasOnline::receiveEvent: bm_receive_event() error %d\n", status);
288#ifndef EVENT_BUFFER_SIZE
289#define EVENT_BUFFER_SIZE 0
297 if (bufferName == NULL)
298 bufferName = EVENT_BUFFER_NAME;
309 if (status!=SUCCESS && status!=BM_CREATED)
311 fprintf(stderr,
"TMidasOnline::eventRequest: Cannot find data buffer \"%s\", bm_open_buffer() error %d\n", bufferName, status);
316 if(samplingType != GET_RECENT){
318 assert(status == BM_SUCCESS);
325 assert(status == BM_SUCCESS);
327 fprintf(stderr,
"TMidasOnline::eventRequest: 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);
350 BUFFER_HEADER buffer_header;
353 return buffer_header.size;
359 if (r->fRequestId == requestId)
361 int status = bm_delete_request(r->fRequestId);
362 assert(status == BM_SUCCESS);
364 r->fBufferHandle = -1;
static void eventCallback(HNDLE buffer_handle, HNDLE request_id, EVENT_HEADER *pheader, void *pevent)
#define EVENT_BUFFER_SIZE
MIDAS online connection, including access to online ODB.
virtual ~TMHandlerInterface()
std::vector< TMHandlerInterface * > fHandlers
void RegisterHandler(TMHandlerInterface *h)
void setEventHandler(EventHandler handler)
Specify user handler for data events.
int receiveEvent(int requestId, void *pevent, int size, bool async)
Receive event by polling.
void registerTransitions()
Ask MIDAS to tell us about run transitions.
bool checkTransitions()
Check for pending transitions, call user handlers. Returns "true" if there were transitions.
TMidasOnline()
default constructor is private for singleton classes
std::string fExptname
experiment name, blank if only one experiment defined in exptab
TransitionHandler fPauseHandler
void deleteEventRequest(int requestId)
Delete data request.
TransitionHandler fStartHandler
bool sleep(int mdelay)
Sleep while checking for and answering MIDAS RPC requests (run transitions, etc)
int disconnect()
Disconnect from MIDAS.
TransitionHandler fStopHandler
TransitionHandler fResumeHandler
static TMidasOnline * instance()
EventRequest * fEventRequests
int getBufferLevel()
Get buffer level (ie the number of bytes in buffer)
int connect(const char *hostname, const char *exptname, const char *progname)
Connect to MIDAS experiment.
void setTransitionHandlers(TransitionHandler start, TransitionHandler stop, TransitionHandler pause, TransitionHandler resume)
Specify user handlers for run transitions.
virtual ~TMidasOnline()
destructor is private for singleton classes
int getBufferSize()
Get buffer size.
int eventRequest(const char *bufferName, int eventId, int triggerMask, int samplingType, bool poll=false)
Request data for delivery via callback (setEventHandler) or by polling (via receiveEvent)
EventHandler fEventHandler
static TMidasOnline * gfMidas
std::string fHostname
hostname where the mserver is running, blank if using shared memory
bool poll(int mdelay)
Check for all MIDAS events (new data events, run transitions)
Request events from online shared memory data buffer.
int fTriggerMask
request trigger mask
int fRequestId
request ID assigned by midas
int fEventId
request event ID
std::string fBufferName
name of the midas data buffer, e.g. "SYSTEM"
EventRequest * fNext
(internal use) list of all requests
HNDLE fBufferHandle
buffer handle from bm_open_buffer()
int fSamplingType
sampling type