OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
ServiceManager.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <map>
7#include <string>
8#include <vector>
9#include <list>
10#include <mutex>
11
12#include "OTCore/JSON.h"
13#include "OTSystem/Network.h"
14
15class Service;
16namespace std { class thread; }
17
19public:
21 virtual ~ServiceManager();
22
26 void addToJsonObject(ot::JsonValue& _object, ot::JsonAllocator& _allocator);
27
28 // ##################################################################################################################################
29
30 // Setter/Getter
31
32 void setServiceIP(const std::string& _ip) { m_servicesIpAddress = _ip; }
33
34 const std::string& lastError(void) const { return m_lastError; }
35
36 void setSiteID(const std::string& _siteID) { m_siteID = _siteID; }
37 const std::string& siteID(void) const { return m_siteID; }
38
39 // ##################################################################################################################################
40
41 // Service control
42
47 bool requestStartService(const SessionInformation& _sessionInformation, const ServiceInformation& _serviceInformation);
48
49 bool requestStartRelayService(const SessionInformation& _sessionInformation, std::string& _websocketUrl, std::string& _relayServiceURL);
50
51 void sessionClosed(const std::string& _sessionID);
52
53 void serviceDisconnected(const std::string& _sessionID, const ServiceInformation& _info, const std::string& _serviceURL);
54
55 void GetSessionInformation(ot::JsonArray &sessionInfo, ot::JsonAllocator &allocator);
56
57private:
58
59 struct RequestedService {
60 SessionInformation session;
61 ServiceInformation service;
62 unsigned int startAttempt;
63 };
64 struct InitializingService {
65 Service * service;
66 RequestedService startInfo;
67 int initializeAttempt;
68 };
69
70 void runThreads(void);
71
72 void serviceStartFailed(RequestedService _info);
73 void sendInitializeMessage(InitializingService _info);
74 void serviceInitializeFailed(InitializingService _info);
75
76 std::vector<Service *> * sessionServices(const SessionInformation& _sessionInformation);
77 bool restartServiceAfterCrash(const SessionInformation& _sessionInformation, Service * _service);
78 void notifySessionEmergencyShutdown(const SessionInformation& _sessionInformation, Service * _crashedService);
79
80 void workerServiceStarter(void);
81 void workerServiceInitializer(void);
82 void workerHealthCheck(void);
83 void workerServiceStopper(void);
84
85 std::string m_servicesIpAddress;
86 std::string m_lastError;
87 std::string m_siteID;
88
89 bool m_isShuttingDown;
90
91 std::thread * m_threadServiceStarter;
92 std::thread * m_threadServiceInitializer;
93 std::thread * m_threadHealthCheck;
94 std::thread * m_threadServiceStopper;
95
96 std::map<SessionInformation, std::vector<Service *> *> m_services;
97 std::mutex m_mutexServices;
98
99 std::list<RequestedService> m_requestedServices;
100 std::mutex m_mutexRequestedServices;
101
102 std::list<InitializingService> m_initializingServices;
103 std::mutex m_mutexInitializingServices;
104
105 std::list<Service *> m_stoppingServices;
106 std::mutex m_mutexStoppingServices;
107};
const char const char const char * _sessionID
Definition dllmain.cpp:62
const char * _serviceURL
Definition dllmain.cpp:93
Definition Service.h:17
Definition ServiceInformation.h:6
Definition ServiceManager.h:18
void sessionClosed(const std::string &_sessionID)
Definition ServiceManager.cpp:226
const std::string & siteID(void) const
Definition ServiceManager.h:37
void serviceDisconnected(const std::string &_sessionID, const ServiceInformation &_info, const std::string &_serviceURL)
Definition ServiceManager.cpp:276
void setServiceIP(const std::string &_ip)
Definition ServiceManager.h:32
const std::string & lastError(void) const
Definition ServiceManager.h:34
bool requestStartService(const SessionInformation &_sessionInformation, const ServiceInformation &_serviceInformation)
Will attempt to start a new service.
Definition ServiceManager.cpp:125
virtual ~ServiceManager()
Definition ServiceManager.cpp:24
void setSiteID(const std::string &_siteID)
Definition ServiceManager.h:36
ServiceManager()
Definition ServiceManager.cpp:17
bool requestStartRelayService(const SessionInformation &_sessionInformation, std::string &_websocketUrl, std::string &_relayServiceURL)
Definition ServiceManager.cpp:148
void addToJsonObject(ot::JsonValue &_object, ot::JsonAllocator &_allocator)
Add the object contents to the provided JSON object.
Definition ServiceManager.cpp:28
void GetSessionInformation(ot::JsonArray &sessionInfo, ot::JsonAllocator &allocator)
Definition ServiceManager.cpp:614
Definition SessionInformation.h:6
JSON Array value.
Definition JSON.h:116
rapidjson::Value JsonValue
Writable JSON value.
Definition JSON.h:27
rapidjson::MemoryPoolAllocator< rapidjson::CrtAllocator > JsonAllocator
Allocator used for writing to JSON values.
Definition JSON.h:30
STL namespace.