SubscriberImpl.cpp 7.56 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 "SubscriberImpl.h"
legoc's avatar
legoc committed
18
19

#include <sstream>
20
21

#include "../Serializer.h"
22
23
#include "CancelIdGenerator.h"
#include "ServicesImpl.h"
24
#include "RequestSocketImpl.h"
25
#include "../Server.h"
legoc's avatar
legoc committed
26
27
28
29
30
31
32
33
34
35
36

using namespace std;

namespace cameo {

const std::string SubscriberImpl::SYNC = "SYNC";
const std::string SubscriberImpl::STREAM = "STREAM";
const std::string SubscriberImpl::ENDSTREAM = "ENDSTREAM";
const std::string SubscriberImpl::CANCEL = "CANCEL";
const std::string SubscriberImpl::STATUS = "STATUS";

37
SubscriberImpl::SubscriberImpl(Server * server, const std::string & url, int publisherPort, int synchronizerPort, const std::string& publisherName, int numberOfSubscribers, const std::string& instanceName, int instanceId, const std::string& instanceEndpoint, const std::string& statusEndpoint) :
legoc's avatar
legoc committed
38
39
	m_server(server),
	m_url(url),
40
	m_publisherName(publisherName),
legoc's avatar
legoc committed
41
42
43
44
45
46
47
	m_publisherPort(publisherPort),
	m_synchronizerPort(synchronizerPort),
	m_numberOfSubscribers(numberOfSubscribers),
	m_instanceName(instanceName),
	m_instanceId(instanceId),
	m_instanceEndpoint(instanceEndpoint),
	m_statusEndpoint(statusEndpoint),
48
49
	m_ended(false),
	m_canceled(false) {
legoc's avatar
legoc committed
50
51
52
53
54
55
56
}

SubscriberImpl::~SubscriberImpl() {
}

void SubscriberImpl::init() {

57
	// Create a socket for publishing.
legoc's avatar
legoc committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
	m_subscriber.reset(new zmq::socket_t(m_server->m_impl->m_context, ZMQ_SUB));
	m_subscriber->setsockopt(ZMQ_SUBSCRIBE, SYNC.c_str(), SYNC.length());
	m_subscriber->setsockopt(ZMQ_SUBSCRIBE, STREAM.c_str(), STREAM.length());
	m_subscriber->setsockopt(ZMQ_SUBSCRIBE, ENDSTREAM.c_str(), ENDSTREAM.length());
	m_subscriber->setsockopt(ZMQ_SUBSCRIBE, CANCEL.c_str(), CANCEL.length());
	m_subscriber->setsockopt(ZMQ_SUBSCRIBE, STATUS.c_str(), STATUS.length());

	stringstream pubEndpoint;
	pubEndpoint << m_url << ":" << m_publisherPort;

	m_subscriber->connect(pubEndpoint.str().c_str());

	// We must first bind the cancel publisher before connecting the subscriber.
	stringstream cancelEndpoint;

73
	// We define a unique name.
legoc's avatar
legoc committed
74
75
76
	cancelEndpoint << "inproc://cancel." << CancelIdGenerator::newId();
	m_cancelEndpoint = cancelEndpoint.str();

legoc's avatar
legoc committed
77
	m_cancelPublisher = unique_ptr<zmq::socket_t>(new zmq::socket_t(m_server->m_impl->m_context, ZMQ_PUB));
legoc's avatar
legoc committed
78
79
80
81
82
	m_cancelPublisher->bind(m_cancelEndpoint.c_str());

	m_subscriber->connect(m_cancelEndpoint.c_str());
	m_subscriber->connect(m_statusEndpoint.c_str());

83
	// Synchronize the subscriber only if the number of subscribers > 0.
legoc's avatar
legoc committed
84
85
86
87
88
89
90
	if (m_numberOfSubscribers > 0) {

		stringstream syncEndpoint;
		syncEndpoint << m_url << ":" << m_synchronizerPort;

		string endpoint = syncEndpoint.str();

91
92
93
94
		// Create a request socket.
		unique_ptr<RequestSocketImpl> requestSocket = m_server->createRequestSocket(endpoint);

		// Poll subscriber.
legoc's avatar
legoc committed
95
		zmq_pollitem_t items[1];
legoc's avatar
legoc committed
96
		items[0].socket = static_cast<void *>(*m_subscriber);
legoc's avatar
legoc committed
97
98
99
100
		items[0].fd = 0;
		items[0].events = ZMQ_POLLIN;
		items[0].revents = 0;

101
102
		while (true) {
			m_server->m_impl->isAvailable(requestSocket.get(), 100);
legoc's avatar
legoc committed
103

104
			// Wait for 100ms.
legoc's avatar
legoc committed
105
106
			int rc = zmq::poll(items, 1, 100);
			if (rc != 0) {
107
				break;
legoc's avatar
legoc committed
108
109
110
			}
		}

111
112
113
		unique_ptr<zmq::message_t> reply = requestSocket->request(m_server->m_impl->createRequestType(PROTO_SUBSCRIBEPUBLISHER), m_server->m_impl->createSubscribePublisherRequest());
		proto::RequestResponse requestResponse;
		requestResponse.ParseFromArray((*reply).data(), (*reply).size());
legoc's avatar
legoc committed
114
115
116
	}
}

117
118
119
120
121
122
bool SubscriberImpl::isEnded() const {
	return m_ended;
}

bool SubscriberImpl::isCanceled() const {
	return m_canceled;
legoc's avatar
legoc committed
123
124
125
126
127
}

bool SubscriberImpl::receiveBinary(std::string& data) {

	while (true) {
128
129
		unique_ptr<zmq::message_t> message(new zmq::message_t());
		m_subscriber->recv(message.get());
legoc's avatar
legoc committed
130
131
132
133

		string response(static_cast<char*>(message->data()), message->size());

		if (response == STREAM) {
134
135
			message.reset(new zmq::message_t());
			m_subscriber->recv(message.get());
legoc's avatar
legoc committed
136
137
138
139
140
			data = string(static_cast<char*>(message->data()), message->size());

			return true;

		} else if (response == ENDSTREAM) {
141
			m_ended = true;
legoc's avatar
legoc committed
142
143
144
			return false;

		} else if (response == CANCEL) {
145
			m_canceled = true;
legoc's avatar
legoc committed
146
147
148
			return false;

		} else if (response == STATUS) {
149
150
			message.reset(new zmq::message_t());
			m_subscriber->recv(message.get());
legoc's avatar
legoc committed
151
152
153
154
155
156
157
158
159
160
161

			proto::StatusEvent protoStatus;
			protoStatus.ParseFromArray(message->data(), message->size());

			if (protoStatus.id() == m_instanceId) {
				application::State state = protoStatus.applicationstate();

				// test the terminal state
				if (state == application::SUCCESS
					|| state == application::STOPPED
					|| state == application::KILLED
legoc's avatar
legoc committed
162
					|| state == application::FAILURE) {
legoc's avatar
legoc committed
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
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
					// Exit because the remote application has terminated.
					return false;
				}
			}
		}
	}

	return false;
}

bool SubscriberImpl::receive(std::string& data) {

	string bytes;
	bool stream = receiveBinary(bytes);

	if (!stream) {
		return false;
	}

	parse(bytes, data);

	return true;
}

bool SubscriberImpl::receive(std::vector<int32_t>& data) {

	string bytes;
	bool stream = receiveBinary(bytes);

	if (!stream) {
		return false;
	}

	parse(bytes, data);

	return true;
}

bool SubscriberImpl::receive(std::vector<int64_t>& data) {

	string bytes;
	bool stream = receiveBinary(bytes);

	if (!stream) {
		return false;
	}

	parse(bytes, data);

	return true;
}

bool SubscriberImpl::receive(std::vector<float>& data) {

	string bytes;
	bool stream = receiveBinary(bytes);

	if (!stream) {
		return false;
	}

	parse(bytes, data);

	return true;
}

bool SubscriberImpl::receive(std::vector<double>& data) {

	string bytes;
	bool stream = receiveBinary(bytes);

	if (!stream) {
		return false;
	}

	parse(bytes, data);

	return true;
}

243
244
245
bool SubscriberImpl::receiveTwoBinaryParts(std::string& data1, std::string& data2) {

	while (true) {
246
247
		unique_ptr<zmq::message_t> message(new zmq::message_t());
		m_subscriber->recv(message.get());
248
249
250
251

		string response(static_cast<char*>(message->data()), message->size());

		if (response == STREAM) {
252
253
			message.reset(new zmq::message_t());
			m_subscriber->recv(message.get());
254
255
			data1 = string(static_cast<char*>(message->data()), message->size());

256
257
			message.reset(new zmq::message_t());
			m_subscriber->recv(message.get());
258
259
260
261
262
			data2 = string(static_cast<char*>(message->data()), message->size());

			return true;

		} else if (response == ENDSTREAM) {
263
			m_ended = true;
264
265
266
267
268
269
			return false;

		} else if (response == CANCEL) {
			return false;

		} else if (response == STATUS) {
270
271
			message.reset(new zmq::message_t());
			m_subscriber->recv(message.get());
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

			proto::StatusEvent protoStatus;
			protoStatus.ParseFromArray(message->data(), message->size());

			if (protoStatus.id() == m_instanceId) {
				application::State state = protoStatus.applicationstate();

				// test the terminal state
				if (state == application::SUCCESS
					|| state == application::STOPPED
					|| state == application::KILLED
					|| state == application::FAILURE) {
					// Exit because the remote application has terminated.
					return false;
				}
			}
		}
	}

	return false;
}

legoc's avatar
legoc committed
294
295
296
WaitingImpl * SubscriberImpl::waiting() {

	// Waiting gets the cancel publisher.
legoc's avatar
legoc committed
297
	return new SocketWaitingImpl(m_cancelPublisher.get(), CANCEL);
legoc's avatar
legoc committed
298
299
300
}

}