PublisherImpl.cpp 8.38 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
#include "PublisherImpl.h"
legoc's avatar
legoc committed
18
19
20

#include <boost/bind.hpp>
#include <sstream>
21
#include "../Application.h"
22
#include "../Serializer.h"
23
#include "ApplicationImpl.h"
legoc's avatar
legoc committed
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

using namespace std;
using namespace boost;

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) {

		zmq::message_t * message = new zmq::message_t;
		synchronizer.recv(message, 0);

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

		if (message->more()) {
			delete message;
			message = new zmq::message_t;
			synchronizer.recv(message, 0);

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

		zmq::message_t * reply = 0;

		if (messageType.type() == proto::MessageType_Type_INIT) {
			reply = processInitCommand();

		} else if (messageType.type() == proto::MessageType_Type_SUBSCRIBEPUBLISHER) {
			counter++;
			reply = processSubscribePublisherCommand();

		} else if (messageType.type() == proto::MessageType_Type_CANCEL) {
			canceled = true;
			counter = m_numberOfSubscribers;
			reply = processCancelPublisherSyncCommand();

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

		// send to the client
		if (reply != 0) {
			synchronizer.send(*reply);
		}

		delete reply;
		delete message;
	}

	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);

150
	auto_ptr<zmq::message_t> reply = m_application->m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, endpoint.str());
legoc's avatar
legoc committed
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
210
211
212
213
214
215
216

	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());
}

217
218
219
220
221
222
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
void PublisherImpl::setEnd() {

	if (!m_ended && m_publisher.get() != 0) {
		// send a dummy ENDSTREAM message by the publisher socket
		string data = "endstream";
		publish(ENDSTREAM, data.c_str(), data.length());

		m_ended = true;
	}
}

bool PublisherImpl::hasEnded() {
	return m_ended;
}

void PublisherImpl::terminate() {

	if (m_publisher.get() != 0) {
		setEnd();
		m_publisher.reset(0);

		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());
255
256

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

legoc's avatar
legoc committed
259
260
261
262
	m_publisher->send(requestType, ZMQ_SNDMORE);
	m_publisher->send(requestData);
}

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
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
316
317
318
319
320
321
322
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;
}


}