Create the source file for the sample C++ client, arith.cpp. You can find a copy of arith.cpp in the html/docs/tutorial/cpp subdirectory of your EAServer installation. Here is the source for arith.cpp:
/* ** arith.cpp -- Example C++ client for the EAServer C++ ** tutorial. ** ** This program connects to EAServer, ** creates an instance of the Tutorial/CPPArithmetic ** component, and invokes the multiply method. ** ** Usage: ** arith iiop://<host>:<port> ** ** Where: ** ** <host> is the host name or IP address of the server machine. ** ** <iiop-port> is the server's IIOP port (9000 in the ** default configuration). ** */ #include <stdio.h> #include <iostream.h> #include <string.h> #include <Jaguar.hpp> #include <SessionManager.hpp> #include <Tutorial.hpp> // Stubs for interfaces in Tutorial IDL // module. int main(int argc, char** argv) { const char *usage = "Usage:\n\tarith iiop://<host>:<iiop-port>\n"; const char *tutorial_help = "Check Jaguar Manager and verify that the" "Tutorial/CPPArithmetic component exists " "and that it implements the " "Tutorial::CPPArithmetic IDL interface."; const char *component_name = "Tutorial/CPPArithmetic"; try { if (argc < 2) { cout << usage; return -1; } char* manager_url = argv[1]; cout << "**** Creating session\n"; // Initialize the ORB CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, 0); // Create a SessionManager::Manager instance CORBA::Object_var obj = orb->string_to_object(manager_url); SessionManager::Manager_var manager = SessionManager::Manager::_narrow(obj); if (CORBA::is_nil(manager)) { cout << "Error: Null SessionManager::Manager instance. Exiting. " << usage ; return -1; } // Create an authenticated session for user Guest // using password GuestPassword SessionManager::Session_var session = manager->createSession("Guest", "GuestPassword"); if (CORBA::is_nil(session)) { cout << "Error: Null session. Exiting. " << usage; return -1; } // Obtain a factory for component instances by // resolving the component name cout << "**** Creating component instance for " << component_name << "\n" ; obj = session->lookup(component_name); SessionManager::Factory_var arithFactory = SessionManager::Factory::_narrow(obj); if (CORBA::is_nil(arithFactory)) { cout << "ERROR: Null component factory for component " << component_name << tutorial_help ; return -1; } // Use the factory to create an instance. Tutorial::CPPArithmetic_var arith = Tutorial::CPPArithmetic::_narrow(arithFactory->create()); // Verify that we really have an instance. if (CORBA::is_nil(arith)) { cout << "ERROR: Null component instance. " << tutorial_help ; return -1; } // Call the multiply method. cout << "**** Multiplying ...\n\n"; CORBA::Double m1 = (CORBA::Double)3.1; CORBA::Double m2 = (CORBA::Double)2.5; CORBA::Double result = arith->multiply(m1, m2); cout << (double)m1 << " * " << (double)m2 << " = " << (double)result << "\n\n"; } // Explicitly catch exceptions that can occur due to user error, // and print a generic error message for any other CORBA system // exception. // Requested object (component) does not exist. catch ( CORBA::OBJECT_NOT_EXIST cone ) { cout << "Error: CORBA OBJECT_NOT_EXIST exception. Check the " << "server log file for more information. Also verify " << "that the " << component_name << " component has been created properly in " << "Jaguar Manager. " << tutorial_help ; } // Authentication or authorization failure. catch ( CORBA::NO_PERMISSION npe ) { cout << "Error: CORBA:: NO_PERMISSION exception. Check whether " << "login authentication is enabled for your server and " << "whether the component has restricted access. If so " << "edit the source file to use a valid user name and " << "password.\n"; } // Invalid object reference. catch ( CORBA::INV_OBJREF cio ) { cout << "Error: CORBA INV_OBJREF exception."; } // Communication failure. Server could be down or URL's port value // could be wrong. catch ( CORBA::COMM_FAILURE ccf ) { cout << "Error: CORBA COMM_FAILURE exception. Check that the " << "specified host and IIOP port number are " << "correct and that the server is running. " << usage; } // Anything else. catch ( CORBA::OBJ_ADAPTER ) { cout << "Error: CORBA::OBJ_ADAPTER \n"; } catch ( CORBA::SystemException cse ) { cout << "Error: CORBA System Exception. Check that the server " << "hostname and IIOP port are specified correctly, and " << "check the server's error log for more information.\n" << usage; } return 0; }
Copyright © 2005. Sybase Inc. All rights reserved. |