The MICO CORBA-Compliant System
by Arno Puder 


Listing One
// File: account.idl

interface Account {
    void deposit( in unsigned long amount );
    void withdraw( in unsigned long amount );
    long balance();
};


Listing Two
// File: server.cc

#include <fstream.h>
#include "account.h"

// Implementation of interface Account
class Account_impl : virtual public Account_skel
{
private:
  CORBA::Long _current_balance;

public:
  Account_impl()
  {
    _current_balance = 0;
  };
  void deposit( CORBA::ULong amount )
  {
    cout << "Operation deposit( " << amount << " )" << endl;
    _current_balance += amount;
  };
  void withdraw( CORBA::ULong amount )
  {
    cout << "Operation withdraw( " << amount << " )" << endl;
    _current_balance -= amount;
  };
  CORBA::Long balance()
  {
    cout << "Operation balance() => " << _current_balance << endl;
    return _current_balance;
  };
};


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

  // Create new Account object
  Account_impl* server = new Account_impl;

  // Write IOR to file
  ofstream out( "account.ior" );
  CORBA::String_var ref = orb->object_to_string( server );
  out << ref << endl;
  out.close();

  // Start processing incoming operations
  boa->impl_is_ready( CORBA::ImplementationDef::_nil() );
  orb->run();

  CORBA::release( server );
  return 0;
}

Listing Three
// File: client.cc

#include <iostream.h>
#include <fstream.h>
#include "account.h"

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

  // Read IOR from file
  ifstream in( "account.ior" );
  if( !in ) {
    cerr << "Can not open file 'account.ior'" << endl;
    return -1;
  }
  char ref[1000];
  in >> ref;
  in.close();

  // Generate object reference from stringified IOR
  CORBA::Object_var obj = orb->string_to_object( ref );
  Account_var client = Account::_narrow( obj );
  if( CORBA::is_nil( client ) ) {
    cerr << "IOR does not refer to an Account object" << endl;
    return -1;
  }

  // Invoke operations on remote object
  client->deposit( 700 );
  client->withdraw( 250 );
  cout << "Balance is " << client->balance() << endl;

  return 0;
}



Example 1:

idl account.idl
mico-c++ -I. -c account.cc -o account.o
mico-c++ -I. -c server.cc -o server.o
mico-c++ -I. -c client.cc -o client.o

mico-ld -I. -o server server.o account.o -lmico2.0.6
mico-ld -I. -o client client.o account.o -lmico2.0.6
