Services.cpp 4.32 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 "Services.h"
legoc's avatar
legoc committed
18
19
20
21

#include <iostream>
#include <sstream>
#include <stdexcept>
22
23
24
25
#include "impl/CancelIdGenerator.h"
#include "impl/ServicesImpl.h"
#include "impl/SocketImpl.h"
#include "ProtoType.h"
legoc's avatar
legoc committed
26
27
28
29
30
31
32
33

using namespace std;

namespace cameo {

Services::Services() :
	m_port(0),
	m_statusPort(0),
legoc's avatar
legoc committed
34
	m_impl(nullptr) {
legoc's avatar
legoc committed
35
36
37
38
}

Services::~Services() {
	// Delete impl here to avoid order troubles.
39
40
41
42
	terminate();
}

void Services::terminate() {
legoc's avatar
legoc committed
43
	m_impl.reset();
legoc's avatar
legoc committed
44
45
46
}

void Services::setImpl(ServicesImpl * impl) {
legoc's avatar
legoc committed
47
	m_impl.reset(impl);
legoc's avatar
legoc committed
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
}

std::vector<std::string> Services::split(const std::string& info) {

	vector<string> result;

	int lastIndex = 0;
	int index = info.find(':');
	while (index != string::npos) {
		result.push_back(info.substr(lastIndex, index - lastIndex));
		lastIndex = index + 1;
		index = info.find(':', lastIndex);
	}
	result.push_back(info.substr(lastIndex));

	return result;
}

void Services::setTimeout(int timeout) {
	m_impl->setTimeout(timeout);
}

int Services::getTimeout() const {
	return m_impl->getTimeout();
}

const std::string& Services::getEndpoint() const {
	return m_serverEndpoint;
}

const std::string& Services::getUrl() const {
	return m_url;
}

int Services::getPort() const {
	return m_port;
}

const std::string& Services::getStatusEndpoint() const {
	return m_serverStatusEndpoint;
}

bool Services::isAvailable(int timeout) const {
	string strRequestType = m_impl->createRequest(PROTO_INIT);
	string strRequestData = m_impl->createInitRequest();
	return m_impl->isAvailable(strRequestType, strRequestData, m_serverEndpoint, timeout);
}

void Services::initStatus() {

	// get the status port
	string strRequestType = m_impl->createRequest(PROTO_STATUS);
	string strRequestData = m_impl->createShowStatusRequest();
legoc's avatar
legoc committed
101
	unique_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

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

	// reply ok
	if (requestResponse.value() == -1) {
		return;
	}

	m_statusPort = requestResponse.value();

	stringstream ss;
	ss << m_url << ":" << m_statusPort;
	m_serverStatusEndpoint = ss.str();
}

legoc's avatar
legoc committed
118
std::unique_ptr<EventStreamSocket> Services::openEventStream() {
legoc's avatar
legoc committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

	// init the status if needed
	if (m_statusPort == 0) {
		initStatus();
	}

	stringstream cancelEndpoint;

	// We define a unique name that depends on the event stream socket object because there can be many (instances).
	cancelEndpoint << "inproc://cancel." << CancelIdGenerator::newId();

	// create sockets
	zmq::socket_t * cancelPublisher = m_impl->createCancelPublisher(cancelEndpoint.str());
	zmq::socket_t * subscriber = m_impl->createEventSubscriber(m_serverStatusEndpoint, cancelEndpoint.str());

	// wait for the connection
	string strRequestType = m_impl->createRequest(PROTO_INIT);
	string strRequestData = m_impl->createInitRequest();
	m_impl->waitForSubscriber(subscriber, strRequestType, strRequestData, m_serverEndpoint);

legoc's avatar
legoc committed
139
	return unique_ptr<EventStreamSocket>(new EventStreamSocket(new SocketImpl(subscriber, cancelPublisher)));
legoc's avatar
legoc committed
140
141
}

legoc's avatar
legoc committed
142
143
144
145
146
147
148
149
150
std::unique_ptr<OutputStreamSocket> Services::createOutputStreamSocket(int port) {

	if (port == -1) {
		return nullptr;
	}

	// Prepare our context and subscriber
	string streamEndpoint = m_url + ":" + to_string(port);

151
152
153
154
155
156
157
	// We define a unique name that depends on the event stream socket object because there can be many (instances).
	string cancelEndpoint = "inproc://cancel." + to_string(CancelIdGenerator::newId());

	zmq::socket_t * cancelPublisher = m_impl->createCancelPublisher(cancelEndpoint);
	zmq::socket_t * subscriber = m_impl->createOutputStreamSubscriber(streamEndpoint, cancelEndpoint);

	return unique_ptr<OutputStreamSocket>(new OutputStreamSocket(new SocketImpl(subscriber, cancelPublisher)));
legoc's avatar
legoc committed
158
159
}

legoc's avatar
legoc committed
160
}