Presentation 4: Principles of Object-Oriented Middleware.

38
Presentation 4: Principles of Object-Oriented Middleware

Transcript of Presentation 4: Principles of Object-Oriented Middleware.

Page 1: Presentation 4: Principles of Object-Oriented Middleware.

Presentation 4:Principles of

Object-Oriented Middleware

Page 2: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 2 af 33

Outline

• Students are assumed to be knowledgeable about Computer Networks – including the OSI model and the TCP, UDP protocols. If not … please read ; )

• Types of Middleware – Transaction-Oriented Middleware– Message-Oriented Middleware– Remote Procedure Calls

• Object-Oriented Middleware• Developing with Object-Oriented Middleware

Page 3: Presentation 4: Principles of Object-Oriented Middleware.

Computer Networks- an ultra short introduction

Page 4: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 4 af 33

Physical

Application

Presentation

Session

Transport

Network

Data link

ISO/OSI Reference Model

MiddlewareSpecific

MiddlewareSpecific

Page 5: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 5 af 33

Physical

Application

Presentation

Session

Transport

Network

Data link

Transport Layer

• Level 4 of ISO/OSI reference model.

• Concerned with the transport of information through a network.

• Two facets in UNIX/Windows networks: – TCP and– UDP

• Actually TCP & UDP do not follow the OSI model!– rather as an abstract model

Page 6: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 6 af 33

Transmission Control Protocol (TCP)

• Provides bi-directional stream of bytes between two distributed components.

• Reliable but slow protocol.• Buffering at both sides de-couples computation

speeds.• Best at fairly unreliable Networks (as the Internet)• Very pratical

– As the WWW uses the TCP/IP protocol family– And most Networking Operating Systems as well

Page 7: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 7 af 33

Application

Presentation

Session

Transport

Application

Presentation

Session

TransportInput Stream

Output Stream

Requests

Results

Client Server

TCP for Request Implementation

Page 8: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 8 af 33

User Datagram Protocol (UDP)

• Enables a component to pass a message containing a sequence of bytes to another component.

• Other component is identified within message.• Unreliable but very fast protocol.• Restricted message length.• Queuing at receiver• Best at highly reliable networks (as a LAN)

Page 9: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 9 af 33Result Datagrams

Application

Presentation

Session

Transport

Application

Presentation

Session

Transport

Request Datagrams

Client Server

UDP for Request Implementation

Page 10: Presentation 4: Principles of Object-Oriented Middleware.

Types of Middleware- and why to use …

Page 11: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 11 af 33

Rolling your own distributed object

• We may build directly on the TCP or UDP protocols to build a distributed system

• In Java: quite easy – thanks to the java.net package – which contains a complete network API incl. socket support for TCP/UDP

• BUT: if we want to ensure OO – more complex

• Lets take a look at how this may be done

Page 12: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 12 af 33

The Basic Class to OO Class

public class HelloWorldBasic { public static void main(String[] args) { System.out.println("HelloWorld!"); }}

HelloWorld – canit be more simple?

