PublisherImpl.cpp 8.35 KB
Newer Older
legoc's avatar
legoc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * 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.
 */

17
18
#include "PublisherImpl.h"
#include "../Application.h"
19
#include "../Serializer.h"
20
#include "ApplicationImpl.h"
legoc's avatar
legoc committed
21
#include <sstream>
legoc's avatar
legoc committed
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

using namespace std;

namespace cameo {

const std::string PublisherImpl::SYNC = "SYNC";
const std::string PublisherImpl::STREAM = "STREAM";
const std::string PublisherImpl::ENDSTREAM = "ENDSTREAM";

PublisherImpl::PublisherImpl(const application::This * application, int publisherPort, int synchronizerPort, const std::string& name, int numberOfSubscribers) :
	m_application(application),
	m_publisherPort(publisherPort),
	m_synchronizerPort(synchronizerPort),
	m_name(name),
	m_numberOfSubscribers(numberOfSubscribers),
	m_ended(false) {

	// create a socket for publishing
	m_publisher.reset(new zmq::socket_t(m_application->m_impl->m_context, ZMQ_PUB));
	stringstream pubEndpoint;
	pubEndpoint << "tcp://*:" << m_publisherPort;

	m_publisher->bind(pubEndpoint.str().c_str());
}

PublisherImpl::~PublisherImpl() {
	terminate();
}

const std::string& PublisherImpl::getName() const {
	return m_name;
}

const std::string& PublisherImpl::getApplicationName() const {
	return m_application->getName();
}

int PublisherImpl::getApplicationId() const {
	return m_application->getId();
}

const std::string& PublisherImpl::getApplicationEndpoint() const {
	return m_application->getEndpoint();
}

bool PublisherImpl::waitForSubscribers() {

	if (m_numberOfSubscribers <= 0) {
		return true;
	}

	// create a socket to receive the messages from the subscribers
	zmq::socket_t synchronizer(m_application->m_impl->m_context, ZMQ_REP);

	stringstream syncEndpoint;
	string url = "tcp://*";

	syncEndpoint << url << ":" << m_synchronizerPort;
	synchronizer.bind(syncEndpoint.str().c_str());

	// loop until the number of subscribers is reached
	int counter = 0;
	bool canceled = false;

	while (counter < m_numberOfSubscribers) {

88
89
		unique_ptr<zmq::message_t> message(new zmq::message_t);
		synchronizer.recv(message.get(), 0);
legoc's avatar
legoc committed
90
91
92
93
94
95

		// multi-part message, first part is the type
		proto::MessageType messageType;
		messageType.ParseFromArray((*message).data(), (*message).size());

		if (message->more()) {
96
97
			message.reset(new zmq::message_t);
			synchronizer.recv(message.get(), 0);
legoc's avatar
legoc committed
98
99
100
101
102
103

		} else {
			cerr << "unexpected number of frames, should be 2" << endl;
			continue;
		}

104
		unique_ptr<zmq::message_t> reply;
legoc's avatar
legoc committed
105
106

		if (messageType.type() == proto::MessageType_Type_INIT) {
107
			reply.reset(processInitCommand());
legoc's avatar
legoc committed
108
109
110

		} else if (messageType.type() == proto::MessageType_Type_SUBSCRIBEPUBLISHER) {
			counter++;
111
			reply.reset(processSubscribePublisherCommand());
legoc's avatar
legoc committed
112
113
114
115

		} else if (messageType.type() == proto::MessageType_Type_CANCEL) {
			canceled = true;
			counter = m_numberOfSubscribers;
116
			reply.reset(processCancelPublisherSyncCommand());
legoc's avatar
legoc committed
117
118
119
120
121
122
123

		} else {
			cerr << "unknown message type " << messageType.type() << endl;
			synchronizer.send(*message);
		}

		// send to the client
legoc's avatar
legoc committed
124
		if (reply != nullptr) {
legoc's avatar
legoc committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
			synchronizer.send(*reply);
		}
	}

	return !canceled;
}

void PublisherImpl::cancelWaitForSubscribers() {

	stringstream endpoint;
	endpoint << m_application->getUrl() << ":" << (m_publisherPort + 1);

	string strRequestType = m_application->m_impl->createRequest(PROTO_CANCEL);
	string strRequestData;

	proto::CancelPublisherSyncCommand cancelPublisherSyncCommand;
	cancelPublisherSyncCommand.SerializeToString(&strRequestData);

legoc's avatar
legoc committed
143
	unique_ptr<zmq::message_t> reply = m_application->m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, endpoint.str());
legoc's avatar
legoc committed
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

	proto::RequestResponse requestResponse;
	requestResponse.ParseFromArray((*reply).data(), (*reply).size());
}

WaitingImpl * PublisherImpl::waiting() {

	return new GenericWaitingImpl(bind(&PublisherImpl::cancelWaitForSubscribers, this));
}

