Документ взят из кэша поисковой машины. Адрес оригинального документа : http://rtm-cs.sinp.msu.ru/manual/mico/doc/node45.html
Дата изменения: Mon Jun 7 21:54:58 1999
Дата индексирования: Mon Oct 1 21:22:11 2012
Кодировка:
Example next up previous
Next: Using a Servant Manager Up: POA Previous: Policies

Example

As an example, let's write a simple POA-based server. You can find the full code in the demo/poa/hello-1 directory in the MICO distribution. Imagine a simple IDL description in the file ``hello.idl'':

  interface HelloWorld {
    void hello ();
  };

The first step is to invoke the IDL to C++ compiler in a way to produce skeleton classes that use the POA:

  idl --poa --no-boa hello.idl

The first option, -poa, turns on code generation for POA-based skeletons. The second option, -no-boa optionally turns off code generation for the old BOA-based skeletons. Next, we rewrite the server.

 1: // file server.cc
 2:
 3: #include "hello.h"
 4:
 5: class HelloWorld_impl : virtual public POA_HelloWorld
 6: {
 7:   public:
 8:     void hello() { printf ("Hello World!\n"); };
 9: };
10: 
11: 
12: int main( int argc, char *argv[] )
13: {
14:   CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, "mico-local-orb");
15:   CORBA::Object_var poaobj = orb->resolve_initial_references ("RootPOA");
16:   PortableServer::POA_var poa = PortableServer::POA::_narrow (poaobj);
17:   PortableServer::POAManager_var mgr = poa->the_POAManager();
18:
19:   HelloWorld_impl * servant = new HelloWorld_impl;
20:
21:   PortableServer::ObjectId_var oid = poa->activate_object (servant);
22:
23:   mgr->activate ();
24:   orb->run();
25:
26:   poa->destroy (TRUE, TRUE);
27:   delete servant;
28:   return 0;
29: }

The object implementation does not change much with respect to a BOA-based one, the only difference is that HelloWorld_impl does not inherit from the BOA-based skeleton HelloWorld_skel any more, but from the POA-based skeleton POA_HelloWorld.

In main(), we first initialize the ORB, then we obtain a reference to the Root POA (lines 15-16) and to its POA Manager (line 17).

Then, we create an instance of our server object. In line 21, the servant is activated. Since the Root POA has the SYSTEM_ID policy, a unique Object Id is generated automatically and returned. At this point, clients can use the MICO binder to connect to the HelloWorld object.

However, client invocations upon the HelloWorld object are not yet processed. The Root POA's POA Manager is created in the holding state, so in line 23, we transition the POA Manager, and therefore the Root POA, to the active state. We then enter the ORB's event loop in 24.

In this example, run() never returns, because we don't provide a means to shut down the ORB. If that ever happened, lines 26-27 would first destroy the Root POA. Since that deactivates our active HelloWorld object, we can then safely delete the servant.

Since the Root POA has the IMPLICIT_ACTIVATION policy, we can also use several other methods to activate the servant instead of activate_object(). We could, for example, use servant_to_reference(), which first implicitly activates the inactive servant and then returns an object reference pointing to the servant. Or, we could invoke the servant's inherited _this() method, which also implicitly activates the servant and returns an object reference.


next up previous
Next: Using a Servant Manager Up: POA Previous: Policies

Arno Puder
Mon Jun 7 10:53:40 PDT 1999