MIDAS
Loading...
Searching...
No Matches
tmfe.cxx File Reference
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <signal.h>
#include <sys/time.h>
#include "tmfe.h"
#include "midas.h"
#include "msystem.h"
#include "mrpc.h"
#include "mstrlcpy.h"
Include dependency graph for tmfe.cxx:

Go to the source code of this file.

Classes

class  TMFrontendRpcHelper
 

Functions

TMFeResult TMFeErrorMessage (const std::string &message)
 
TMFeResult TMFeMidasError (const std::string &message, const char *midas_function_name, int midas_status)
 
static INT rpc_callback (INT index, void *prpc_param[])
 
static INT binary_rpc_callback (INT index, void *prpc_param[])
 
static INT tr_start (INT run_number, char *errstr)
 
static INT tr_stop (INT run_number, char *errstr)
 
static INT tr_pause (INT run_number, char *errstr)
 
static INT tr_resume (INT run_number, char *errstr)
 
static INT tr_startabort (INT run_number, char *errstr)
 

Function Documentation

◆ binary_rpc_callback()

static INT binary_rpc_callback ( INT  index,
void prpc_param[] 
)
static

Definition at line 1175 of file tmfe.cxx.

1176{
1177 const char* cmd = CSTRING(0);
1178 const char* args = CSTRING(1);
1179 char* return_buf = CSTRING(2);
1180 size_t return_max_length = CINT(3);
1181
1182 if (TMFE::gfVerbose)
1183 printf("TMFE::binary_rpc_callback: index %d, max_length %zu, cmd [%s], args [%s]\n", index, return_max_length, cmd, args);
1184
1185 TMFE* mfe = TMFE::Instance();
1186
1187 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleRpc() modifies fEquipments. K.O.
1188 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1189 TMFeRpcHandlerInterface* h = mfe->fRpcHandlers[i];
1190 if (!h)
1191 continue;
1192 std::vector<char> result;
1193 TMFeResult r = h->HandleBinaryRpc(cmd, args, result);
1194 if (result.size() > 0) {
1195 if (result.size() > return_max_length) {
1196 TMFE::Instance()->Msg(MERROR, "TMFE::binary_rpc_callback", "RPC handler returned too much data, %zu bytes truncated to %zu bytes", result.size(), return_max_length);
1197 result.resize(return_max_length);
1198 }
1199 //printf("Handler reply [%s]\n", C(r));
1200 assert(result.size() <= return_max_length);
1201 memcpy(return_buf, result.data(), result.size());
1202 CINT(3) = result.size();
1203 return RPC_SUCCESS;
1204 }
1205 }
1206
1207 CINT(3) = 0;
1208 return_buf[0] = 0;
1209 return RPC_SUCCESS;
1210}
Definition tmfe.h:381
static bool gfVerbose
Definition tmfe.h:424
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
virtual TMFeResult HandleBinaryRpc(const char *cmd, const char *args, std::vector< char > &result)
Definition tmfe.h:153
#define RPC_SUCCESS
Definition midas.h:698
#define MERROR
Definition midas.h:559
INT index
Definition mana.cxx:271
INT i
Definition mdump.cxx:32
#define CINT(_i)
Definition midas.h:1622
#define CSTRING(_i)
Definition midas.h:1646
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rpc_callback()

static INT rpc_callback ( INT  index,
void prpc_param[] 
)
static

Definition at line 1145 of file tmfe.cxx.

1146{
1147 const char* cmd = CSTRING(0);
1148 const char* args = CSTRING(1);
1149 char* return_buf = CSTRING(2);
1150 int return_max_length = CINT(3);
1151
1152 if (TMFE::gfVerbose)
1153 printf("TMFE::rpc_callback: index %d, max_length %d, cmd [%s], args [%s]\n", index, return_max_length, cmd, args);
1154
1155 TMFE* mfe = TMFE::Instance();
1156
1157 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleRpc() modifies fEquipments. K.O.
1158 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1159 TMFeRpcHandlerInterface* h = mfe->fRpcHandlers[i];
1160 if (!h)
1161 continue;
1162 std::string result = "";
1163 TMFeResult r = h->HandleRpc(cmd, args, result);
1164 if (result.length() > 0) {
1165 //printf("Handler reply [%s]\n", C(r));
1166 mstrlcpy(return_buf, result.c_str(), return_max_length);
1167 return RPC_SUCCESS;
1168 }
1169 }
1170
1171 return_buf[0] = 0;
1172 return RPC_SUCCESS;
1173}
virtual TMFeResult HandleRpc(const char *cmd, const char *args, std::string &result)
Definition tmfe.h:152
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TMFeErrorMessage()

