OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / include / mico / orb_mico.h
1 // -*- c++ -*-
2 /*
3  *  MICO --- an Open Source CORBA implementation
4  *  Copyright (c) 1997-2003 by The Mico Team
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  *  For more information, visit the MICO Home Page at
21  *  http://www.mico.org/
22  */
23
24 #ifndef __mico_orb_mico_h__
25 #define __mico_orb_mico_h__
26
27 #ifndef HAVE_THREADS
28 #  define USE_ORB_CACHE
29 #endif
30
31 namespace CORBA {
32
33 class ObjectAdapter;
34 //class ORBRequest;
35 struct ORBCallback;
36
37 typedef ORBRequest *ORBRequest_ptr;
38 typedef ObjVar<ORBRequest> ORBRequest_var;
39 typedef ObjOut<ORBRequest> ORBRequest_out;
40
41
42 class PrincipalCurrent_impl : public CORBA::PrincipalCurrent {
43 public:
44     CORBA::Principal_ptr get_principal ();
45 };
46
47
48 class ORBInvokeRec {
49 public:
50     typedef ULong MsgId;
51 private:
52     MsgId _myid;
53     RequestType _type;
54     Boolean _have_result;
55     Boolean _active;
56     Object_ptr _obj;
57     Object_ptr _target;
58     Principal_ptr _principal;
59     Boolean _response_expected;
60     std::string _repoid;
61     InvokeStatus _invoke_stat;
62     LocateStatus _locate_stat;
63     ObjectAdapter *_adapter;
64     ORBRequest *_req;
65     ORBCallback *_cb;
66     ORB_ptr _orb;
67     OctetSeq _objtag;
68     Address *_addr;
69     void *_req_hint;
70     void *_inv_hint;
71  
72     GIOP::AddressingDisposition _ad;
73     PInterceptor::ServerRequestInfo_impl* _sri;
74
75 public:
76     ORBInvokeRec (MsgId);
77     virtual ~ORBInvokeRec ();
78
79     void free();
80
81     void init_invoke (ORB_ptr, Object_ptr target,
82                       ORBRequest *, Principal_ptr pr,
83                       Boolean response_expected,
84                       ORBCallback *, ObjectAdapter * = 0);
85
86     void init_locate (ORB_ptr, Object_ptr,
87                       ORBCallback *, ObjectAdapter * = 0);
88
89     void init_bind (ORB_ptr, const char *,
90                     const OctetSeq &tag, Address *, ORBCallback *,
91                     ObjectAdapter * = 0);
92
93     void set_request_hint(void *hint) 
94     { _req_hint = hint; }
95     void *get_request_hint()
96     { return _req_hint; }
97
98     void set_invoke_hint(void *hint) 
99     { _inv_hint = hint; }
100     void *get_invoke_hint()
101     { return _inv_hint; }
102
103     Boolean active() const
104     { return _active; };
105     void deactivate()
106     { _active = FALSE; };
107
108     MsgId id () const
109     { return _myid; }
110
111     Boolean completed () const
112     { return _have_result; }
113
114     ObjectAdapter *oa ()
115     { return _adapter; }
116
117     void oa (ObjectAdapter *oaval)
118     { _adapter = oaval; }
119     
120     RequestType request_type ()
121     { return _type; }
122
123     ORBRequest *request ()
124     { return _req; }
125
126     const OctetSeq &tag () const
127     { return _objtag; }
128
129     ORBCallback *callback ()
130     { return _cb; }
131
132     Principal_ptr principal ()
133     { return _principal; }
134     
135     PInterceptor::ServerRequestInfo_impl* requestinfo()
136     { return _sri; }
137
138     CORBA::Boolean response_expected()
139     { return _response_expected; }
140     
141     void redo ();
142
143     void set_answer_invoke (InvokeStatus, Object_ptr, ORBRequest *,
144                             GIOP::AddressingDisposition);
145     void set_answer_bind (LocateStatus, Object_ptr);
146     void set_answer_locate (LocateStatus, Object_ptr,
147                             GIOP::AddressingDisposition);
148
149     Boolean get_answer_invoke (InvokeStatus &, Object_ptr &, ORBRequest *&,
150                                GIOP::AddressingDisposition &);
151     Boolean get_answer_bind (LocateStatus &, Object_ptr &);
152     Boolean get_answer_locate (LocateStatus &, Object_ptr &,
153                                GIOP::AddressingDisposition &);
154 };
155
156
157
158 class ORB : public ServerlessObject {
159 public:
160     typedef CORBA::ULong MsgId;
161 private:
162 #ifdef HAVE_THREADS
163     class ResourceManager {
164       ULong _connection_limit;
165       ULong _request_limit;
166       ULong _connection_count;
167       MICOMT::Mutex conn_cnt_mutex;
168     public:
169       ResourceManager () {
170         _connection_limit = 10;
171         _request_limit = 10;
172         _connection_count = 0;
173       }
174       inline ULong connection_limit (ULong NewLimit) {
175         MICOMT::AutoLock l(conn_cnt_mutex);
176         return _connection_limit = NewLimit;
177       }
178       inline ULong connection_limit (void) const {
179         return _connection_limit;
180       }
181       inline ULong request_limit (ULong NewLimit) {
182         return _request_limit = NewLimit;
183       }
184       inline ULong request_limit (void) const {
185         return _request_limit;
186       }
187
188       //
189       // Not used, but might be handy if we ever want to know how many con-
190       // nections are open at any point in time...
191       //
192       inline ULong connection_count (void) const {
193         return _connection_count;
194       }
195
196       inline CORBA::Boolean acquire_connection (void) {
197         MICOMT::AutoLock l(conn_cnt_mutex);
198         if (_connection_limit > 0) {
199           if (_connection_count < _connection_limit) {
200             _connection_count++;
201             return TRUE;
202           }
203           return FALSE;
204         }
205         else {
206           // connection checking disabled by -ORBConnLimit 0
207           _connection_count++;
208           return TRUE;
209         }
210       }
211       inline void release_connection (void) {
212         MICOMT::AutoLock l(conn_cnt_mutex);
213         _connection_count--;
214       }
215     } resourceManager;
216 #endif // HAVE_THREADS
217     typedef std::vector<ObjectAdapter *> OAVec;
218     typedef std::map<MsgId, ORBInvokeRec *, std::less<MsgId> > InvokeMap;
219     typedef std::map<std::string, Object_var, std::less<std::string> >
220     InitialRefMap;
221     typedef std::map<std::string, ValueFactoryBase_var, std::less<std::string> >
222     ValueFactoryMap;
223     typedef std::list<std::string> isa_cacheList;
224
225     std::string _default_init_ref;
226
227     MICOMT::Locked<isa_cacheList> _isa_cache;
228     std::vector<std::string> _bindaddrs;
229     MICOMT::RWLocked<OAVec> _adapters;
230     MICOMT::RWLocked<InvokeMap> _invokes;
231
232     Dispatcher *_disp;
233     IOR *_tmpl;
234
235     MsgId _theid, _currentid;
236     MICOMT::Mutex _theid_lock;
237 #ifdef HAVE_THREADS
238     MICOMT::Thread::ThreadKey _current_rec_key;
239     MICOMT::Mutex run_lock_;
240     MICOMT::Thread::ThreadID main_thread_id_;
241     MICOMT::Mutex shutdown_lock_;
242 #endif // HAVE_THREADS
243     InitialRefMap _init_refs;
244     MICOMT::Mutex _init_refs_lock;
245
246     std::string _rcfile;
247     DomainManager_var _def_manager;
248
249     MICOMT::Locked<ValueFactoryMap> _value_facs;
250
251     Boolean _is_running;
252     Boolean _is_shutdown;
253     Boolean _is_stopped;
254     Boolean _wait_for_completion;
255     OAVec _shutting_down_adapters;
256
257 #ifdef USE_ORB_CACHE
258     ORBInvokeRec *_cache_rec;
259     CORBA::Boolean _cache_used;
260 #endif
261
262     Boolean is_local (Object_ptr);
263     ObjectAdapter *get_oa (Object_ptr);
264     ORBInvokeRec *create_invoke (MsgId);
265     void add_invoke (ORBInvokeRec *);
266     ORBInvokeRec *get_invoke (MsgId);
267     void del_invoke (MsgId);
268
269     // internal ORBMsgId handling
270     ORBInvokeRec *get_invoke (ORBMsgId id) { return id; };
271
272     void do_shutdown ();
273
274     CORBA::Object_ptr corbaloc_to_object (const char *);
275     CORBA::Object_ptr corbaname_to_object (const char *);
276     CORBA::Object_ptr iioploc_to_object (const char *);
277     CORBA::Object_ptr iiopname_to_object (const char *);
278     CORBA::Object_ptr file_to_object (const char *);
279     CORBA::Object_ptr http_to_object (const char *);
280
281     Boolean builtin_invoke (ORBMsgId, Object_ptr,
282                             ORBRequest *, Principal_ptr);
283     Object_ptr _resolve_initial_references (const char *id);
284 public:
285 #ifdef HAVE_THREADS
286     inline ResourceManager &resource_manager (void) {
287       return resourceManager;
288     }
289
290     void
291     set_main_thread_id(MICOMT::Thread::ThreadID id)
292     { main_thread_id_ = id; }
293 #endif // HAVE_THREADS
294     typedef IfaceSequenceTmpl<Request_var,Request_ptr> RequestSeq;
295     typedef TSeqVar<RequestSeq> RequestSeq_var;
296
297     typedef OctetSeq ObjectTag;
298     typedef TSeqVar<ObjectTag> ObjectTag_var;
299
300     ORB (int &argc, char **argv, const char *rcfile);
301     virtual ~ORB ();
302
303     char *object_to_string (Object_ptr);
304     Object_ptr string_to_object (const char *);
305     Object_ptr ior_to_object (IOR *);
306     Boolean is_impl (Object_ptr);
307     const char *rcfile();
308
309     static char *tag_to_string (const ObjectTag &);
310     static ObjectTag *string_to_tag (const char *);
311
312     void create_list (Long, NVList_out);
313     void create_operation_list (OperationDef *,
314                                 NVList_out);
315     void create_named_value (NamedValue_out);
316     void create_exception_list (ExceptionList_out);
317     void create_context_list (ContextList_out);
318     void create_environment (Environment_out);
319     
320     void get_default_context (Context_out);
321
322     // mico-extension
323     void get_default_domain_manager (DomainManager_out);
324
325     CORBA::Boolean get_service_information (
326         CORBA::ServiceType service_type,
327         CORBA::ServiceInformation_out service_information);
328
329     void send_multiple_requests_oneway (const RequestSeq &);
330     void send_multiple_requests_deferred (const RequestSeq &);
331     Boolean poll_next_response ();
332     void get_next_response (Request_out);
333
334     Boolean work_pending ();
335     void perform_work ();
336     void run ();
337     void shutdown (Boolean wait_for_completion);
338     void destroy ();
339
340     typedef char *OAid;
341     BOA_ptr BOA_init (int &argc, char **argv, const char *boa_id = "");
342     BOA_ptr BOA_instance (const char *id, Boolean create=TRUE);
343
344     typedef CORBA::ORB_InvalidName InvalidName;
345     typedef CORBA::ORB_InvalidName_catch InvalidName_catch;
346
347     typedef char *ObjectId;
348     typedef StringSequenceTmpl<String_var> ObjectIdList;
349     typedef ObjectIdList *ObjectIdList_ptr;
350     typedef TSeqVar<ObjectIdList> ObjectIdList_var;
351
352     ObjectIdList_ptr list_initial_services ();
353     Object_ptr resolve_initial_references (const char *id)
354         {
355             MICOMT::AutoLock l(_init_refs_lock);
356             return _resolve_initial_references(id);
357         }
358     Boolean set_initial_reference (const char *id, Object_ptr obj);
359     // added according to PI spec
360     void register_initial_reference (const char *id, Object_ptr obj);
361     // end
362     Boolean set_default_initial_reference (const char *);
363
364     virtual TypeCode_ptr create_struct_tc (const char *rep_id,
365                                            const char *name,
366                                            const StructMemberSeq &);
367     virtual TypeCode_ptr create_union_tc (const char *rep_id,
368                                           const char *name,
369                                           TypeCode_ptr disc_type,
370                                           const UnionMemberSeq &);
371     virtual TypeCode_ptr create_enum_tc (const char *rep_id,
372                                          const char *name,
373                                          const EnumMemberSeq &);
374     virtual TypeCode_ptr create_exception_tc (const char *rep_id,
375                                               const char *name,
376                                               const StructMemberSeq &);
377     virtual TypeCode_ptr create_alias_tc (const char *rep_id,
378                                           const char *name,
379                                           TypeCode_ptr orig_type);
380     virtual TypeCode_ptr create_interface_tc (const char *rep_id,
381                                               const char *name);
382     virtual TypeCode_ptr create_string_tc (ULong bound);
383     virtual TypeCode_ptr create_wstring_tc (ULong bound);
384     virtual TypeCode_ptr create_fixed_tc (UShort digits, Short scale);
385     virtual TypeCode_ptr create_sequence_tc (ULong bound,
386                                              TypeCode_ptr el_type);
387     virtual TypeCode_ptr create_recursive_sequence_tc (ULong bound,
388                                                        ULong offset);
389     virtual TypeCode_ptr create_array_tc (ULong length,
390                                           TypeCode_ptr el_type);
391     virtual TypeCode_ptr create_value_tc (const char *rep_id, const char *name,
392                                           ValueModifier mod,
393                                           TypeCode_ptr concrete_base,
394                                           const ValueMemberSeq &members);
395     virtual TypeCode_ptr create_value_box_tc (const char *rep_id,
396                                               const char *name,
397                                               TypeCode_ptr boxed_type);
398     virtual TypeCode_ptr create_native_tc (const char *rep_id,
399                                            const char *name);
400     virtual TypeCode_ptr create_recursive_tc (const char *rep_id);
401     virtual TypeCode_ptr create_abstract_interface_tc (const char *rep_id,
402                                                        const char *name);
403
404     // begin-value
405     ValueFactory register_value_factory (const char *repoid,
406                                          ValueFactory factory);
407     void unregister_value_factory (const char *repoid);
408     ValueFactory lookup_value_factory (const char *repoid);
409     // end-value
410
411     CORBA::Policy_ptr create_policy (CORBA::PolicyType type,
412                                      const CORBA::Any &any);
413
414     // begin-mico-extension
415     const std::vector<std::string> &bindaddrs () const
416     {
417         return _bindaddrs;
418     }
419     void bindaddrs (const std::vector<std::string> &b)
420     {
421         _bindaddrs = b;
422     }
423
424     void dispatcher (Dispatcher *);
425     Dispatcher *dispatcher ()
426     {
427         return _disp;
428     }
429     IOR *ior_template ()
430     {
431         return _tmpl;
432     }
433
434     void register_oa (ObjectAdapter *);
435     void unregister_oa (ObjectAdapter *);
436
437     void register_profile_id (CORBA::ULong id);
438     void unregister_profile_id (CORBA::ULong id);
439
440     MsgId new_msgid ();
441     
442 #ifdef USE_CSL2
443     Principal_ptr get_principal (Object_ptr);
444 #endif // USE_CSL2
445
446     ORBMsgId new_orbid (MsgId msgid = 0);
447     static MsgId get_msgid (ORBMsgId rec) { return rec ? rec->id() : 0; };
448     ORBMsgId get_orbid (MsgId id) { return get_invoke(id); };
449
450     //FIXME: hopefully nobody is going to shot me for this :-)
451     void set_request_hint(ORBMsgId id, void *hint) {
452         ORBInvokeRec *rec = get_invoke (id);
453         assert( rec );
454         rec->set_request_hint( hint );
455     };
456     void *get_request_hint(ORBMsgId id) {
457         ORBInvokeRec *rec = get_invoke (id);
458         assert( rec );
459         return rec->get_request_hint();
460     };
461     void set_invoke_hint(ORBMsgId id, void *hint) {
462         ORBInvokeRec *rec = get_invoke (id);
463         assert( rec );
464         rec->set_invoke_hint( hint );
465     };
466     void *get_invoke_hint(ORBMsgId id) {
467         ORBInvokeRec *rec = get_invoke (id);
468         assert( rec );
469         return rec->get_invoke_hint();
470     };
471
472     ORBMsgId invoke_async (Object_ptr, ORBRequest *, Principal_ptr,
473                            Boolean rply = TRUE, ORBCallback * = 0,
474                            ORBMsgId = 0);
475     ORBMsgId locate_async (Object_ptr, ORBCallback * = 0, ORBMsgId = 0);
476     ORBMsgId bind_async (const char *repoid, const ObjectTag &,
477                          Address *, ORBCallback * = 0, ORBMsgId = 0);
478
479     InvokeStatus invoke (Object_ptr &, ORBRequest *, Principal_ptr,
480                          Boolean rply = TRUE);
481     LocateStatus locate (Object_ptr &);
482     LocateStatus bind (const char *repoid, const ObjectTag &,
483                        Address *, Object_ptr &);
484     Object_ptr bind (const char *repoid, const char *addr = 0);
485     Object_ptr bind (const char *repoid, const ObjectTag &,
486                      const char *addr = 0);
487
488     void    cancel (ORBMsgId);
489     void    cancel (MsgId);
490     Boolean wait (ORBMsgId, Long tmout = -1);
491     // Long    wait (vector<MsgId> &, Long tmout = -1);
492
493     void answer_invoke (ORBMsgId, InvokeStatus, Object_ptr, ORBRequest *,
494                         GIOP::AddressingDisposition);
495     void answer_bind   (ORBMsgId, LocateStatus, Object_ptr);
496     void answer_locate (ORBMsgId, LocateStatus, Object_ptr,
497                         GIOP::AddressingDisposition);
498     void answer_shutdown (ObjectAdapter *);
499
500     InvokeStatus get_invoke_reply (ORBMsgId, Object_out, ORBRequest *&,
501                                    GIOP::AddressingDisposition &);
502     LocateStatus get_locate_reply (ORBMsgId, Object_out,
503                                    GIOP::AddressingDisposition &);
504     LocateStatus get_bind_reply   (ORBMsgId, Object_out);
505
506     RequestType request_type (ORBMsgId);
507     void redo_request (ORBMsgId);
508
509     ImplementationDef    *get_impl (Object_ptr);
510     InterfaceDef         *get_iface (Object_ptr);
511     Object               *get_component (Object_ptr);
512     Boolean               is_a (Object_ptr, const char *repoid);
513     Boolean               non_existent (Object_ptr);
514
515     ORBInvokeRec         *get_current_invoke_rec ();
516     // end-mico-extension
517
518     static ORB_ptr _duplicate (ORB_ptr o)
519     {
520         if (o)
521             o->_ref();
522         return o;
523     }
524     static ORB_ptr _nil ()
525     {
526         return 0;
527     }
528 };
529
530 typedef ObjVar<ORB> ORB_var;
531 typedef ObjOut<ORB> ORB_out;
532
533
534 // ORB initialization
535 typedef char *ORBid;
536 extern ORB_ptr ORB_init (int &argc, char **argv,
537                                        const char *id = "");
538 extern ORB_ptr ORB_instance (const char *id, Boolean create=TRUE);
539
540
541 class ObjectAdapter {
542 public:
543     typedef CORBA::ULong MsgId;
544
545     virtual const char *get_oaid () const = 0;
546     virtual Boolean has_object (Object_ptr) = 0;
547     virtual Boolean is_local () const = 0;
548
549 #ifdef USE_CSL2
550     virtual CORBA::Principal_ptr get_principal (CORBA::Object_ptr) = 0;
551 #endif // USE_CSL2
552
553     virtual Boolean invoke (ORBMsgId, Object_ptr, ORBRequest *,
554                             Principal_ptr, Boolean response_exp = TRUE) = 0;
555     virtual Boolean bind (ORBMsgId, const char *repoid,
556                           const ORB::ObjectTag &,
557                           Address *addr) = 0;
558     virtual Boolean locate (ORBMsgId, Object_ptr) = 0;
559     virtual Object_ptr skeleton (Object_ptr) = 0;
560     virtual void cancel (ORBMsgId) = 0;
561     virtual void shutdown (Boolean wait_for_completion) = 0;
562
563     virtual void answer_invoke (ORBMsgId, CORBA::Object_ptr,
564                                 CORBA::ORBRequest *,
565                                 CORBA::InvokeStatus) = 0;
566
567     virtual ~ObjectAdapter ();
568 };
569
570 }
571
572 #endif // __mico_orb_mico_h__