Show TOC Start of Content Area

Procedure documentation Developing the Client  Locate the document in its SAP Library structure

Use

Develop the client that will invoke methods of the UtilCallback bean remotely.

The basic tasks that you must accomplish are:

·        Initialize the ORB

·        Obtain an object reference using the Naming System

·        Resolve the bean name and narrow the object reference to the bean’s home interface

·        Create an instance of the bean

·        Call the bean’s business methods

Before you call the isPrime method of the bean, you must first activate the callback object on the client-side. This is done according to the CORBA POA mechanism.

Procedure

...

       1.      Define the following include statements:

#include <OB/CORBA.h>

#include <iostream.h>

#include <CosNaming.h>

#include <utilcallback.h>

#include <utilcallbackHome.h>

#include <notificator_impl.h>

       2.      Declare the run() function:

int run(CORBA::ORB_ptr);

       3.      Implement the main() method, in which you initialize and destroy the ORB:

int main(int argc, char* argv[]) {

   int status = EXIT_SUCCESS;

   CORBA::ORB_var orb;

 

   try{

      orb = CORBA::ORB_init(argc, argv);

      status = run(orb);

   }catch(const CORBA::Exception&){

      status = EXIT_FAILURE;

   }

   if(!CORBA::is_nil(orb)){

      try {

         orb->destroy();

      }catch(const CORBA::Exception&){

         status = EXIT_FAILURE;

      }

   }

   return status;

}

       4.      Start implementing the run function. Obtain the naming context that you will use to obtain a reference to the UtilCallback bean from:

