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

Last change on this file since 10856 was 10856, checked in by tbretz, 14 years ago
Further improvement in Start(), accept negative numbers in NumConnect as disconnected.
File size: 5.8 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 map<int,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 int cnt = 0;
92 for (int i=0; i<40; i++)
93 {
94 //g_ip[i].port = 5000;
95 memset(g_ip[cnt].addr, 0, sizeof(g_ip[cnt].addr));
96 //strncpy(g_ip[i].addr, it->c_str(), 99);
97
98 const map<int,string>::const_iterator it = addr.find(i);
99 if (it==addr.end())
100 continue;
101
102 const string &dest = it->second;
103
104 size_t pos = dest.find_first_of(':');
105 if (pos==string::npos)
106 continue;
107
108 string a = dest.substr(0, pos);
109 string p = dest.substr(pos+1);
110
111 strcpy(g_ip[cnt].addr, "127.0.0.1");
112 g_ip[cnt].port = atoi(p.c_str());
113
114 cnt++;
115 }
116
117 g_maxBoards = cnt;
118 g_actBoards = cnt;
119
120 g_runStat = kModeRun;
121
122 fThread = boost::thread(StartEvtBuild);
123 }
124 void Abort()
125 {
126 fMsg.Message("Signal abort to EventBuilder thread...");
127 g_runStat = kAbort;
128 }
129
130 void Exit()
131 {
132 fMsg.Message("Signal exit to EventBuilder thread...");
133 g_runStat = kExit;
134 }
135
136 /*
137 void Wait()
138 {
139 fThread.join();
140 fMsg.Message("EventBuilder stopped.");
141 }*/
142
143 void Hybernate() const { g_runStat = kHybernate; }
144 void Sleep() const { g_runStat = kSleep; }
145 void FlushMode() const { g_runStat = kModeFlush; }
146 void TestMode() const { g_runStat = kModeTest; }
147 void FlagMode() const { g_runStat = kModeFlag; }
148 void RunMode() const { g_runStat = kModeRun; }
149
150 // FIXME: To be removed
151 void SetMode(int mode) const { g_runStat = mode; }
152
153 bool IsConnected(int i) const { return gi_NumConnect[i]==7; }
154 bool IsDisconnected(int i) const { return gi_NumConnect[i]<=0; }
155 int GetNumConnected(int i) const { return gi_NumConnect[i]; }
156
157 size_t GetUsedMemory() const { return gi_usedMem; }
158
159
160 // -------------- Mapped event builder callbacks ------------------
161
162 int runOpen(uint32_t runid, RUN_HEAD *h, size_t)
163 {
164 cout << "OPEN_FILE #" << runid << endl;
165 cout << " Ver= " << h->Version << endl;
166 cout << " Typ= " << h->RunType << endl;
167 cout << " Nb = " << h->NBoard << endl;
168 cout << " Np = " << h->NPix << endl;
169 cout << " NTm= " << h->NTm << endl;
170 cout << " roi= " << h->Nroi << endl;
171
172 return 0;
173 }
174
175 int runWrite(int, EVENT *e, size_t)
176 {
177 cout << "WRITE_EVENT" << endl;
178
179 cout << " Evt=" << e->EventNum << endl;
180 cout << " Typ=" << e->TriggerType << endl;
181 cout << " roi=" << e->Roi << endl;
182 cout << " trg=" << e->SoftTrig << endl;
183 cout << " tim=" << e->PCTime << endl;
184
185 return 0;
186 }
187
188 int runClose(int, RUN_TAIL *, size_t)
189 {
190 cout << "CLOSE_RUN" << endl;
191 return 0;
192 }
193};
194
195EventBuilderWrapper *EventBuilderWrapper::This = 0;
196
197// ----------- Event builder callbacks implementation ---------------
198extern "C"
199{
200 int runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len)
201 {
202 return EventBuilderWrapper::This->runOpen(irun, runhd, len);
203 }
204
205 int runWrite(int fileId, EVENT *event, size_t len)
206 {
207 return EventBuilderWrapper::This->runWrite(fileId, event, len);
208 }
209
210 int runClose(int fileId, RUN_TAIL *runth, size_t len)
211 {
212 return EventBuilderWrapper::This->runClose(fileId, runth, len);
213 }
214
215 void message(int severity, const char *msg)
216 {
217 EventBuilderWrapper::This->Update(msg, severity);
218 }
219}
220
221#endif
Note: See TracBrowser for help on using the repository browser.