1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
5 /* Universidad de Cantabria, SPAIN */
6 /* University of York, UK */
7 /* Scuola Superiore Sant'Anna, ITALY */
8 /* Kaiserslautern University, GERMANY */
9 /* Univ. Politécnica Valencia, SPAIN */
10 /* Czech Technical University in Prague, CZECH REPUBLIC */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
19 /* FRESCOR project (FP6/2005/IST/5-034026) is funded */
20 /* in part by the European Union Sixth Framework Programme */
21 /* The European Union is not liable of any use that may be */
22 /* made of this code. */
25 /* This file is part of FORB (Frescor Object Request Broker) */
27 /* FORB is free software; you can redistribute it and/or modify it */
28 /* under terms of the GNU General Public License as published by the */
29 /* Free Software Foundation; either version 2, or (at your option) any */
30 /* later version. FORB is distributed in the hope that it will be */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
33 /* General Public License for more details. You should have received a */
34 /* copy of the GNU General Public License along with FORB; see file */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, */
36 /* Cambridge, MA 02139, USA. */
38 /* As a special exception, including FORB header files in a file, */
39 /* instantiating FORB generics or templates, or linking other files */
40 /* with FORB objects to produce an executable application, does not */
41 /* by itself cause the resulting executable application to be covered */
42 /* by the GNU General Public License. This exception does not */
43 /* however invalidate any other reasons why the executable file might be */
44 /* covered by the GNU Public License. */
45 /**************************************************************************/
49 * @author Michal Sojka <sojkam1@fel.cvut.cz>
50 * @date Sun Oct 12 19:04:21 2008
52 * @brief Implementation of request handling functions
57 #include <forb/forb-internal.h>
59 #include <forb/iop-idl.h>
60 #include <forb/object.h>
66 extern UL_LOG_CUST(ulogd_forb_request);
68 static inline int forb_request_cmp(const CORBA_unsigned_long *a,
69 const CORBA_unsigned_long *b)
76 GAVL_CUST_NODE_INT_IMP(forb_request_nolock /* cust_prefix */,
77 forb_t /* cust_root_t */,
78 forb_request_t /* cust_item_t */,
79 CORBA_unsigned_long /* cust_key_t */,
80 requests /* cust_root_node */,
81 node /* cust_item_node */,
82 request_id /* cust_item_key */,
83 forb_request_cmp/* cust_cmp_fnc */);
86 * Initializes @a req structure for sending new request to object @a obj.
88 * @param req Request structure to initialize
89 * @param obj Destination object
91 * @return Zero on success, FOSA error code on error.
94 forb_request_init(forb_request_t *req, forb_object obj)
97 forb_t *forb = forb_object_to_forb(obj);
100 memset(req, 0, sizeof(*req));
103 fosa_mutex_lock(&forb->request_id_mutex);
104 req->request_id = forb->request_id++;
105 fosa_mutex_unlock(&forb->request_id_mutex);
107 FORB_CDR_codec_init_static(&req->cdr_request, obj->orb);
108 bret = FORB_CDR_buffer_init(&req->cdr_request, 4096-8, forb_iop_MESSAGE_HEADER_SIZE);
109 if (bret == CORBA_FALSE) {
113 ret = forb_syncobj_init(&req->reply_ready, 0);
118 forb_request_destroy(forb_request_t *req)
120 forb_t *forb = forb_object_to_forb(req->obj);
122 forb_request_delete(forb, req);
123 FORB_CDR_codec_release_buffer(&req->cdr_request);
124 forb_syncobj_destroy(&req->reply_ready);
128 forb_request_signal_reply(forb_request_t *req)
130 forb_syncobj_signal(&req->reply_ready);
134 forb_request_wait_for_reply(forb_request_t *req)
136 fosa_abs_time_t timeout;
139 /* FIXME: What is the appropriate length for timeout? Maybe,
140 * we can specify timeout in forb_env, but since it is
141 * out-direction parameter, we cannot distinguish between
142 * initialized and uninitialized env.timeout. */
143 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
144 timeout = fosa_abs_time_incr(timeout,
145 fosa_msec_to_rel_time(10000));
147 ret = forb_syncobj_timedwait(&req->reply_ready, &timeout);
149 if (ret == ETIMEDOUT) {
150 req->env->major = FORB_EX_TIMEOUT;
152 req->env->major = FORB_EX_INTERNAL;
157 forb_request_signal_processed(forb_request_t *req)
159 /* Signal, that we are done */
160 forb_syncobj_signal(req->reply_processed);
164 * Sends REQUEST message to another FORB.
166 * The request @a req has to be prepared by
167 * forb_iop_prepare_request(). Then, this function adds a message
168 * header, connects to the destination FORB and sends the request.
170 * If no exception is reported, then the caller must wait for response
171 * by calling forb_wait_for_reply().
173 * @param req A request prepared by forb_iop_prepare_request()
174 * @param env Environment for returning exceptions
177 forb_request_send(forb_request_t *req, CORBA_Environment *env)
182 fosa_abs_time_t timeout;
183 forb_t *forb = forb_object_to_forb(req->obj);
186 env->major = FORB_EX_INTERNAL;
190 req->env = env; /* Remember, where to return exceptions */
192 ret = forb_iop_prepend_message_header(&req->cdr_request, forb_iop_REQUEST);
194 /* This should never happen */
195 env->major = FORB_EX_INTERNAL;
199 fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
200 timeout = fosa_abs_time_incr(timeout,
201 fosa_msec_to_rel_time(1000));
202 peer = forb_get_next_hop(forb, &req->obj->server, &timeout);
205 ul_logmsg("Cannot find peer to send request for server %s\n",
206 forb_server_id_to_string(str, &req->obj->server, sizeof(str)));
207 env->major = FORB_EX_COMM_FAILURE;
210 /* Register the request with forb so we can match incomming
211 * reply to this request. */
212 ret = forb_request_insert(forb, req);
214 ul_logerr("Insert request error %d\n", ret);
215 env->major = FORB_EX_INTERNAL;
219 len = FORB_CDR_data_size(&req->cdr_request);
220 size = forb_proto_send(peer, &req->cdr_request);
221 if (size <= 0 || size != len) {
222 env->major = FORB_EX_COMM_FAILURE;
223 /* Request is deleted when the stub calls forb_request_destroy() */