void PublisherImpl::sendBinary(const std::string& data) {

	// send a STREAM message by the publisher socket
	publish(STREAM, data.c_str(), data.length());
}

void PublisherImpl::send(const std::string& data) {

	// encode the data
	string result;
	serialize(data, result);

	// send a STREAM message by the publisher socket
	publish(STREAM, result.c_str(), result.length());
}

void PublisherImpl::send(const int32_t* data, std::size_t size) {

	// encode the data
	string result;
	serialize(data, size, result);

	// send a STREAM message by the publisher socket
	publish(STREAM, result.c_str(), result.length());
}

void PublisherImpl::send(const int64_t* data, std::size_t size) {

	// encode the data
	string result;
	serialize(data, size, result);

	// send a STREAM message by the publisher socket
	publish(STREAM, result.c_str(), result.length());
}

void PublisherImpl::send(const float* data, std::size_t size) {

	// encode the data
	string result;
	serialize(data, size, result);

	// send a STREAM message by the publisher socket
	publish(STREAM, result.c_str(), result.length());
}

void PublisherImpl::send(const double* data, std::size_t size) {

	// encode the data
	string result;
	serialize(data, size, result);

	// send a STREAM message by the publisher socket
	publish(STREAM, result.c_str(), result.length());
}

210
211
212
213
214
215
void PublisherImpl::sendTwoBinaryParts(const std::string& data1, const std::string& data2) {

	// send a STREAM message by the publisher socket
	publishTwoParts(STREAM, data1.c_str(), data1.length(), data2.c_str(), data2.length());
}

legoc's avatar
legoc committed
216
217
void PublisherImpl::setEnd() {

legoc's avatar
legoc committed
218
	if (!m_ended && m_publisher.get() != nullptr) {
legoc's avatar
legoc committed
219
220
221
222
223
224
225
226
		// send a dummy ENDSTREAM message by the publisher socket
		string data = "endstream";
		publish(ENDSTREAM, data.c_str(), data.length());

		m_ended = true;
	}
}

227
bool PublisherImpl::isEnded() {
legoc's avatar
legoc committed
228
229
230
231
232
	return m_ended;
}

void PublisherImpl::terminate() {

legoc's avatar
legoc committed
233
	if (m_publisher.get() != nullptr) {
legoc's avatar
legoc committed
234
		setEnd();
legoc's avatar
legoc committed
235
		m_publisher.reset(nullptr);
legoc's avatar
legoc committed
236
237
238
239
240
241
242
243
244
245
246
247

		bool success = m_application->destroyPublisher(m_name);
		if (!success) {
			cerr << "server cannot destroy publisher " << m_name << endl;
		}
	}
}

void PublisherImpl::publish(const std::string& header, const char* data, std::size_t size) {

	zmq::message_t requestType(header.length());
	memcpy((void *) requestType.data(), header.c_str(), header.length());
248
249

	zmq::message_t requestData(size);
legoc's avatar
legoc committed
250
	memcpy((void *) requestData.data(), data, size);
251

legoc's avatar
legoc committed
252
253
254
255
	m_publisher->send(requestType, ZMQ_SNDMORE);
	m_publisher->send(requestData);
}

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
void PublisherImpl::publishTwoParts(const std::string& header, const char* data1, std::size_t size1, const char* data2, std::size_t size2) {

	zmq::message_t requestType(header.length());
	memcpy((void *) requestType.data(), header.c_str(), header.length());

	zmq::message_t requestData1(size1);
	memcpy((void *) requestData1.data(), data1, size1);

	zmq::message_t requestData2(size2);
	memcpy((void *) requestData2.data(), data2, size2);

	m_publisher->send(requestType, ZMQ_SNDMORE);
	m_publisher->send(requestData1, ZMQ_SNDMORE);
	m_publisher->send(requestData2);
}

legoc's avatar
legoc committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
zmq::message_t * PublisherImpl::processInitCommand() {

	// send a dummy SYNC message by the publisher socket
	string data = "sync";
	publish(SYNC, data.c_str(), data.length());

	data = "Connection OK";
	size_t size = data.length();
	zmq::message_t * reply = new zmq::message_t(size);
	memcpy((void *) reply->data(), data.c_str(), size);

	return reply;
}

zmq::message_t * PublisherImpl::processSubscribePublisherCommand() {

	proto::RequestResponse requestResponse;
	requestResponse.set_value(0);
	requestResponse.set_message("OK");

	string result = requestResponse.SerializeAsString();

	zmq::message_t * reply = new zmq::message_t(result.length());
	memcpy((void *) reply->data(), result.c_str(), result.length());

	return reply;
}

zmq::message_t * PublisherImpl::processCancelPublisherSyncCommand() {

	proto::RequestResponse requestResponse;
	requestResponse.set_value(0);
	requestResponse.set_message("OK");

	string result = requestResponse.SerializeAsString();

	zmq::message_t * reply = new zmq::message_t(result.length());
	memcpy((void *) reply->data(), result.c_str(), result.length());

	return reply;
}


}