Update of /cvsroot/micomt/mico/demo/services/naming In directory usw-pr-cvs1:/tmp/cvs-serv17698/demo/services/naming Modified Files: Makefile Makefile.win32 account.bat account.idl account_test client.cc server.cc Log Message: - merged with MICO 2.3.6 Index: Makefile =================================================================== RCS file: /cvsroot/micomt/mico/demo/services/naming/Makefile,v retrieving revision 1.1.1.2 retrieving revision 1.2 diff -C2 -r1.1.1.2 -r1.2 *** Makefile 2001/01/06 23:17:54 1.1.1.2 --- Makefile 2001/10/26 19:59:57 1.2 *************** *** 42,46 **** account.h account.cc : account.idl $(IDLGEN) ! $(IDL) --no-poa --boa account.idl clean: --- 42,46 ---- account.h account.cc : account.idl $(IDLGEN) ! $(IDL) account.idl clean: Index: Makefile.win32 =================================================================== RCS file: /cvsroot/micomt/mico/demo/services/naming/Makefile.win32,v retrieving revision 1.1.1.2 retrieving revision 1.2 diff -C2 -r1.1.1.2 -r1.2 *** Makefile.win32 2001/01/06 23:17:54 1.1.1.2 --- Makefile.win32 2001/10/26 19:59:57 1.2 *************** *** 23,27 **** all: client.exe server.exe ! DIR_PREFIX=..\ !include ..\..\MakeVars.win32 CXXFLAGS = $(COS_CXXFLAGS) -I..\..\include $(CXXFLAGS) --- 23,27 ---- all: client.exe server.exe ! DIR_PREFIX=..\..\ !include ..\..\MakeVars.win32 CXXFLAGS = $(COS_CXXFLAGS) -I..\..\include $(CXXFLAGS) *************** *** 41,45 **** account.h account.cc : account.idl $(IDLGEN) ! $(IDL) --no-poa --boa account.idl clean: --- 41,45 ---- account.h account.cc : account.idl $(IDLGEN) ! $(IDL) account.idl clean: Index: account.bat =================================================================== RCS file: /cvsroot/micomt/mico/demo/services/naming/account.bat,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -r1.1.1.1 -r1.2 *** account.bat 1999/11/03 23:20:52 1.1.1.1 --- account.bat 2001/10/26 19:59:57 1.2 *************** *** 1,27 **** ! ! set ADDR=inet:127.0.0.1:12456 ! SET MICORC=NUL ! set RC=-ORBImplRepoAddr %ADDR% -ORBNamingAddr %ADDR% ! set path=..\..\win32-bin\;%path% ! REM run BOA daemon ! echo "starting BOA daemon ..." ! start micod -ORBIIOPAddr %ADDR% ! ! ! REM register server ! echo "register name service ..." ! imr create NameService poa nsd.exe "IDL:omg.org/CosNaming/NamingContext:1.0#NameService" %RC% ! ! echo "register account service ..." ! imr create Account shared "server %RC%" IDL:Account:1.0 %RC% ! ! ! REM activate the account server to make itself register with the ! REM name service so the client can find it ... ! echo "activating Account implementation" ! imr activate Account %RC% ! pause ! REM run client ! echo "and run client ..." ! ./client %RC% ! --- 1,12 ---- ! ! set MICORC=NUL ! set PATH=..\..\..\win32-bin;%PATH% ! ! echo "starting Naming Service daemon ..." ! start ..\..\..\win32-bin\nsd -ORBIIOPAddr inet:127.0.0.1:12456 ! ! echo "starting Bank server ..." ! start .\server -ORBInitRef NameService=corbaloc::127.0.0.1:12456/NameService ! ! echo "running client ..." ! .\client -ORBInitRef NameService=corbaloc::127.0.0.1:12456/NameService Index: account.idl =================================================================== RCS file: /cvsroot/micomt/mico/demo/services/naming/account.idl,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -r1.1.1.1 -r1.2 *** account.idl 1999/11/03 23:20:52 1.1.1.1 --- account.idl 2001/10/26 19:59:57 1.2 *************** *** 1,2 **** --- 1,3 ---- + // -*- c++ -*- interface Account { *************** *** 4,6 **** --- 5,11 ---- void withdraw( in unsigned long amount ); long balance(); + }; + + interface Bank { + Account create (); }; Index: account_test =================================================================== RCS file: /cvsroot/micomt/mico/demo/services/naming/account_test,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -r1.1.1.1 -r1.2 *** account_test 1999/11/03 23:20:52 1.1.1.1 --- account_test 2001/10/26 19:59:57 1.2 *************** *** 4,40 **** export PATH - ADDR=inet:`uname -n`:12456 - RC="-ORBImplRepoAddr $ADDR -ORBNamingAddr $ADDR" - rm -f NameService-root.inf ! # run BOA daemon ! echo "starting BOA daemon ..." ! micod -ORBIIOPAddr $ADDR --forward & ! micod_pid=$! ! ! trap "kill $micod_pid" 0 ! ! sleep 1 ! ! # register server ! echo "register name service ..." ! imr create NameService poa `which nsd` \ ! IDL:omg.org/CosNaming/NamingContext:1.0#NameService $RC ! ! echo "register account service ..." ! imr create Account shared "`pwd`/server $RC" \ ! IDL:Account:1.0 $RC ! ! sleep 1 ! # activate the account server to make itself register with the ! # name service so the client can find it ... ! echo "activating Account implementation" ! imr activate Account $RC ! ! sleep 1 ! # nsadmin $RC # ls myAccount ! ! # run client ! echo "and run client ..." ! ./client $RC --- 4,24 ---- export PATH rm -f NameService-root.inf + + echo "starting Naming Service daemon ..." + nsd -ORBIIOPAddr inet:`uname -n`:12456 & + nsd_pid=$! + + trap "kill $nsd_pid" 0 + sleep 3 + + + echo "starting Bank server ..." + ./server -ORBInitRef NameService=corbaloc::`uname -n`:12456/NameService & + server_pid=$! + + trap "kill $nsd_pid $server_pid" 0 + sleep 3 ! echo "running client ..." ! ./client -ORBInitRef NameService=corbaloc::`uname -n`:12456/NameService Index: client.cc =================================================================== RCS file: /cvsroot/micomt/mico/demo/services/naming/client.cc,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -r1.1.1.1 -r1.2 *** client.cc 1999/11/03 23:20:52 1.1.1.1 --- client.cc 2001/10/26 19:59:57 1.2 *************** *** 1,51 **** ! #include <CORBA-SMALL.h> ! #include "account.h" #include <mico/CosNaming.h> ! int main( int argc, char *argv[] ) { // ORB initialization ! CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "mico-local-orb" ); ! CORBA::BOA_var boa = orb->BOA_init (argc, argv, "mico-local-boa"); ! CORBA::Object_var nobj = ! orb->resolve_initial_references ("NameService"); ! assert (! CORBA::is_nil (nobj)); ! CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow (nobj); CosNaming::Name name; name.length (1); ! name[0].id = CORBA::string_dup ("myAccount"); name[0].kind = CORBA::string_dup (""); CORBA::Object_var obj; #ifdef HAVE_EXCEPTIONS try { obj = nc->resolve (name); } ! catch (CosNaming::NamingContext::NotFound_catch &exc) { ! cerr << "NotFound exception" << endl; } ! catch (CosNaming::NamingContext::CannotProceed_catch &exc) { ! cerr << "CannotProceed exception" << endl; } ! catch (CosNaming::NamingContext::InvalidName_catch &exc) { ! cerr << "InvalidName exception" << endl; } #else obj = nc->resolve (name); #endif - Account_var client = Account::_narrow( obj ); ! client->deposit( 100 ); ! client->deposit( 100 ); ! client->deposit( 100 ); ! client->deposit( 100 ); ! client->deposit( 100 ); ! client->withdraw( 240 ); ! client->withdraw( 10 ); ! cout << "Balance is " << client->balance() << endl; ! return 0; } --- 1,99 ---- ! #include <CORBA.h> #include <mico/CosNaming.h> + #include "account.h" ! int ! main (int argc, char *argv[]) { // ORB initialization ! CORBA::ORB_var orb = CORBA::ORB_init (argc, argv); ! /* ! * In demo/poa/account-1, the object reference comes from a file via ! * the "file://" object URL. Here, we retrieve the reference from the ! * Naming Service ! */ ! /* ! * Acquire a reference to the Naming Service ! */ ! ! CORBA::Object_var nsobj = ! orb->resolve_initial_references ("NameService"); ! ! CosNaming::NamingContext_var nc = ! CosNaming::NamingContext::_narrow (nsobj); ! ! if (CORBA::is_nil (nc)) { ! cerr << "oops, I cannot access the Naming Service!" << endl; ! exit (1); ! } ! ! /* ! * Construct Naming Service name for our Bank ! */ ! CosNaming::Name name; name.length (1); ! name[0].id = CORBA::string_dup ("Bank"); name[0].kind = CORBA::string_dup (""); + /* + * try to find that node in the Naming Service tree + */ + CORBA::Object_var obj; + cout << "Looking up Bank ... " << flush; + #ifdef HAVE_EXCEPTIONS try { obj = nc->resolve (name); } ! catch (CosNaming::NamingContext::NotFound &exc) { ! cout << "NotFound exception." << endl; ! exit (1); } ! catch (CosNaming::NamingContext::CannotProceed &exc) { ! cout << "CannotProceed exception." << endl; ! exit (1); } ! catch (CosNaming::NamingContext::InvalidName &exc) { ! cout << "InvalidName exception." << endl; ! exit (1); } #else obj = nc->resolve (name); #endif ! cout << "done." << endl; ! ! /* ! * The Naming Service returns a generic reference as a CORBA::Object ! * We need to narrow this to the desired type ! */ ! ! Bank_var bank = Bank::_narrow (obj); ! ! /* ! * Open an account ! */ ! ! Account_var account = bank->create (); ! ! if (CORBA::is_nil (account)) { ! printf ("oops: account is nil\n"); ! exit (1); ! } ! ! /* ! * Deposit and withdraw some money ! */ ! ! account->deposit (700); ! account->withdraw (450); ! ! printf ("Balance is %ld.\n", account->balance ()); ! return 0; } Index: server.cc =================================================================== RCS file: /cvsroot/micomt/mico/demo/services/naming/server.cc,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -r1.1.1.1 -r1.2 *** server.cc 1999/11/03 23:20:52 1.1.1.1 --- server.cc 2001/10/26 19:59:57 1.2 *************** *** 1,121 **** ! #define MICO_CONF_IMR ! #include <CORBA-SMALL.h> ! #include <iostream.h> ! #include <fstream.h> ! #include <unistd.h> ! #include "account.h" #include <mico/CosNaming.h> /* ! * Account implementation */ ! class Account_impl : virtual public Account_skel { ! CORBA::Long _current_balance; ! CORBA::Long _calls; public: ! Account_impl () ! { ! _calls = 0; ! _current_balance = 0; ! } ! ! Account_impl (CORBA::Object_ptr obj) ! : Account_skel (obj) ! { ! // cout << "restoring id " << obj->_ident() << endl; ! _calls = 0; ! ifstream inp (obj->_ident()); ! assert (inp); ! inp >> _current_balance; ! } ! ! CORBA::Boolean _save_object () ! { ! // cout << "saving id " << _ident() << endl; ! ofstream out (_ident()); ! assert (out); ! out << _current_balance; ! return TRUE; ! } ! ! void check_exit () ! { ! if (++_calls > 5) { ! CORBA::ORB_var orb = _orb(); ! CORBA::BOA_var boa = _boa(); ! /* ! * beware: while calling deactivate_impl() we can get ! * recursive invocations to the Account methods ... ! */ ! boa->deactivate_impl (CORBA::ImplementationDef::_nil()); ! orb->shutdown (TRUE); ! } ! } ! ! void deposit( CORBA::ULong amount ) ! { ! _current_balance += amount; ! check_exit (); ! } ! ! void withdraw( CORBA::ULong amount ) ! { ! _current_balance -= amount; ! check_exit (); ! } ! ! CORBA::Long balance() ! { ! check_exit (); ! return _current_balance; ! } }; /* ! * Account object restorer */ ! class AccountLoader : public CORBA::BOAObjectRestorer { public: ! CORBA::Boolean restore (CORBA::Object_ptr obj) ! { ! if (!strcmp (obj->_repoid(), "IDL:Account:1.0")) { ! (void)new Account_impl (obj); ! return TRUE; ! } ! // cout << "cannot restore " << obj->_repoid() << " objects" << endl; ! return FALSE; ! } }; ! int main( int argc, char *argv[] ) { ! AccountLoader loader; ! CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "mico-local-orb" ); ! CORBA::BOA_var boa = orb->BOA_init (argc, argv, "mico-local-boa"); ! if (!boa->restoring()) { ! cerr << "creating ..." << endl; ! Account_ptr acc = new Account_impl (); ! ! CORBA::Object_var nsobj = ! orb->resolve_initial_references ("NameService"); ! assert (! CORBA::is_nil (nsobj)); ! CosNaming::NamingContext_var nc = ! CosNaming::NamingContext::_narrow (nsobj); ! CosNaming::Name name; ! name.length (1); ! name[0].id = CORBA::string_dup ("myAccount"); ! name[0].kind = CORBA::string_dup (""); ! cerr << "binding name ..." << endl; ! nc->bind (name, acc); ! } ! boa->impl_is_ready (CORBA::ImplementationDef::_nil()); ! orb->run (); return 0; --- 1,170 ---- ! #include <CORBA.h> #include <mico/CosNaming.h> + #include "account.h" /* ! * Implementation of the Account */ ! class Account_impl : virtual public POA_Account ! { public: ! Account_impl (); ! ! void deposit (CORBA::ULong); ! void withdraw (CORBA::ULong); ! CORBA::Long balance (); ! ! private: ! CORBA::Long bal; }; + Account_impl::Account_impl () + { + bal = 0; + } + + void + Account_impl::deposit (CORBA::ULong amount) + { + bal += amount; + } + + void + Account_impl::withdraw (CORBA::ULong amount) + { + bal -= amount; + } + + CORBA::Long + Account_impl::balance () + { + return bal; + } + /* ! * Implementation of the Bank */ ! class Bank_impl : virtual public POA_Bank ! { public: ! Account_ptr create (); }; + Account_ptr + Bank_impl::create () + { + /* + * Create a new account (which is never deleted) + */ + + Account_impl * ai = new Account_impl; + + /* + * Obtain a reference using _this. This implicitely activates the + * account servant (the RootPOA, which is the object's _default_POA, + * has the IMPLICIT_ACTIVATION policy) + */ + + Account_ptr aref = ai->_this (); + assert (!CORBA::is_nil (aref)); + + /* + * Return the reference + */ + + return aref; + } + + /* + * Main + */ ! int ! main (int argc, char *argv[]) { ! /* ! * Initialize ORB, get the Root POA, and register Account object as usual ! */ ! ! CORBA::ORB_var orb = CORBA::ORB_init (argc, argv); ! /* ! * Obtain a reference to the RootPOA and its Manager ! */ ! ! CORBA::Object_var poaobj = orb->resolve_initial_references ("RootPOA"); ! PortableServer::POA_var poa = PortableServer::POA::_narrow (poaobj); ! PortableServer::POAManager_var mgr = poa->the_POAManager(); ! ! /* ! * Create a Bank ! */ ! ! Bank_impl * micocash = new Bank_impl; ! ! /* ! * Activate the Bank ! */ ! ! PortableServer::ObjectId_var oid = poa->activate_object (micocash); ! CORBA::Object_var ref = poa->id_to_reference (oid.in()); ! ! /* ! * In demo/poa/account-1, this object reference (ref) is written to a ! * file. Here, we store the reference in the Naming Service ! */ ! /* ! * Acquire a reference to the Naming Service ! */ ! ! CORBA::Object_var nsobj = ! orb->resolve_initial_references ("NameService"); ! ! CosNaming::NamingContext_var nc = ! CosNaming::NamingContext::_narrow (nsobj); ! ! if (CORBA::is_nil (nc)) { ! cerr << "oops, I cannot access the Naming Service!" << endl; ! exit (1); ! } ! /* ! * Construct Naming Service name for our Bank ! */ ! ! CosNaming::Name name; ! name.length (1); ! name[0].id = CORBA::string_dup ("Bank"); ! name[0].kind = CORBA::string_dup (""); ! ! /* ! * Store a reference to our Bank in the Naming Service. We use 'rebind' ! * here instead of 'bind', because rebind does not complain if the desired ! * name "Bank" is already registered, but silently overwrites it (the ! * existing reference is probably from an old incarnation of this server). ! */ ! ! cout << "Binding Bank in the Naming Service ... " << flush; ! nc->rebind (name, ref); ! cout << "done." << endl; ! ! /* ! * Activate the POA and start serving requests ! */ ! ! printf ("Running.\n"); ! ! mgr->activate (); ! orb->run(); ! ! /* ! * Shutdown (never reached) ! */ ! poa->destroy (TRUE, TRUE); ! delete micocash; return 0; |