Services.cpp 3.61 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
34
35
36
37
38

using namespace std;

namespace cameo {

Services::Services() :
	m_port(0),
	m_statusPort(0),
	m_impl(0) {
}

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

void Services::terminate() {
legoc's avatar
legoc committed
43
	delete m_impl;
44
	m_impl = 0;
legoc's avatar
legoc committed
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
}

void Services::setImpl(ServicesImpl * impl) {
	m_impl = impl;
}

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();
102
	auto_ptr<zmq::message_t> reply = m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_serverEndpoint);
legoc's avatar
legoc committed
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

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

std::auto_ptr<EventStreamSocket> Services::openEventStream() {

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

	return auto_ptr<EventStreamSocket>(new EventStreamSocket(new SocketImpl(subscriber, cancelPublisher)));
}

}