public class HelloWorldOO { public HelloWorldOO() { } public void sayHelloWorld() { return “HelloWorld"; } public static void main(String[] args) { HelloWorldOO helloWorldOO = new HelloWorldOO(); System.out.println(helloWorldOO.sayHelloWorld()); }}

To OO:

Page 13: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 13 af 33

First Issue: Making the Object Distributed- Method Call vs. Object Request

CalledCalledCalledCalled

StubStub

StubStubStubStub

CallerCaller

CalledCalledCalledCalled

CallerCallerCallerCaller

Transport Layer (e.g. TCP or UDP)Transport Layer (e.g. TCP or UDP)Transport Layer (e.g. TCP or UDP)Transport Layer (e.g. TCP or UDP)

Page 14: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 14 af 33

Server Class – The Skeleton (Stub)

…public class HelloWorldServerThread extends Thread { protected DatagramSocket socket = null; public HelloWorldServerThread() throws IOException { super(); socket = new DatagramSocket(4450); }

public void run() { while (true) { …

if (request.startsWith("sayHelloWorld")) { new String(packet.getData()); // figure out response String response = null; //Call the HelloWorldOO method & place it in response HelloWorldOO helloWorldOO = new HelloWorldOO(); response = helloWorldOO.sayHelloWorld(); buf = response.getBytes(); … socket.send(packet);…

CalledCalledCalledCalled

StubStub

Page 15: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 15 af 33

The Client Stub - HelloWorldProxypublic class HelloWorldProxy { public HelloWorldProxy() { } public String sayHelloWorld() {… // get a datagram socket socket = new DatagramSocket(); // send the request byte[] buf = new byte[256]; buf = methodCalled.getBytes(); InetAddress address = InetAddress.getByName("localhost"); DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4450); socket.send(packet); // get a response packet = new DatagramPacket(buf, buf.length); socket.receive(packet); response = new String(packet.getData()); } catch (Exception e) { e.printStackTrace(); } socket.close(); return response;…

StubStubStubStub

CallerCaller

Page 16: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 16 af 33

Achieving Access Transparency

package chapter1;

public class HelloWorldClient {

public static void main(String[] args) {

System.out.println("Recieved from Server: " +

new HelloWorldProxy().sayHelloWorld());

}

}

… and thus we have achieved access transparencyusing rather simple constucts (the Proxy pattern).

We could have achieved an even higher degree of transparency by using an interface definition – and thus a contract between Client & Server.

StubStubStubStub

CallerCaller

Page 17: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 17 af 33

Direct Use of Network Protocols implies

• Manual mapping of complex request parameters to byte streams or datagrams – is complex & error prone

• Manual resolution of data heterogeneity– encoding differs on NT & UNIX (what is the format of an Integer?)

• Manual identification of components– References must be established manually (host add., port nr. etc.)

• Manual implementation of component activation• No guarantees for type safety• Manual synchronization of interaction between distributed

components– Developers need to handle TCP/UDP output stream/responses

• No quality of service guarantees– Transactions, Security, Concurrency, Scalability, Reliability

Page 18: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 18 af 33

= Reason for Using Middleware

• Layered between Application and OS/Network• Makes distribution transparent• Resolves heterogeneity of

– Hardware– Operating Systems– Networks– Programming Languages

• Provides development and run-time environment for distributed systems

• “Gets you of the hook” – concerning the nasty stuff in network programming

Page 19: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 19 af 33

Forms of Middleware

• Transaction-Oriented– Suited for Database Applications– IBM CICS– BEA Tuxedo– Encina

• Message-Oriented– Suited for Asynchronous (EDI,Batch)– IBM MQSeries– DEC Message Queue– NCR TopEnd– (SOAP)

• RPC Systems– Suited for Access transparency etc– ANSA– Sun ONC– OSF/DCE– (SOAP)

• Object-Oriented– OMG/CORBA– DCOM– Java/RMI– (SOAP)

• First look at RPCs to understand origin of object-oriented middleware

Page 20: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 20 af 33

Remote Procedure Calls

• Enable procedure calls across host boundaries• Call interfaces are defined using an Interface

Definition Language (IDL)• RPC compiler generates presentation and session

layer implementation from IDL• RPC is the “legacy” of oo middleware

Page 21: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 21 af 33

Type Safety

• How can we make sure that – servers are able to perform operations requested by

clients?– actual parameters provided by clients match the

expected parameters of the server?– results provided by the server match the expectations

of client?

• Middleware acts as mediator between client and server to ensure type safety.

• Achieved by interface definition in an agreed language.

Page 22: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 22 af 33

InterfaceDefinition

Facilitating Type Safety

ServerClient

RequestRequest

ReplyReply

Client & Server has a contractClient & Server has a contract

Page 23: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 23 af 33

IDL Example (Unix RPCs)

const NL=64;

struct Player {

struct DoB {int day; int month; int year;}

string name<NL>;

};

program PLAYERPROG {

version PLAYERVERSION {

void PRINT(Player)=0;

int STORE(Player)=1;

Player LOAD(int)=2;

}= 0;

} = 105040;

Page 24: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 24 af 33

Flashback to HelloWorld Example

• How to map complex types?– e.g. returning a struct (in C, C++) or an object (in C++,

Java, C#, etc.)?– How would you do it?– Use 5 minutes to discuss this with your neighbor

• What are the issues?• Which technology might help us?• How would you do this in Java?

Page 25: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 25 af 33

RPC Marshalling and Unmarshalling

• Marshalling: Disassemble data structures into transmittable form

• Unmarshalling: Reassemble the complex data structure

char * marshal() { char * msg; msg=new char[4*(sizeof(int)+1) + strlen(name)+1]; sprintf(msg,"%d %d %d %d %s", dob.day,dob.month,dob.year, strlen(name),name); return(msg);};void unmarshal(char * msg) { int name_len; sscanf(msg,"%d %d %d %d ", &dob.day,&dob.month, &dob.year,&name_len); name = new char[name_len+1]; sscanf(msg,"%d %d %d %d %s", &dob.day,&dob.month, &dob.year,&name_len,name);};

char * marshal() { char * msg; msg=new char[4*(sizeof(int)+1) + strlen(name)+1]; sprintf(msg,"%d %d %d %d %s", dob.day,dob.month,dob.year, strlen(name),name); return(msg);};void unmarshal(char * msg) { int name_len; sscanf(msg,"%d %d %d %d ", &dob.day,&dob.month, &dob.year,&name_len); name = new char[name_len+1]; sscanf(msg,"%d %d %d %d %s", &dob.day,&dob.month, &dob.year,&name_len,name);};

Page 26: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 26 af 33

RPC Stubs

• Creating code for marshalling and unmarshalling is tedious and error-prone.

• Code can be generated fully automatically from interface definition.

• Code is embedded (hidden away – de-coupled) in stubs for client and server.

• Client stub represents server for client, Server stub represents client for server.

• Stubs achieve type safety.• Stubs also perform synchronization.• NOT object-oriented, no support for exceptions or

advanced error handloing

Page 27: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 27 af 33

Synchronization

• Goal: achieve similar synchronization to local method invocation

• Achieved by stubs:– Client stub sends request and waits until server finishes– Server stub waits for requests and calls server when

request arrives

Page 28: Presentation 4: Principles of Object-Oriented Middleware.

Object-Oriented Middleware

Page 29: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 29 af 33

Interface Definition Language

• Every object-oriented middleware has an interface definition language (IDL)

• Beyond RPCs, object-oriented IDLs support object types as parameters, failure handling and inheritance

• Object-oriented middleware provide IDL compilers that create client and server stubs to implement session and presentation layer

Page 30: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 30 af 33

IDL Example

interface Player : Object { typedef struct _Date { short day; short month; short year; } Date; attribute string name; readonly attribute Date DoB;};interface PlayerStore : Object { exception IDNotFound{}; short save (in Player p); Player load(in short id) raises(IDNotFound); void print(in Player p);};

Page 31: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 31 af 33

Presentation Layer Implementation

• Ensuring compatible encoding across platforms• In addition to RPC presentation layer

implementation, object-oriented middleware needs to– define a transport representation for object references– deal with exceptions– need to marshal inherited attributes

Page 32: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 32 af 33

ObjectReferences Hosts Processes Objects

Session Layer Implementation- Mapping of Object references to hosts-Activation & deactivion of objects-Invocation of requested operation-Synchronization of client & server (state)

- Mapping of Object references to hosts-Activation & deactivion of objects-Invocation of requested operation-Synchronization of client & server (state)

Page 33: Presentation 4: Principles of Object-Oriented Middleware.

Developing with Object-Oriented Middleware

Page 34: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 34 af 33

InterfaceDefinition

Design

Server StubGeneration

Client StubGeneration

ServerCoding

ClientCoding

ServerRegistration

Development Steps

SOAP: WSDLSOAP: WSDLJava2WSDLJava2WSDL

WSDL2JAVAWSDL2JAVA

AXISSOAPAXISSOAP

Page 35: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 35 af 33

Facilitating Access Transparency

• Client stubs have the same operations as server objects– If implemented …

• Hence, clients can– make local call to client stub– or local call to server object

without changing the call.

• Middleware can accelerate communication if objects are local by not using the stub– Some Middleware products does this implicitly

Page 36: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 36 af 33

Facilitating Location Transparency

• Based on Object identity• … and this is Object references• Client requests operation from server object identified by object

reference• No information about physical location of server necessary• How to obtain object references?

– Need of a registry – an object adapter– Administrator uses Middleware tool for registering

• COM uses Windows Registry• Java RMI daemon• CORBA object Adapter• SOAP UDDI

– RPC’s – daemon “portmap”• Client contacts one portmap daemon or broadcasts

– Middleware resolves this (in some cases)• SOAP UDDI

Page 37: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 37 af 33

Team.idlTeam.idl

included ingeneratesreads

IDL-Compiler

Teamcl.hh

Teamcl.cc Teamsv.cc

Teamsv.hh

Stub Generation

Team.wsdlTeam.wsdlTeam.javaTeam.java

Team.midlTeam.midl

Page 38: Presentation 4: Principles of Object-Oriented Middleware.

Ingeniørhøjskolen i ÅrhusSlide 38 af 33

C++ Compiler, Linker

Server

Client.ccClient.cc Server.ccServer.cc

C++ Compiler, LinkerC++ Compiler, Linker

Client

Team.idlTeam.idl

included ingeneratesreads

IDL-Compiler

Teamcl.hh

Teamcl.cc Teamsv.cc

Teamsv.hh

Client and Server Implementation