OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / include / mico / poa_impl.h
1 // -*- c++ -*-
2 /*
3  *  MICO --- an Open Source CORBA implementation
4  *  Copyright (C) 1998 Frank Pilhofer
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 __POA_IMPL_H__
25 #define __POA_IMPL_H__
26
27 #include <CORBA.h>
28 #include <mico/impl.h>
29
30 #ifdef USE_CSL2
31 #include <mico/security/odm_impl.h>
32 #endif /* USE_CSL2  */
33
34 /*
35  * Template definition for Policy objects must be outside
36  * a "struct" (namespace)
37  */
38
39 template<class T, class V>
40 class POA_Policy :
41   virtual public T,
42   virtual public MICO::Policy_impl
43 {
44 private:
45   V _value;
46
47 public:
48   POA_Policy (CORBA::PolicyType t, V val)
49       : MICO::Policy_impl(t), _value (val) {};
50   V value () { return _value; };
51
52   CORBA::Policy_ptr copy ()
53   {
54     return new POA_Policy<T,V> (policy_type(), _value);
55   }
56 };
57
58 /*
59  * ----------------------------------------------------------------------
60  *
61  * All in the MICOPOA Namespace
62  *
63  * ----------------------------------------------------------------------
64  */
65
66 namespace MICOPOA {
67
68 extern void _init ();
69
70 class POA_impl;
71 class POACurrent_impl;
72
73 /*
74  * Remove all POA options from the command line so that we can delay
75  * the actual POA initialization until a POA is actually needed.
76  */
77
78 class POAOptions {
79 public:
80   CORBA::Boolean parse (CORBA::ORB_ptr, int &, char *[]);
81   const char * operator[] (const char *);
82   
83 private:
84   std::map<std::string, std::string, std::less<std::string> > options;
85 };
86
87 extern MICO_EXPORT POAOptions poaopts;
88
89 /*
90  * ----------------------------------------------------------------------
91  *
92  * Policy Interfaces
93  *
94  * ----------------------------------------------------------------------
95  */
96
97 typedef POA_Policy<PortableServer::ThreadPolicy, PortableServer::ThreadPolicyValue> ThreadPolicy_impl;
98 typedef POA_Policy<PortableServer::LifespanPolicy, PortableServer::LifespanPolicyValue> LifespanPolicy_impl;
99 typedef POA_Policy<PortableServer::IdUniquenessPolicy, PortableServer::IdUniquenessPolicyValue> IdUniquenessPolicy_impl;
100 typedef POA_Policy<PortableServer::IdAssignmentPolicy, PortableServer::IdAssignmentPolicyValue> IdAssignmentPolicy_impl;
101 typedef POA_Policy<PortableServer::ImplicitActivationPolicy, PortableServer::ImplicitActivationPolicyValue> ImplicitActivationPolicy_impl;
102 typedef POA_Policy<PortableServer::ServantRetentionPolicy, PortableServer::ServantRetentionPolicyValue> ServantRetentionPolicy_impl;
103 typedef POA_Policy<PortableServer::RequestProcessingPolicy, PortableServer::RequestProcessingPolicyValue> RequestProcessingPolicy_impl;
104
105 /*
106  * ----------------------------------------------------------------------
107  *
108  * POAManager interface
109  *
110  * ----------------------------------------------------------------------
111  */
112
113 class POAManager_impl : virtual public PortableServer::POAManager
114 {
115 private:
116 //    typedef vector<PortableServer::POA_ptr> POAPtrVec;
117
118   State _state;
119
120   std::vector<PortableServer::POA_ptr> managed;
121
122   MICOMT::Mutex managed_lock;
123
124   void change_state (State,
125                      CORBA::Boolean = FALSE,
126                      CORBA::Boolean = FALSE);
127
128 public:
129   POAManager_impl ();
130   virtual ~POAManager_impl ();
131
132   void activate         ();
133   void hold_requests    (CORBA::Boolean);
134   void discard_requests (CORBA::Boolean);
135   void deactivate       (CORBA::Boolean, CORBA::Boolean);
136   State get_state       ();
137
138   // begin-mico-extension
139   void add_managed_poa  (PortableServer::POA_ptr);
140   void del_managed_poa  (PortableServer::POA_ptr);
141   // end-mico-extension
142 };
143
144 /*
145  * ----------------------------------------------------------------------
146  *
147  * Helper Classes
148  *
149  * ----------------------------------------------------------------------
150  */
151
152 /*
153  * Keeps an Object Id as a vector of octets so that we can compare
154  * them faster than using a CORBA sequence. We can then use this
155  * ObjectId as the key in a STL map, which should be implemented
156  * efficiently.
157  */
158
159 class ObjectId {
160 public:
161   ObjectId ();
162   ObjectId (const ObjectId &, bool = true);
163   ObjectId (const PortableServer::ObjectId &);
164   ObjectId (const char *, CORBA::ULong, bool = true);
165   ~ObjectId ();
166
167   ObjectId &     operator=  (const ObjectId &);
168   CORBA::Boolean operator== (const ObjectId &);
169   bool           operator<  (const ObjectId &) const;
170
171   // get reference
172   const char * get_data (CORBA::ULong &) const;
173   const PortableServer::ObjectId & get_id ();
174
175   // get copy
176   PortableServer::ObjectId * id ();
177
178 private:
179   bool own;
180   char * octets;
181   CORBA::ULong idlength;
182   PortableServer::ObjectId * oid;
183 };
184
185 /*
186  * Generating and examining Object References
187  *
188  * An Object Reference encapsulates the POA's identity (IP address,
189  * PID, fully qualified name) and a unique identifier for the object
190  * within that ORB
191  */
192
193 class POAObjectReference {
194 public:
195   POAObjectReference (POA_impl *,
196                       const PortableServer::ObjectId &,
197                       const char *,
198                       PortableServer::Servant = NULL);
199   POAObjectReference (POA_impl *, CORBA::Object_ptr);
200   POAObjectReference (const POAObjectReference &);
201   ~POAObjectReference ();
202
203   bool is_legal ();
204   
205   POAObjectReference & operator= (const CORBA::Object_ptr);
206   POAObjectReference & operator= (const POAObjectReference &);
207
208   // get reference
209   const ObjectId & get_oid ();
210   CORBA::Object_ptr get_ref ();
211   void set_ref (CORBA::Object_ptr);
212   const PortableServer::ObjectId & get_id ();
213
214   // get copy
215   CORBA::Object_ptr ref ();
216   PortableServer::ObjectId * id ();
217
218   // which POA do we belong to?
219   const char * poa_name ();
220   bool in_poa (const char *);
221   bool in_descendant_poa (const char *, const char *);
222   char * next_descendant_poa (const char *, const char *);
223   
224 private:
225   MICOMT::Mutex _ref_lock;
226
227   void make_ref ();
228   bool decompose_ref ();
229
230   /*
231    * Our POA
232    */
233   
234   POA_impl * poa;
235
236   /*
237    * Object Identity
238    */
239
240   bool iddirty;
241   std::string poaname;
242   std::string repoid;
243   MICOPOA::ObjectId oid;
244   CORBA::Object_ptr obj;
245   PortableServer::Servant servant;
246 };
247
248 /*
249  * Data structure for our Active Object Map.
250  *
251  * We maintain two maps so that we can search efficiently for Object Ids
252  * and Servants.
253  */
254
255 class ObjectMap {
256 public:
257   struct DeletionRecord {
258     CORBA::Long  cnt;
259     CORBA::Short signal;
260     MICOMT::CondVar condition;
261
262     DeletionRecord(MICOMT::Mutex* m)
263       : cnt(0), signal(0), condition(m)
264     {}
265   };
266
267   struct ObjectRecord {
268     ObjectRecord (POAObjectReference *,
269                   PortableServer::Servant = NULL);
270     ~ObjectRecord ();
271
272     CORBA::Boolean active;
273     CORBA::Long invoke_cnt;
274     DeletionRecord * delref;
275
276     POAObjectReference * por;
277     PortableServer::Servant serv;
278   };
279
280   typedef std::map<ObjectId, ObjectRecord *, std::less<ObjectId> > IdMap;
281   typedef IdMap::iterator iterator;
282
283 public:
284   ~ObjectMap ();
285
286   bool empty () const;
287   void clear ();
288
289   iterator begin ();
290   iterator end ();
291
292   ObjectRecord * pop ();
293
294   ObjectRecord * add (POAObjectReference *, PortableServer::Servant);
295   ObjectRecord * del (const ObjectId &);
296   ObjectRecord * del (const PortableServer::ObjectId &);
297
298   bool exists (const ObjectId &);
299   bool exists (const PortableServer::ObjectId &);
300   bool exists (const POAObjectReference &);
301   bool exists (PortableServer::Servant);
302
303   ObjectRecord * find (const ObjectId &);
304   ObjectRecord * find (const PortableServer::ObjectId &);
305   ObjectRecord * find (const POAObjectReference &);
306   ObjectRecord * find (POA_impl *, CORBA::Object_ptr);
307   ObjectRecord * find (PortableServer::Servant);
308
309 private:
310   typedef std::map<PortableServer::Servant, std::vector<ObjectRecord *>,
311     std::less<PortableServer::Servant> > SvMap;
312
313   IdMap objs;
314   SvMap servants;
315 };
316
317 /*
318  * Unique Id generator. Can an ULong as Id be too weak? Sure!
319  */
320
321 class UniqueIdGenerator {
322 public:
323   UniqueIdGenerator ();
324   UniqueIdGenerator (const char *);
325   ~UniqueIdGenerator ();
326
327   char * new_id ();
328
329   char * state ();
330   void state (const char *);
331
332 private:
333   int ulen, pfxlen;
334   char * uid;
335   char * prefix;
336 };
337
338 /*
339  * ----------------------------------------------------------------------
340  *
341  * The POA
342  *
343  * ----------------------------------------------------------------------
344  */
345
346 /*
347  * POA
348  */
349
350 class POA_impl : public PortableServer::POA,
351                  public CORBA::ObjectAdapter
352 {
353 public:
354   /*
355    * InvocationRecord to queue incoming invocations
356    */
357
358   class InvocationRecord;
359   typedef InvocationRecord *InvocationRecord_ptr;
360   typedef ObjVar<InvocationRecord> InvocationRecord_var;
361   
362   class InvocationRecord : public CORBA::ServerlessObject {
363   public:
364     InvocationRecord (CORBA::ORBMsgId,
365                       POAObjectReference *,
366                       CORBA::ORBRequest *,
367                       CORBA::Principal_ptr);
368     ~InvocationRecord ();
369
370     void exec (POA_impl *);
371
372     CORBA::ORBMsgId id ();
373     CORBA::ORBRequest * get_or ();
374     POAObjectReference * get_por ();
375     CORBA::ServerRequestBase_ptr make_req (POA_impl *,
376                                            PortableServer::Servant);
377     CORBA::ServerRequest_ptr make_dyn_req (POA_impl *);
378
379     static InvocationRecord_ptr _duplicate (InvocationRecord_ptr o)
380     {
381       if (o)
382         o->_ref();
383       return o;
384     }
385     static InvocationRecord_ptr _nil ()
386     {
387       return 0;
388     }
389
390   private:
391     CORBA::ORBMsgId orbid;
392     POAObjectReference * por;
393     CORBA::ORBRequest * req;
394     CORBA::Principal_ptr pr;
395     CORBA::ServerRequestBase_ptr svr;
396   };
397
398 private:
399   /*
400    * POA Policies
401    */
402
403   PortableServer::ThreadPolicy_var thread_policy;
404   PortableServer::LifespanPolicy_var lifespan_policy;
405   PortableServer::IdUniquenessPolicy_var id_uniqueness_policy;
406   PortableServer::IdAssignmentPolicy_var id_assignment_policy;
407   PortableServer::ImplicitActivationPolicy_var implicit_activation_policy;
408   PortableServer::ServantRetentionPolicy_var servant_retention_policy;
409   PortableServer::RequestProcessingPolicy_var request_processing_policy;
410
411   /*
412    * POA Attributes
413    */
414
415   std::string name;            // name relative to parent
416   std::string fqn;             // fully qualified name
417   std::string oaid;            // Unique Identifier
418   static std::string oaprefix; // Prefix for Unique Names
419
420   POA_impl * parent;
421   PortableServer::POAManager_ptr manager;
422   PortableServer::Servant default_servant;
423   PortableServer::ServantManager_var servant_manager;
424   static MICOMT::Mutex S_servant_manager_lock;
425   PortableServer::AdapterActivator_var adapter_activator;
426
427   /*
428    * POAMediator Stuff
429    */
430
431   static std::string impl_name;
432   static CORBA::IOR poamed_ior;
433   static CORBA::POAMediator_var poamed;
434   static CORBA::Boolean ever_been_active;
435
436   /*
437    * Data
438    */
439
440   int destructed;
441   MICOMT::RWLock destroy_lock_;
442   CORBA::ULong unique_id;
443   CORBA::ORB_ptr orb;
444
445   ObjectMap ActiveObjectMap;
446   MICOMT::Mutex ObjectActivationLock;
447
448   std::vector<InvocationRecord_ptr> InvocationQueue;
449   PortableServer::POAManager::State state;
450
451   /*
452    * serialization
453    */
454   MICOMT::Mutex serialize_invoke;
455   static MICOMT::Mutex S_global_invoke_lock;
456
457   /*
458    * Map of our children
459    */
460
461   typedef std::map<std::string, POA_impl *, std::less<std::string> > POAMap;
462
463   void register_child (const char *, POA_impl *);
464   void unregister_child (const char *);
465
466   POAMap children;
467
468   /*
469    * Map of all POAs
470    */
471
472   static void register_poa (const char *, POA_impl *);
473   static void unregister_poa (const char *);
474
475   static POAMap AllPOAs;
476   static UniqueIdGenerator poauid;
477   static UniqueIdGenerator idfactory;
478
479   /*
480    * Current data
481    */
482
483   static POACurrent_impl * current;
484
485 #ifdef USE_CSL2
486 #ifdef HAVE_SSL // ###ras
487   // ODM Manager
488   MICOSODM::Manager_impl* odm_manager_;
489   // ODM Factory
490   MICOSODM::Factory_impl* odm_factory_;
491 #endif // HAVE_SSL
492 #endif // USE_CSL2
493
494   /*
495    * private ops
496    */
497
498   void set_policies (const CORBA::PolicyList &);
499   void etherealize ();
500
501   POA_impl * _find_POA (const char *, CORBA::Boolean);
502
503   /*
504    * Constructor for my children
505    */
506
507   POA_impl (const char *,
508             PortableServer::POAManager_ptr,
509             const CORBA::PolicyList &,
510             POA_impl *,
511             CORBA::ORB_ptr);
512
513   
514   /*
515    * private object activation and deactivation
516    */
517   PortableServer::ObjectId * __activate_object (PortableServer::Servant);
518   void remove_object (const PortableServer::ObjectId & id);
519
520 public:
521   POA_impl (CORBA::ORB_ptr);
522   virtual ~POA_impl ();
523
524   /*
525    * POA creation and destruction
526    */
527
528   PortableServer::POA_ptr create_POA (const char *,
529                                       PortableServer::POAManager_ptr,
530                                       const CORBA::PolicyList &);
531   PortableServer::POA_ptr find_POA   (const char *,
532                                       CORBA::Boolean);
533   void destroy (CORBA::Boolean, CORBA::Boolean);
534
535   /*
536    * POA Attributes
537    */
538
539   char * the_name ();
540   PortableServer::POA_ptr the_parent ();
541   PortableServer::POAManager_ptr the_POAManager ();
542   PortableServer::AdapterActivator_ptr the_activator ();
543   PortableServer::POAList * the_children ();
544   void the_activator (PortableServer::AdapterActivator_ptr);
545   CORBA::OctetSeq* id();
546
547   /*
548    * Factories for Policy objects
549    */
550
551   PortableServer::ThreadPolicy_ptr create_thread_policy (PortableServer::ThreadPolicyValue);
552   PortableServer::LifespanPolicy_ptr create_lifespan_policy (PortableServer::LifespanPolicyValue);
553   PortableServer::IdUniquenessPolicy_ptr create_id_uniqueness_policy (PortableServer::IdUniquenessPolicyValue);
554   PortableServer::IdAssignmentPolicy_ptr create_id_assignment_policy (PortableServer::IdAssignmentPolicyValue);
555   PortableServer::ImplicitActivationPolicy_ptr create_implicit_activation_policy (PortableServer::ImplicitActivationPolicyValue);
556   PortableServer::ServantRetentionPolicy_ptr create_servant_retention_policy (PortableServer::ServantRetentionPolicyValue);
557   PortableServer::RequestProcessingPolicy_ptr create_request_processing_policy (PortableServer::RequestProcessingPolicyValue);
558
559   /*
560    * ServantManager registration
561    */
562
563   PortableServer::ServantManager_ptr get_servant_manager ();
564   void set_servant_manager (PortableServer::ServantManager_ptr);
565
566   /*
567    * Default servant registration
568    */
569
570   PortableServer::Servant get_servant ();
571   void set_servant (PortableServer::Servant);
572
573   /*
574    * object activation and deactivation
575    */
576
577   PortableServer::ObjectId * activate_object (PortableServer::Servant);
578   void activate_object_with_id (const PortableServer::ObjectId &,
579                                 PortableServer::Servant);
580
581   void deactivate_object (const PortableServer::ObjectId &);
582
583   /*
584    * Reference creation operations
585    */
586
587   CORBA::Object_ptr create_reference (const char *);
588   CORBA::Object_ptr create_reference_with_id (const PortableServer::ObjectId &,
589                                               const char *);
590   
591   /*
592    * Perform activation upon _this();
593    */
594
595   CORBA::Object_ptr activate_for_this (PortableServer::Servant);
596
597   /*
598    * Which IOR to use
599    */
600
601   CORBA::IOR * ior_template ();
602
603   /*
604    * Identity mapping operations
605    */
606
607   PortableServer::ObjectId * servant_to_id (PortableServer::Servant);
608   CORBA::Object_ptr servant_to_reference (PortableServer::Servant);
609   PortableServer::Servant reference_to_servant (CORBA::Object_ptr);
610   PortableServer::ObjectId * reference_to_id (CORBA::Object_ptr);
611   PortableServer::Servant id_to_servant (const PortableServer::ObjectId &);
612   CORBA::Object_ptr id_to_reference (const PortableServer::ObjectId &);
613
614   /*
615    * POA Manager callback
616    */
617
618   void poa_manager_callback (PortableServer::POAManager::State,
619                              CORBA::Boolean, CORBA::Boolean);
620
621   /*
622    * Service for colocated stubs
623    */
624
625   PortableServer::Servant preinvoke (CORBA::Object_ptr);
626   void postinvoke ();
627
628   /*
629    * ObjectAdapter Interface
630    */
631
632   const char *   get_oaid   () const;
633   CORBA::Boolean has_object (CORBA::Object_ptr);
634   CORBA::Boolean is_local   () const;
635
636 #ifdef USE_CSL2
637   CORBA::Principal_ptr get_principal (CORBA::Object_ptr);
638 #endif /* USE_CSL2  */
639
640   CORBA::Boolean invoke (CORBA::ORBMsgId,
641                          CORBA::Object_ptr,
642                          CORBA::ORBRequest *,
643                          CORBA::Principal_ptr,
644                          CORBA::Boolean = TRUE);
645   CORBA::Boolean bind   (CORBA::ORBMsgId, const char *,
646                          const CORBA::ORB::ObjectTag &,
647                          CORBA::Address *);
648   CORBA::Boolean locate (CORBA::ORBMsgId, CORBA::Object_ptr);
649   CORBA::Object_ptr skeleton (CORBA::Object_ptr);
650
651   void cancel   (CORBA::ORBMsgId);
652   void shutdown (CORBA::Boolean);
653
654   void answer_invoke (CORBA::ORBMsgId, CORBA::Object_ptr,
655                       CORBA::ORBRequest *,
656                       CORBA::InvokeStatus);
657
658 #ifdef USE_CSL2
659   // ODM stuff ###ras
660   virtual void register_ODM_factory(ObjectDomainMapping::Factory_ptr fry);
661   virtual ObjectDomainMapping::Manager_ptr get_ODM();
662 #endif /* USE_CSL2  */
663
664   /*
665    * Helper functions for OA interface
666    */
667
668   void builtin_is_a   (InvocationRecord_ptr, PortableServer::Servant);
669   void builtin_interface (InvocationRecord_ptr, PortableServer::Servant);
670   void builtin_component (InvocationRecord_ptr, PortableServer::Servant);
671   void builtin_non_existent (InvocationRecord_ptr, PortableServer::Servant);
672   bool builtin_invoke (InvocationRecord_ptr, PortableServer::Servant);
673
674   bool is_builtin     (InvocationRecord_ptr);
675   void local_invoke   (InvocationRecord_ptr);
676   void perform_invoke (InvocationRecord_ptr);
677 };
678
679 /*
680  * ----------------------------------------------------------------------
681  *
682  * Current Interface
683  *
684  * ----------------------------------------------------------------------
685  */
686
687 class POACurrent_impl : public PortableServer::Current
688 {
689 private:
690   CORBA::ORB_ptr orb;
691
692   struct CurrentState {
693     CurrentState ();
694     CurrentState (PortableServer::POA_ptr,
695                   POAObjectReference *,
696                   PortableServer::Servant);
697     CurrentState (const CurrentState &);
698     ~CurrentState ();
699     PortableServer::POA_ptr poa;
700     POAObjectReference * por;
701     PortableServer::Servant serv;
702   };
703
704 public:
705   typedef std::vector<CurrentState> CurrentStateStack;
706
707 private:
708 #ifndef HAVE_THREADS
709   CurrentStateStack* state_stack_;
710 #else // HAVE_THREADS
711   MICOMT::Thread::ThreadKey current_key_;
712 #endif // HAVE_THREADS
713
714 public:
715   POACurrent_impl (CORBA::ORB_ptr);
716   ~POACurrent_impl ();
717
718   PortableServer::POA_ptr get_POA ();
719   PortableServer::ObjectId * get_object_id ();
720   CORBA::Object_ptr get_reference();
721   PortableServer::Servant get_servant();
722   // begin-mico-extension
723   CORBA::Boolean iscurrent ();
724   POAObjectReference * get_por ();
725
726   void set (PortableServer::POA_ptr, POAObjectReference *,
727             PortableServer::Servant);
728   void unset ();
729
730 #ifndef HAVE_THREADS
731   //
732   // In single thread mode, only one POA can be active at any given time
733   // a stack of POA/Currents is OK here, because nested invocation are not possible 
734   //
735   CurrentStateStack* get_current()
736   { return state_stack_; }
737
738   void set_current(CurrentStateStack* stack)
739   { state_stack_ = stack; }
740
741 #else // HAVE_THREADS
742   //
743   // In multi threaded mode, more than one POA can be active (doing invocations),
744   // but there can be only one active Object per thread 
745   // 
746   CurrentStateStack* get_current()
747   { return static_cast<CurrentStateStack *>(MICOMT::Thread::get_specific(current_key_)); }
748
749   void set_current(CurrentStateStack* current)
750   { MICOMT::Thread::set_specific(current_key_, current); }
751
752 #endif // HAVE_THREADS
753   // end-mico-extension
754 };
755
756 }  // namespace MICOPOA
757
758 #endif