RequesterImpl.cpp 5.88 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 "RequesterImpl.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

using namespace std;
using namespace boost;

namespace cameo {

const std::string RequesterImpl::REQUESTER_PREFIX = "req.";
31
32
boost::mutex RequesterImpl::m_mutex;
int RequesterImpl::m_requesterCounter = 0;
legoc's avatar
legoc committed
33

34
RequesterImpl::RequesterImpl(const application::This * application, const std::string& url, int requesterPort, int responderPort, const std::string& name, int responderId, int requesterId) :
legoc's avatar
legoc committed
35
36
	m_application(application),
	m_requesterPort(requesterPort),
37
	m_name(name),
38
39
	m_responderId(responderId),
	m_requesterId(requesterId) {
legoc's avatar
legoc committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

	stringstream repEndpoint;
	repEndpoint << url << ":" << responderPort;
	m_responderEndpoint = repEndpoint.str();

	// create a socket REP
	m_requester.reset(new zmq::socket_t(m_application->m_impl->m_context, ZMQ_REP));
	stringstream reqEndpoint;
	reqEndpoint << "tcp://*:" << m_requesterPort;

	m_requester->bind(reqEndpoint.str().c_str());
}

RequesterImpl::~RequesterImpl() {
	terminate();
}

57
58
59
60
61
62
63
64
65
int RequesterImpl::newRequesterId() {

	boost::mutex::scoped_lock lock(m_mutex);
	m_requesterCounter++;

	return m_requesterCounter;
}

std::string RequesterImpl::getRequesterPortName(const std::string& name, int responderId, int requesterId) {
66
67

	stringstream requesterPortName;
68
	requesterPortName << REQUESTER_PREFIX << name << "." << responderId << "." << requesterId;
69
70
71
72

	return requesterPortName.str();
}

legoc's avatar
legoc committed
73
74
75
76
77
78
79
80
81
82
WaitingImpl * RequesterImpl::waiting() {
	return new GenericWaitingImpl(bind(&RequesterImpl::cancel, this));
}

void RequesterImpl::sendBinary(const std::string& request) {

	string strRequestType = m_application->m_impl->createRequest(PROTO_REQUEST);
	string strRequestData;

	proto::Request requestCommand;
83
	requestCommand.set_applicationname(m_application->getName());
legoc's avatar
legoc committed
84
85
	requestCommand.set_applicationid(m_application->getId());
	requestCommand.set_message(request);
86
87
88
	requestCommand.set_serverurl(m_application->getUrl());
	requestCommand.set_serverport(m_application->getPort());
	requestCommand.set_requesterport(m_requesterPort);
legoc's avatar
legoc committed
89
90
	requestCommand.SerializeToString(&strRequestData);

91
	auto_ptr<zmq::message_t> reply = m_application->m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_responderEndpoint);
legoc's avatar
legoc committed
92
93
94
95
96
97
98
99
100
101
102
103
104

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

void RequesterImpl::send(const std::string& request) {

	// encode the data
	string result;
	serialize(request, result);
	sendBinary(result);
}

105
106
107
108
109
110
void RequesterImpl::sendTwoBinaryParts(const std::string& request1, const std::string& request2) {

	string strRequestType = m_application->m_impl->createRequest(PROTO_REQUEST);
	string strRequestData;

	proto::Request requestCommand;
111
	requestCommand.set_applicationname(m_application->getName());
112
113
114
	requestCommand.set_applicationid(m_application->getId());
	requestCommand.set_message(request1);
	requestCommand.set_message2(request2);
115
116
117
	requestCommand.set_serverurl(m_application->getUrl());
	requestCommand.set_serverport(m_application->getPort());
	requestCommand.set_requesterport(m_requesterPort);
118
119
	requestCommand.SerializeToString(&strRequestData);

120
	auto_ptr<zmq::message_t> reply = m_application->m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_responderEndpoint);
121
122
123
124
125

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

legoc's avatar
legoc committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
bool RequesterImpl::receiveBinary(std::string& response) {

	zmq::message_t * message = new zmq::message_t;
	m_requester->recv(message, 0);

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

	bool canceled = false;

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

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

	if (messageType.type() == proto::MessageType_Type_RESPONSE) {
		response = string(static_cast<char*>(message->data()), message->size());

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

	// Create the reply
	string data = "OK";
	size_t size = data.length();
	zmq::message_t * reply = new zmq::message_t(size);
	memcpy((void *) reply->data(), data.c_str(), size);

	m_requester->send(*reply);

	delete reply;

	return !canceled;
}

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

	string bytes;
	bool result = receiveBinary(bytes);

	parse(bytes, data);

	return result;
}

void RequesterImpl::cancel() {

	stringstream requesterEndpoint;
	requesterEndpoint << m_application->getUrl() << ":" << m_requesterPort;

	string strRequestType = m_application->m_impl->createRequest(PROTO_CANCEL);
	string strRequestData = "cancel";

185
	auto_ptr<zmq::message_t> reply = m_application->m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, requesterEndpoint.str());
legoc's avatar
legoc committed
186
187
188
189
190
191
192
193
194
195

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

void RequesterImpl::terminate() {

	if (m_requester.get() != 0) {
		m_requester.reset(0);

196
		bool success = m_application->removePort(getRequesterPortName(m_name, m_responderId, m_requesterId));
legoc's avatar
legoc committed
197
198
199
200
201
202
203
		if (!success) {
			cerr << "server cannot destroy requester " << m_name << endl;
		}
	}
}

}