]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/request.c
libforb is now compiled as shared library
[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(CORBA_unsigned_long *a, CORBA_unsigned_long *b)
69 {
70         return (*a<*b) ? -1 :
71                 ((*a>*b) ? +1 :
72                  0);
73 }
74
75 GAVL_CUST_NODE_INT_IMP(forb_request_nolock /* cust_prefix */,
76                        forb_t /* cust_root_t */,
77                        forb_request_t /* cust_item_t */,
78                        CORBA_unsigned_long /* cust_key_t */,
79                        requests /* cust_root_node */,
80                        node /* cust_item_node */,
81                        request_id /* cust_item_key */,
82                        forb_request_cmp/* cust_cmp_fnc */);
83
84 /** 
85  * Initializes @a req structure for sending new request to object @a obj.
86  * 
87  * @param req Request structure to initialize
88  * @param obj Destination object
89  *
90  * @return Zero on success, FOSA error code on error.
91  */
92 int
93 forb_request_init(forb_request_t *req, forb_object obj)
94 {
95         int ret = 0;
96         forb_t *forb = forb_object_to_forb(obj);
97         CORBA_boolean bret;
98
99         memset(req, 0, sizeof(*req));
100         req->obj = obj;
101
102         fosa_mutex_lock(&forb->request_id_mutex);
103         req->request_id = forb->request_id++;
104         fosa_mutex_unlock(&forb->request_id_mutex);
105
106         CDR_codec_init_static(&req->cdr_request);
107         bret = CDR_buffer_init(&req->cdr_request, 256, forb_iop_MESSAGE_HEADER_SIZE);
108         if (bret == CORBA_FALSE) {
109                 return FOSA_ENOMEM;
110         }
111         
112         ret = forb_syncobj_init(&req->reply_ready, 0);
113         return ret;
114 }
115
116 void
117 forb_request_destroy(forb_request_t *req)
118 {
119         CDR_codec_release_buffer(&req->cdr_request);
120         forb_syncobj_destroy(&req->reply_ready);
121 }
122
123 void
124 forb_request_signal_reply(forb_request_t *req)
125 {
126         forb_syncobj_signal(&req->reply_ready); 
127 }
128
129 void
130 forb_request_wait_for_reply(forb_request_t *req)
131 {
132         forb_syncobj_wait(&req->reply_ready); 
133 }
134
135 void
136 forb_request_signal_processed(forb_request_t *req)
137 {
138         /* Signal, that we are done */
139         forb_syncobj_signal(req->reply_processed);
140 }
141
142 /** 
143  * Sends REQUEST message to another FORB.
144  *
145  * The request @a req has to be prepared by
146  * forb_iop_prepare_request(). Then, this function adds a message
147  * header, connects to the destination FORB and sends the request.
148  *
149  * If no exception is reported, then the caller must wait for response
150  * by calling forb_wait_for_reply().
151  * 
152  * @param req A request prepared by forb_iop_prepare_request()
153  * @param env Environment for returning exceptions
154  */
155 void
156 forb_request_send(forb_request_t *req, CORBA_Environment *env)
157 {
158         CORBA_boolean ret;
159         forb_peer_t *peer;
160         size_t size;
161         fosa_abs_time_t timeout;
162         forb_t *forb = forb_object_to_forb(req->obj);
163
164         if (!forb) {
165                 env->major = FORB_EX_INTERNAL;
166                 return;
167         }
168
169         req->env = env;     /* Remember, where to return exceptions */
170
171         ret = forb_iop_prepend_message_header(&req->cdr_request, forb_iop_REQUEST);
172         if (!ret) {
173                 /* This should never happen */
174                 env->major = FORB_EX_INTERNAL;
175                 return;
176         }
177
178         fosa_clock_get_time(FOSA_CLOCK_ABSOLUTE, &timeout);
179         timeout = fosa_abs_time_incr(timeout,
180                                      fosa_msec_to_rel_time(1000));
181         peer = forb_get_next_hop(forb, &req->obj->server, &timeout);
182         if (!peer) {
183                 char str[50];
184                 ul_logerr("Cannot find peer to send request for server %s\n",
185                           forb_server_id_to_string(str, &req->obj->server, sizeof(str)));
186                 env->major = FORB_EX_TRANSIENT;
187                 return;
188         }
189         
190         ret = forb_request_insert(forb, req);
191         if (ret <= 0) {
192                 ul_logerr("Insert request error %d\n", ret);
193                 env->major = FORB_EX_INTERNAL;
194                 return;
195         }
196
197         size = forb_proto_send(peer, &req->cdr_request);
198         if (size <= 0) {
199                 forb_request_delete(forb, req);
200                 env->major = FORB_EX_COMM_FAILURE;
201         }
202 }