|
| 1 | +#ifndef OPENCOVER_DATACLIENT_H |
| 2 | +#define OPENCOVER_DATACLIENT_H |
| 3 | + |
| 4 | +#include "export.h" |
| 5 | +#include "MultiDimensionalArray.h" |
| 6 | +#include "ObserverHandle.h" |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | +#include <functional> |
| 10 | +#include <memory> |
| 11 | +#include <string> |
| 12 | +#include <typeindex> |
| 13 | +#include <typeinfo> |
| 14 | +#include <vector> |
| 15 | +#include <mutex> |
| 16 | + |
| 17 | +namespace opencover { namespace dataclient { |
| 18 | + |
| 19 | +extern DATACLIENTEXPORT const char *NoNodeName; |
| 20 | + |
| 21 | +class DATACLIENTEXPORT Client { |
| 22 | +public: |
| 23 | + virtual ~Client() = default; |
| 24 | + |
| 25 | + // Connection lifecycle |
| 26 | + virtual void connect() = 0; |
| 27 | + virtual void disconnect() = 0; |
| 28 | + virtual bool isConnected() const = 0; |
| 29 | + enum StatusChange{ Unchanged, Connected, Disconnected}; |
| 30 | + StatusChange statusChanged(void* caller); |
| 31 | + |
| 32 | + |
| 33 | + //get the available data nodes from the server |
| 34 | + std::vector<std::string> allAvailableScalars() const; |
| 35 | + std::vector<std::string> availableNumericalScalars() const; |
| 36 | + template<typename T> |
| 37 | + std::vector<std::string> availableScalars() const |
| 38 | + { |
| 39 | + return getNodesWith(std::type_index(typeid(T)), true); |
| 40 | + } |
| 41 | + std::vector<std::string> allAvailableArrays() const; |
| 42 | + std::vector<std::string> availableNumericalArrays() const; |
| 43 | + template<typename T> |
| 44 | + std::vector<std::string> availableArrays() const |
| 45 | + { |
| 46 | + return getNodesWith(std::type_index(typeid(T)), false); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + //register nodes to get updates pushed by the server |
| 51 | + //keep the ObserverHandle as long as you want to observe |
| 52 | + virtual [[nodiscard]] ObserverHandle observeNode(const std::string &name) = 0; |
| 53 | + |
| 54 | + // Pull-style access |
| 55 | + virtual double getNumericScalar(const std::string &name, double *timestep = nullptr) = 0; |
| 56 | + virtual double getNumericScalar(const ObserverHandle &handle, double *timestep = nullptr) = 0; |
| 57 | + |
| 58 | + virtual size_t numNodeUpdates(const std::string &name) = 0; |
| 59 | + template<typename T> |
| 60 | + MultiDimensionalArray<T> getArray(const std::string &name) |
| 61 | + { |
| 62 | + auto p = getArrayImpl(std::type_index(typeid(T)), name); |
| 63 | + if (!p) return MultiDimensionalArray<T>{}; |
| 64 | + if (auto casted = dynamic_cast<MultiDimensionalArray<T>*>(p.get())) { |
| 65 | + return *casted; // copies the MultiDimensionalArray<T> contents |
| 66 | + } |
| 67 | + return MultiDimensionalArray<T>{}; |
| 68 | + } |
| 69 | + |
| 70 | + //called by ObserverHandle |
| 71 | + void queueUnregisterNode(size_t id); |
| 72 | + |
| 73 | +protected: |
| 74 | + void statusChanged(); |
| 75 | + Client** getClientReference(const ObserverHandle &handle); |
| 76 | +private: |
| 77 | + |
| 78 | + virtual std::unique_ptr<detail::MultiDimensionalArrayBase> getArrayImpl(std::type_index type, const std::string &name) = 0; |
| 79 | + virtual std::vector<std::string> getNodesWith(std::type_index type, bool isScalar) const = 0; |
| 80 | + virtual std::vector<std::string> getNodesWith(bool isArithmetic, bool isScalar) const = 0; |
| 81 | + std::mutex m_mutex; |
| 82 | + std::vector<size_t> m_nodesToUnregister; |
| 83 | + std::map<void*, bool> m_statusObservers; //stores which objects received a changed status |
| 84 | + bool m_connected = false; |
| 85 | +}; |
| 86 | + |
| 87 | +}} // namespace |
| 88 | + |
| 89 | +#endif // OPENCOVER_DATACLIENT_H |
0 commit comments