/*
  Stock_Factory_impl.cpp

  This program is the implementation of a Corba server object which acts as
  a factory for (Corba) Stock objects. A call to the getStock function returns
  every time a fresh Stock object which also is activated implicitly by _this().

  Author: Ingo Kloeckl
 */

#include <OB/CORBA.h>
#include <Stock_impl.h>         // header of a single Stock object
#include <Stock_Factory_impl.h>

using namespace std;

// The constructor gets a reference to the server's ORB for controlling it.
// This should only be used by a superuser control program!
Quote_Stock_Factory_impl::Quote_Stock_Factory_impl(CORBA::ORB_ptr orb) : 
  orb_(CORBA::ORB::_duplicate(orb))
  /*
    other possibility for returning objects: initialize the test stocks in the
    initialization list of the constructor
  Quote_Stock_Factory_impl::Quote_Stock_Factory_impl(CORBA::ORB_ptr orb) : 
    orb_(CORBA::ORB::_duplicate(orb)),
    ibm(200,"IBM Bussiness Machines", "IBM"),
    sun(90,"Sun Microsystems", "SUN"){
  }
   */
 {
}

// gets a Stock object which is freshly created (we can call this criminological
// a 'Bluete' if printed) and activate it when passing back the reference to it
Quote::Stock_ptr Quote_Stock_Factory_impl::getStock(const char* pSymbol) throw (Quote::InvalidStockSymbol, CORBA::SystemException) {
  cout << "got request for " << pSymbol << endl;
  Quote_Stock_impl* stock;
  if (!strcmp(pSymbol,"IBM")){
    // this simply returns predefined objects
    //return this->ibm._this();
    stock = new Quote_Stock_impl(200,"IBM Business Models", "IBM");
  } else if (!strcmp(pSymbol,"SUN")){
    // this simply returns predefined objects
    //return this->sun._this();
    stock = new Quote_Stock_impl(176,"Sunshine Machines", "SUN");
  } else {
    throw Quote::InvalidStockSymbol();
  }
  return stock->_this();
}

// control the ORB of the mainline of this server
void Quote_Stock_Factory_impl::shutdown() throw (CORBA::SystemException){
  cout << "shutting down the ORB" << endl;
  orb_->shutdown(false);
}


