SOLA
Loading...
Searching...
No Matches
sola.h
1// Copyright The SOLA Contributors
2//
3// Licensed under the MIT License.
4// For details on the licensing terms, see the LICENSE file.
5// SPDX-License-Identifier: MIT
6
7#ifndef SOLA_SOLA_H_
8#define SOLA_SOLA_H_
9
10#include <any>
11#include <cstdint>
12#include <functional>
13#include <future>
14#include <iostream>
15#include <map>
16#include <memory>
17#include <string>
18#include <vector>
19
20#include "SOLA/logger_interface.h"
21#include "event_dissemination/event_dissemination.h"
22#include "message.h"
23#include "service.h"
24#include "solanet/uuid.h"
25#include "solanet/uuid_generator.h"
26#include "storage/storage.h"
27
28namespace sola {
29
30template <typename StorageT, typename EventDisseminationT> class SOLA {
31 static_assert(std::is_base_of<Storage, StorageT>());
32 static_assert(std::is_base_of<EventDissemination, EventDisseminationT>());
33
34public:
42 SOLA(const typename StorageT::Config &storage_config,
43 const typename EventDisseminationT::Config &event_dissemination_config,
44 TopicMessageReceiveFct topic_recv, LoggerPtr logger)
45 : storage_(std::make_shared<StorageT>(storage_config)),
46 ed_(std::make_unique<EventDisseminationT>(
47 [this, topic_recv](const TopicMessage &m) {
48 logger_->logReceiveTopicMessage(m);
49 topic_recv(m);
50 },
51 storage_, event_dissemination_config, logger)),
52 logger_(std::move(logger)) {
53 logger_->setApplicationUUID(solanet::uuidToString(uuid_));
54 }
55
56 void stop() {
57 storage_->stop();
58 ed_->stop();
59 }
60
61 bool canStop() const { return storage_->canStop() && ed_->canStop(); }
62
63 //***** CRUD operations for service
64 void addService(Service service) {
65 std::cout << "PUBLISH SERVICE" << std::endl;
66 std::vector<Entry> entry;
67
68 for (auto [key, value] : service.key_values) {
69 if (value.type() == typeid(std::string)) {
70 entry.emplace_back(key, std::any_cast<std::string>(value));
71 } else if (value.type() == typeid(float)) {
72 entry.emplace_back(key, std::any_cast<float>(value));
73 } else if (value.type() == typeid(int)) {
74 entry.emplace_back(key, std::any_cast<int>(value));
75 } else {
76 throw std::runtime_error("invalid any");
77 }
78 }
79
80 storage_->insert(entry);
81 }
82
83 std::future<minhton::FindResult> findService(Request r) { return storage_->find(r); }
84
85 // event dissemination
86 void subscribeTopic(const std::string &topic) {
87 logger_->logSubscribeTopic(topic);
88 ed_->subscribe(topic);
89 }
90
91 void unsubscribeTopic(const std::string &topic) {
92 logger_->logUnsubscribeTopic(topic);
93 ed_->unsubscribe(topic);
94 }
95
96 solanet::UUID publishMessage(const std::string &topic, const std::string &message) {
97 sola::TopicMessage msg{topic, uuid_, message, solanet::generateUUID()};
98 logger_->logPublishTopicMessage(msg);
99 ed_->publish(msg);
100 return msg.uuid;
101 }
102
103 bool isStorageRunning() const { return storage_->isRunning(); }
104
105private:
106 std::shared_ptr<StorageT> storage_;
107 std::unique_ptr<EventDisseminationT> ed_;
108 LoggerPtr logger_;
109 solanet::UUID uuid_ = solanet::generateUUID();
110};
111} // namespace sola
112
113#endif // SOLA_SOLA_H_
Definition sola.h:30
SOLA(const typename StorageT::Config &storage_config, const typename EventDisseminationT::Config &event_dissemination_config, TopicMessageReceiveFct topic_recv, LoggerPtr logger)
Starts the SOLA instance.
Definition sola.h:42
Definition message.h:20