1 | /********************************************************************\
|
---|
2 |
|
---|
3 | Tentative FACT central control
|
---|
4 |
|
---|
5 | - Provides central run number dispatcher.
|
---|
6 |
|
---|
7 | Note: Declaring class static ensures destructor is called when
|
---|
8 | exit() is invoked
|
---|
9 |
|
---|
10 | Oliver Grimm, March 2010
|
---|
11 |
|
---|
12 | \********************************************************************/
|
---|
13 |
|
---|
14 | #define SERVER_NAME "Chief"
|
---|
15 | #include "Evidence.h"
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 | // ========= Class Declarations =========
|
---|
20 |
|
---|
21 | class ChiefClass;
|
---|
22 |
|
---|
23 | class RunNumberRpc: public DimRpc {
|
---|
24 |
|
---|
25 | ChiefClass *Chief;
|
---|
26 | void rpcHandler();
|
---|
27 |
|
---|
28 | public:
|
---|
29 | RunNumberRpc(class ChiefClass *Chief): DimRpc("NextRunNumber", "C", "I"),
|
---|
30 | Chief(Chief) {};
|
---|
31 | };
|
---|
32 |
|
---|
33 | class ChiefClass: public EvidenceServer {
|
---|
34 |
|
---|
35 | RunNumberRpc *RN_Rpc;
|
---|
36 |
|
---|
37 | public:
|
---|
38 | ChiefClass();
|
---|
39 | ~ChiefClass();
|
---|
40 |
|
---|
41 | DimService *LastRunNumber;
|
---|
42 | string RunNumFilename;
|
---|
43 | int RunNumber;
|
---|
44 | };
|
---|
45 |
|
---|
46 | // ========= Class Definitions =========
|
---|
47 |
|
---|
48 | // Constructor
|
---|
49 | ChiefClass::ChiefClass(): EvidenceServer(SERVER_NAME) {
|
---|
50 |
|
---|
51 | RunNumFilename = GetConfig("RunNumFile");
|
---|
52 | RunNumber = -2;
|
---|
53 | LastRunNumber = new DimService(SERVER_NAME"/LastRunNumber", RunNumber);
|
---|
54 |
|
---|
55 | // RPC for dispatching run numbers
|
---|
56 | RN_Rpc = new RunNumberRpc(this);
|
---|
57 | }
|
---|
58 |
|
---|
59 | // Destructor
|
---|
60 | ChiefClass::~ChiefClass() {
|
---|
61 |
|
---|
62 | delete RN_Rpc;
|
---|
63 | delete LastRunNumber;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | // Remote procedure call: Request for next run number
|
---|
68 | void RunNumberRpc::rpcHandler() {
|
---|
69 |
|
---|
70 | bool OK = true;
|
---|
71 | int RunNumber;
|
---|
72 |
|
---|
73 | // Open file containing last run number
|
---|
74 | FILE *RunNumFile = fopen(Chief->RunNumFilename.c_str(), "r+");
|
---|
75 | if(RunNumFile == NULL) {
|
---|
76 | Chief->Message(Chief->ERROR, "Could not open file '%s' that contains the last run number (%s)", Chief->RunNumFilename.c_str(), strerror(errno));
|
---|
77 | OK = false;
|
---|
78 | }
|
---|
79 | else {
|
---|
80 | // Read last run number
|
---|
81 | if(fscanf(RunNumFile,"%d", &RunNumber) != 1 ) {
|
---|
82 | Chief->Message(Chief->ERROR, "Could not read run number from file '%s'", Chief->RunNumFilename.c_str());
|
---|
83 | OK = false;
|
---|
84 | }
|
---|
85 | else RunNumber++;
|
---|
86 |
|
---|
87 | // Update run number in file
|
---|
88 | rewind(RunNumFile);
|
---|
89 | if(OK && fprintf(RunNumFile,"%.8u ",RunNumber) < 0) {
|
---|
90 | Chief->Message(Chief->ERROR, "Could not write to run number file '%s'", Chief->RunNumFilename.c_str());
|
---|
91 | OK = false;
|
---|
92 | }
|
---|
93 |
|
---|
94 | // Close run number file
|
---|
95 | if(fclose(RunNumFile) != 0) {
|
---|
96 | Chief->Message(Chief->ERROR, "Could not close run number file '%s'", Chief->RunNumFilename.c_str());
|
---|
97 | OK = false;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Dispatch real run number only if all successful
|
---|
102 | if (!OK) RunNumber = -1;
|
---|
103 | Chief->RunNumber = RunNumber;
|
---|
104 | setData(RunNumber);
|
---|
105 | Chief->LastRunNumber->updateService();
|
---|
106 | Chief->Message(Chief->INFO, "Dispatched run number %d", RunNumber);
|
---|
107 | }
|
---|
108 |
|
---|
109 | //
|
---|
110 | // Main program
|
---|
111 | //
|
---|
112 | int main() {
|
---|
113 |
|
---|
114 | static ChiefClass TheBoss;
|
---|
115 |
|
---|
116 | // Sleep until exit requested
|
---|
117 | while (!TheBoss.ExitRequest) pause();
|
---|
118 | }
|
---|