Документ взят из кэша поисковой машины. Адрес оригинального документа : http://rtm-cs.sinp.msu.ru/manual/mico/doc/node16.html
Дата изменения: Mon Jun 7 21:54:57 1999
Дата индексирования: Mon Oct 1 21:21:24 2012
Кодировка:
Stringified Object References next up previous
Next: Naming Service Up: Separating client and server Previous: Separating client and server

Stringified Object References

 

The example in section 3.3.2 already used the ORB methods object_to_string() and string_to_object() to make a stringified representation of an object reference and to turn back this string into an object, respectively.

When separating client and server you have to find a way to transmit the stringified object reference from the server to the client. If client and server run on machines that share a single file system you can make the server write the string into a file which is read by the client. Here is how to do it:

 1: // file account_server.cc
 2:
 3: #include <iostream.h>
 4: #include <fstream.h>
 5: #include "account.h"
 6: 
 7: class Account_impl : virtual public Account_skel
 8: {
 9:   // unchanged, see section "MICO Application"
10:   // ...
11: };
12: 
13: 
14: int main( int argc, char *argv[] )
15: {
16:   // ORB initialization
17:   CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "mico-local-orb" );
18:   CORBA::BOA_var boa = orb->BOA_init( argc, argv, "mico-local-boa" );
19: 
20:   Account_impl* server = new Account_impl;
21:   CORBA::String_var ref = orb->object_to_string( server );
22:   ofstream out ("/tmp/account.objid");
23:   out << ref << endl;
24:   out.close ();
25:
26:   boa->impl_is_ready( CORBA::ImplementationDef::_nil() );
27:   orb->run ();
28:   CORBA::release( server );
29:   return 0;
30: }

Account_impl, the implementation of the account object in lines 7-11 is the same as in section 3.3.2. The main() function performs ORB and BOAgif initialization in lines 16-18, which will evaluate and remove CORBA specific command line options from argv, see section 4.1.1 for details. In line 20 an account object is created, lines 21-24 obtain a stringified object reference for this object and write it to a file called account.objid.

In line 26 the impl_is_ready() method of the BOA is called to activate the objects implemented by the server. The ORB method run(), which is invoked in line 27 will enter a loop to process incoming invocationsgif. Just before returning from main(), CORBA::release() is used in line 28 to destroy the account server object.

 1: // file account_client.cc
 2:
 3: #include <iostream.h>
 4: #include <fstream.h>
 5: #include "account.h"
 6: 
 7: int main( int argc, char *argv[] )
 8: {
 9:   // ORB initialization
10:   CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "mico-local-orb" );
11:   CORBA::BOA_var boa = orb->BOA_init( argc, argv, "mico-local-boa" );
12:
13:   ifstream in ("/tmp/account.objid");
14:   char ref[1000];
15:   in >> ref;
16:   in.close ();
17:
18:   CORBA::Object_var obj = orb->string_to_object (ref);
19:   Account_var client = Account::_narrow( obj );
20: 
21:   client->deposit( 700 );
22:   client->withdraw( 250 );
23:   cout << "Balance is " << client->balance() << endl;
24:
25:   return 0;
26: }

After ORB and BOA initialization the client's main() function reads the stringified object reference in lines 13-16 and turns it back into an account object stub in lines 18-19. After making some method invocations in lines 21-23 client will be destroyed automatically because we used and Account_var smart pointer.

Compile the client and server programs like this:

  mico-c++ -I. -c account_server.cc -o account_server.o
  mico-c++ -I. -c account_client.cc -o account_client.o
  mico-c++ -I. -c account.cc -o account.o
  mico-ld -o server account_server.o account.o -lmico2.2.7
  mico-ld -o client account_client.o account.o -lmico2.2.7

First run server and then client in a different shell. The output from client will look like this:

  Balance is 450

Note that running the client several times without restarting the server inbetween will increase the balance the client prints out by 450 each time! You should also note that client and server do not necessarily have to run on the same machine. The stringified object reference, which is written to a file called /tmp/account.objid, contains the IP address and port number of the server's address. This way the client can locate the server over the network. The same example would also work in a heterogeneous environment. In that case you would have to compile two versions of account.o, one for each hardware architecture. But the conversion of the parameters due to different data representations is taken care of by MICO.


next up previous
Next: Naming Service Up: Separating client and server Previous: Separating client and server

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