source: trunk/FACT++/src/EventBuilderWrapper.h@ 10897

Last change on this file since 10897 was 10861, checked in by tbretz, 13 years ago
Adapted Start() once more.
File size: 5.4 KB
Line 
1#ifndef FACT_EventBuilderWrapper
2#define FACT_EventBuilderWrapper
3
4/*
5#if BOOST_VERSION < 104400
6#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
7#undef BOOST_HAS_RVALUE_REFS
8#endif
9#endif
10#include <boost/thread.hpp>
11
12using namespace std;
13*/
14
15#include <boost/date_time/posix_time/posix_time_types.hpp>
16
17#include "EventBuilder.h"
18
19class EventBuilderWrapper
20{
21public:
22 // FIXME
23 static EventBuilderWrapper *This;
24
25private:
26 boost::thread fThread;
27
28 enum CommandStates_t // g_runStat
29 {
30 kAbort = -2, // quit as soon as possible ('abort')
31 kExit = -1, // stop reading, quit when buffered events done ('exit')
32 kInitialize = 0, // 'initialize' (e.g. dim not yet started)
33 kHybernate = 1, // do nothing for long time ('hybernate') [wakeup within ~1sec]
34 kSleep = 2, // do nothing ('sleep') [wakeup within ~10msec]
35 kModeFlush = 10, // read data from camera, but skip them ('flush')
36 kModeTest = 20, // read data and process them, but do not write to disk ('test')
37 kModeFlag = 30, // read data, process and write all to disk ('flag')
38 kModeRun = 40, // read data, process and write selected to disk ('run')
39 };
40
41 MessageImp &fMsg;
42
43public:
44 EventBuilderWrapper(MessageImp &msg) : fMsg(msg)
45 {
46 if (This)
47 throw logic_error("EventBuilderWrapper cannot be instantiated twice.");
48
49 This = this;
50 }
51 ~EventBuilderWrapper()
52 {
53 Abort();
54 // FIXME: Used timed_join and abort afterwards
55 // What's the maximum time the eb need to abort?
56 fThread.join();
57 //fMsg.Info("EventBuilder stopped.");
58 }
59
60 void Update(const char *msg, int severity)
61 {
62 fMsg.Update(msg, severity);
63 }
64
65 bool IsThreadRunning()
66 {
67 return !fThread.timed_join(boost::posix_time::microseconds(0));
68 }
69
70 void SetMaxMemory(unsigned int mb) const
71 {
72 if (mb*1000000<GetUsedMemory())
73 {
74 // fMsg.Warn("...");
75 return;
76 }
77
78 g_maxMem = mb*1000000;
79 }
80
81 void Start(const vector<string> &addr)
82 {
83 if (IsThreadRunning())
84 {
85 fMsg.Warn("Start - EventBuilder still running");
86 return;
87 }
88
89 fMsg.Message("Starting EventBuilder thread");
90
91 for (size_t i=0; i<addr.size(); i++)
92 {
93 memset(g_ip[i].addr, 0, sizeof(g_ip[i].addr));
94
95 const size_t pos = addr[i].find_first_of(':');
96
97 const string a = addr[i].substr(0, pos);
98 const string p = addr[i].substr(pos+1);
99
100 strcpy(g_ip[i].addr, "127.0.0.1");
101 g_ip[i].port = atoi(p.c_str());
102 }
103
104 g_maxBoards = addr.size();
105 g_actBoards = addr.size();
106
107 g_runStat = kModeRun;
108
109 fThread = boost::thread(StartEvtBuild);
110 }
111 void Abort()
112 {
113 fMsg.Message("Signal abort to EventBuilder thread...");
114 g_runStat = kAbort;
115 }
116
117 void Exit()
118 {
119 fMsg.Message("Signal exit to EventBuilder thread...");
120 g_runStat = kExit;
121 }
122
123 /*
124 void Wait()
125 {
126 fThread.join();
127 fMsg.Message("EventBuilder stopped.");
128 }*/
129
130 void Hybernate() const { g_runStat = kHybernate; }
131 void Sleep() const { g_runStat = kSleep; }
132 void FlushMode() const { g_runStat = kModeFlush; }
133 void TestMode() const { g_runStat = kModeTest; }
134 void FlagMode() const { g_runStat = kModeFlag; }
135 void RunMode() const { g_runStat = kModeRun; }
136
137 // FIXME: To be removed
138 void SetMode(int mode) const { g_runStat = mode; }
139
140 bool IsConnected(int i) const { return gi_NumConnect[i]==7; }
141 bool IsDisconnected(int i) const { return gi_NumConnect[i]<=0; }
142 int GetNumConnected(int i) const { return gi_NumConnect[i]; }
143
144 size_t GetUsedMemory() const { return gi_usedMem; }
145
146
147 // -------------- Mapped event builder callbacks ------------------
148
149 int runOpen(uint32_t runid, RUN_HEAD *h, size_t)
150 {
151 cout << "OPEN_FILE #" << runid << endl;
152 cout << " Ver= " << h->Version << endl;
153 cout << " Typ= " << h->RunType << endl;
154 cout << " Nb = " << h->NBoard << endl;
155 cout << " Np = " << h->NPix << endl;
156 cout << " NTm= " << h->NTm << endl;
157 cout << " roi= " << h->Nroi << endl;
158
159 return 0;
160 }
161
162 int runWrite(int, EVENT *e, size_t)
163 {
164 cout << "WRITE_EVENT" << endl;
165
166 cout << " Evt=" << e->EventNum << endl;
167 cout << " Typ=" << e->TriggerType << endl;
168 cout << " roi=" << e->Roi << endl;
169 cout << " trg=" << e->SoftTrig << endl;
170 cout << " tim=" << e->PCTime << endl;
171
172 return 0;
173 }
174
175 int runClose(int, RUN_TAIL *, size_t)
176 {
177 cout << "CLOSE_RUN" << endl;
178 return 0;
179 }
180};
181
182EventBuilderWrapper *EventBuilderWrapper::This = 0;
183
184// ----------- Event builder callbacks implementation ---------------
185extern "C"
186{
187 int runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len)
188 {
189 return EventBuilderWrapper::This->runOpen(irun, runhd, len);
190 }
191
192 int runWrite(int fileId, EVENT *event, size_t len)
193 {
194 return EventBuilderWrapper::This->runWrite(fileId, event, len);
195 }
196
197 int runClose(int fileId, RUN_TAIL *runth, size_t len)
198 {
199 return EventBuilderWrapper::This->runClose(fileId, runth, len);
200 }
201
202 void message(int severity, const char *msg)
203 {
204 EventBuilderWrapper::This->Update(msg, severity);
205 }
206}
207
208#endif
Note: See TracBrowser for help on using the repository browser.