//#
//# Time Client in C++
//# Copyright 2004 by Eric Y. Theriault
//# All Rights Reserved.
//# http://www.eyt.ca/CORBA
//#
#ifdef EYT_MICO
#include "Mico/Time.h"
#else
#ifdef EYT_TAO
#include "TAO/TimeC.h"
#else
#error Undefined ORB type.
#endif
#endif
#include <string>
#include <iostream>
#include <fstream>
#include <errno.h>

int main( int argc, char ** argv )
{
   try {
      std::string iorFile = "time.ior";

      // Create and Initialize the ORB
      CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );

      // Read in the ior file
      std::string ior;
      {
         std::ifstream file( iorFile.c_str(), std::ios::in );
         if ( file.fail() ) {
            std::cout << "Failed to open " << iorFile << ": "
                      << strerror( errno ) << std::endl;
            return 1;
         }
         file >> ior;
         file.close();
      }

      // Resolve the ior into an object.
      CORBA::Object_var object = orb->string_to_object( ior.c_str() );
      if ( CORBA::is_nil( object ) ) {
         std::cerr << "Invalid Time Reference" << std::endl;
         return 1;
      }
      TimeTools::Time_var timeImpl = TimeTools::Time::_narrow( object );
      if ( CORBA::is_nil( timeImpl ) ) {
         std::cerr << "Argument is not a Time reference." << std::endl;
         return 1;
      }

      // OK.  Let's call the Time interface's methods.
      TimeTools::TimeOfDay time = timeImpl->getTime();
      std::cout << "Time server's time is: "
                << time.hour << ":" << time.minute << ":"
                << time.second << std::endl;
      return 0;
   } catch ( const CORBA::Exception & e ) {
      std::cerr << "Caught a CORBA exception." << std::endl;
#ifdef EYT_MICO
      e._print( std::cerr );
      std::cerr << std::endl;
#else
#ifdef EYT_TAO
      std::cerr << e._name() << std::endl;
#else
#error More verbose error here required.
#endif
#endif
      return 1;
   } catch ( const std::exception & e ) {
      std::cerr << "Caught Exception: " << e.what() << std::endl;
      return 1;
   } catch ( ... ) {
      std::cerr << "Unknown exception caught." << std::endl;
      return 1;
   }
}

