Application.cpp 13.2 KB
Newer Older
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright 2015 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;

21
22
using namespace pybind11::literals;

23
#include <cameo/api/cameo.h>
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
24
25
using namespace cameo::application;

26
27
#include <iostream>

Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
28
29
PYBIND11_MODULE(cameopy, m) {

legoc's avatar
legoc committed
30
	m.doc() = "Python binding of C++ API"; // optional module docstring
legoc's avatar
legoc committed
31
	m.def("toString", &toString, "Function converting numerical state to its string representation");
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
32
33
34
35
36
37
38
39
40
41
42
43

	m.attr("OUTPUTSTREAM")       = cameo::OUTPUTSTREAM;
	m.attr("UNKNOWN")            = UNKNOWN;
	m.attr("STARTING")           = STARTING;
	m.attr("RUNNING")            = RUNNING;
	m.attr("STOPPING")           = STOPPING;
	m.attr("KILLING")            = KILLING;
	m.attr("PROCESSING_ERROR")   = PROCESSING_ERROR;
	m.attr("FAILURE")            = FAILURE;
	m.attr("SUCCESS")            = SUCCESS;
	m.attr("STOPPED")            = STOPPED;
	m.attr("KILLED")             = KILLED;
44
45
46
47
48
49


	// Important note:
	// The call_guard policy is set to py::gil_scoped_release for all bindings except for getters and setters that use a local member of the object.
	// If the policy is not set, the bindings are blocking other Python running threads.

Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
50
	py::class_<This>(m, "This")
51
	    .def_static(
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
52
53
54
55
56
57
58
		"init",
		[](std::vector<std::string> args) {
			std::vector<char*> cstrs;
			cstrs.reserve(args.size());
			for (auto& s : args)
				cstrs.push_back(const_cast<char*>(s.c_str()));
			This::init(args.size(), cstrs.data());
59
		}, py::call_guard<py::gil_scoped_release>(),
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
60
		"initialize the application")
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	    .def_static("terminate", &This::terminate, py::call_guard<py::gil_scoped_release>())
	    .def_static("getName", &This::getName)
	    .def_static("getId", &This::getId)
		.def_static("setTimeout", &This::setTimeout,
				"value"_a)
	    .def_static("getTimeout", &This::getTimeout)
	    .def_static("getEndpoint", &This::getEndpoint)
	    .def_static("getServer", &This::getServer, py::return_value_policy::reference)
	    .def_static("getCom", &This::getCom)
	    .def_static("getStarterServer", &This::getStarterServer)
	    .def_static("isAvailable", &This::isAvailable,
	    		"timeout"_a = 10000,
	    		py::call_guard<py::gil_scoped_release>()) //, py::arg("timeout") = 1000) // this does not work!
	    .def_static("isStopping", &This::isStopping, py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
75
	    //	    .def("handleStop", &This::handleStop)
76
77
78
79
80
81
82
83
84
	    .def_static("cancelWaitings", &This::cancelWaitings, py::call_guard<py::gil_scoped_release>())
	    .def_static("setRunning", &This::setRunning, py::call_guard<py::gil_scoped_release>())
	    .def_static("setBinaryResult", &This::setBinaryResult,
	    		"data"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def_static("setResult", &This::setResult,
	    		"data"_a,
				py::call_guard<py::gil_scoped_release>())
	    .def_static("connectToStarter", &This::connectToStarter, py::call_guard<py::gil_scoped_release>());
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
85

86
87
88
89
90
91
92
93
94
95
96
	py::class_<cameo::Output>(m, "Output")
		.def("getId", &cameo::Output::getId)
		.def("getMessage", &cameo::Output::getMessage)
		.def("isEndOfLine", &cameo::Output::isEndOfLine);

	py::class_<cameo::OutputStreamSocket>(m, "OutputStreamSocket")
		.def("receive", &cameo::OutputStreamSocket::receive, py::call_guard<py::gil_scoped_release>())
		.def("cancel", &cameo::OutputStreamSocket::cancel, py::call_guard<py::gil_scoped_release>())
		.def("isEnded", &cameo::OutputStreamSocket::isEnded)
		.def("isCanceled", &cameo::OutputStreamSocket::isCanceled);

Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
97
98
99
100
101
	py::class_<Instance>(m, "Instance")
	    .def("getName", &Instance::getName)
	    .def("getId", &Instance::getId)
	    .def("getEndpoint", &Instance::getEndpoint)
	    .def("getNameId", &Instance::getNameId)
102
	    //.def("getCom", &Instance::getCom)
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
103
104
105
	    .def("hasResult", &Instance::hasResult)
	    .def("exists", &Instance::exists)
	    .def("getErrorMessage", &Instance::getErrorMessage)
106
107
	    .def("stop", &Instance::stop, py::call_guard<py::gil_scoped_release>())
	    .def("kill", &Instance::kill, py::call_guard<py::gil_scoped_release>())
108
	    .def("waitFor", py::overload_cast<>(&Instance::waitFor), py::call_guard<py::gil_scoped_release>())
109
110
111
112
113
114
	    .def("waitFor", py::overload_cast<int>(&Instance::waitFor),
	    		"states"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("waitFor", py::overload_cast<const std::string&>(&Instance::waitFor),
				"eventName"_a,
	    		py::call_guard<py::gil_scoped_release>())
115
116
117
118
	    .def("cancelWaitFor", &Instance::cancelWaitFor, py::call_guard<py::gil_scoped_release>())
	    .def("getLastState", &Instance::getLastState, py::call_guard<py::gil_scoped_release>())
	    .def("getActualState", &Instance::getActualState, py::call_guard<py::gil_scoped_release>())
	    .def("getPastStates", &Instance::getPastStates, py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
119
120
121
122
123
124
125
	    .def("getExitCode", &Instance::getExitCode)
	    .def("getBinaryResult",
		 [](Instance* instance) {
			 auto result = instance->getBinaryResult();
			 if (result.has_value() == false)
				 return py::bytes("");
			 return py::bytes(result.value());
126
127
		 }, py::call_guard<py::gil_scoped_release>())
	    .def("getResult", &Instance::getResult, py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
128
129
130
131
132
	    .def("getOutputStreamSocket", &Instance::getOutputStreamSocket);

	py::class_<InstanceArray>(m, "InstanceArray");

	py::class_<Publisher>(m, "Publisher")
133
134
135
136
	    .def_static("create", &Publisher::create,
	    		"name"_a,
	    		"numberOfSubscribers"_a = 0,
				py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
137
138
139
140
	    .def("getName", &Publisher::getName)
	    .def("getApplicationName", &Publisher::getApplicationName)
	    .def("getApplicationId", &Publisher::getApplicationId)
	    .def("getApplicationEndpoint", &Publisher::getApplicationEndpoint)
141
	    .def("waitForSubscribers", &Publisher::waitForSubscribers, py::call_guard<py::gil_scoped_release>())
142
	    .def("cancelWaitForSubscribers", &Publisher::cancelWaitForSubscribers, py::call_guard<py::gil_scoped_release>())
143
144
145
146
147
148
149
150
151
	    .def("sendBinary", &Publisher::sendBinary,
	    		"data"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("send", &Publisher::send,
	    		"data"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("sendTwoBinaryParts", &Publisher::sendTwoBinaryParts,
	    		"data1"_a, "data2"_a,
	    		py::call_guard<py::gil_scoped_release>())
152
	    .def("sendEnd", &Publisher::sendEnd, py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
153
154
155
	    .def("isEnded", &Publisher::isEnded);

	py::class_<Subscriber>(m, "Subscriber")
156
157
158
159
	    .def_static("create", &Subscriber::create,
	    		"instance"_a,
	    		"publisherName"_a,
	    		py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
160
161
162
163
164
165
	    .def("getPublisherName", &Subscriber::getPublisherName)
	    .def("getInstanceName", &Subscriber::getInstanceName)
	    .def("getInstanceId", &Subscriber::getInstanceId)
	    .def("getInstanceEndpoint", &Subscriber::getInstanceEndpoint)
	    .def("isEnded", &Subscriber::isEnded)
	    .def("isCanceled", &Subscriber::isCanceled)
166
167
168
	    .def("receiveBinary", &Subscriber::receiveBinary, py::call_guard<py::gil_scoped_release>())
	    .def("receive", &Subscriber::receive, py::call_guard<py::gil_scoped_release>())
	    .def("receiveTwoBinaryParts", &Subscriber::receiveTwoBinaryParts, py::call_guard<py::gil_scoped_release>())
169
	    .def("cancel", &Subscriber::cancel, py::call_guard<py::gil_scoped_release>());
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
170
171
172
173
174
175
176

	py::class_<Request>(m, "Request")
	    .def("getObjectId", &Request::getObjectId)
	    .def("getRequesterEndpoint", &Request::getRequesterEndpoint)
	    .def("getBinary", &Request::getBinary)
	    .def("get", &Request::get)
	    .def("getSecondBinaryPart", &Request::getSecondBinaryPart)
177
178
179
180
181
182
183
184
	    .def("setTimeout", &Request::setTimeout,
	    		"value"_a)
	    .def("replyBinary", &Request::replyBinary,
	    		"response"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("reply", &Request::reply,
	    		"response"_a,
	    		py::call_guard<py::gil_scoped_release>())
185
	    .def("connectToRequester", &Request::connectToRequester, py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
186
187
188
189
	    // the following require "Server.h"
	    .def("getServer", &Request::getServer);

	py::class_<Responder>(m, "Responder")
190
191
192
	    .def_static("create", &Responder::create,
	    		"name"_a,
	    		py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
193
	    .def("getName", &Responder::getName)
194
	    .def("cancel", &Responder::cancel, py::call_guard<py::gil_scoped_release>())
195
	    .def("receive", &Responder::receive, py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
196
197
198
199
	    .def("isCanceled", &Responder::isCanceled);


	py::class_<Requester>(m, "Requester")
200
201
202
203
	    .def_static("create", &Requester::create,
	    		"instance"_a,
				"name"_a,
	    		py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
204
	    .def("getName", &Requester::getName)
205
206
207
208
209
210
211
212
213
	    .def("sendBinary", &Requester::sendBinary,
	    		"request"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("send", &Requester::send,
	    		"request"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("sendTwoBinaryParts", &Requester::sendTwoBinaryParts,
	    		"request1"_a, "request2"_a,
	    		py::call_guard<py::gil_scoped_release>())
214
215
	    .def("receiveBinary", &Requester::receiveBinary, py::call_guard<py::gil_scoped_release>())
	    .def("receive", &Requester::receive, py::call_guard<py::gil_scoped_release>())
216
	    .def("cancel", &Requester::cancel, py::call_guard<py::gil_scoped_release>())
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
	    .def("isCanceled", &Requester::isCanceled);


	py::class_<Configuration>(m, "Configuration")
	    .def(py::init<const std::string&, const std::string&, bool, bool, int, int>())
	    .def("getName", &Configuration::getName)
	    .def("getDescription", &Configuration::getDescription)
	    .def("hasSingleInstance", &Configuration::hasSingleInstance)
	    .def("canRestart", &Configuration::canRestart)
	    .def("getStartingTime", &Configuration::getStartingTime)
	    .def("getStoppingTime", &Configuration::getStoppingTime);

	py::class_<Info>(m, "Info")
	    .def(py::init<const std::string&, int, int, State, State, const std::string&>())
	    .def("getId", &Info::getId)
	    .def("getState", &Info::getState)
	    .def("getPastStates", &Info::getPastStates)
	    .def("getArgs", &Info::getArgs)
	    .def("getName", &Info::getName)
	    .def("getPid", &Info::getPid);

	py::class_<Port>(m, "Port")
	    .def(py::init<int, const std::string&, const std::string&>())
	    .def("getPort", &Port::getPort)
	    .def("getStatus", &Port::getStatus)
	    .def("getOwner", &Port::getOwner);

	py::class_<cameo::Server>(m, "Server")
	    .def(py::init<const std::string&>())
246
247
	    .def("setTimeout", &cameo::Server::setTimeout,
	    		"value"_a)
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
248
249
250
	    .def("getTimeout", &cameo::Server::getTimeout)
	    .def("getEndpoint", &cameo::Server::getEndpoint)
	    .def("getVersion", &cameo::Server::getVersion)
251
	    .def("isAvailable", py::overload_cast<>(&cameo::Server::isAvailable, py::const_), py::call_guard<py::gil_scoped_release>())
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
	    .def("isAvailable", py::overload_cast<int>(&cameo::Server::isAvailable, py::const_),
	    		"timeout"_a,
	    		py::call_guard<py::gil_scoped_release>())

		.def("start", py::overload_cast<const std::string&, int>(&cameo::Server::start),
				"name"_a, "options"_a = 0,
				py::call_guard<py::gil_scoped_release>())

	    .def("start", py::overload_cast<const std::string&, const std::vector<std::string>&, int>(&cameo::Server::start),
	    		"name"_a, "args"_a, "options"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("connectAll", &cameo::Server::connectAll,
	    		"name"_a, "options"_a = 0,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("connect", py::overload_cast<const std::string&, int>(&cameo::Server::connect),
	    		"name"_a, "options"_a = 0,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("connect", py::overload_cast<int, int>(&cameo::Server::connect),
	    		"id"_a, "options"_a = 0,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("killAllAndWaitFor", &cameo::Server::killAllAndWaitFor,
	    		"name"_a,
	    		py::call_guard<py::gil_scoped_release>())
275
276
	    .def("getApplicationConfigurations", &cameo::Server::getApplicationConfigurations, py::call_guard<py::gil_scoped_release>())
	    .def("getApplicationInfos", py::overload_cast<>(&cameo::Server::getApplicationInfos, py::const_), py::call_guard<py::gil_scoped_release>())
277
278
279
	    .def("getApplicationInfos", py::overload_cast<const std::string&>(&cameo::Server::getApplicationInfos, py::const_),
	    		"name"_a,
	    		py::call_guard<py::gil_scoped_release>())
280
	    .def("getPorts", &cameo::Server::getPorts, py::call_guard<py::gil_scoped_release>())
281
282
283
284
285
286
	    .def("getActualState", &cameo::Server::getActualState,
	    		"id"_a,
	    		py::call_guard<py::gil_scoped_release>())
	    .def("getPastStates", &cameo::Server::getPastStates,
	    		"id"_a,
	    		py::call_guard<py::gil_scoped_release>());
287
	    //.def("openEventStream", &cameo::Server::openEventStream, py::call_guard<py::gil_scoped_release>());
Shervin Nourbakhsh's avatar
Shervin Nourbakhsh committed
288
}