MICO

account_impl.cc

#include "account.h"

class Account_impl : virtual public Account_skel
{
private:
  CORBA::Long _current_balance;

public:
  Account_impl()
  {
    _current_balance = 0;
  };

  void deposit(CORBA::ULong amount)
  {
    _current_balance += amount;
  };

  void withdraw(CORBA::ULong amount)
  {
    _current_balance -= amount;
  };

  CORBA::Long balance()
  {
    return _current_balance;
  };
};

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

  //server side code
  //Notice how we allocate just the pointer, not the object.
  //This is because CORBA requires each object to be deleted with a
  // CORBA::release(obj) call only. Automatic allocation will call the
  // destructor when out of scope.
  Account_impl* server = new Account_impl;

  //This is how we turn the reference into a string.
  CORBA::String_var ref = orb->object_to_string(server);
  cour << "Server refrence: " << ref << endl;

  //client side code
  CORBA::Object_var obj = orb->string_to_object(ref);

  //Account_var is a smart pointer to Account instances that calls
  // release() when the object is destroyed. Account_ptr is a regular
  // smart pointer.
  Account_var client = Account::_narrow(obj);

  client->deposit(700);
  client->withdraw(250);
  cout << "Balance is " << client->balance() << endl;

  //We don't need the server object any more. This code belongs
  // to the server implementation
  CORBA::release(server);
  return 0;
}
  

José M. Vidal .

5 of 8