Sunteți pe pagina 1din 27

Common Object Request

Broker Architecture

Dr. Eng. Haiyan HOUSROUM


housroum@yahoo.com

2007-2008 CORBA 1
23/10/08 CORBA 2
Goal of lecture

 Give an example of CORBA Application.


 Bank Account.
 Time

2007-2008 CORBA 3
Stand-Alone Application in
C++
 1: // File: account.cc
 2:
 3: #include <iostream>
 4:
 5:
 6: using namespace std;
 7:
 8: // Implementation for interface Account
 9: class Account_impl
 10: {
 11: private:
 12: int _balance;
 13:
 14: public:
 15: Account_impl ()
 16: {
 17: _balance = 0;
 18: }

2007-2008 CORBA 4
Stand-Alone Application in
C++
 20: void deposit (int amount)
 21: {
 22: cout << "Server: deposit " << amount << endl;
 23: _balance += amount;
 24: }
 25:
 26: void withdraw (int amount)
 27: {
 28: cout << "Server: withdraw " << amount << endl;
 29: if (_balance >= amount)
 30: _balance -= amount;
 31: else
 32: cout << "Server: withdraw failed" << endl;
 33: }
 34:
 35: int balance ()
 36: {
 37: cout << "Server: balance " << _balance << endl;
 38: return _balance;
 39: }
 40:
2007-2008 }; CORBA 5
Stand-Alone Application in
C++
 42: int
 43: main (int argc, char *argv[])
 44: {
 45: int balance;
 46: Account_impl* account = new Account_impl();
 47: account->deposit (700);
 48: balance = account->balance ();
 49: cout << "Client: balance is " << balance << endl;
 50: account->withdraw (50);
 51: balance = account->balance ();
 52: cout << "Client: balance is " << balance << endl;
 53: account->withdraw (200);
 54: balance = account->balance ();
 55: cout << "Client: balance is " << balance << endl;
 56:
 57: return 0;
 58: }

2007-2008 CORBA 6
IDL Specification
 59: // File: account.idl
 60:
 61: interface Account {
 62: void deposit (in long amount);
 63: void withdraw (in long amount);
 64: long balance ();
 65: };

2007-2008 CORBA 7
IDL Specification
 Account: is an abstract base class that contains all local definitions
for the interface. As all interfaces, the C++ class Account is derived
from the class CORBA::Object, which is part of the CORBA library.

 POA_Account: is the skeleton of the interface Account. The prefix


POA_ is a convention in accordance with the Portable Object Adapter
(POA), which is an object adapter described in the CORBA
specification. The implementation of the interface Account provided
by the developer is derived from POA_Account.

 Account_var: is a helper class whose instances represent pointers to


a CORBA object. The client uses this class to hold a reference to a
remote CORBA object. Note that an instance of Account_var
represents a reference to the CORBA object, not the CORBA object
itself.

2007-2008 CORBA 8
Implementation of the
server
 66: // File: server.cc
 67:
 68: #include <iostream>
 69: #include <fstream>
 70: #include "account.h"
 71:
 72: using namespace std;
 73:
 74: // Implementation for interface Account
 75: class Account_impl : virtual public POA_Account
 76: {
 77: private:
 78: CORBA::Long _balance;
 79:
 80: public:
 81: Account_impl ()
 82: {
 83: _balance = 0;
 84: }
2007-2008 CORBA 9
Implementation of the
server
 86: void deposit (CORBA::Long amount)
 87: {
 88: cout << "Server: deposit " << amount << endl;
 89: _balance += amount;
 90: }
 91:
 92: void withdraw (CORBA::Long amount)
 93: {
 94: cout << "Server: withdraw " << amount << endl;
 95: if (_balance >= amount)
 96: _balance -= amount;
 97: else
 98: cout << "Server: withdraw failed" << endl;
 99: }
 100:
 101: CORBA::Long balance ()
 102: {
 103: cout << "Server: balance " << _balance << endl;
 104: return _balance;
 105: }
 106:
2007-2008 }; CORBA 10
Implementation of the
server
 108: int
 109: main (int argc, char *argv[])
 110: {
 111: // Initialize the ORB
 112: CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
 113:
 114: // Obtain a reference to the RootPOA and its Manager
 115: CORBA::Object_var poaobj =
 116: orb->resolve_initial_references ("RootPOA");
 117: PortableServer::POA_var poa =
 118: PortableServer::POA::_narrow (poaobj);
 119: PortableServer::POAManager_var mgr =
 120: poa->the_POAManager();
 121:
 122: // Create an Account
 123: PortableServer::Servant account_servant =
 124: new Account_impl;
 125:
 126: // Activate the Account
2007-2008 CORBA 11
Implementation of the
server
 127: CORBA::Object_var the_account =
 128: account_servant->_this();
 129:
 130: // Write the object’s IOR to a file
 131: CORBA::String_var ior =
 132: orb->object_to_string (the_account);
 133: ofstream of ("account.ior");
 134: of << ior;
 135: of.close ();
 136:
 137: // Activate the POA and start serving requests
 138: cout << "Running." << endl;
 139: mgr->activate ();
 140: orb->run();
 141:
 142:
 143: return 0;
 144: }

