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

#include <sstream>
20
21

#include "../Serializer.h"
22
23
24
#include "CancelIdGenerator.h"
#include "ServicesImpl.h"
#include "../Server.h"
legoc's avatar
legoc committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

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

SubscriberImpl::SubscriberImpl(const 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) :
	m_server(server),
	m_publisherName(publisherName),
	m_url(url),
	m_publisherPort(publisherPort),
	m_synchronizerPort(synchronizerPort),
	m_numberOfSubscribers(numberOfSubscribers),
	m_instanceName(instanceName),
	m_instanceId(instanceId),
	m_instanceEndpoint(instanceEndpoint),
	m_statusEndpoint(statusEndpoint),
47
48
	m_ended(false),
	m_canceled(false) {
legoc's avatar
legoc committed
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
}

SubscriberImpl::~SubscriberImpl() {
}

void SubscriberImpl::init() {

	// create a socket for publishing
	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;

	// We define a unique name
	cancelEndpoint << "inproc://cancel." << CancelIdGenerator::newId();
	m_cancelEndpoint = cancelEndpoint.str();

legoc's avatar
legoc committed
76
	m_cancelPublisher = unique_ptr<zmq::socket_t>(new zmq::socket_t(m_server->m_impl->m_context, ZMQ_PUB));
legoc's avatar
legoc committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	m_cancelPublisher->bind(m_cancelEndpoint.c_str());

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

	// synchronize the subscriber only if the number of subscribers > 0
	if (m_numberOfSubscribers > 0) {

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

		string endpoint = syncEndpoint.str();

		// polling subscriber
		zmq_pollitem_t items[1];
legoc's avatar
legoc committed
92
		items[0].socket = static_cast<void *>(*m_subscriber);
legoc's avatar
legoc committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
		items[0].fd = 0;
		items[0].events = ZMQ_POLLIN;
		items[0].revents = 0;

		bool ready = false;

		while (!ready) {

			string strRequestType = m_server->m_impl->createRequest(PROTO_INIT);
			string strRequestData = m_server->m_impl->createInitRequest();
			m_server->m_impl->isAvailable(strRequestType, strRequestData, endpoint, 100);
	
			// wait for 100ms
			int rc = zmq::poll(items, 1, 100);
			if (rc != 0) {
				ready = true;
			}
		}

		m_server->m_impl->subscribeToPublisher(endpoint);
	}
}

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

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

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

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

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

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

			return true;

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

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

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

			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
161
					|| state == application::FAILURE) {
legoc's avatar
legoc committed
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
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
					// 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;
}

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

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

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

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

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

			return true;

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

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

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

			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
293
294
295
WaitingImpl * SubscriberImpl::waiting() {

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

}