// 
// FILE:
// MyClient.C
//
// FUNCTION:
// This is the client.  It performs 100K corba object calls.
//
// USAGE:
// run MyClient at the command line
//
// HISTORY:
// Linas Vepstas October 1997

#include <errno.h>
#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
#include <string.h>

#ifdef USE_OMNIBROKER
#include <OB/CORBA.h>
#include <OB/Util.h>
#endif /* USE_OMNIBROKER */

#include "MyClass.h"

// a simple utility subroutine, retreives the
// stringified IOR from a file.
char * get_ref (void) {
   const char* refFile = "/tmp/Ref.ref";
   ifstream in (refFile);
   if (in.fail()) {
       // extern int errno;
       cerr << "Error: can't open `" << refFile << "': "
   	 << strerror(errno) << endl;
       exit (1);
   }
   static char s[1000];
   in >> s;
   return s;
}
	

int
main (int argc, char **argv) 
{
   // Create ORB
#ifdef USE_OMNIORB
   CORBA::ORB_ptr orb = CORBA::ORB_init (argc, argv, "omniORB2");
   CORBA::BOA_ptr boa = orb -> BOA_init (argc, argv, "omniORB2_BOA");
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
   CORBA_ORB_var orb = CORBA_ORB_init(argc, argv);
#endif /* USE_OMNIBROKER */

#ifdef USE_MICO
   CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, "mico-local-orb");
   CORBA::BOA_var boa = orb -> BOA_init (argc, argv, "mico-local-boa");
#endif /* USE_MICO */

   char * obj_string = get_ref();

   try { 
#if defined (USE_OMNIORB) || defined (USE_MICO)
      CORBA::Object_var obj = orb -> string_to_object (obj_string);
      assert (!CORBA :: is_nil(obj));
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
      CORBA_Object_var obj = orb -> string_to_object (obj_string);
      assert (!CORBA_is_nil(obj));
#endif /* USE_OMNIBROKER */

      MyClass_var myobj = MyClass::_narrow (obj);

      // force the use of the copy explicit casting to operator=(). 
#if defined (USE_OMNIORB) || defined (USE_MICO)
      CORBA::String_var src = (const char*) "Hello!"; 
      CORBA::String_var dest;
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
      CORBA_String_var src = (const char*) "Hello!"; 
      CORBA_String_var dest;
#endif /* USE_OMNIBROKER */

      cerr << "Doing 100K repetitons .. this may take a minute or two \n";
      for (int i=0; i<100000; i++) {
         dest = myobj -> DoStuff (src);
      }

      cerr << "Client said,\"" << src << "\"."
           << " The Server Responded,\"" << dest <<"\"" << endl;

   }

#if defined(USE_OMNIORB) || defined (USE_MICO)
   catch (CORBA::COMM_FAILURE& ex) {
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
   catch (CORBA_COMM_FAILURE& ex) {
#endif /* USE_OMNIBROKER */
      cerr << "Caught system exception COMM_FAILURE, unable to contact the "
           << "object." << endl;
      return 1;
   }

#ifdef USE_OMNIORB
   catch (omniORB::fatalException& ex) {
      cerr << "Caught omniORB2 fatalException. This indicates a bug is caught "
           << "within omniORB2.\nPlease send a bug report.\n"
           << "The exception was thrown in file: " << ex.file() << "\n"
           << "                            line: " << ex.line() << "\n"
           << "The error message is: " << ex.errmsg() << endl;
     return 1;
  }
#endif /* USE_OMNIORB */

  catch(...) {
     cerr << "Caught a system exception." << endl;
     return 1;
  }

  return 0;
}
// **********************************************************************

