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