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

19
#include "../Application.h"
20
#include "../Serializer.h"
21
#include "ServicesImpl.h"
legoc's avatar
legoc committed
22
#include "RequestSocketImpl.h"
23
#include <sstream>
legoc's avatar
legoc committed
24
25
26
27
28

using namespace std;

namespace cameo {

legoc's avatar
legoc committed
29
RequestImpl::RequestImpl(application::This * application, const std::string & requesterApplicationName, int requesterApplicationId, const std::string& message, const std::string& serverUrl, int serverPort, int requesterPort) :
legoc's avatar
legoc committed
30
31
	m_application(application),
	m_message(message),
32
	m_requesterApplicationName(requesterApplicationName),
33
34
	m_requesterApplicationId(requesterApplicationId),
	m_timeout(0) {
35
36
37
38
39
40
41
42

	stringstream requesterEndpoint;
	requesterEndpoint << serverUrl << ":" << requesterPort;
	m_requesterEndpoint = requesterEndpoint.str();

	stringstream requesterServerEndpoint;
	requesterServerEndpoint << serverUrl << ":" << serverPort;
	m_requesterServerEndpoint = requesterServerEndpoint.str();
legoc's avatar
legoc committed
43
44
45
46
47
}

RequestImpl::~RequestImpl() {
}

48
49
50
51
void RequestImpl::setTimeout(int value) {
	m_timeout = value;
}

legoc's avatar
legoc committed
52
void RequestImpl::replyBinary(const std::string& response) {
legoc's avatar
legoc committed
53
54

	// Create a request socket. It is created for each request that could be optimized.
55
56
57
58
59
60
61
62
63
	unique_ptr<RequestSocketImpl> requestSocket = m_application->createRequestSocket(m_requesterEndpoint, m_timeout);
	//requestSocket->requestAsync(m_application->m_impl->createRequestType(PROTO_RESPONSE), response);

	try {
		requestSocket->request(m_application->m_impl->createRequestType(PROTO_RESPONSE), response);
	}
	catch (const ConnectionTimeout&) {
		cout << "timeout while replying" << endl;
	}
legoc's avatar
legoc committed
64
65
}

legoc's avatar
legoc committed
66
void RequestImpl::reply(const std::string& response) {
legoc's avatar
legoc committed
67

legoc's avatar
legoc committed
68
	// Encode the data.
legoc's avatar
legoc committed
69
70
71
	string result;
	serialize(response, result);

legoc's avatar
legoc committed
72
	replyBinary(result);
legoc's avatar
legoc committed
73
74
75
76
}

}