Line |
Branch |
Exec |
Source |
1 |
|
|
/* GATE PROJECT LICENSE: |
2 |
|
|
+----------------------------------------------------------------------------+ |
3 |
|
|
| Copyright(c) 2018-2025, Stefan Meislinger | |
4 |
|
|
| All rights reserved. | |
5 |
|
|
| | |
6 |
|
|
| Redistribution and use in source and binary forms, with or without | |
7 |
|
|
| modification, are permitted provided that the following conditions are met:| |
8 |
|
|
| | |
9 |
|
|
| 1. Redistributions of source code must retain the above copyright notice, | |
10 |
|
|
| this list of conditions and the following disclaimer. | |
11 |
|
|
| 2. Redistributions in binary form must reproduce the above copyright | |
12 |
|
|
| notice, this list of conditions and the following disclaimer in the | |
13 |
|
|
| documentation and/or other materials provided with the distribution. | |
14 |
|
|
| | |
15 |
|
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| |
16 |
|
|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 |
|
|
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 |
|
|
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
19 |
|
|
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 |
|
|
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 |
|
|
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 |
|
|
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 |
|
|
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 |
|
|
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
25 |
|
|
| THE POSSIBILITY OF SUCH DAMAGE. | |
26 |
|
|
+----------------------------------------------------------------------------+ |
27 |
|
|
*/ |
28 |
|
|
|
29 |
|
|
#include "gate/system/services.hpp" |
30 |
|
|
|
31 |
|
|
namespace gate |
32 |
|
|
{ |
33 |
|
|
namespace sys |
34 |
|
|
{ |
35 |
|
|
|
36 |
|
|
uint32_t const ServiceInfo::Flag_Autostart = GATE_SERVICE_FLAG_AUTOSTART; |
37 |
|
|
uint32_t const ServiceInfo::Flag_Disabled = GATE_SERVICE_FLAG_DISABLED; |
38 |
|
|
|
39 |
|
✗ |
ServiceInfo::ServiceInfo(String const& name, String const& description, String const& address, uint32_t flags) |
40 |
|
|
{ |
41 |
|
✗ |
name.copyTo(this->impl.name, sizeof(this->impl.name)); |
42 |
|
✗ |
description.copyTo(this->impl.description, sizeof(this->impl.description)); |
43 |
|
✗ |
address.copyTo(this->impl.address, sizeof(this->impl.address)); |
44 |
|
✗ |
this->impl.flags = flags; |
45 |
|
✗ |
} |
46 |
|
✗ |
ServiceInfo::ServiceInfo(gate_service_t const& svc) |
47 |
|
|
{ |
48 |
|
✗ |
gate_mem_copy(&this->impl, &svc, sizeof(this->impl)); |
49 |
|
✗ |
} |
50 |
|
|
|
51 |
|
✗ |
String ServiceInfo::getName() const |
52 |
|
|
{ |
53 |
|
✗ |
return String(this->impl.name); |
54 |
|
|
} |
55 |
|
✗ |
String ServiceInfo::getDescription() const |
56 |
|
|
{ |
57 |
|
✗ |
return String(this->impl.description); |
58 |
|
|
} |
59 |
|
✗ |
String ServiceInfo::getAddress() const |
60 |
|
|
{ |
61 |
|
✗ |
return String(this->impl.address); |
62 |
|
|
} |
63 |
|
✗ |
uint32_t ServiceInfo::getFlags() const |
64 |
|
|
{ |
65 |
|
✗ |
return this->impl.flags; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
✗ |
ServiceConfig::ServiceConfig(String const& name, String const& description, uint32_t flags, |
71 |
|
✗ |
String const& command, Array<String> const& dependencies) |
72 |
|
|
{ |
73 |
|
✗ |
name.copyTo(this->impl.name, sizeof(this->impl.name)); |
74 |
|
✗ |
description.copyTo(this->impl.description, sizeof(this->impl.description)); |
75 |
|
✗ |
this->impl.flags = flags; |
76 |
|
✗ |
command.copyTo(this->impl.command, sizeof(this->impl.command)); |
77 |
|
|
|
78 |
|
✗ |
StringBuilder builder(this->impl.dependencies, sizeof(this->impl.dependencies), 0); |
79 |
|
✗ |
for (Array<String>::const_iterator it(dependencies.begin()), end(dependencies.end()); ; ) |
80 |
|
|
{ |
81 |
|
✗ |
builder << *it; |
82 |
|
✗ |
++it; |
83 |
|
✗ |
if (it != end) |
84 |
|
|
{ |
85 |
|
✗ |
builder << ","; |
86 |
|
|
} |
87 |
|
|
else |
88 |
|
|
{ |
89 |
|
✗ |
break; |
90 |
|
|
} |
91 |
|
|
} |
92 |
|
✗ |
} |
93 |
|
✗ |
ServiceConfig::ServiceConfig(gate_service_config_t const& cfg) |
94 |
|
|
{ |
95 |
|
✗ |
gate_mem_copy(&this->impl, &cfg, sizeof(cfg)); |
96 |
|
✗ |
} |
97 |
|
✗ |
String ServiceConfig::getName() const |
98 |
|
|
{ |
99 |
|
✗ |
return String(this->impl.name); |
100 |
|
|
} |
101 |
|
✗ |
String ServiceConfig::getDescription() const |
102 |
|
|
{ |
103 |
|
✗ |
return String(this->impl.description); |
104 |
|
|
} |
105 |
|
✗ |
uint32_t ServiceConfig::getFlags() const |
106 |
|
|
{ |
107 |
|
✗ |
return this->impl.flags; |
108 |
|
|
} |
109 |
|
✗ |
String ServiceConfig::getCommand() const |
110 |
|
|
{ |
111 |
|
✗ |
return String(this->impl.command); |
112 |
|
|
} |
113 |
|
✗ |
Array<String> ServiceConfig::getDependencies() const |
114 |
|
|
{ |
115 |
|
✗ |
ArrayList<String> deps; |
116 |
|
✗ |
static String const separator = String::createStatic(",", 1); |
117 |
|
✗ |
String text(this->impl.dependencies); |
118 |
|
✗ |
String head, tail; |
119 |
|
✗ |
while (!text.empty()) |
120 |
|
|
{ |
121 |
|
✗ |
if (text.parse(separator, 0, &head, &tail, false)) |
122 |
|
|
{ |
123 |
|
✗ |
deps.add(head); |
124 |
|
✗ |
text = tail; |
125 |
|
|
} |
126 |
|
|
else |
127 |
|
|
{ |
128 |
|
|
// last token |
129 |
|
✗ |
deps.add(text); |
130 |
|
✗ |
text = String(); |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
✗ |
return deps.toArray(); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
|
✗ |
static gate_bool_t ServiceMgr_enum_callback(gate_service_t const* service, void* user_param) |
139 |
|
|
{ |
140 |
|
✗ |
ServiceMgr::EnumCallback const* cb = (ServiceMgr::EnumCallback const*)user_param; |
141 |
|
|
try |
142 |
|
|
{ |
143 |
|
✗ |
ServiceInfo info(*service); |
144 |
|
✗ |
cb->invoke(info); |
145 |
|
|
} |
146 |
|
✗ |
catch (...) {} |
147 |
|
✗ |
return true; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
✗ |
static gate_bool_t ServiceMgr_get_services_callback(gate_service_t const* service, void* user_param) |
151 |
|
|
{ |
152 |
|
✗ |
ArrayList<ServiceInfo>* ptrLst = (ArrayList<ServiceInfo>*)user_param; |
153 |
|
|
try |
154 |
|
|
{ |
155 |
|
✗ |
ServiceInfo info(*service); |
156 |
|
✗ |
ptrLst->add(info); |
157 |
|
|
} |
158 |
|
✗ |
catch (...) {} |
159 |
|
✗ |
return true; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
✗ |
void ServiceMgr::enumServices(EnumCallback const& cb) |
163 |
|
|
{ |
164 |
|
✗ |
result_t result = gate_services_enum(&ServiceMgr_enum_callback, (void*)(&cb)); |
165 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
166 |
|
✗ |
} |
167 |
|
✗ |
Array<ServiceInfo> ServiceMgr::getServices() |
168 |
|
|
{ |
169 |
|
✗ |
ArrayList<ServiceInfo> lst; |
170 |
|
✗ |
result_t result = gate_services_enum(&ServiceMgr_get_services_callback, (void*)(&lst)); |
171 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
172 |
|
✗ |
return lst.toArray(); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
✗ |
static void ServiceMgr_message_to_delegate(gate_string_t const* message, void* user_param) |
176 |
|
|
{ |
177 |
|
✗ |
ServiceMgr::MessageCallback const* ptrDelegate = (ServiceMgr::MessageCallback const*)user_param; |
178 |
|
✗ |
if (ptrDelegate) |
179 |
|
|
{ |
180 |
|
|
try |
181 |
|
|
{ |
182 |
|
✗ |
String text = String::clone(*message); |
183 |
|
✗ |
ptrDelegate->invoke(text); |
184 |
|
|
} |
185 |
|
✗ |
catch (...) {} |
186 |
|
|
} |
187 |
|
✗ |
} |
188 |
|
|
|
189 |
|
✗ |
void ServiceMgr::start(String const& name, MessageCallback const& cb) |
190 |
|
|
{ |
191 |
|
✗ |
result_t result = gate_service_start(name.c_impl(), &ServiceMgr_message_to_delegate, !cb ? NULL : (void*)&cb); |
192 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
193 |
|
✗ |
} |
194 |
|
✗ |
void ServiceMgr::stop(String const& name, uint32_t waitTimeout, bool_t force, MessageCallback const& cb) |
195 |
|
|
{ |
196 |
|
✗ |
result_t result = gate_service_stop(name.c_impl(), waitTimeout, force, &ServiceMgr_message_to_delegate, !cb ? NULL : (void*)&cb); |
197 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
198 |
|
✗ |
} |
199 |
|
✗ |
ServiceConfig ServiceMgr::getConfig(String const& name) |
200 |
|
|
{ |
201 |
|
✗ |
gate_service_config_t cfg = GATE_INIT_EMPTY; |
202 |
|
✗ |
result_t result = gate_service_get_config(name.c_impl(), &cfg); |
203 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
204 |
|
✗ |
return ServiceConfig(cfg); |
205 |
|
|
} |
206 |
|
✗ |
ServiceMgr::StateEnum ServiceMgr::getStatus(String const& name, String* ptrProcessId) |
207 |
|
|
{ |
208 |
|
✗ |
enumint_t ret = 0; |
209 |
|
✗ |
gate_string_t proc = GATE_STRING_INIT_EMPTY; |
210 |
|
✗ |
result_t result = gate_service_get_status(name.c_impl(), &ret, &proc); |
211 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
212 |
|
✗ |
if (ptrProcessId) |
213 |
|
|
{ |
214 |
|
✗ |
*ptrProcessId = String::createFrom(proc); |
215 |
|
|
} |
216 |
|
|
else |
217 |
|
|
{ |
218 |
|
✗ |
gate_string_release(&proc); |
219 |
|
|
} |
220 |
|
✗ |
return ServiceMgr::StateEnum(ret); |
221 |
|
|
} |
222 |
|
|
|
223 |
|
✗ |
void ServiceMgr::registerHost(String const& name, String const& command, String const& description, |
224 |
|
|
uint32_t flags, Array<String> const& dependencies, MessageCallback const& cb) |
225 |
|
|
{ |
226 |
|
✗ |
gate_string_t const* ptr_deps = NULL; |
227 |
|
✗ |
String deps; |
228 |
|
✗ |
if (!dependencies.empty()) |
229 |
|
|
{ |
230 |
|
✗ |
StringBuilder builder; |
231 |
|
✗ |
for (Array<String>::const_iterator it(dependencies.begin()), end(dependencies.end()); it != end; ) |
232 |
|
|
{ |
233 |
|
✗ |
builder << *it; |
234 |
|
✗ |
++it; |
235 |
|
✗ |
if (it == end) |
236 |
|
|
{ |
237 |
|
✗ |
break; |
238 |
|
|
} |
239 |
|
✗ |
builder << ","; |
240 |
|
|
} |
241 |
|
✗ |
deps = builder.toString(); |
242 |
|
✗ |
ptr_deps = deps.c_impl(); |
243 |
|
|
} |
244 |
|
|
|
245 |
|
✗ |
result_t result = gate_service_register(name.c_impl(), command.c_impl(), description.c_impl(), flags, ptr_deps, |
246 |
|
✗ |
&ServiceMgr_message_to_delegate, !cb ? NULL : (void*)&cb); |
247 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
248 |
|
✗ |
} |
249 |
|
✗ |
void ServiceMgr::unregisterHost(String const& name, MessageCallback const& cb) |
250 |
|
|
{ |
251 |
|
✗ |
result_t result = gate_service_unregister(name.c_impl(), &ServiceMgr_message_to_delegate, !cb ? NULL : (void*)&cb); |
252 |
|
✗ |
GATEXX_CHECK_EXCEPTION(result); |
253 |
|
✗ |
} |
254 |
|
|
|
255 |
|
|
|
256 |
|
|
} // end of namespace sys |
257 |
|
|
} // end of namespace gate |
258 |
|
|
|