-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiasServiceManager.cpp
More file actions
171 lines (134 loc) · 4.21 KB
/
iasServiceManager.cpp
File metadata and controls
171 lines (134 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* File: iasServiceManager.cpp
* Author: georgez
*
* Created on 27 October 2014, 12:35
*
* serviceManager is responsible for managing the services provided
* by the application. Applications can create "services" and register
* them with the Service manager. SM is responsible for the internal
* starting, publicising and connections to the services. Services will
* be registered through configuration scripts or internal application
* logic at startup.
*
*
*
*
*
*/
#include "iasServiceManager.h"
#include "iaService.h"
iasServiceManager::iasServiceManager(iasServer* ps){
_pServer=ps;
// Set up available slots, slot 0 is always empty
for(int x=1;x<32;x++){
_available_slots.push(x);
}
}
/**
*
* @param : Name of Service being checked
* @return : true if service exists
*/
bool iasServiceManager::serviceExists(std::string sn){
std::lock_guard<std::mutex> lock(_mx);
auto s=_active_services.find(sn);
return s!=_active_services.end();
}
/*
*
* @param : Name of service requested
* @return : Pointer to Service or nullptr
*/
iaService* iasServiceManager::getService(std::string sn){
std::lock_guard<std::mutex> lock(_mx);
auto s=_active_services.find(sn);
if(s!=_active_services.end()){
return _service_slots[s->second].get();
}
return nullptr;
}
/**
* Fast access to service. No checking is done but pointers in the
* service_slots array ALWAYS point to a valid object, even if it
* points to a dummy iaService
*
*
* @param id // Service id
* @return // Pointer to iaAervice
*/
iaService* iasServiceManager::getService(uint id){
return _service_slots[id].get();
}
/*
* This function registers a service but does not start it
*
*
* @param sname : Service name
* @param pMaker : Factory Function for creating service
* @return : true for success, false for failure
*/
bool iasServiceManager::registerService(std::string sname,
iaService* (*pMaker)()){
// simply add service & factory method to internal map
std::lock_guard<std::mutex> lock(_mx);
_registered_services[sname]=pMaker;
return false;
}
/**
*
* @param : Service Name
* @return : success or failure
*/
bool iasServiceManager::createService(std::string sn){
bool result=false;
// guard the shared resources accessed here
std::lock_guard<std::mutex> lock(_mx);
// Check if service already active
if(_active_services.find(sn) == _active_services.end()){
// Get the factory method
auto s=_registered_services.find(sn);
if(s!=_registered_services.end()){
// create the service object
std::shared_ptr<iaService> pService(s->second());
// Get available slot
if(!_available_slots.empty()){
uint serviceid=_available_slots.front();
_available_slots.pop();
// Map Serviceid to service name, entry in this
// map controls the lifetime of the service
_active_services[sn]=serviceid;
// Add raw service pointer to service vector
_service_slots[serviceid]=pService;
// Initialise Service
result=pService->initialise(this);
}else{
// No Free Slots
}
}else{
// Service NOT registered
}
}else{
result=true;
}
return result;
}
/**
*
* @param : Service Name
* @return : success or failure
*/
bool iasServiceManager::isActive(std::string sn){
std::lock_guard<std::mutex> lock(_mx);
return _active_services.find(sn) != _active_services.end();
}
uint iasServiceManager::getServiceID(std::string sn){
if(createService(sn)){
std::lock_guard<std::mutex> lock(_mx);
auto s =_active_services.find(sn);
if(s!=_active_services.end()){
return s->second;
}
}
return 0;
}