TMFeResult TMFeErrorMessage ( const std::string &  message)

Definition at line 29 of file tmfe.cxx.

30{
31 return TMFeResult(0, message);
32}
#define message(type, str)
Here is the caller graph for this function:

◆ TMFeMidasError()

TMFeResult TMFeMidasError ( const std::string &  message,
const char midas_function_name,
int  midas_status 
)

Definition at line 34 of file tmfe.cxx.

35{
37}
std::string msprintf(const char *format,...)
Definition midas.cxx:410
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tr_pause()

static INT tr_pause ( INT  run_number,
char errstr 
)
static

Definition at line 1353 of file tmfe.cxx.

1354{
1355 cm_msg(MINFO, "tr_pause", "tr_pause");
1356
1357 TMFeResult result;
1358
1359 TMFE* mfe = TMFE::Instance();
1360
1361 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandlePauseRun() modifies fEquipments. K.O.
1362 // NOTE: tr_pause runs in reverse order to match tr_stop. K.O.
1363 for (int i = (int)mfe->fRpcHandlers.size() - 1; i >= 0; i--) {
1364 TMFeRpcHandlerInterface* h = mfe->fRpcHandlers[i];
1365 if (!h)
1366 continue;
1367 result = h->HandlePauseRun(run_number);
1368 if (result.error_flag) {
1369 // error handling in this function matches general transition error handling:
1370 // logic is same as "start run"
1371 break;
1372 }
1373 }
1374
1375 if (result.error_flag) {
1376 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1377 return FE_ERR_DRIVER;
1378 }
1379
1380 return SUCCESS;
1381}
bool error_flag
Definition tmfe.h:89
std::string error_message
Definition tmfe.h:91
virtual TMFeResult HandlePauseRun(int run_number)
Definition tmfe.h:149
#define FE_ERR_DRIVER
Definition midas.h:721
#define SUCCESS
Definition mcstd.h:54
#define MINFO
Definition midas.h:560
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
Definition midas.cxx:915
INT run_number[2]
Definition mana.cxx:246
#define TRANSITION_ERROR_STRING_LENGTH
Definition midas.h:280
Here is the call graph for this function:

◆ tr_resume()

static INT tr_resume ( INT  run_number,
char errstr 
)
static

Definition at line 1383 of file tmfe.cxx.

1384{
1385 if (TMFE::gfVerbose)
1386 printf("TMFE::tr_resume!\n");
1387
1388 TMFeResult result;
1389
1390 TMFE* mfe = TMFE::Instance();
1391
1392 mfe->fRunNumber = run_number;
1393 mfe->fStateRunning = true;
1394
1395 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleResumeRun() modifies fEquipments. K.O.
1396 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1397 TMFeRpcHandlerInterface* h = mfe->fRpcHandlers[i];
1398 if (!h)
1399 continue;
1400 result = h->HandleResumeRun(run_number);
1401 if (result.error_flag) {
1402 // error handling in this function matches general transition error handling:
1403 // logic is same as "start run"
1404 break;
1405 }
1406 }
1407
1408 if (result.error_flag) {
1409 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1410 return FE_ERR_DRIVER;
1411 }
1412
1413 return SUCCESS;
1414}
virtual TMFeResult HandleResumeRun(int run_number)
Definition tmfe.h:150
Here is the call graph for this function:

◆ tr_start()

static INT tr_start ( INT  run_number,
char errstr 
)
static

Definition at line 1275 of file tmfe.cxx.

