Transcript name

Object-Oriented
Middleware
(OOMI)
Locating Distributed Objects
Emmerich – Chapter 8.
16.03.2003
Outline
1. Quick recap on object references
2. Object Naming
– Principles
– CORBA Naming Service
3. Object Trading
(not part of Course Curriculum)
Slide 2 of 41
© Ingeniørhøjskolen i Århus
Recap on IOR – so far
• IOR: Interoperable Object Reference
– Includes info on: Repository ID
(standard), Endpoint Info (standard) including IP and port number, Object Key
(proprietary)
• Can be written into a file & read
– as we have seen the last two lectures
• Not really nice with a file-based
reference – or what?
IOR:000000000000001749444c3
a48656c6c6f4170702f48656c6c6
f3a312e30000000000001000000
000000006c000102000000000e
3139322e3136382e312e313030
0011b600000021afabcb0000000
020a80a2503000000010000000
00000000000000004000000000
a0000000000000100000001000
00020000000000001000100000
00205010001000100200001010
90000000100010100
• File-based may be necessary due to
firewall problems
• Possible to use a HTTP or FTP
server for distributing the references
Slide 3 of 41
© Ingeniørhøjskolen i Århus
#include <OB/CORBA.h>
HelloCorba
#include <Hello.h>
#include <fstream.h>
C++ Client
int run(CORBA::ORB_ptr);
int main(int argc, char* argv[])
{
with file-based IOR
int status = EXIT_SUCCESS;
Part 1
CORBA::ORB_var orb;
try {
Init ORB
orb = CORBA::ORB_init(argc, argv);
status = run(orb);
} catch(const CORBA::Exception&) {
status = EXIT_FAILURE;
}
if(!CORBA::is_nil(orb))
{ try
{
Destroy ORB
orb -> destroy();
} catch(const CORBA::Exception&) { status = EXIT_FAILURE; }
}
return status;
}
Slide 4 of 41
© Ingeniørhøjskolen i Århus
HelloCorba
…
C++ Client
with file-based IOR
int run(CORBA::ORB_ptr orb)
{
Part 1
const char* refFile = "object.ref";
Object reference
Read from file
ifstream in(refFile);
char s[2048]; in >> s;
CORBA::Object_var obj = orb -> string_to_object(s);
HelloApp::Hello_var hello = HelloApp::Hello::_narrow(obj);
cout << hello->sayHello() << endl;
Narrow the call (CORBA type cast)
Call via Proxy and print
return 0;
}
Slide 5 of 41
© Ingeniørhøjskolen i Århus
Motivation
• Avoid using physical locations for locating
components! Why?
• Naming:
– Locating components by external names
– Similar to white pages
– Java RMI Registry, CORBA Naming Service
• Trading: Locating components by service
characteristics
– Similar to yellow pages
– Web services UDDI
Slide 6 of 41
© Ingeniørhøjskolen i Århus
1. Naming - Common Principles (1)
• Object-oriented middleware uses object
references to address server objects
• We need to find a way to get hold of these
object references without assuming physical
locations
• A name is a sequence of character strings
that can be bound to an object reference
• A name binding can be resolved to obtain
the object reference
Slide 7 of 41
© Ingeniørhøjskolen i Århus
Common Principles (2)
• There may be many server objects in a
distributed object system
• Server objects may have several names
• Leads to large number of name bindings
• Name space has to be arranged in a
hierarchy to avoid
– Name clashes
– Poor performance when binding/resolving names
• Hierarchy achieved by naming contexts
Slide 8 of 41
© Ingeniørhøjskolen i Århus
Common Principles: Naming Contexts
Naming
Context
objects
Servant objects
Slide 9 of 41
© Ingeniørhøjskolen i Århus
Common Principles: Composite Names
• Names are composed of possibly more than
one component
• Used to describe traversals across several
naming contexts
• Examples:
("UEFA","England","Premier",
“Manchester United")
("UEFA",“Cup Winners",
“Manchester United")
• HOWEVER: Often only a flat naming structure is used!
Slide 10 of 41
© Ingeniørhøjskolen i Århus
Common Principles: Name Server Behavior
• Name bindings are managed by Name
Servers
• Not every server object needs a name
• Server objects may have several names
(aliases)
• Name servers must store bindings
persistently
• Name servers should be tuned towards
efficiently resolving name bindings
• Name server may itself be distributed
Slide 11 of 41
© Ingeniørhøjskolen i Århus
Stock Quoter Example with NameServer
:Name
Server
Stock
Quoter
client 1
TCP/IP
Network
CPH: Stock
Exchange
Server
Stock
Quoter
client 2
Broker Architecture
Pattern
LON: Stock
Exchange
Server
Slide 12 of 41
© Ingeniørhøjskolen i Århus
CORBA Naming Service
Application
Objects
Domain
Interfaces
CORBA
Facilities
Object Request Broker
CORBA Services
Slide 13 of 41
Naming
Service
© Ingeniørhøjskolen i Århus
CORBA Naming Service
• Supports bindings of names to CORBA object
references
• Consists of two elements:
– Standardized CORBA Interfaces
– Vendor dependent server implementation
• Names are scoped in naming contexts
• Multiple names can be defined for object
references
• Not all object references need names
– Factory objects have names
Slide 14 of 41
© Ingeniørhøjskolen i Århus
Client and Server using a NameServer
:Name
Server
2. objref=
resolve(StockFactoryName)
Stock
Quoter
client 1
1. bind(StockFactoryName,objref)
3. call operations on
StockFactory object
Slide 15 of 41
CPH: Stock
Exchange
Server
© Ingeniørhøjskolen i Århus
CORBA Names
• Names are composed of simple names
• Simple names are value-kind pairs
• Value attribute is used for resolving
names
• Kind attribute is used to provide
information about the role of the object
Slide 16 of 41
© Ingeniørhøjskolen i Århus
IDL Types for Names
module CosNaming {
typedef string Istring;
struct NameComponent {
Istring id;
Istring kind;
};
typedef sequence <NameComponent> Name;
...
};
Slide 17 of 41
© Ingeniørhøjskolen i Århus
Example of a bind() operation
Registration of a simple Name binding at a
Name Server:
CosNaming::Name name;
name.length(1);
name[0].id=
CORBA::string_dup(“MyFactory");
name[0].kind=
CORBA::string_dup("");
// bind name with my_factory servant at NameServer
naming_context->bind(name,my_factory.in());
NameServer
“MyFactory”
MyFactoryImp
ior
Slide 18 of 41
© Ingeniørhøjskolen i Århus
The IDL Interfaces
• Naming Service is specified by two IDL
interfaces:
– NamingContext defines operations to bind
objects to names and resolve name
bindings
– BindingInterator defines operations to
iterate over a set of names defined in a
naming context
Slide 19 of 41
© Ingeniørhøjskolen i Århus
Excerpt of NamingContext Interface
interface NamingContext {
void bind(in Name n,in Object obj)
raises (NotFound, ...);
Object resolve(in Name n)
raises(NotFound,CannotProceed,...);
void unbind(in Name n)
raises(NotFound,CannotProceed...);
void list(in unsigned long how_many,
out BindingList bl,
out BindingIterator bi);
...
};
Slide 20 of 41
© Ingeniørhøjskolen i Århus
Naming Scenario: Binding
Promote Bielefeld to German '1. Liga' and
relegate Frankfurt
root: Naming
Context
c:Client
1L=resolve("UEFA","Germany","1. Liga")
bind("Arm. Bielefeld", bielefeld)
1L:Naming
Context
unbind("Eintr. Frankfurt")
Slide 21 of 41
© Ingeniørhøjskolen i Århus
Bootstrapping Naming Service
• How to get the Root Naming Context?
• ORB Interface provides for initialization:
module CORBA {
interface ORB {
typedef string ObjectId;
typedef sequence <ObjectId> ObjectIdList;
exception InvalidName{};
ObjectIdList list_initial_services();
Object resolve_initial_references(
in ObjectId identifier)
raises(InvalidName);
}
}
Slide 22 of 41
© Ingeniørhøjskolen i Århus
Naming Scenario: Resolving
Print squad of Borussia Dortmund
root: Naming
Context
c:Client
D=resolve("UEFA","Germany")
do=resolve("1. Liga","BVB")
D:Naming
Context
do:Team
print()
Slide 23 of 41
© Ingeniørhøjskolen i Århus
Implementation details
• Consists of two elements:
– Standardized CORBA Interfaces
– Vendor dependent server implementation
• Need a server proces running
• We look at Orbacus & Java J2SE SDK
Slide 24 of 41
© Ingeniørhøjskolen i Århus
Vendor specific Naming Servers
• Orbacus C++
– nameserv OR ntnameservice (native Windows NT service)
• Orbacus Java
– com.ooc.CosNaming.Server
• Java J2SE (prior to 1.4.1)
– tnameserv
• Java J2SE (after 1.4.1)
– orbd (ORB Daemon – which includes a naming server)
– servertool (if you want to use a persistent naming service)
• Example: start orbd –ORBIntialPort 2500
– will start a naming service listening on port 2500
Slide 25 of 41
© Ingeniørhøjskolen i Århus
Example implementation
• HelloWorld using the ORBD
Nameservice
• We need:
– Hello.idl
– HelloServer.java (pos. HelloServant.java)
– HelloClient.java
• Then we need
– the IDLJ tool (with –fall)
– the ORBD daemon – as the nameserver
Slide 26 of 41
© Ingeniørhøjskolen i Århus
Hello.idl
module HelloApp
{
interface Hello
{
string sayHello();
oneway void shutdown();
};
};
Setting up the module, interface & the
methods needed for our example
Slide 27 of 41
© Ingeniørhøjskolen i Århus
import
import
import
import
import
import
HelloApp.*;
org.omg.CosNaming.*;
org.omg.CosNaming.NamingContextPackage.*;
org.omg.CORBA.*;
org.omg.PortableServer.*;
org.omg.PortableServer.POA;
HelloWorld Server
1 of 3
import java.util.Properties;
class HelloImpl extends HelloPOA {
private ORB orb;
public void setORB(ORB orb_val) {
orb = orb_val;
}
// implement sayHello() method
public String sayHello() {
return "\nHello world !!\n";
}
// implement shutdown() method
public void shutdown() {
orb.shutdown(false);
}
}
(continued on next slide) …
The Servant part – our implementation of
the IDL interface -nothing new here
Slide 28 of 41
© Ingeniørhøjskolen i Århus
… (continued from last slide)
HelloWorld Server
public class HelloServer {
public static void main(String args[]) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
2 of 3
// get reference to rootpoa & activate the POAManager
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
// create servant and register it with the ORB
HelloImpl helloImpl = new HelloImpl();
helloImpl.setORB(orb);
// get object reference from the servant
org.omg.CORBA.Object ref = rootpoa.servant_to_reference(helloImpl);
Hello href = HelloHelper.narrow(ref);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
// Use NamingContextExt which is part of the Interoperable
// Naming Service (INS) specification.
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
(continued on next slide) …
The Server part – getting the Nameservice context
Slide 29 of 41
© Ingeniørhøjskolen i Århus
… (continued from last slide)
// bind the Object Reference in Naming
String name = "Hello";
NameComponent path[] = ncRef.to_name( name );
ncRef.rebind(path, href);
HelloWorld Server
3 of 3
System.out.println("HelloServer ready and waiting ...");
// wait for invocations from clients
orb.run();
}
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("HelloServer Exiting ...");
}
}
Binding the HelloApp object reference to the
Nameserver context using the name ”Hello”
Slide 30 of 41
© Ingeniørhøjskolen i Århus
import
import
import
import
HelloApp.*;
org.omg.CosNaming.*;
org.omg.CosNaming.NamingContextPackage.*;
org.omg.CORBA.*;
public class HelloClient
{
static Hello helloImpl;
HelloWorld Client
Using same nameservice to
get the name – now using
public static void main(String args[])
{
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
the resolve operation
// get the root naming context – getting hold of the Name Service (ORBD)
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
// Use NamingContextExt instead of NamingContext. This is
// part of the Interoperable naming Service – meaning that you may com with
// other ORBs
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
// resolve the Object Reference in Naming – using ncRef’s resolve operation
String name = "Hello";
helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));
System.out.println("Obtained a handle on server object: " + helloImpl);
System.out.println(helloImpl.sayHello());
helloImpl.shutdown();
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}}
Slide 31 of 41
© Ingeniørhøjskolen i Århus
Commands used
• To compile static stubs
– idlj -fall Hello.idl
• Compile all Java files
– using javac
• To start-up the Naming Service
– orbd -ORBInitialPort 1050
• Starting up the server:
– start java HelloServer -ORBInitialPort 1050 -ORBInitialHost
localhost
• Starting up the client:
– java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost
Slide 32 of 41
© Ingeniørhøjskolen i Århus
Now with a C++ client
• We want to contact the ORBD
Nameserver from a C++ Client
• Using the Orbacus C++ ORB
• Needed:
– Hello.idl
– HelloCORBAClient.cpp
– Orbbacus idl compiler
Slide 33 of 41
© Ingeniørhøjskolen i Århus
#include <OB/CORBA.h>
#include <OB/CosNaming.h>
#include <Hello.h>
HelloWorld C++ Client
Part 1 of 2
int run(CORBA::ORB_ptr);
int main(int argc, char* argv[])
{
int status = EXIT_SUCCESS;
CORBA::ORB_var orb;
try
{
//Connect to the ORB with supplied arguments
orb = CORBA::ORB_init(argc, argv);
status = run(orb);
}
catch(const CORBA::Exception&){ status = EXIT_FAILURE;
}
if(!CORBA::is_nil(orb))
{
try
{
//remember to inform the orb that we no longer need it
orb -> destroy();
}
catch(const CORBA::Exception&){ status = EXIT_FAILURE; }
}
Do the usual initialization to
connect to ORB
return status;
} … (continued on next) …
Slide 34 of 41
© Ingeniørhøjskolen i Århus
… (continued from last slide)
HelloWorld C++ Client
int run(CORBA::ORB_ptr orb)
{
//OBTAINING REFERENCE TO Name Server
CORBA::Object_var obj = orb->resolve_initial_references("NameService");
Part 2 of 2
//Narrow the Call – we need to turn the Name server obj into a NamingContext
CosNaming::NamingContext_var naming_context;
naming_context = CosNaming::NamingContext::_narrow(obj);
//Now setup the CosNaming name that we want to find (Hello)
CosNaming::Name name;
name.length(1);
name[0].id= CORBA::string_dup("Hello");
name[0].kind= CORBA::string_dup("");
//Using the resolve operation on the root naming_context - get the Hello Servant stub
CORBA::Object_var tempObj = naming_context->resolve(name);
//Call Narrow now to transform the generic CORBA Obj ínto an HelloApp::Hello class
HelloApp::Hello_var hello = HelloApp::Hello::_narrow(tempObj);
cout << hello->sayHello() << endl;
return 0;
}
Using the ORBD nameservice to get the IOR to
the Java Servant
Slide 35 of 41
© Ingeniørhøjskolen i Århus
HelloWorld C++ Client
…
int run(CORBA::ORB_ptr orb)
{ CORBA::Object_var tempObj;
// OBTAINING REFERENCE TO Name Server
CORBA::Object_var obj = orb->resolve_initial_references("NameService");
CosNaming::NamingContext_var naming_context;
try { naming_context = CosNaming::NamingContext::_narrow(obj);}
catch (const CORBA::Exception & e)
{ cerr << " Naming_Context narrow error " << endl; throw 0; }
if (CORBA::is_nil(naming_context))
{ cerr << " No naming context found" << endl; throw 0;}
//Now setup the CosNaming name that we want to find (Hello)
CosNaming::Name name;
name.length(1);
name[0].id= CORBA::string_dup("Hello");
name[0].kind= CORBA::string_dup("");
try
{ //Using the resolve operation on the root naming_context
tempObj = naming_context->resolve(name);
}
catch (const CosNaming::NamingContext::NotFound&)
{ cerr << " Hello Object not found " << endl; }
//Call Narrow using
HelloApp::Hello_var hello = HelloApp::Hello::_narrow(tempObj);
cout << hello->sayHello() << endl;
return 0;
}
Part 2 with error hand.
Same code –
but this time
including error
handling
Slide 36 of 41
© Ingeniørhøjskolen i Århus
Commands used
• To compile static stubs
– \ooc\bin\idl Hello.idl
• Compile all files
• Remember to start-up ORBD and the Java Servant on port 1050
• Starting up the client:
– C:\OOC_DEV\HelloCorbaCppNaming\Debug>HelloCorba -ORBInitRef
NameService=corbaloc::localhost:1050/NameService
Slide 37 of 41
© Ingeniørhøjskolen i Århus
Transient or Persistent Naming
• Transient Naming Service
– object references will not survive termination of
nameserver
– servants need to reregister
– clients need to resolve again
• Persistent Naming Service
– Servants are activated automatically
– Object references are persisted
– Need to register the HelloWorld Server with the
servertool
Slide 38 of 41
© Ingeniørhøjskolen i Århus
Tutorials
• How to implement a Transient Name
binding using ”orbd”
– http://java.sun.com/j2se/1.4.1/docs/guide/id
l/tutorial/GSserver.html
• How to implement a Persistent Name
binding using ”orbd & servertool”
– http://java.sun.com/j2se/1.4.1/docs/guide/id
l/jidlExample2.html
Slide 39 of 41
© Ingeniørhøjskolen i Århus
Limitations of Naming
• Limitation of Naming in all approaches:
Client always has to identify the server
by name
• Inappropriate if client just wants to use a
service at a certain quality but does not
know from who:
– Automatic cinema ticketing
– Video on demand
– Electronic commerce
Slide 40 of 41
© Ingeniørhøjskolen i Århus
CORBA Trading Service
Application
Objects
Domain
Interfaces
CORBA
Facilities
Object Request Broker
CORBA Services
Slide 41 of 41
Object
Trader
© Ingeniørhøjskolen i Århus