// 
// File:          TaoDriver_Component_Impl.cc
// Symbol:        TaoDriver.Component-v0.0.2
// Symbol Type:   class
// Babel Version: 0.10.2
// Description:   Server-side implementation for TaoDriver.Component
// 
// WARNING: Automatically generated; only changes within splicers preserved
// 
// babel-version = 0.10.2
// 
#include "TaoDriver_Component_Impl.hh"

// DO-NOT-DELETE splicer.begin(TaoDriver.Component._includes)
#include <iostream>
#include "Optimize_OptimizationModel.hh"
#include "OptimizationPorts_SolverPort.hh"
#include "Solver_OptimizationSolver.hh"
#include "gov_cca_Port.hh"
#include "sidl.hh"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

long
cputime ()
{
  struct rusage rus;

  getrusage (RUSAGE_SELF, &rus);
  return rus.ru_utime.tv_sec + rus.ru_utime.tv_usec / 1000000l;
}
// DO-NOT-DELETE splicer.end(TaoDriver.Component._includes)

// user-defined constructor.
void TaoDriver::Component_impl::_ctor() {
  // DO-NOT-DELETE splicer.begin(TaoDriver.Component._ctor)

  // DO-NOT-DELETE splicer.end(TaoDriver.Component._ctor)
}

// user-defined destructor.
void TaoDriver::Component_impl::_dtor() {
  // DO-NOT-DELETE splicer.begin(TaoDriver.Component._dtor)

  // DO-NOT-DELETE splicer.end(TaoDriver.Component._dtor)
}

// static class initializer.
void TaoDriver::Component_impl::_load() {
  // DO-NOT-DELETE splicer.begin(TaoDriver.Component._load)
  // Insert-Code-Here {TaoDriver.Component._load} (class initialization)
  // DO-NOT-DELETE splicer.end(TaoDriver.Component._load)
}

// user-defined static methods: (none)

// user-defined non-static methods:
/**
 * Execute some encapsulated functionality on the component. 
 * Return 0 if ok, -1 if internal error but component may be 
 * used further, and -2 if error so severe that component cannot
 * be further used safely.
 */
int32_t
TaoDriver::Component_impl::go ()
throw () 

{
  // DO-NOT-DELETE splicer.begin(TaoDriver.Component.go)
  gov::cca::Port p;
  OptimizationPorts::SolverPort solver;
  int info;
  int starttime, time;

  std::cout << "goed" << std::endl;
  starttime = cputime();
  
  // Connect to OptSolver
  try {
    solver = this->myServices.getPort("OptimizationSolver");
  } catch (gov::cca::CCAException e) {
    std::cerr << "Could not get OptimizationSolver port:" << e.getNote() <<std::endl;
  }
  

  info = solver.Initialize();
  info = solver.SetFromOptions();

  info = solver.Solve();
  info = solver.View();
  info = solver.Finalize();

  // Release solver
  this->myServices.releasePort("OptimizationSolver");

  time = cputime() - starttime;
  std::cout << "Time to run: "<< time << std::endl;
  return 0;
  
  // DO-NOT-DELETE splicer.end(TaoDriver.Component.go)
}

/**
 * Starts up a component presence in the calling framework.
 * @param Svc the component instance's handle on the framework world.
 * Contracts concerning Svc and setServices:
 * 
 * The component interaction with the CCA framework
 * and Ports begins on the call to setServices by the framework.
 * 
 * This function is called exactly once for each instance created
 * by the framework.
 * 
 * The argument Svc will never be nil/null.
 * 
 * Those uses ports which are automatically connected by the framework
 * (so-called service-ports) may be obtained via getPort during
 * setServices.
 */
void
TaoDriver::Component_impl::setServices (
  /* in */ ::gov::cca::Services services ) 
throw ( 
  ::gov::cca::CCAException
){
  // DO-NOT-DELETE splicer.begin(TaoDriver.Component.setServices)
  if (services._is_nil()) {
    return;
  }
  std::cout << "Setting service in driver" << std::endl;
  this->myServices = services;
  this->myServices.registerUsesPort("OptimizationSolver",
				    "OptimizationSolver.SolverPort",
				    0);
  this->myServices.addProvidesPort(self,
				   "go",
				   "gov.cca.ports.GoPort",
				   0);
				   
  
  // DO-NOT-DELETE splicer.end(TaoDriver.Component.setServices)
}


// DO-NOT-DELETE splicer.begin(TaoDriver.Component._misc)
// Put miscellaneous code here
// DO-NOT-DELETE splicer.end(TaoDriver.Component._misc)

