// eg2_clt.cc - This is the source code of example 2 used in Chapter 2
//              "The Basics" of the omniORB user guide.
//
//              This is the client. The object reference is given as a
//              stringified IOR on the command line.
//
// Usage: eg2_clt <object reference> [message]
//

#include <iostream.h>
#include "tp1_i.hh"

void ajout(bookmark_ptr e)
{
    CORBA::String_var alias,url;
    alias = CORBA::string_alloc(50);
    url = CORBA::string_alloc(50);
    
    cout << "##### AJOUTER #####";
    cout << "Entrer un Alias : ";
    cin >> alias;
    
    cout << "Entrer une url : ";
    cin >> url;
    
    e->Ajouter(alias,url);

    cout << "Ajout ok" << endl;
}

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

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

    if( argc < 2 ) {
      cerr << "usage:  client_bookmark <object reference>" << endl;
      return 1;
    }

    CORBA::Object_var obj = orb->string_to_object(argv[1]);
    bookmark_ptr mybookmark = bookmark::_narrow(obj);
    
    if( CORBA::is_nil(mybookmark) ) {
      cerr << "Can't narrow reference to type bookmark (or it was nil)." << endl;
      return 1;
    }
    
    int numero=0;
    
    do
    {
    	cout << "Que voulez-vous faire ?" << endl;
    	cout << "1. Ajouter\n2. Supprimer\n3. Rechercher\n4. Sauvegarder\n5. Quitter" << endl;
    	cout << "Numero choisi : ";
    	cin >> numero;
	if (numero == 1) ajout(mybookmark);
    } while (numero != 5);
    
    orb->destroy();
  }
  catch(CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
         << "object." << endl;
  }
  catch(CORBA::SystemException&) {
    cerr << "Caught a CORBA::SystemException." << endl;
  }
  catch(CORBA::Exception&) {
    cerr << "Caught CORBA::Exception." << endl;
  }
  catch(omniORB::fatalException& fe) {
    cerr << "Caught omniORB::fatalException:" << endl;
    cerr << "  file: " << fe.file() << endl;
    cerr << "  line: " << fe.line() << endl;
    cerr << "  mesg: " << fe.errmsg() << endl;
  }
  catch(...) {
    cerr << "Caught unknown exception." << endl;
  }

  return 0;
}
