]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/request.c
46ad81da1d7074731f1678067888d0fe66d4c699
[frescor/forb.git] / src / request.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
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                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
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.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FORB (Frescor Object Request Broker)             */
26 /*                                                                        */
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.                                              */
37 /*                                                                        */
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 /**************************************************************************/
46
47 /**
48  * @file   request.c
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Sun Oct 12 19:04:21 2008
51  * 
52  * @brief  Implementation of request handling functions
53  * 
54  * 
55  */
56
57 #include <forb/forb-internal.h>
58 #include "request.h"
59 #include <forb/iop-idl.h>
60 #include <forb/object.h>
61 #include "peer.h"
62 #include "iop.h"
63 #include "proto.h"
64 #include <ul_log.h>
65
66 extern UL_LOG_CUST(ulogd_forb_request);
67
68 static inline int forb_request_cmp(const CORBA_unsigned_long *a,
69                                    const CORBA_unsigned_long *b)
70 {
71         return (*a<*b) ? -1 :
72                 ((*a>*b) ? +1 :
73                  0);
74 }
75
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 */);
84
85 /** 
86  * Initializes @a req structure for sending new request to object @a obj.
87  * 
88  * @param req Request structure to initialize
89  * @param obj Destination object
90  *
91  * @return Zero on success, FOSA error code on error.
92  */
93 int
94 forb_request_init(forb_request_t *req, forb_object obj)
95 {
96         int ret = 0;
97         forb_t *forb = forb_object_to_forb(obj);
98         CORBA_boolean bret;
99
100         memset(req, 0, sizeof(*req));
101         req->obj = obj;
102
103         fosa_mutex_lock(&forb->request_id_mutex);
104         req->request_id = forb->request_id++;
105         fosa_mutex_unlock(&forb->request_id_mutex);
106
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) {
110                 return FOSA_ENOMEM;
111         }
112         
113         ret = forb_syncobj_init(&req->reply_ready, 0);
114         return ret;
115 }
116
117 void
118 forb_request_destroy(forb_request_t *req)
119 {
120         forb_t *forb = forb_object_to_forb(req->obj);
121
122         forb_request_delete(forb, req);
123         FORB_CDR_codec_release_buffer(&req->cdr_request);
124         forb_syncobj_destroy(&req->reply_ready);
125 }
126
127 void
128 forb_request_signal_reply(forb_request_t *req)
129 {
130         forb_syncobj_signal(&req->reply_ready); 
131 }
132
133 void
134 forb_request_wait_for_reply(forb_request_t *req)
135 {
136         fosa_abs_time_t timeout;
137         int ret;
138
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));
146
147         ret = forb_syncobj_timedwait(&req->reply_ready, &timeout);
148
149         if (ret == ETIMEDOUT) {
150                 req->env->major = FORB_EX_TIMEOUT;
151         } else if (ret) {
152                 req->env->major = FORB_EX_INTERNAL;
153         }
154 }
155
156 void
157 forb_request_signal_processed(forb_request_t *req)
158 {
159         /* Signal, that we are done */
160         forb_syncobj_signal(req->reply_processed);
161 }
162
163 /** 
164  * Sends REQUEST message to another FORB.
165  *
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.
169  *
170  * If no exception is reported, then the caller must wait for response
171  * by calling forb_wait_for_reply().
172  * 
173  * @param req A request prepared by forb_iop_prepare_request()
174  * @param env Environment for returning exceptions
175  */
176 void
177 forb_request_send(forb_request_t *req, CORBA_Environment *env)
178 {
179         CORBA_boolean ret;
180         forb_peer_t *peer;
181         size_t size, len;
182         fosa_abs_time_t timeout;
183         forb_t *forb = forb_object_to_forb(req->obj);
184
185         if (!forb) {
186                 env->major = FORB_EX_INTERNAL;
187                 return;
188         }
189
190         req->env = env;     /* Remember, where to return exceptions */
191
192         ret = forb_iop_prepend_message_header(&req->cdr_request, forb_iop_REQUEST);
193         if (!ret) {
194                 /* This should never happen */
195                 env->major = FORB_EX_INTERNAL;
196                 return;
197         }
198
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);
203         if (!peer) {
204                 char str[50];
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;
208                 return;
209         }
210         /* Register the request with forb so we can match incomming
211          * reply to this request. */
212         ret = forb_request_insert(forb, req);
213         if (ret <= 0) {
214                 ul_logerr("Insert request error %d\n", ret);
215                 env->major = FORB_EX_INTERNAL;
216                 goto err_peer_put;
217         }
218
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() */
224         }
225  err_peer_put:
226         forb_peer_put(peer);
227 }