// 
// FILE:
// MyServer.C
//
// FUNCTION:
// Implements simple example service.
//
// USAGE:
// run MyServer at the command line.
//
// HISTORY:
// Linas Vepstas, October 1997

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

// ---------------------------------
// a simple utility to store the stringified 
// object reference (IOR) in a file
void write_to_file (const char * s) {
   const char* refFile = "/tmp/Ref.ref";
   ofstream out (refFile);
   if (out.fail ()) {
       extern int errno;
       cerr << "Error: can't open `" << refFile << "': "
   	 << strerror(errno) << endl;
       exit (1);
   }
   out << s << endl;
   out.close();
}

// ---------------------------------
// Notice the need for different include files ...

#if defined (USE_OMNIORB) || defined (USE_MICO)
#include "MyClass.h"
#endif /* USE_OMNIORB */

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


// ---------------------------------
// The names of the "stubs" or "skeleton classes" are different too 

#ifdef USE_OMNIORB
#define  MyClass_skel    _sk_MyClass
#endif /* USE_OMNIORB */

class MyClass_impl : virtual public MyClass_skel
{
   public:

// ---------------------------------
// some implementations require a special constructor ...
#if defined (USE_OMNIORB) || defined (USE_MICO)
      MyClass_impl (void) {}
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
      MyClass_impl (const char* n) : MyClass_skel(n) {}
#endif /* USE_OMNIBROKER */

      virtual ~MyClass_impl () {}
      virtual char * DoStuff (const char *mesg);
};


// ---------------------------------
// the implementation

char *
MyClass_impl :: DoStuff (const char *mesg) 
{
   char buff[1000];
#ifdef USE_OMNIORB
   strcpy (buff, "OmniORB flew around: ");
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
   strcpy (buff, "OmniBroker arbitraged: ");
#endif /* USE_OMNIBROKER */

#ifdef USE_MICO
   strcpy (buff, "Mico heard the aphid say: ");
#endif /* USE_MICO */

   strcat (buff, mesg);

#if defined (USE_OMNIORB) || defined (USE_MICO)
   char *p = CORBA::string_dup(buff);
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
   char *p = CORBA_string_dup(buff);
#endif /* USE_OMNIBROKER */
   return p;
}

// ---------------------------------
// main

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

   try {
      // Create ORB and BOA
#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);
      CORBA_BOA_var boa = orb -> BOA_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 */
	

      // Create implementation object
#ifdef USE_OMNIORB
      MyClass_impl * myobj = new MyClass_impl ();
      myobj -> _obj_is_ready(boa);
#endif /* USE_OMNIORB */

#ifdef USE_MICO
      MyClass_impl * myobj = new MyClass_impl ();
#endif /* USE_MICO */

#ifdef USE_OMNIBROKER
      MyClass_var myobj = new MyClass_impl ("some_unique_string");
#endif /* USE_OMNIBROKER */

      // Get stringified reference
#if defined(USE_OMNIORB) || defined (USE_MICO)
      CORBA::String_var str = orb -> object_to_string (myobj);
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
      CORBA_String_var str = orb -> object_to_string (myobj);
#endif /* USE_OMNIBROKER */

      write_to_file (str);

      // Tell the BOA we are ready. The BOA's default behaviour is to block
      // on this call indefinitely.
#ifdef USE_OMNIORB
      boa -> impl_is_ready ();
#endif /* USE_OMNIORB */

#ifdef USE_OMNIBROKER
      boa -> impl_is_ready (CORBA_ImplementationDef::_nil ());
#endif /* USE_OMNIBROKER */

#ifdef USE_MICO
      boa -> impl_is_ready (CORBA::ImplementationDef::_nil());
#endif /* USE_MICO */
   }

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

#ifdef USE_OMNIBROKER
   catch (CORBA_SystemException& ex)
#endif /* USE_OMNIBROKER */
   {
      cerr << "Error: caught a system exception\n";
      return 1;
   }
   catch (...) {
      cerr << "Error: caught some unknown exception \n";
   }

   return 0;
}
// ============================ end of file ======================


