// Filename   : client.cpp
// Written By : Stephen A. Cerniglia	cernigls@cs.rpi.edu

#include "front.h"
#include <iostream.h>

void bookSeat(CORBA::Object_ptr obj) {
FrontOffice_var foVar = FrontOffice::_narrow(obj);
  if (CORBA::is_nil(foVar)) {
    cerr << "Cannot invoke on a nil object reference.\n" << endl;
    return;
  }

  CORBA::String_var itsName = foVar->name();
  cout << "Name is " << itsName << endl;
  cout << "Number of seats is " << foVar->numberOfSeats()<< endl;
  cout << "Type row and seat to book: " << flush;

  CORBA::Char row;
  CORBA::ULong seat;
  cin >> row >> seat;

  Place p = {row, seat};
  cout << "Price of seat " << row << seat << " is "
       << foVar->getPrice(p) << "." << endl;
  
  if (foVar->bookSingleSeat(p, "4531 345 2222 3333"))
    cout << "Seat " << row << seat << " booked ok." << endl;
  else
    cout << "Seat " << row << seat << " cannot be booked." << endl;
  
}

int main ( int argc, char * argv[]) {
// ORB initialization
  CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB2" );
  CORBA::BOA_var boa = orb->BOA_init (argc, argv, "omniORB2_BOA");

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

try {
    CORBA::Object_var obj = orb->string_to_object(argv[1]);
    bookSeat(obj);
}
  catch(CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE, unable to contact the "
         << "object." << endl;
  }
  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;
  }
  catch(...) {
    cerr << "Caught a system exception." << endl;
  }

  return 0;
}
