1 | #include "input_pin.hpp" |
---|
2 | #include "output_pin.hpp" |
---|
3 | #include "garbage_collector.hpp" |
---|
4 | #include "exception.hpp" |
---|
5 | #include "workflow_graph.hpp" |
---|
6 | |
---|
7 | namespace xios |
---|
8 | { |
---|
9 | CInputPin::CInputPin(CGarbageCollector& gc, size_t slotsCount) |
---|
10 | : gc(gc) |
---|
11 | , slotsCount(slotsCount) |
---|
12 | , triggers(slotsCount) |
---|
13 | , hasTriggers(false) |
---|
14 | { } |
---|
15 | |
---|
16 | StdString CInputPin::GetName(void) |
---|
17 | { |
---|
18 | return StdString("Input pin"); |
---|
19 | } |
---|
20 | |
---|
21 | void CInputPin::setInput(size_t inputSlot, CDataPacketPtr packet) |
---|
22 | { |
---|
23 | if (inputSlot >= slotsCount) |
---|
24 | ERROR("void CInputPin::setInput(size_t inputSlot, CDataPacketPtr packet)", |
---|
25 | "The input slot " << inputSlot << " does not exist."); |
---|
26 | if (!packet) |
---|
27 | ERROR("void CInputPin::setInput(size_t inputSlot, CDataPacketPtr packet)", |
---|
28 | "The packet cannot be null."); |
---|
29 | |
---|
30 | std::map<Time, InputBuffer>::iterator it = inputs.find(packet->timestamp); |
---|
31 | if (it == inputs.end()) |
---|
32 | { |
---|
33 | it = inputs.insert(std::make_pair(packet->timestamp, InputBuffer(slotsCount))).first; |
---|
34 | gc.registerObject(this, packet->timestamp); |
---|
35 | } |
---|
36 | it->second.slotsFilled++; |
---|
37 | it->second.packets[inputSlot] = packet; |
---|
38 | |
---|
39 | if (it->second.slotsFilled == slotsCount) |
---|
40 | { |
---|
41 | // Unregister before calling onInputReady in case the filter registers again |
---|
42 | gc.unregisterObject(this, packet->timestamp); |
---|
43 | onInputReady(it->second.packets); |
---|
44 | inputs.erase(it); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | void CInputPin::setInputTrigger(size_t inputSlot, COutputPin* trigger) |
---|
49 | { |
---|
50 | if (inputSlot >= slotsCount) |
---|
51 | ERROR("void CInputPin::setInputTrigger(size_t inputSlot, COutputPin* trigger)", |
---|
52 | "The input slot " << inputSlot << " does not exist."); |
---|
53 | if (triggers[inputSlot]) |
---|
54 | ERROR("void CInputPin::setInputTrigger(size_t inputSlot, COutputPin* trigger)", |
---|
55 | "The trigger for input slot " << inputSlot << " has already been set."); |
---|
56 | |
---|
57 | triggers[inputSlot] = trigger; |
---|
58 | hasTriggers = true; |
---|
59 | } |
---|
60 | |
---|
61 | void CInputPin::trigger(Time timestamp) |
---|
62 | { |
---|
63 | if (hasTriggers) // Don't use canBeTriggered here, this function is virtual and can be overriden |
---|
64 | { |
---|
65 | std::map<Time, InputBuffer>::iterator it = inputs.find(timestamp); |
---|
66 | bool nothingReceived = (it == inputs.end()); |
---|
67 | |
---|
68 | for (size_t s = 0; s < slotsCount; s++) |
---|
69 | { |
---|
70 | if (triggers[s] && (nothingReceived || !it->second.packets[s])) |
---|
71 | triggers[s]->trigger(timestamp); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | bool CInputPin::canBeTriggered() const |
---|
77 | { |
---|
78 | return hasTriggers; |
---|
79 | } |
---|
80 | |
---|
81 | void CInputPin::invalidate(Time timestamp) |
---|
82 | { |
---|
83 | inputs.erase(inputs.begin(), inputs.lower_bound(timestamp)); |
---|
84 | } |
---|
85 | } // namespace xios |
---|