1276{
1277 if (TMFE::gfVerbose)
1278 printf("TMFE::tr_start!\n");
1279
1280 TMFE* mfe = TMFE::Instance();
1281
1282 mfe->fRunNumber = run_number;
1283 mfe->fStateRunning = true;
1284
1285 TMFeResult result;
1286
1287 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleBeginRun() modifies fEquipments. K.O.
1288 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1289 TMFeRpcHandlerInterface* h = mfe->fRpcHandlers[i];
1290 if (!h)
1291 continue;
1292 result = h->HandleBeginRun(run_number);
1293 if (result.error_flag) {
1294 // error handling in this function matches general transition error handling:
1295 // on run start, the first user handler to return an error code
1296 // will abort the transition. This leaves everything in an
1297 // inconsistent state: frontends called before the abort
1298 // think the run is running, which it does not. They should register
1299 // a handler for the "start abort" transition. This transition calls
1300 // all already started frontends so they can cleanup their state. K.O.
1301 //
1302 break;
1303 }
1304 }
1305
1306 if (result.error_flag) {
1307 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1308 return FE_ERR_DRIVER;
1309 }
1310
1311 return SUCCESS;
1312}
virtual TMFeResult HandleBeginRun(int run_number)
Definition tmfe.h:147
Here is the call graph for this function:

◆ tr_startabort()

static INT tr_startabort ( INT  run_number,
char errstr 
)
static

Definition at line 1416 of file tmfe.cxx.

1417{
1418 if (TMFE::gfVerbose)
1419 printf("TMFE::tr_startabort!\n");
1420
1421 TMFeResult result;
1422
1423 TMFE* mfe = TMFE::Instance();
1424
1425 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleStartAbortRun() modifies fEquipments. K.O.
1426 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1427 TMFeRpcHandlerInterface* h = mfe->fRpcHandlers[i];
1428 if (!h)
1429 continue;
1430 result = h->HandleStartAbortRun(run_number);
1431 if (result.error_flag) {
1432 // error handling in this function matches general transition error handling:
1433 // logic is same as "start run"
1434 break;
1435 }
1436 }
1437
1438 mfe->fStateRunning = false;
1439
1440 if (result.error_flag) {
1441 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1442 return FE_ERR_DRIVER;
1443 }
1444
1445 return SUCCESS;
1446}
virtual TMFeResult HandleStartAbortRun(int run_number)
Definition tmfe.h:151
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tr_stop()

static INT tr_stop ( INT  run_number,
char errstr 
)
static

Definition at line 1314 of file tmfe.cxx.

1315{
1316 if (TMFE::gfVerbose)
1317 printf("TMFE::tr_stop!\n");
1318
1319 TMFeResult result;
1320
1321 TMFE* mfe = TMFE::Instance();
1322
1323 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleEndRun() modifies fEquipments. K.O.
1324 // NOTE: we need to stop thing in reverse order, otherwise TMFrontend code
1325 // does not work right - TMFrontend is registered first, and (correctly) runs
1326 // first at begin of run (to clear statistics, etc). But at the end of run
1327 // it needs to run last, to update the statistics, etc. after all the equipments
1328 // have done their end of run things and are finished. K.O.
1329 for (int i = (int)mfe->fRpcHandlers.size() - 1; i >= 0; i--) {
1330 TMFeRpcHandlerInterface* h = mfe->fRpcHandlers[i];
1331 if (!h)
1332 continue;
1334 if (xresult.error_flag) {
1335 // error handling in this function matches general transition error handling:
1336 // the "run stop" transition is always sucessful, the run always stops.
1337 // if some frontend returns an error, this error is remembered and is returned
1338 // as the transition over all status. K.O.
1339 result = xresult;
1340 }
1341 }
1342
1343 mfe->fStateRunning = false;
1344
1345 if (result.error_flag) {
1346 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1347 return FE_ERR_DRIVER;
1348 }
1349
1350 return SUCCESS;
1351}
virtual TMFeResult HandleEndRun(int run_number)
Definition tmfe.h:148
Here is the call graph for this function: