1 | #include "store_filter.hpp" |
---|
2 | #include "context.hpp" |
---|
3 | #include "grid.hpp" |
---|
4 | #include "timer.hpp" |
---|
5 | |
---|
6 | namespace xios |
---|
7 | { |
---|
8 | CStoreFilter::CStoreFilter(CGarbageCollector& gc, CContext* context, CGrid* grid) |
---|
9 | : CInputPin(gc, 1) |
---|
10 | , context(context) |
---|
11 | , grid(grid) |
---|
12 | { |
---|
13 | if (!context) |
---|
14 | ERROR("CStoreFilter::CStoreFilter(CContext* context, CGrid* grid)", |
---|
15 | "Impossible to construct a store filter without providing a context."); |
---|
16 | if (!grid) |
---|
17 | ERROR("CStoreFilter::CStoreFilter(CContext* context, CGrid* grid)", |
---|
18 | "Impossible to construct a store filter without providing a grid."); |
---|
19 | } |
---|
20 | |
---|
21 | CConstDataPacketPtr CStoreFilter::getPacket(Time timestamp) |
---|
22 | { |
---|
23 | CTimer timer("CStoreFilter::getPacket"); |
---|
24 | CConstDataPacketPtr packet; |
---|
25 | const double timeout = 10; // 10 seconds timeout |
---|
26 | |
---|
27 | do |
---|
28 | { |
---|
29 | timer.resume(); |
---|
30 | |
---|
31 | std::map<Time, CDataPacketPtr>::const_iterator it = packets.find(timestamp); |
---|
32 | if (it != packets.end()) |
---|
33 | packet = it->second; |
---|
34 | else // if the packet is not available yet, check if it can be received |
---|
35 | context->checkBuffersAndListen(); |
---|
36 | |
---|
37 | timer.suspend(); |
---|
38 | } while (!packet && timer.getCumulatedTime() < timeout); |
---|
39 | |
---|
40 | if (!packet) |
---|
41 | ERROR("CConstDataPacketPtr CStoreFilter::getPacket(Time timestamp) const", |
---|
42 | << "Impossible to get the packet with timestamp = " << timestamp); |
---|
43 | |
---|
44 | return packet; |
---|
45 | } |
---|
46 | |
---|
47 | template <int N> |
---|
48 | CDataPacket::StatusCode CStoreFilter::getData(Time timestamp, CArray<double, N>& data) |
---|
49 | { |
---|
50 | CConstDataPacketPtr packet = getPacket(timestamp); |
---|
51 | |
---|
52 | if (packet->status == CDataPacket::NO_ERROR) |
---|
53 | grid->outputField(packet->data, data); |
---|
54 | |
---|
55 | return packet->status; |
---|
56 | } |
---|
57 | |
---|
58 | template CDataPacket::StatusCode CStoreFilter::getData<1>(Time timestamp, CArray<double, 1>& data); |
---|
59 | template CDataPacket::StatusCode CStoreFilter::getData<2>(Time timestamp, CArray<double, 2>& data); |
---|
60 | template CDataPacket::StatusCode CStoreFilter::getData<3>(Time timestamp, CArray<double, 3>& data); |
---|
61 | |
---|
62 | void CStoreFilter::onInputReady(std::vector<CDataPacketPtr> data) |
---|
63 | { |
---|
64 | packets.insert(std::make_pair(data[0]->timestamp, data[0])); |
---|
65 | // The packet is always destroyed by the garbage collector |
---|
66 | // so we register but never unregister |
---|
67 | gc.registerFilter(this, data[0]->timestamp); |
---|
68 | } |
---|
69 | |
---|
70 | void CStoreFilter::invalidate(Time timestamp) |
---|
71 | { |
---|
72 | CInputPin::invalidate(timestamp); |
---|
73 | packets.erase(packets.begin(), packets.lower_bound(timestamp)); |
---|
74 | } |
---|
75 | } // namespace xios |
---|