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

#include <boost/bind.hpp>
#include <sstream>
21
#include "../Application.h"
22
#include "../Serializer.h"
23
24
#include "ApplicationImpl.h"
#include "RequestImpl.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
47
48
49
50
51
52
53
54
55
56
57
58

using namespace std;
using namespace boost;

namespace cameo {

const std::string ResponderImpl::RESPONDER_PREFIX = "rep.";

ResponderImpl::ResponderImpl(const application::This * application, int responderPort, const std::string& name) :
	m_application(application),
	m_responderPort(responderPort),
	m_name(name),
	m_ended(false) {

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

	m_responder->bind(repEndpoint.str().c_str());
}

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

void ResponderImpl::cancel() {

	stringstream endpoint;
	endpoint << m_application->getUrl() << ":" << m_responderPort;

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

59
	auto_ptr<zmq::message_t> reply = m_application->m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, endpoint.str());
legoc's avatar
legoc committed
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
102
103
104

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

WaitingImpl * ResponderImpl::waiting() {
	return new GenericWaitingImpl(bind(&ResponderImpl::cancel, this));
}

std::auto_ptr<RequestImpl> ResponderImpl::receive() {

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

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

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

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

		return auto_ptr<RequestImpl>(0);
	}

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

	auto_ptr<RequestImpl> result;

	if (messageType.type() == proto::MessageType_Type_REQUEST) {

		// Parse the message
		proto::Request messageRequest;
		messageRequest.ParseFromArray((*message).data(), (*message).size());

		// Create the request
105
106
107
108
109
110
111
		result = auto_ptr<RequestImpl>(new RequestImpl(m_application,
				messageRequest.applicationname(),
				messageRequest.applicationid(),
				messageRequest.message(),
				messageRequest.serverurl(),
				messageRequest.serverport(),
				messageRequest.requesterport()));
legoc's avatar
legoc committed
112

113
114
115
116
117
		// Set message 2 if it exists.
		if (messageRequest.has_message2()) {
			result->m_message2 = messageRequest.message2();
		}

legoc's avatar
legoc committed
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
144
145
146
147
148
149
	} else if (messageType.type() == proto::MessageType_Type_CANCEL) {
		m_ended = true;

	} else {
		cerr << "unknown message type " << messageType.type() << endl;
		m_responder->send(*message);
	}

	// send to the client
	if (reply != 0) {
		m_responder->send(*reply);
	}

	delete reply;
	delete message;

	return result;
}

void ResponderImpl::terminate() {

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

		bool success = m_application->removePort(RESPONDER_PREFIX + m_name);
		if (!success) {
			cerr << "server cannot destroy responder " << m_name << endl;
		}
	}
}

}