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