]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - src/object.h
fd255cc34b20623bfa744ff23fd42550c7172acc
[frescor/forb.git] / src / object.h
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   object.h
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Sun Oct 12 17:03:44 2008
51  * 
52  * @brief  Declarations for FORB's object references.
53  * 
54  * 
55  */
56
57 #ifndef FORB_OBJECT_H
58 #define FORB_OBJECT_H
59
60 #include <ul_gavlcust.h>
61 #include <forb/forb-internal.h>
62 #include <stdbool.h>
63 #include <forb/refcnt.h>
64 #include <forb/basic_types.h>
65 #include <forb/executor.h>
66 #include <forb/exec_req.h>
67
68 /**
69  * Object reference structure.
70  *
71  * The only ways for applications to create an object reference are:
72  * - call IDL generated forb_<interface>_new() function,
73  * - call forb_string_to_object().
74  * - call forb_get_orb_of()
75  * - call forb_init()
76  *
77  * When the created object is not needed, the application should call
78  * forb_object_release().
79  *
80  * If the object reference is passed as a parameter of interface
81  * method, the same rules as in CORBA C mapping objref_ptr applies,
82  * which is:
83  * 
84  * - if the method needs to store the object reference, it must call
85  *   forb_object_duplicate() and store the duplicate.
86  */
87 struct _forb_object {
88         /** @name Fields valid for both local and remote object references */
89         /*@{*/
90         
91         /** Server implementing this object. @see forb::server_id */
92         forb_server_id server;
93         /** Object key of the object. @see forb::object_key */
94         forb_object_key objkey;
95
96         forb_orb orb;           /**< FORB reference */
97         forb_ref_t ref;         /**< Reference count */
98         /*@}*/
99
100         /** @name Fields used only in implementation (server) */
101         /*@{*/
102
103         /** Any data for implementation */
104         void *instance_data;
105
106         /** Description of interface implemented by this object (for
107          * servers); FIXME: What about clients? */
108         const forb_interface_t *interface;
109
110         /** Pointer to the object implementation methods or NULL in
111          * case of remote object. */
112         const void *implementation;
113
114         /** Executor object, which represents the thread the requests
115          * are executed in. */
116         forb_executor_t *executor;
117
118         /**
119          * Pointer to the execute request in case of remote
120          * invocation.
121          *
122          * @warning If the object can be called both localy and
123          * remotely this field may contain wrong value as the local
124          * and remote requests can be executed in parallel, in
125          * different threads. This will be fixed after even local
126          * invocations will be executed through executors. */
127         forb_exec_req_t *exec_req;
128
129         gavl_node_t node;       /**< Node for forb->objects tree */
130         /*@}*/
131 };
132
133 /**
134  * Description of an IDL interface.
135  *
136  * Instances of this structure are generated by forb-idl (IDL compiler).
137  */
138 struct forb_interface {
139         char *name;             /**< Name of the interface */
140         unsigned num_methods;   /**< Number of methods */
141         const forb_skel_func *skeletons; /**< Array of pointers to skeleton functions (one function for every method) */
142         uint32_t type_hash;     /**< Not implemented */
143 };
144
145 /**
146  * Returns forb_object::instance_data of an object.
147  * 
148  */
149 #define forb_object_instance_data(obj) ((obj)->instance_data)
150
151 /** Return forb_t from an object reference. */
152 static inline forb_t *
153 forb_data(forb_orb orb)
154 { return forb_object_instance_data(orb); }
155
156
157 /** 
158  * Returns pointer to forb_t from any object reference.
159  * 
160  * @param obj Object reference.
161  * 
162  * @return Pointer to forb_t which handles the @a obj.
163  */
164 static inline forb_t *
165 forb_object_to_forb(forb_object obj)
166 { return forb_data(obj->orb); }
167
168 forb_object_key forb_object_to_key(forb_object obj);
169 forb_object forb_key_to_object(forb_t *forb, forb_object_key key);
170
171 static inline int forb_objkey_cmp(const forb_object_key *a, const forb_object_key *b)
172 {
173         return (*a<*b) ? -1 :
174                 ((*a>*b) ? +1 :
175                  0);
176 }
177
178 forb_object
179 forb_object_new(forb_orb orb,
180                 forb_server_id *server_id,
181                 forb_object_key key);
182
183 /* Declaration of typesafe function for storing objects in GAVL tree
184  * ordered by object keys. */
185 GAVL_CUST_NODE_INT_DEC(forb_objects_nolock /* cust_prefix */,
186                        forb_t /* cust_root_t */,
187                        struct _forb_object /* cust_item_t */,
188                        forb_object_key /* cust_key_t */,
189                        objects /* cust_root_node */,
190                        node /* cust_item_node */,
191                        objkey /* cust_item_key */,
192                        forb_objkey_cmp/* cust_cmp_fnc */);
193
194
195 /** 
196  * Deletes object reference of an implementation from forb_t::objects.
197  * 
198  * @param forb 
199  * @param obj 
200  */
201 static inline void
202 forb_objects_delete(forb_t *forb, forb_object obj)
203 {
204         fosa_mutex_lock(&forb->objkey_mutex);
205         forb_objects_nolock_delete(forb, obj);
206         fosa_mutex_unlock(&forb->objkey_mutex);
207 }
208
209 /** 
210  * Finds implementation of an object with a given key.
211  * 
212  * @param forb 
213  * @param objkey 
214  * 
215  * @return Object references if it exists or NULL otherwise.
216  */
217 static inline forb_object
218 forb_objects_find(forb_t *forb, forb_object_key objkey)
219 {
220         forb_object ret;
221         fosa_mutex_lock(&forb->objkey_mutex);
222         ret = forb_objects_nolock_find(forb, &objkey);
223         ret = forb_object_duplicate(ret);
224         fosa_mutex_unlock(&forb->objkey_mutex);
225         return ret;
226 }
227
228 /** 
229  * Determines whether the object is local (in-process) or remote.
230  * 
231  * @param obj 
232  * 
233  * @return true if local, false otherwise or in case of invalid object reference.
234  */
235 static inline bool
236 forb_object_is_local(forb_object obj)
237 {
238         return obj && obj->implementation;
239 }
240 /** 
241  * Determines whether the object is remote.
242  * 
243  * @param obj 
244  * 
245  * @return true if remote, false otherwise or in case of invalid object reference.
246  */
247 static inline bool
248 forb_object_is_remote(forb_object obj)
249 {
250         return obj && (obj->implementation == NULL);
251 }
252
253 CORBA_boolean
254 forb_object_serialize(FORB_CDR_Codec *codec, const forb_object *obj);
255 CORBA_boolean
256 forb_object_deserialize(FORB_CDR_Codec *codec, forb_object *obj);
257
258 #endif