cout << "Getting name service reference....." << endl;

   CORBA::Object_var obj = orb -> resolve_initial_references("NameService");

   cout << obj << endl;

 

   cout << "Narrowing reference to naming context......." << endl;

   CosNaming::NamingContext_var context;

   try{

      context = CosNaming::NamingContext::_narrow(obj);

   }catch(const CORBA::Exception& ex){

      cout << "EXCEPTION CAUGHT!" << endl;

      cout << ex._to_string()<< endl;

      return 0;

   }

   cout << "Narrow successful!" <<endl;

       5.      Resolve the UtilCallback’s name using the naming context:

   CosNaming::Name name;

   name.length(2);

   name[0].id = CORBA::string_dup("CallbackApplication");

   name[0].kind = CORBA::string_dup("");

   name[1].id = CORBA::string_dup("UtilCallbackBean");

   name[1].kind = CORBA::string_dup("");

 

   cout << "Resolving the name....." << endl;

   CORBA::Object_var object;

   try{

      object = context -> resolve(name);

   }catch(const CORBA::Exception& ex){

      cout << ex._to_string() << endl;

      return 0;

   }

   cout << "Resolving the name successful!" << endl;

       6.      Narrow the object reference to the UtilCallback bean’s home interface:

   CORBA::String_var s = orb -> object_to_string(object);

   cout << "Object to string gets: " << s << endl;

  

   cout << "Narrowing to Callback object....." << endl;

   ::examples::iiop::UtilCallbackHome_var callbackHome = examples::iiop::UtilCallbackHome::_narrow(object);

  

 

   if(CORBA::is_nil(callbackHome)) {

      cout << "Can't narrow the object to Callback!" << endl;

       7.      Call the reverseString and squareRoot methods of the bean:

examples::iiop::UtilCallback_var callback = callbackHome -> create();

 

      const CORBA::WChar* wch = (wchar_t*)L"Hello customer!";

      CORBA::WStringValue* param = new CORBA::WStringValue(wch); 

     

      cout << endl << "----------FIRST CALL reverseString(String s)-----------!" << endl;

      CORBA::WStringValue* msg = callback -> reverseString(param);

      const CORBA::WChar*  w = msg->_boxed_in();

      while(*w != 0)

         cout << (char)(*w++);

     

      cout << endl << endl << "----------SECOND CALL reverseString(String s)-----------!" << endl;

      CORBA::WStringValue* msg_2 = callback -> reverseString(msg);

      const CORBA::WChar*  w_2 = msg_2->_boxed_in(); 

      while(*w_2 != 0)

         cout << (char)(*w_2++);

 

        

      cout << endl << endl << "---------- CALL squareRoot(double d)-----------!" << endl;

      double d = 5.4;

      double result = callback -> squareRoot(d);

      cout << "The square root of " << d << " is " << result << endl;

       8.      The following code is aimed at activating the remote callback obejct and calling the isPrime method of the UtilCallback bean afterwards:

                            a.      Obtain the rootPOA:

cout << endl << endl<< "Resolve Initial Reference POA......." << endl;

CORBA::Object_var poaObj = orb -> resolve_initial_references("RootPOA");

     

cout << "Narrowing to POA object............" << endl;

PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(poaObj);

 

                            b.      Get the POAManager from the rootPOA:

cout << "Getting the POA Manager" << endl;

PortableServer::POAManager_var manager = rootPOA -> the_POAManager();

                            c.      Instantiate the callback object and get a reference to it:

cout << "Instantiating Notificator Impl......" << endl;

examples::iiop::Notificator_impl* notificator_impl = new examples::iiop::Notificator_impl(rootPOA);

     

cout << "Obtaining Notificator reference....... " << endl;

examples::iiop::Notificator_var notificator_var = notificator_impl -> _this();

                            d.      Activate the POAManager:

cout << "Activating the manager......." << endl;

manager->activate();

                            e.      Call the isPrime method of the UtilCallback bean:

cout << endl << "-----------Calling EJB isPrime(long l, Notificator callback)-----------------" << endl;

     

__int64 l = 2222222222222241;   

printf("Testing %I64d .....\n", l);

     

bool prime = callback -> isPrime(l, notificator_var);

     

printf("\n%I64d is prime: %s\n", l, prime ? "true" : "false");

Result

The full source code of the Client.cpp must be the following:

#include <OB/CORBA.h>

#include <iostream.h>

#include <CosNaming.h>

#include <utilcallback.h>

#include <utilcallbackHome.h>

#include <notificator_impl.h>

 

int run(CORBA::ORB_ptr);

 

int main(int argc, char* argv[]) {

   int status = EXIT_SUCCESS;

   CORBA::ORB_var orb;

 

   try{

      orb = CORBA::ORB_init(argc, argv);

      status = run(orb);

   }catch(const CORBA::Exception&){

      status = EXIT_FAILURE;

   }

 

   if(!CORBA::is_nil(orb)){

      try {

         orb->destroy();

      }catch(const CORBA::Exception&){

         status = EXIT_FAILURE;

      }

   }

 

   return status;

}

 

int run(CORBA::ORB_ptr orb) {

 

   cout << "Getting name service reference....." << endl;

   CORBA::Object_var obj = orb -> resolve_initial_references("NameService");

   cout << obj << endl;

 

   cout << "Narrowing reference to naming context......." << endl;

   CosNaming::NamingContext_var context;

   try{

      context = CosNaming::NamingContext::_narrow(obj);

   }catch(const CORBA::Exception& ex){

      cout << "EXCEPTION CAUGHT!" << endl;

      cout << ex._to_string()<< endl;

      return 0;

   }

  

   cout << "Narrow successful!" <<endl;

 

   CosNaming::Name name;

   name.length(2);

   name[0].id = CORBA::string_dup("CallbackApplication");

   name[0].kind = CORBA::string_dup("");

   name[1].id = CORBA::string_dup("UtilCallbackBean");

   name[1].kind = CORBA::string_dup("");

 

 

   cout << "Resolving the name....." << endl;

   CORBA::Object_var object;

   try{

      object = context -> resolve(name);

   }catch(const CORBA::Exception& ex){

      cout << ex._to_string() << endl;

      return 0;

   }

   cout << "Resolving the name successful!" << endl;

 

   CORBA::String_var s = orb -> object_to_string(object);

   cout << "Object to string gets: " << s << endl;

  

 

   cout << "Narrowing to Callback object....." << endl;

   ::examples::iiop::UtilCallbackHome_var callbackHome = examples::iiop::UtilCallbackHome::_narrow(object);

  

 

   if(CORBA::is_nil(callbackHome)) {

      cout << "Can't narrow the object to Callback!" << endl;

 

   }

   else {

 

      examples::iiop::UtilCallback_var callback = callbackHome -> create();

 

      const CORBA::WChar* wch = (wchar_t*)L"Hello customer!";

      CORBA::WStringValue* param = new CORBA::WStringValue(wch); 

     

 

      cout << endl << "----------FIRST CALL reverseString(String s)-----------!" << endl;

      CORBA::WStringValue* msg = callback -> reverseString(param);

      const CORBA::WChar*  w = msg->_boxed_in();

      while(*w != 0)

         cout << (char)(*w++);

     

      cout << endl << endl << "----------SECOND CALL reverseString(String s)-----------!" << endl;

      CORBA::WStringValue* msg_2 = callback -> reverseString(msg);

      const CORBA::WChar*  w_2 = msg_2->_boxed_in(); 

      while(*w_2 != 0)

         cout << (char)(*w_2++);

 

        

      cout << endl << endl << "---------- CALL squareRoot(double d)-----------!" << endl;

      double d = 5.4;

      double result = callback -> squareRoot(d);

      cout << "The square root of " << d << " is " << result << endl;

  

      cout << endl << endl<< "Resolve Initial Reference POA......." << endl;

      CORBA::Object_var poaObj = orb -> resolve_initial_references("RootPOA");

     

      cout << "Narrowing to POA object............" << endl;

      PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(poaObj);

     

      cout << "Getting the POA Manager" << endl;

      PortableServer::POAManager_var manager = rootPOA -> the_POAManager();

     

      cout << "Instantiating Notificator Impl......" << endl;

      examples::iiop::Notificator_impl* notificator_impl = new examples::iiop::Notificator_impl(rootPOA);

     

      cout << "Obtaining Notificator reference....... " << endl;

      examples::iiop::Notificator_var notificator_var = notificator_impl -> _this();   

     

      cout << "Activating the manager......." << endl;

      manager->activate();

 

      if(CORBA::is_nil(notificator_var)) {

         cout << "notificator reference is null!" << endl<< endl;;

         return 0;

      }

 

      cout << endl << "-----------Calling EJB isPrime(long l, Notificator callback)-----------------" << endl;

     

      __int64 l = 2222222222222241;   

      printf("Testing %I64d .....\n", l);

     

      bool prime = callback -> isPrime(l, notificator_var);

     

      printf("\n%I64d is prime: %s\n", l, prime ? "true" : "false");

   }

   cout << endl << endl << "Program terminated" << endl;

   return 0;

}

When you have finished developing the client, you can compile and build the client application using your Microsoft Visual Studio. This tutorial does not describe how you work with the Microsoft Visual Studio.

Next Step:

Running the Application

 

End of Content Area