CSIS0402 System Architecture CSIS0402 System Architecture Distributed ComputingDistributed Computing
- - Middleware Middleware Technology (2)Technology (2)
K.P. Chow
University of Hong Kong
Object MiddlewareObject Middleware
Calling an operation of an object that resides in another system Object in another system is accessed through the object reference Processing steps:
1. Get a reference to an object
2. Call an operation of the object E.g.
AccountRef = AccountSet.GetAccount(012345678);
AccountRef.GetNameAndBalance(…);
// print account’s detail
…
// debit the account
AccrountRef.Debit(100);
RPC:
GetNameAndBalance(012345678, Name, Balance);
// print account’s detail
Debit(012345678,100);
How to obtain an object reference?How to obtain an object reference?
Possible approaches: A special object reference is returned to the client when it first
connected to the middleware The client calls a special “naming” service (directory) with a name,
and the directory returns the object reference The client requests that a new object of a certain class is created
(through the object factory) and the new object reference is returned An operation on one object returns a reference to another object
Object Middleware Compilation and InterpretationObject Middleware Compilation and Interpretation
Similar to RPC: Operation is
declared in IDL
IDL generates stubs and skeletons
Stub
Skeleton
Interface Definition Language (IDL)
Client Server
Interface Repository
Stub
Skeleton
Compiled Runtime Environment
Interpreted Runtime Environment
Unlike RPC: Operations can be
called through an interpretative interface
Why use object middleware instead of RPC?Why use object middleware instead of RPC?
Object middleware fits naturally with object-oriented languages: data and logic are in the object
Object middleware is more flexible:– Interface is de-linked from the program, e.g. an interface can be
implemented by many servers
– Updates to interface can be incremented, i.e. a server object can keep both the old and the new interfaces simultaneously
Force the programmer writing the client to think about state in the server
Object Middleware TechnologiesObject Middleware Technologies
CORBA– Object can last for ages
– One can convert a CORBA object reference to a string and send the string to somebody else, the recipient can write a program that convert the string back to an object reference and calls the object
COM/DCOM– COM object is pinned to a memory location and belongs to an active
object
– Object is automatically deleted when reference count to the object is 0 Java RMI (Remote Method Invocation)
CORBACORBA
A standard, not a product; developed by OMG (Object Management Group, a consortium of major software vendors and user organizations)
Common Object Request Broker Architecture CORBA model:
IIOP
ORB Core
IDL Stub
D I O
I
Client
ORB Core
SIS DSO I
Object Implementation
Object Adapter
DI: Dynamic InvocationOI: ORB InterfaceSIS: Static IDL SkeletonDS: Dynamic IDL Skeleton
CORBA StructureCORBA Structure
CORBA defines GIOP (General Inter-ORB Protocol), IIOP is GIOP over TCP/IP
CORBA client programming:– Initialization
– Obtain object reference
– Call operations on object reference ORB is also an object Many services, naming, event notification, transaction, … 2 kinds of interfaces:
– Compiled interface: IDL is used to create the IDL stub for client and Static IDL skeleton for the server, stub and skeleton are used to marshal the parameters
– Interpreted interface: client use Interface Repository to find out about the interface, server uses Dynamic Skeleton for interpreted interface
Compiled InterfaceCompiled Interface// Interface definition
interface ThisOrThatServer {
string doThis(in string what);
string doThat(in string what);
};
public interface ThisOrThatServer extends org.omg.CORBA.Object {
String doThis(String what);
String doThat(String what);
}
public classThisOrThatServerImpl extends _ThisOrThatServerImplBase {
public ThisOrThatServerImpl() { }
public String doThis(String what) { return doSomething(“this”, what); }
public String doThat(String what) { return doSomething(“that”, what); }
private String doSomething(String todo, String what) { … }
}
idltojava ThisOrThatServer
// client implementation
objRef = object referemce of the target object;
objRef.doThis(“what”);
Java RMIJava RMI
RMI is a java-specific version of a CORBA framework An object on one system can call a method in an object somewhere
else on the network
Stub
Skeleton
ClientServer
Client method calls into local stub
Skeleton calls server method
RMI characteristics: Objects are sent between client and server across the network Type of the object arguments are sent with the data If class doesn’t exist on the receiving side, the class object is also
loaded over the network (mobile code)
COM – Component Object ModelCOM – Component Object Model
From Microsoft, a standard for components Designed to support OLE: Object Linking and Embedding
Technology A COM component: a separate code file that has one or more object
interfaces (not source, and can be written in any language) To start a COM component, the client needs to supply:
– Class ID (CLSID): 128-bit integer, used to search the registry for the component’s code file and initiates it if needed
– Interface ID (IID): 128 integer, use to identify the required interface
– Both can be generated by the system
– When an object is created, an interface pointer is returned of type IID Details to be discussed
What is COM?What is COM? A distributed object architecture Objects can be written in any language, adhere to a few rules, the
objects will be able to communicate with client applications that want to use them and with one another across process and network boundaries
Object exports its functionalities by interfaces, which are groups of logically related functions
Example in C++class ICalc {
public: virtual Add()=0;virutal Subtract()=0;
};class IFinancial {
public: virtual GetMortgagePayment()=0;virtual GetPrimeRate()=0;
};class CalcObject: public ICalc, public IFinancial { };
InterfaceInterface
Act as a contract between client and object: all methods declared in the interface must be defined
Clients can think about objects as black boxes that support one or many interfaces
Server object can implement the functions any way it chooses
ComCalc
IUnknown
ICalc
Add()
Subtract()
IFinancialGetMortgagePayment()
GetPrimeRate()
Origin of COM InterfacesOrigin of COM Interfaces
RPC– Proxy: an entity that stands in for something else and appears to
a client to be an actual instance of that something else
– Stub: proxy’s packaged information is unpackaged by an entity called stub on the server
RPC IDL:– IDL compiler (MIDL.EXE) generates C code from an .IDL file,
generates code defines a proxy and stub
– E.g.
[ uuid (C2557720-CA46-1067-B31C-00DD010662DA),
]
interface Calc {
int Add([in] int x, [in] int y);
int Subtract([in] int x, [in] int y);
}
Client
proxy
COM object
stub
COM and RPCCOM and RPC
Today’s COM is built on RPC Additional features:
– Support for languages other than C
– Support for objects, not just flat, ANSI-C style functions
– Capability to automatically find and run server when client requests it MS implementation: type library (TLB file)
– a universal, binary header file
– a compiled, tokenized form of an IDL file
– contains all the function prototypes, interface definitions, coclasses
– can be parsed and read by an application or development environment using Win32 functions
– used for marshalling, i.e. construct the proxy and stubs necessary for communication
– does not need to compiled into the client and server: COM read the type library at runtime
COM object definition in C++ (1)COM object definition in C++ (1)
import “oaidl.idl”;
import “ocidl.idl”;
[
object,
uuid(638094E5-758F-11d1-8366-0000E83B6EF3),
dual, …
]
interface ICalc : IUnknown
{
[id(1), helpstring(“method Add”)] HRESULT Add([in] int x,[in] int y, [out,retval] int *r);
[id(2), helpstring(“method Divide”)] HRESULT Divide([in] int x,
[in] int y, [out,retval] int *r);
};
COM Object definition in C++ (2)COM Object definition in C++ (2)
[ uuid(638094E0-758F-11d1-8366-0000E83B6EF3),
version(1.0),
helpstring(“calcsdk 1.0 Type Library”)
]
library COMCALCLib
{
importlib(“stdole32.tlb”);
importlib(“stdole2.tlb”);
[ uuid(638094E0-758F-11d1-8366-0000E83B6EF3),
helpstring(“Calc Class”)
]
coclass CalcSDK
{ [default] interface ICalc; };
};
C++ class implements ICalc interface
C++ ImplementationC++ Implementation
#include “comcalc.h”
class CalcSDK: public ICalc
{
public:
STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
STDMETHODIMP _(ULONG) AddRef(void);
STDMETHODIMP _(ULONG) Release(void);
STDMETHOD(Add)(int x, int y, int *r)
{ *r = x + y; return S_OK; }
STDMETHOD(Subtract)(int x, int y, int *r)
{ *r = x - y; return S_OK; }
ComCalc() { };
};
What is IUnknown?What is IUnknown? All interfaces must inherit from IUnknown IUnknown contains 3 methods:
– QueryInterface(): to enable clients to get one or many interfaces from objects
– AddRef(): to control lifetime of the objects
– Release() Example:
const GUID CLSID_Calc = {0x638094e0, 0x758f, 0x11d1,
{0x83,0x66,0x00,0x00,0xe8,0x3b,0x6e,0xf3} };
const GUID IID_ICalc = {0x638094e5, 0x758f, 0x11d1,
{0x83,0x66,0x00,0x00,0xe8,0x3b,0x6e,0xf3} };
const GUID IID_IFinancial = { … };
ICalc *pICalc;
IFInancial *PIFin;
What is IUnknown? (cont)What is IUnknown? (cont)// create an instance of the object whose GUID is CLSID_Calc and get me
// the interface of the object whose GUID is IID_ICalc and put that
// interface in variable pICalc
CoCreateInstance(CLSID_Calc,NULL,CLSCTX_ALL,IID_ICalc,
(void **)&pICalc);
int result = pICalc->Add(1,2);
pICalc->QueryInterface(IID_IFinancial, (void **)&pIFin);
float rate = pIFin->GetPrimeRate();
pICalc->Release();
pIFin->Release();
ComCalc
IUnknown
ICalc
Add()
Subtract()
IFinancialGetMortgagePayment()
GetPrimeRate()
Dynamic Link Libraries (DLL)Dynamic Link Libraries (DLL)
In early Windows, DLLs were groupings of functions that could be loaded into an application when the application wanted them, and discarded when no longer needed, allowed EXEs to be smaller
A binary standard: sources can be written in any language and compiled into DLLs, which can be interoperable: clean up the call stack, list exported functions, …
COM DLL:– Other names for COM DLL: OCX, ActiveX Component, ActiveX
Control, ActiveX server
– Repositories for COM objects (COM object is the actual implementation of a specific coclass in a type library)
– A DLL can contain many objects
– A DLL also has a .TLB file (type library): describes the objects and interfaces
COM DLLCOM DLL
Example: 2 coclasses and its type library
coclass1
ISomeInterface
coclass2
IOtherInterface
IUnknown
IUnknown
Resource Section
Type Library
Registry and Self-RegistrationRegistry and Self-Registration
Type library and component are 2 separate things, how can COM associate the two?
Self-registration: COM object needs to know how to put the entries in the Registry, which can then be used later
How COM DLL put the entry in the registry? (DLLs are loaded and acted upon by a host process (EXE))
2 approaches: manually or use utility regsvr32.exe, e.g.– regsvr32.exe comcalc.dll
COM and Virtual TableCOM and Virtual Table
Virtual Table:– Also called vtable, used in C++ for dynamic binding
– An array of pointers to virtual function
– Used in COM to implement interface
Address of ICalc vtable
Address IFinancial vtable
COMCalc: ICalc, IFinancial
QueryInterface()
AddRef()
Release()
Add ()
Subtract ()QueryInterface()
AddRef()
Release()
MortgagePayment ()
GetPrimeRate ()
ICalc: IUnknown
IFinancial: IUnknown Virtual Table
COM Interface
Putting it all togetherPutting it all together RPC, DLL, Type Libraries, vtables
Client
coclass1
ISomeInterface
IUnknown
COM DLL
RPC
vtable
Type Library
Registry regsvr32.exe xyz.dll
Service Control Manager
request COM service
COM Object CreationCOM Object Creation
Call CoCreateInstance with arguments: CLSID and IID, and others:1. Map the given CLSID to an actual component that contains the requested
class, and the mapping is stored in the registry
2. Create a COM object that is instantiated from the COM class
3. Return an interface of the request type IID The registry stores which servers are available and which classes they
support: 3 types of servers– In-process server: objects that live in the client’s process, started by
loading and linking a DLL
– Local server: objects on the same machine, but in a separate process, started by a separate executable (EXE)
– Remote server: objects on a different machine, started by loading and starting the required server on the remote machine through the service control manager (SCM)
Compound Document and OLECompound Document and OLE
OLE is Microsoft’s compound document standard OLE is a single document-centric paradigm
– Some objects are created and existed in OLE setting only, e.g. ActiveX object
– Some standard-alone applications can be integrated with OLE OLE can be viewed as a collection of predefined COM interfaces OLE compound document can be divided into following parts:
– Document server: provides some content model and the capabilities to display and manipulate that content
– Document container: has no native content, but can accept parts provided by arbitrary document servers
– Some document containers are also document servers, e.g. MS Word, Excel, …
OLE – Object Linking and EmbeddingOLE – Object Linking and Embedding
Object embedding:– In-place editing in MS Office applications
– In-place activation: the container has to hand off part of the container’s screen state to the server of an embedded part, need to change all menus, toolbars, and others
Object linking:– A container stores a reference to the linked object
– The linked object advises the container of changes
OLE Containers and ServersOLE Containers and Servers In-process server: container and server are in the same process Local out-of-process server: need a “representative” of the server
object, executing in the container process, called in-process handler Remote server: an out-of-process server on a remote machine
IOleClientSite
IAdviseSink
Container
Client site object
IOleObject IOleObject
IDataObject IDataObject
IPersistentStorage PersistentStorage
In-process handler Local server
Others Others
ControlsControls
Visual Basic controls (VBX):– Simple and fixed model: controls are embedded into forms
– A form binds the embedded controls together and allows the attachment of scripts that enable the controls to interact
– Entire applications can be assembled by composing controls into forms OLE controls (OCX):
– Migrate the VBX concept to the OLE containers
– OCXs are COM objects
– To qualify as an OLE control, a COM object has to implement a large number of interfaces, e.g. IOleInPlaceActiveObject, IOleInPLaceObject, …
ActiveX controls:– Revised specification for OCX: large number of features and interactions,
and all of them are optional
– ActiveX controls are COM objects supported by a special server
– Has to be implemented by a self-registering server
Building Application out of OCX Building Application out of OCX ControlsControls
Spell checker
Page layout
Print services
Screen layout
Compound document
Word processing data
Spreadsheet data
Arith. evaluation
Screen layout
Numeric I/O
Table
Elementary word processor
made from OCX controls
Elementary spreadsheet from OCX
controls
Controls can be reused
ContainerServer
Transactional Component MiddlewareTransactional Component Middleware
Also called COMWare and Object Transaction Managers (OTM) Covers following technologies
– Microsoft Transaction Server (MTS), part of COM+
– Enterprise Java Beans EJB
– CORBA-based standard for transactional component middleware from OMG
Components:– Runtime code with an object interface that can be taken out of one context
and run in another
– E.g. COM code files and EJB
COM Component vs. Enterprise Java BeansCOM Component vs. Enterprise Java Beans
COM component– Run in any Windows operating systems
– DCOM to call remote COM objects
– Can call database systems and perform transactions Enterprise Java Beans:
– Run in any Java Virtual Machine
– RMI to call remote EJB
– Can call database systems and perform transactions
Transactional Component Middleware StructureTransactional Component Middleware Structure
Makes transaction processing systems easier to implement and more scalable based on object oriented framework
Container– Likes the the transaction monitor for traditional systems
– Provides many useful features, e.g. transaction support and resource pooling
– Provides standard facilities instead of component implementer to write the “ugly” system calls
Components can be deployed with different settings to behave in different ways, e.g. change the security environment
Transactional EnvironmentTransactional Environment
Typical transactional environments (supported by COM+ and EJB):– Requires a transaction: either the client is in transaction state (within the scope
of a transaction) or COM+/EJB will start a new transaction when the component’s object is created
– Requires a new transaction: COM+/EJB will start a new transaction when the component’s object is created, even if the caller is in transaction state
– Supports transactions: the client may or may not be in transaction state, the component’s object does not care
– Does not support transactions: the object will not run in transaction state, even if the client is in transaction state
Develop components to support transaction handling:– Component developer defines the transactional environment supported by the
component
– Container defines the transaction start and end points (based on the transactional environment)
– Coding: program code needs to do only commit or abort
– Attribute setting: deployer sets the attributes for transactional environment
COM+ ModelCOM+ Model
User-Written COM Object
IMyInterface
Life Cycle Control
Object Wrapper
IClassFactory
IObjectContext
Context Object
COM+ Container
IMyInterface
IClassFactory
Client DCOM
COM+ Explorer:• For administrator to provide
information to the container, e.g. transactional requirement
Client reference:• Does not point to user-written
component, only interacts with the object wrapper
Object WrapperObject Wrapper
A barrier between the client and the component Every operation call is intercepted: can provide additional security
checking Performance improvement by saving resource:
– Object wrapper can deactivate component objects without the client knowing about the deactivation
– When client next tries to use the object, the wrapper reactivates the object again
– E.g. the application support thousands of end users, there will be thousands of clients, which will needs many thousands of objects, object deactivation will make the memory utilization more efficient
Connection pooling (managed by container):– Pool of database connection shared by objects
– When an object is deactivated, the connection will be returned to the pool
– When an object is activated, it reuses an inactive connection from the pool
EJB ModelEJB Model
User-Written Enterprise Java
Bean
MyInterface
EJBHome
Object Wrapper
MyHomeInterface
Java.ejbSessionContext
Context Object
EJB Container
MyInterface
MyHomeInterface
Client RMI or IIOP
• Administrator uses XML to provide information to the container, e.g. transactional requirement
Client reference:• Does not point to user-written
component, only interacts with the object wrapper
COM+COM+ Extension of the COM model by merging MTS into core COM
implementation Underlying protocol: DCOM SOAP (Simple Object Access Protocol): an alternative network
protocol which uses XML, may allow a client running on a non-Microsoft platform to call a COM+ server
Object deactivation:– Same as elimination, i.e. object is recreated every time
– Can be deactivated after every operation: same as traditional transaction monitor, data object attributes are reset to their initial state at the beginning of an operation
– Can be at the end of a transaction: allows the client to make several calls to the same object, e.g. search for a record in the DB in a call, update the DB in another call
Does not support temporary data on a session basis: in traditional transaction monitor, data area is provided to store temporary data which can be used by multiple transactions from the same terminal
EJBEJB
A standard, not a product EJB products by BEA, IBM, Oracle, … Network connection to EJB is RMI and IIOP 3 kinds of components: Entity beans, session beans and message beans Entity beans
– An entity bean is designed to represent a row in the database: a proxy object
– Persistence: can be container managed or bean managed Session beans
– A session bean implements a business process that is performed on the behalf of the client in a single session: an agent object
– Can be stateless or stateful Message-driven beans: use the Java Message Services to support
asynchronous interactions between clients and beans
More details in the EJB tutorial