2007-2008 CORBA 12
Implementation of the
Client
 148: // File: client.cc
 149:
 150: #include <iostream>
 151: #include <fstream>
 152: #include "account.h"
 153:
 154: using namespace std;
 155:
 156: int
 157: main (int argc, char *argv[])
 158: {
 159: // Initialize the ORB
 160: CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
 161:
 162: // Connect to the Account
 163: ifstream f ("account.ior");
 164: string ior;
 165: f >> ior;
 166: CORBA::Object_var obj =
 167: orb->string_to_object (ior.c_str());
 168:
2007-2008 Account_var account =CORBA
Account::_narrow (obj); 13
Implementation of the
Client
 170: // Deposit and withdraw some money
 171: account->deposit (700);
 172: cout << "Client: balance is “
 173: << account->balance () << endl;
 174: account->withdraw (50);
 175: cout << "Client: balance is "
 176: << account->balance () << endl;
 177: account->withdraw (200);
 178: cout << "Client: balance is "
 179: << account->balance () << endl;
 180:
 181: return 0;
 182: }

2007-2008 CORBA 14
Compiling & Executing
 account.idl: IDL specification of the interfaces.

 account.cc: Contains stubs and skeletons of object


interfaces. This file is generated automatically by the IDL
compiler.

 server.cc: Implementation of the server.

 client.cc: Implementation of the client.

2007-2008 CORBA 15
Compiling & Executing
 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 -o server server.o account.o -lmicoX.Y.Z
 mico-ld -o client client.o account.o -lmicoX.Y.Z

2007-2008 CORBA 16
Compiling & Executing
 Account.ior:
 IOR:010000001000000049444c3a4163636f756e743a312e30\
 0001000000000000002c000000010100000a0000003132372e\
 302e302e31001685140000002f31393938332f313036313334\
 373034342f5f30
 Iordump
 Repo Id: IDL:Account:1.0
 IIOP Profile
 Version: 1.0
 Address: inet:127.0.0.1:34070
 Key: 2f 31 39 39 38 33 2f 31 30 36 31 33 34 37 30 34
34 2f 5f 30

2007-2008 CORBA 17
Compiling & Executing
 Client:
 Client: balance is 700
 Client: balance is 650
 Client: balance is 450

 Server:
 Server: deposit 700
 Server: balance 700
 Server: withdraw 50
 Server: balance 650
 Server: withdraw 200
 Server: balance 450

2007-2008 CORBA 18
Time Example (IDL)
struct TimeOfDay {
short hour; // 0 - 23
short minute; // 0 - 59
short second; // 0 - 59
};
interface Time {
TimeOfDay get_gmt();
};

23/10/08 CORBA 19
Time Example (Server)
#include <time.h>
#include <iostream.h>
#include "timeS.hh"
class Time_impl : public virtual POA_Time {
public:
virtual TimeOfDay get_gmt()
throw(CORBA::SystemException);
};

TimeOfDay Time_impl::
get_gmt() throw(CORBA::SystemException) {
23/10/08 CORBA 20
Time Example (Server)
time_t time_now = time(0);
struct tm * time_p = gmtime(&time_now);
TimeOfDay tod;
tod.hour = time_p->tm_hour;
tod.minute = time_p->tm_min;
tod.second = time_p->tm_sec;
return tod;
}

23/10/08 CORBA 21
Time Example (Server)
int main(int argc, char * argv[]) {
try {
// Initialize orb
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
// Get reference to Root POA.
CORBA::Object_var obj
= orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa
= PortableServer::POA::_narrow(obj);
// Activate POA manager
PortableServer::POAManager_var mgr
= poa->the_POAManager();
mgr->activate();
23/10/08 CORBA 22
Time Example (Server)
// Create an object
Time_impl time_servant;
// Write its stringified reference to stdout
Time_var tm = time_servant._this();
CORBA::String_var str = orb->object_to_string(tm);
cout < str < endl;
// Accept requests
orb->run();
} catch (const CORBA::Exception &) {
cerr < "Uncaught CORBA exception" < endl;
return 1;
}
return 0;
}

23/10/08 CORBA 23
Time Example (POA_Time)
// In file timeS.hh:
class POA_Time :
public virtual PortableServer::ServantBase {
public:
virtual ~POA_Time();
Time_ptr _this();
virtual TimeOfDay get_gmt()
throw(CORBA::SystemException) = 0;
};

23/10/08 CORBA 24
Time Example (Client)
#include <iostream.h>
#include <iomanip.h>
#include "time.hh"
int main(int argc, char * argv[]) {
try {
// Check arguments
if (argc != 2) {
cerr < "Usage: client IOR_string" < endl;
throw 0;
}
// Initialize orb
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
// Destringify argv[1]
CORBA::Object_var obj = orb->string_to_object(argv[1]);
if (CORBA::is_nil(obj)) {
cerr < "Nil Time reference" < endl;
throw 0;
}

23/10/08 CORBA 25
Time Example (Client)
// Narrow
Time_var tm = Time::_narrow(obj);
if (CORBA::is_nil(tm)) {
cerr < "Argument is not a Time reference" < endl;
throw 0;
}
// Get time
TimeOfDay tod = tm->get_gmt();
cout < "Time in Greenwich is "
< setw(2) < setfill('0') < tod.hour < ":"
< setw(2) < setfill('0') < tod.minute < ":"
< setw(2) < setfill('0') < tod.second < endl;
} catch (const CORBA::Exception &) {
cerr < "Uncaught CORBA exception" < endl;
return 1;
} catch (...) {
return 1;
}
return 0;
}

23/10/08 CORBA 26
The End

THANK YOU!

2007-2008 CORBA 27

S-ar putea să vă placă și