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 rpc_cxx_callback (INT index, void *prpc_param[])
 
static INT binary_rpc_callback (INT index, void *prpc_param[])
 
static INT binary_rpc_cxx_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 1203 of file tmfe.cxx.

1204{
1205 const char* cmd = CSTRING(0);
1206 const char* args = CSTRING(1);
1207 char* return_buf = CSTRING(2);
1208 size_t return_max_length = CINT(3);
1209
1210 if (TMFE::gfVerbose)
1211 printf("TMFE::binary_rpc_callback: index %d, max_length %zu, cmd [%s], args [%s]\n", index, return_max_length, cmd, args);
1212
1213 TMFE* mfe = TMFE::Instance();
1214
1215 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleRpc() modifies fEquipments. K.O.
1216 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1218 if (!h)
1219 continue;
1220 std::vector<char> result;
1221 TMFeResult r = h->HandleBinaryRpc(cmd, args, result);
1222 if (result.size() > 0) {
1223 if (result.size() > return_max_length) {
1224 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);
1225 result.resize(return_max_length);
1226 }
1227 //printf("Handler reply [%s]\n", C(r));
1228 assert(result.size() <= return_max_length);
1229 memcpy(return_buf, result.data(), result.size());
1230 CINT(3) = result.size();
1231 return RPC_SUCCESS;
1232 }
1233 }
1234
1235 CINT(3) = 0;
1236 return_buf[0] = 0;
1237 return RPC_SUCCESS;
1238}
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
std::vector< TMFeRpcHandlerInterface * > fRpcHandlers
Definition tmfe.h:464
virtual TMFeResult HandleBinaryRpc(const char *cmd, const char *args, std::vector< char > &result)
Definition tmfe.h:153
#define RPC_SUCCESS
Definition midas.h:699
#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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ binary_rpc_cxx_callback()

static INT binary_rpc_cxx_callback ( INT  index,
void *  prpc_param[] 
)
static

Definition at line 1240 of file tmfe.cxx.

1241{
1242 const char* cmd = CSTRING(0);
1243 const char* args = CSTRING(1);
1244 std::vector<char>* pbuf = CPSTDVECTOR(2);
1245
1246 if (TMFE::gfVerbose)
1247 printf("TMFE::binary_rpc_callback: index %d, cmd [%s], args [%s]\n", index, cmd, args);
1248
1249 pbuf->clear();
1250
1251 TMFE* mfe = TMFE::Instance();
1252
1253 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleRpc() modifies fEquipments. K.O.
1254 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1256 if (!h)
1257 continue;
1258 TMFeResult r = h->HandleBinaryRpc(cmd, args, *pbuf);
1259 if (pbuf->size() > 0) {
1260 return RPC_SUCCESS;
1261 }
1262 }
1263
1264 return RPC_SUCCESS;
1265}
#define CPSTDVECTOR(_i)
Definition midas.h:1686
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++) {
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:

◆ rpc_cxx_callback()

static INT rpc_cxx_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 std::string* pstr = CPSTDSTRING(2);
1180
1181 pstr->clear();
1182
1183 if (TMFE::gfVerbose)
1184 printf("TMFE::rpc_cxx_callback: index %d, cmd [%s], args [%s]\n", index, cmd, args);
1185
1186 TMFE* mfe = TMFE::Instance();
1187
1188 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleRpc() modifies fEquipments. K.O.
1189 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1191 if (!h)
1192 continue;
1193 TMFeResult r = h->HandleRpc(cmd, args, *pstr);
1194 if (pstr->length() > 0) {
1195 //printf("Handler reply [%s]\n", pstr->c_str());
1196 return RPC_SUCCESS;
1197 }
1198 }
1199
1200 return RPC_SUCCESS;
1201}
#define CPSTDSTRING(_i)
Definition midas.h:1685
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{
36 return TMFeResult(midas_status, message + msprintf(", %s() status %d", midas_function_name, midas_status));
37}
std::string msprintf(const char *format,...)
Definition midas.cxx:419
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 1408 of file tmfe.cxx.

1409{
1410 cm_msg(MINFO, "tr_pause", "tr_pause");
1411
1412 TMFeResult result;
1413
1414 TMFE* mfe = TMFE::Instance();
1415
1416 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandlePauseRun() modifies fEquipments. K.O.
1417 // NOTE: tr_pause runs in reverse order to match tr_stop. K.O.
1418 for (int i = (int)mfe->fRpcHandlers.size() - 1; i >= 0; i--) {
1420 if (!h)
1421 continue;
1422 result = h->HandlePauseRun(run_number);
1423 if (result.error_flag) {
1424 // error handling in this function matches general transition error handling:
1425 // logic is same as "start run"
1426 break;
1427 }
1428 }
1429
1430 if (result.error_flag) {
1431 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1432 return FE_ERR_DRIVER;
1433 }
1434
1435 return SUCCESS;
1436}
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:722
#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:931
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 1438 of file tmfe.cxx.

