roo_transceivers
API Documentation for roo_transceivers
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <vector>
5
6#include "roo_collections.h"
7#include "roo_collections/flat_small_hash_map.h"
8#include "roo_collections/flat_small_hash_set.h"
9#include "roo_threads.h"
10#include "roo_threads/mutex.h"
11#include "roo_transceivers.h"
12#include "roo_transceivers/id.h"
15
16namespace roo_transceivers {
17
18/// Communication channel for UniverseClient.
20 public:
22 std::function<void(const roo_transceivers_ServerMessage&)>;
23
24 virtual ~UniverseClientChannel() = default;
25
27
29};
30
31/// Universe that mirrors a remote universe via a bidirectional channel.
32class UniverseClient : public Universe {
33 public:
38
40
42
43 void begin();
44
45 size_t deviceCount() const override;
46
47 bool forEachDevice(
48 std::function<bool(const DeviceLocator&)> callback) const override;
49
51 const DeviceLocator& locator,
52 roo_transceivers_Descriptor& descriptor) const override;
53
54 Measurement read(const SensorLocator& locator) const override;
55
56 bool write(const ActuatorLocator& locator, float value) override;
57
58 void requestUpdate() override;
59
60 void addEventListener(EventListener* listener) override;
61
62 void removeEventListener(EventListener* listener) override;
63
64 private:
65 // Convenience function to lookup a descriptor by device locator. Additionally
66 // returns the descriptor key, assigned by the server.
67 const roo_transceivers_Descriptor* lookupDeviceDescriptor(
68 const DeviceLocator& locator, int& descriptor_key) const;
69
70 // Called when a message is received from the server. Returns true on success;
71 // false on protocol error. If returns false, the client will attempt to
72 // recover by issuing RequestState.
73 bool handleServerMessage(const roo_transceivers_ServerMessage& msg);
74
75 bool handleInit();
76
77 bool handleDescriptorAdded(int key,
78 const roo_transceivers_Descriptor& descriptor);
79
80 bool handleDescriptorRemoved(int key);
81
82 bool handleDevice(const DeviceLocator& locator, int descriptor_key);
83
84 bool handleDeviceAdded(const DeviceLocator& locator, int descriptor_key);
85
86 bool handleDeviceRemoved(int prev_index);
87
88 bool handleDevicePreserved(int prev_index_first, size_t count);
89
90 bool handleDeviceModified(int prev_index, int descriptor_key);
91
92 // Helper method to propagate the devices event to listeners.
93 void notifyDevicesChanged();
94
95 // Helper method to propagate the readings event to listeners.
96 void notifyReadingsAvailable();
97
98 void clearAll();
99
100 bool handleUpdateBegin(bool delta);
101
102 bool handleUpdateEnd();
103
104 bool handleReadingsBegin();
105
106 bool handleReadings(
107 const DeviceLocator& device,
109 size_t readings_count);
110
111 bool handleReadingsEnd();
112
113 // The channel used to synchronize the state (i.e., receive measurements from
114 // and push writes to) with the remote universe.
115 UniverseClientChannel& channel_;
116
117 // Stores all descriptors for devices handled by the remote universe. The
118 // descriptors are assigned integer keys by the remote universe. These are
119 // used as keys into this map.
120 roo_collections::FlatSmallHashMap<int, roo_transceivers_Descriptor>
121 descriptors_;
122
123 // For each device known to the remote universe, points to the key of
124 // the descriptor for that device. That key can be used to look up the
125 // descriptor in the descriptors_ map.
126 roo_collections::FlatSmallHashMap<DeviceLocator, int> device_idx_by_locator_;
127
128 std::vector<DeviceEntry> devices_;
129
130 std::vector<DeviceEntry> updated_devices_;
131
132 bool synced_;
133
134 // Cached sensor measurements.
135 roo_collections::FlatSmallHashMap<SensorLocator, Measurement> readings_;
136
137 // Actuators by actuator ID per device type. Allows us to quickly verify that
138 // an actuator locator used for write actually does exist, before sending the
139 // write request to the server.
140 roo_collections::FlatSmallHashSet<ActuatorLocator> actuators_;
141
142 // Event listeners for state updates.
143 roo_collections::FlatSmallHashSet<EventListener*> listeners_;
144
145 mutable roo::mutex state_guard_;
146 mutable roo::mutex listener_guard_;
147};
148
149} // namespace roo_transceivers
Identifies actuator within a transceiver device.
Definition id.h:104
Identifies a transceiver device by schema and device id.
Definition id.h:21
Listener for universe-level change notifications.
Definition notification.h:9
Measurement of a quantity at a specific time.
Definition measurement.h:11
Identifies sensor within a transceiver device.
Definition id.h:57
Communication channel for UniverseClient.
Definition client.h:19
virtual void registerServerMessageCallback(ServerMessageCb cb)=0
virtual void sendClientMessage(const roo_transceivers_ClientMessage &msg)=0
std::function< void(const roo_transceivers_ServerMessage &)> ServerMessageCb
Definition client.h:22
Universe that mirrors a remote universe via a bidirectional channel.
Definition client.h:32
void removeEventListener(EventListener *listener) override
Removes a previously registered event listener.
Definition client.cpp:132
size_t deviceCount() const override
Returns the total number of transceiver devices in this universe.
Definition client.cpp:30
Measurement read(const SensorLocator &locator) const override
Returns the latest known reading of the sensor identified by locator.
Definition client.cpp:89
bool forEachDevice(std::function< bool(const DeviceLocator &)> callback) const override
Iterates over all transceiver devices in this universe, calling callback for each device.
Definition client.cpp:35
void requestUpdate() override
Requests sensor reading update from underlying devices.
Definition client.cpp:123
void addEventListener(EventListener *listener) override
Registers a listener for device-set and reading update events.
Definition client.cpp:127
bool write(const ActuatorLocator &locator, float value) override
Writes to the actuator identified by locator.
Definition client.cpp:102
bool getDeviceDescriptor(const DeviceLocator &locator, roo_transceivers_Descriptor &descriptor) const override
Retrieves the descriptor for the transceiver identified by locator.
Definition client.cpp:78
An abstract collection of transceiver devices.
Definition universe.h:19