// eg1.cc - This example is identical to that found in:
//             <top>/src/examples/echo/eg1.cc
//          except that implicit activation is used to activate
//          the echo object.
//
// Usage: eg1
//

#include <echo.hh>

#include <iostream>
using namespace std;


class Echo_i : public POA_Echo
{
public:
  inline Echo_i() {}
  virtual ~Echo_i() {}
  virtual char* echoString(const char* mesg);
};


char* Echo_i::echoString(const char* mesg)
{
  return CORBA::string_dup(mesg);
}

//////////////////////////////////////////////////////////////////////

static void hello(Echo_ptr e)
{
  if (CORBA::is_nil(e)) {
    cout << "hello: The object reference is nil!\n" << endl;
    return;
  }

  CORBA::String_var src = (const char*) "Hello!";

  CORBA::String_var dest = e->echoString(src);

  cout << "I said, \"" << (char*)src << "\"." << endl
       << "The Echo object replied, \"" << (char*)dest <<"\"." << endl;
}

//////////////////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
  try {
    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

    {
      CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
      PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);

      // NB. You can activate the POA before or after
      // activating objects in that POA.
      PortableServer::POAManager_var pman = poa->the_POAManager();
      pman->activate();

      Echo_i* myecho = new Echo_i();

      // This activates the object in the root POA (by default), and
      // returns a reference to it.
      Echo_var myechoref = myecho->_this();

      myecho->_remove_ref();

      hello(myechoref);

    }
    orb->destroy();
  }
  catch (CORBA::TRANSIENT&) {
    cerr << "Caught system exception TRANSIENT -- unable to contact the "
         << "server." << endl;
  }
  catch (CORBA::SystemException& ex) {
    cerr << "Caught a CORBA::" << ex._name() << endl;
  }
  catch (CORBA::Exception& ex) {
    cerr << "Caught CORBA::Exception: " << ex._name() << endl;
  }
  return 0;
}
