-
Notifications
You must be signed in to change notification settings - Fork 122
/
FakeISISEventDAE.cpp
242 lines (215 loc) · 7.51 KB
/
FakeISISEventDAE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidLiveData/ISIS/FakeISISEventDAE.h"
#include "MantidLiveData/ISIS/TCPEventStreamDefs.h"
#include "MantidKernel/MersenneTwister.h"
#include "MantidKernel/Timer.h"
#include <Poco/Net/TCPServer.h>
#include <Poco/Net/StreamSocket.h>
#include <Poco/ActiveResult.h>
#include <Poco/Thread.h>
#include <boost/random/uniform_int.hpp>
#include <numeric>
namespace Mantid {
namespace LiveData {
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(FakeISISEventDAE)
using namespace Kernel;
using namespace API;
namespace {
/**
* Implements Poco TCPServerConnection and does the actual job of interpreting
* commands
* from a client and sending data.
*/
class TestServerConnection : public Poco::Net::TCPServerConnection {
int m_nPeriods;
int m_nSpectra;
int m_Rate;
int m_nEvents;
boost::shared_ptr<Progress> m_prog;
public:
/**
* Constructor. Defines the simulated dataset dimensions.
* @param soc :: A socket that provides communication with the client.
*/
TestServerConnection(const Poco::Net::StreamSocket &soc, int nper, int nspec,
int rate, int nevents, boost::shared_ptr<Progress> prog)
: Poco::Net::TCPServerConnection(soc), m_nPeriods(nper),
m_nSpectra(nspec), m_Rate(rate), m_nEvents(nevents), m_prog(prog) {
m_prog->report(0, "Client Connected");
sendInitialSetup();
}
/// Sends an OK message when there is nothing to send or an error occured
void sendOK() {
std::string comm = "OK";
socket().sendBytes(comm.c_str(), static_cast<int>(comm.size()));
}
/// Send initial setup header
void sendInitialSetup() {
TCPStreamEventDataSetup setup;
setup.head_setup.run_number = 1234;
strcpy(setup.head_setup.inst_name, "MUSR");
socket().sendBytes(&setup, static_cast<int>(sizeof(setup)));
}
/**
* Main method that sends out the data.
*/
void run() override {
Kernel::MersenneTwister tof(0, 10000.0, 20000.0);
Kernel::MersenneTwister spec(1234, 0.0, static_cast<double>(m_nSpectra));
Kernel::MersenneTwister period(0, 0.0, static_cast<double>(m_nPeriods));
std::vector<TCPStreamEventNeutron> neutronVector(m_nEvents);
Timer timer;
int eventTotal = 0;
for (;;) {
Poco::Thread::sleep(m_Rate);
TCPStreamEventDataNeutron data;
data.head_n.nevents = m_nEvents;
data.head_n.period = static_cast<uint32_t>(period.nextValue());
socket().sendBytes(&data.head, static_cast<int>(sizeof(data.head)));
socket().sendBytes(&data.head_n, static_cast<int>(sizeof(data.head_n)));
for (uint32_t i = 0; i < data.head_n.nevents; ++i) {
TCPStreamEventNeutron neutron;
neutron.time_of_flight = static_cast<float>(tof.nextValue());
neutron.spectrum = static_cast<uint32_t>(spec.nextValue());
neutronVector[i] = neutron;
}
int bytesSent = 0;
int targetSize =
m_nEvents * static_cast<int>(sizeof(TCPStreamEventNeutron));
while (bytesSent < targetSize) {
bytesSent += socket().sendBytes(neutronVector.data() + bytesSent,
targetSize - bytesSent);
}
// report progress
float secondsElapsed = timer.elapsed(false);
eventTotal += m_nEvents;
// only report once per second
if (secondsElapsed > 1) {
float rate = static_cast<float>(eventTotal) / secondsElapsed;
std::stringstream sstm;
sstm << static_cast<int>(rate) << " events/sec";
m_prog->report(0, sstm.str());
eventTotal = 0;
timer.reset();
}
}
}
};
/**
* Implements Poco TCPServerConnectionFactory
*/
class TestServerConnectionFactory
: public Poco::Net::TCPServerConnectionFactory {
int m_nPeriods; ///< Number of periods in the fake dataset
int m_nSpectra; ///< Number of spectra in the fake dataset
int m_Rate;
int m_nEvents;
boost::shared_ptr<Progress> m_prog;
public:
/**
* Constructor.
*/
TestServerConnectionFactory(int nper, int nspec, int rate, int nevents,
boost::shared_ptr<Progress> prog)
: Poco::Net::TCPServerConnectionFactory(), m_nPeriods(nper),
m_nSpectra(nspec), m_Rate(rate), m_nEvents(nevents), m_prog(prog) {}
/**
* The factory method.
* @param socket :: The socket.
*/
Poco::Net::TCPServerConnection *
createConnection(const Poco::Net::StreamSocket &socket) override {
return new TestServerConnection(socket, m_nPeriods, m_nSpectra, m_Rate,
m_nEvents, m_prog);
}
};
} // end anonymous
/// (Empty) Constructor
FakeISISEventDAE::FakeISISEventDAE() : m_server(nullptr) {}
/// Destructor
FakeISISEventDAE::~FakeISISEventDAE() {
if (m_server) {
m_server->stop();
delete m_server;
}
}
/**
* Declare the algorithm properties
*/
void FakeISISEventDAE::init() {
declareProperty(
make_unique<PropertyWithValue<int>>("NPeriods", 1, Direction::Input),
"Number of periods.");
declareProperty(
make_unique<PropertyWithValue<int>>("NSpectra", 100, Direction::Input),
"Number of spectra.");
declareProperty(
make_unique<PropertyWithValue<int>>("Rate", 20, Direction::Input),
"Rate of sending the data: stream of NEvents events is sent "
"every Rate milliseconds.");
declareProperty(
make_unique<PropertyWithValue<int>>("NEvents", 1000, Direction::Input),
"Number of events in each packet.");
declareProperty(
make_unique<PropertyWithValue<int>>("Port", 59876, Direction::Input),
"The port to broadcast on (default 59876, ISISDAE 10000).");
}
/**
* Execute the algorithm.
*/
void FakeISISEventDAE::exec() {
int nper = getProperty("NPeriods");
int nspec = getProperty("NSpectra");
int rate = getProperty("Rate");
int nevents = getProperty("NEvents");
int port = getProperty("Port");
// start the live HistoDAE as well
API::IAlgorithm_sptr histoDAE =
createChildAlgorithm("FakeISISHistoDAE", -1.0, -1.0);
histoDAE->setLoggingOffset(-2); // make most messages from the HistoDAE
// invisible to default logging levels
histoDAE->setProperty("NPeriods", nper);
histoDAE->setProperty("NSpectra", nspec);
histoDAE->setProperty("Port", port + 1);
Poco::ActiveResult<bool> histoDAEHandle = histoDAE->executeAsync();
auto prog = boost::make_shared<Progress>(this, 0.0, 1.0, 100);
prog->setNotifyStep(0);
prog->report(0, "Waiting for client");
std::lock_guard<std::mutex> lock(m_mutex);
Poco::Net::ServerSocket socket(static_cast<Poco::UInt16>(port));
socket.listen();
m_server = new Poco::Net::TCPServer(
TestServerConnectionFactory::Ptr(
new TestServerConnectionFactory(nper, nspec, rate, nevents, prog)),
socket);
m_server->start();
// Keep going until you get cancelled
while (true) {
try {
// Exit if the user presses cancel
interruption_point();
} catch (...) {
// This is caught as a result of cancel being set - so exit the loop
break;
}
// Sleep for 50 msec
Poco::Thread::sleep(50);
}
histoDAE->cancel();
histoDAEHandle.wait();
if (m_server) {
m_server->stop();
m_server = nullptr;
}
socket.close();
prog->report(90, "Closing ISIS event DAE");
histoDAE->setLogging(false); // hide the final closedown message to the log it
// is confusing as it is a child alg.
histoDAE->cancel();
histoDAEHandle.wait();
}
} // namespace LiveData
} // namespace Mantid