1439{
1440 if (TMFE::gfVerbose)
1441 printf("TMFE::tr_resume!\n");
1442
1443 TMFeResult result;
1444
1445 TMFE* mfe = TMFE::Instance();
1446
1447 mfe->fRunNumber = run_number;
1448 mfe->fStateRunning = true;
1449
1450 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleResumeRun() modifies fEquipments. K.O.
1451 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1453 if (!h)
1454 continue;
1455 result = h->HandleResumeRun(run_number);
1456 if (result.error_flag) {
1457 // error handling in this function matches general transition error handling:
1458 // logic is same as "start run"
1459 break;
1460 }
1461 }
1462
1463 if (result.error_flag) {
1464 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1465 return FE_ERR_DRIVER;
1466 }
1467
1468 return SUCCESS;
1469}
bool fStateRunning
run state is running or paused
Definition tmfe.h:402
int fRunNumber
current run number
Definition tmfe.h:401
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 1330 of file tmfe.cxx.

1331{
1332 if (TMFE::gfVerbose)
1333 printf("TMFE::tr_start!\n");
1334
1335 TMFE* mfe = TMFE::Instance();
1336
1337 mfe->fRunNumber = run_number;
1338 mfe->fStateRunning = true;
1339
1340 TMFeResult result;
1341
1342 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleBeginRun() modifies fEquipments. K.O.
1343 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1345 if (!h)
1346 continue;
1347 result = h->HandleBeginRun(run_number);
1348 if (result.error_flag) {
1349 // error handling in this function matches general transition error handling:
1350 // on run start, the first user handler to return an error code
1351 // will abort the transition. This leaves everything in an
1352 // inconsistent state: frontends called before the abort
1353 // think the run is running, which it does not. They should register
1354 // a handler for the "start abort" transition. This transition calls
1355 // all already started frontends so they can cleanup their state. K.O.
1356 //
1357 break;
1358 }
1359 }
1360
1361 if (result.error_flag) {
1362 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1363 return FE_ERR_DRIVER;
1364 }
1365
1366 return SUCCESS;
1367}
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 1471 of file tmfe.cxx.

1472{
1473 if (TMFE::gfVerbose)
1474 printf("TMFE::tr_startabort!\n");
1475
1476 TMFeResult result;
1477
1478 TMFE* mfe = TMFE::Instance();
1479
1480 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleStartAbortRun() modifies fEquipments. K.O.
1481 for (unsigned i=0; i<mfe->fRpcHandlers.size(); i++) {
1483 if (!h)
1484 continue;
1485 result = h->HandleStartAbortRun(run_number);
1486 if (result.error_flag) {
1487 // error handling in this function matches general transition error handling:
1488 // logic is same as "start run"
1489 break;
1490 }
1491 }
1492
1493 mfe->fStateRunning = false;
1494
1495 if (result.error_flag) {
1496 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1497 return FE_ERR_DRIVER;
1498 }
1499
1500 return SUCCESS;
1501}
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 1369 of file tmfe.cxx.

1370{
1371 if (TMFE::gfVerbose)
1372 printf("TMFE::tr_stop!\n");
1373
1374 TMFeResult result;
1375
1376 TMFE* mfe = TMFE::Instance();
1377
1378 // NOTE: cannot use range-based for() loop, it uses an iterator and will crash if HandleEndRun() modifies fEquipments. K.O.
1379 // NOTE: we need to stop thing in reverse order, otherwise TMFrontend code
1380 // does not work right - TMFrontend is registered first, and (correctly) runs
1381 // first at begin of run (to clear statistics, etc). But at the end of run
1382 // it needs to run last, to update the statistics, etc. after all the equipments
1383 // have done their end of run things and are finished. K.O.
1384 for (int i = (int)mfe->fRpcHandlers.size() - 1; i >= 0; i--) {
1386 if (!h)
1387 continue;
1388 TMFeResult xresult = h->HandleEndRun(run_number);
1389 if (xresult.error_flag) {
1390 // error handling in this function matches general transition error handling:
1391 // the "run stop" transition is always sucessful, the run always stops.
1392 // if some frontend returns an error, this error is remembered and is returned
1393 // as the transition over all status. K.O.
1394 result = xresult;
1395 }
1396 }
1397
1398 mfe->fStateRunning = false;
1399
1400 if (result.error_flag) {
1401 mstrlcpy(errstr, result.error_message.c_str(), TRANSITION_ERROR_STRING_LENGTH);
1402 return FE_ERR_DRIVER;
1403 }
1404
1405 return SUCCESS;
1406}
virtual TMFeResult HandleEndRun(int run_number)
Definition tmfe.h:148
Here is the call graph for this function: