/**
   NNOcpp.cc
   c++ CORBA client implementation of Ni-NiO calculator
   Victor Kress, Paris 2003
   $Id: NNOcpp.cc,v 1.9 2007/09/18 23:32:05 kress Exp $
*/

#include <iostream>
#include <stdio.h>
#include "phases.hh"
#include "common.hh"
#include "Connection.h"

using namespace std;

int main (int argc, char **argv) {
  double r=8.314472,tk,rtlnfo2,logfo2;
  phases::dPhase_var nmet,o2gas,nio;
  phases::PhaseFactory_var pFact;
  Connection *con;

  try {
    con=new Connection(argc,argv);
    pFact = con->getdPhaseFactory();
  }
  catch(CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
         << "object.\n";
  }
  catch(CORBA::SystemException&) {
    cerr << "Caught CORBA::SystemException.\n";
  }
  catch(CORBA::Exception&) {
    cerr << "Caught CORBA::Exception.\n";
  }
  catch(omniORB::fatalException& fe) {
    cerr << "Caught omniORB::fatalException:\n";
    cerr << "  file: " << fe.file() << endl;
    cerr << "  line: " << fe.line() << endl;
    cerr << "  mesg: " << fe.errmsg() << endl;
  }
  catch(...) {
    cerr << "Caught unknown exception." << endl;
  }

  common::ClientData_var cd = con->getClientData();

  // Everything up to now is required to contact server and get
  // phase factory. Now we are ready to
  // Spawn phase objects 
  try {
    nmet=pFact->spawnPhase("NiMet",cd);
    o2gas=pFact->spawnPhase("O2Gas",cd);
    nio=pFact->spawnPhase("Bunsenite",cd);
  }
  catch (CORBA::Exception& ex) {
    cout << "Error in spawning Phase object:\n";
  }
  if (CORBA::is_nil(nmet))
    cout << "Null object returned from spawning NiMetal\n";
  if (CORBA::is_nil(o2gas))
    cout << "Null object returned from spawning O2Gas\n";
  if (CORBA::is_nil(nio))
    cout << "Null object returned from spawning Bunsenite\n";

  // We now have everything we need to do calculations 

  cout << "CORBA client to calculate log10(fO2) at T for Ni-NiO assemblage.\n";
  cout << "Input temperature in C: ";
  cin >>  tk;
  tk += 273.15;

  nmet->setTk(tk);
  o2gas->setTk(tk);
  nio->setTk(tk);

  rtlnfo2 = 2.*nio->getG() - 2.*nmet->getG() - o2gas->getG();

  logfo2 = rtlnfo2/(2.3026*r*tk);

  printf("log10(fO2) at %6.1f C = %6.2f\n",tk-273.15,logfo2);

  // Everything from here on is cleanup.
  // ALL objects must be manually removed.
  // Failure to do this will result in accumulation of
  // live but unused objects on the server.  
  // These will be cleaned up eventually,
  // but they add to server overhead.
  nmet->remove();
  o2gas->remove();
  nio->remove();
  delete con;
  return 0;
}

