Download presentation
Published byScot Carpenter Modified over 8 years ago
1
Distributed Object-Oriented Programming (1) – Socket, RPC, CORBA
SNU iDB Lab. Taewhi Lee May 2nd, 2007
2
Outline Overview – Distributed Programming Socket RPC CORBA References
3
Overview – Distributed Programming [1/2] Paradigm Shift
Distributed Computing SOA DC – client/server are tightly coupled SOA – everything is decoupled Service-Oriented Architecture Web Services Distributed Component Model OOP CBD Object – limited reusability Component – independent service Several components are plugged in to a component architecture system CORBA Component Model EJB Distributed Object Model Structured Programming OOP SP – Complexity of system modeling Difficulty in code change/extension OOP – Natural object modeling Reusability by inheritance Flexibility by polymorphism CORBA RMI Distributed Structural Model RPC Basic Inter-Process Communication Socket
4
Overview – Distributed Programming [2/2] Comparison of the Paradigms
Structured Development Object-Oriented Development Component Based Development Service Oriented Development Granularity Very fine Fine Medium Coarse Contract Defined Private/public Public Published Reusability Low High Coupling Tight Loose Very loose Dependencies Compile-time Run-time Communication Scope Intra-application Inter-application Inter-enterprise [Java Web Services Architecture, McGovern]
5
Outline Overview – Distributed Programming Socket RPC CORBA References
6
Socket [1/8] What is Socket?
Interface for network access Originated from the ARPA network in 1971 Socket = Internet address + port number Only one receiver / Multiple senders per port
7
Socket [2/8] Characteristics of Socket
Endpoint for inter-process communication Message transmission between sockets Socket associated with either UDP or TCP No port sharing Advantage Several points of entry to process Disadvantage Location dependence
8
Socket [3/8] Three Types of Socket
SOCK_DGRAM For datagram communication (UDP) SOCK_STREAM For stream communication (TCP) SOCK_RAW For advanced user Direct access to network layer Security problem may occur (not supported in Java)
9
Socket [4/8] Communication Service Types
UDP (User Datagram Protocol) Connectionless Unreliable delivery – ‘send and pray’ Each message contains source and destination address Each message may be delivered through different path Messages are possibly lost / duplicated / delivered out of order, without telling the user Efficient and easy to implement
10
Socket [5/8] Communication Service Types (cont’d)
TCP (User Datagram Protocol) Connection-based Reliable delivery Establishes data stream connection to ensure reliable, in-sequence delivery Error checking and reporting to both ends Attempts to match speeds (timeouts, buffering) Less efficient, memory and time overhead for error correction
11
Socket [6/8] Socket Programming
Create socket Read & write Close socket Example: TCP socket programming in Java Server Client 1) Create ServerSocket 3) Create Socket 4) Connect 2) Wait accept() from ServerSocket 6) Get InputStream & OutputStream from the socket 5) Client’s socket is returned by accept() 7) Communicate using the InputStream & OutputStream 8) Communicate 6) Get InputStream & OutputStream from the socket 9) Close Socket 7) Communicate using the InputStream & OutputStream 9) Close Socket
12
Socket [7/8] TCP Socket Programming in Java (1/2) Code Example: Echo Server
EchoServer.java import java.net.*; import java.io.*; public class EchoServer { public static void main (String args[]) { try { int serverPort = 10001; ServerSocket listenSocket = new ServerSocket(serverPort); Socket clientSocket = listenSocket.accept(); OutputStream out = sock.getOutputStream(); InputStream in = sock.getInputStream(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(out)); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line = null; while ((line = br.readLine()) != null) { System.out.println(“String from client : “ + line); pw.println(line); pw.flush(); } pw.close(); br.close(); sock.close(); } catch(Exception e) { System.out.println(e) 1) 2), 5) 6) 7), 8) 9)
13
Socket [8/8] TCP Socket Programming in Java (2/2) Code Example: Echo Client
EchoClient.java import java.net.*; import java.io.*; public class EchoClient { public static void main (String args[]) { try { int serverPort = 10001; Socket sock = new ServerSocket(“ ”, serverPort); BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in)); OutputStream out = sock.getOutputStream(); InputStream in = sock.getInputStream(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(out)); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line = null; while ((line = keyboard.readLine()) != null) { if (line.equals(“quit”)) break; pw.println(line); pw.flush(); String echo = br.readLine(); System.out.println(“String from server : “ + echo); } pw.close(); br.close(); sock.close(); } catch(Exception e) { System.out.println(e) 3), 4) 6) 7), 8) 9)
14
Outline Overview – Distributed Programming Socket RPC CORBA References
15
RPC (Remote Procedure Call)
RPC [1/10] What is RPC? RPC (Remote Procedure Call) Birell and Nelson at Xerox PARC, 1980 To support distributed programming in procedural languages To make distributed nature of service transparent to the programmer – ‘RPC like a local call’
16
RPC is a more natural way to communicate!
RPC [2/10] Message RPC Message Flexible But, not natural for programming Programmers have to worry about message formats Messages must be packed and unpacked Messages have to be decoded by server to figure out what is requested Messages are often asynchronous They may require special error handling functions RPC is a more natural way to communicate!
17
RPC [3/10] RPC Architecture
Client Stub Network Network Interface Server Stub Client Server Parameter Network Interface Parameter B’ B A A’ Result Result
18
The stubs send messages to each other for RPC Client-side stub
RPC [4/10] RPC Stubs The stubs send messages to each other for RPC Client-side stub Looks to the client as if it were a callable server procedure Is linked with the client program Server-side stub Looks to the server as if it’s a calling client Is linked with the server program Stub compiler Reads the IDL Produces two stub procedures for each server procedure
19
The packing of procedure parameters into a message packet
RPC [5/10] Marshalling The packing of procedure parameters into a message packet The RPC stubs call type-specific procedures to marshall (or unmarshall) all of the parameters to the call Representation needs to deal with byte ordering issues, strings, alignment, etc. Pentium SPARC
20
RPC [6/10] Interface Definition
IDL (Interface Definition Language) Defines the interface of server program e.g., names, parameters, and types Files interface in Sun XDR(IDL) /* PrintSquare service interface definition in file square.x */ program SQUAREPROG { version SQUAREVERS { int PRINTSQUARE(string) = 1; } = 1; } = 99; # of bytes returned version # program #
21
RPC [7/10] RPC Programming (Sun RPC)
HP RPC IDL HP RPCGEN Interface Compiler Client Stub Server Stub Header File C Compiler Client Process Server HP RPC Runtime Library square.x rpcgen square.x square.h Program Client.c Server.c client server square_svc.c square_clnt.c 21 21
22
RPC [8/10] RPC Programming in C – Sun RPC (1/3) Code Example: PrintSquare Client
Client.c #include <stdio.h> #include <rpc/rpc.h> #include “square.h" main( argc, argv ) int argc ; char *argv[] ; { CLIENT *cl ; int *result ; char *server ; char *message ; if( argc != 3 ) { printf( "Usage : %s host message \n", argv[0] ) ; exit( 1 ) ; } server = argv[1] ; message = argv[2]; cl = clnt_create(server, SQUAREPROG, SQUAREVERS, "tcp" ) ; // get a client handle if( cl == NULL ) { clnt_pcreateerror(server) ; exit( 1 ) ; } // unable to contact server result = printsquare_1(&message, cl) ; if( result == NULL ) { clnt_perror(cl, server) ; exit( 1 ) ; } if( result == 0 ) { printf( "%s : %s could not print your messgae \n", argv[0], server) ; exit( 1 ); } printf( "Returned Message %d !!!!!\n", *result ) ; exit( 0 ) ; }
23
RPC [9/10] RPC Programming in C – Sun RPC (2/3) Code Example: PrintSquare Server
Server.c #include <stdio.h> #include <rpc/rpc.h> #include “square.h“ int *printsquare_1_svc(msg, req) char **msg; struct svc_req *req; { static int result; int x = atoi(*msg); printf("%d Recieved \n", x); result = x * x; printf("Transfer Processing Message.....\n"); return (&result); }
24
RPC [10/10] RPC Programming in C – Sun RPC (3/3) Code Example: Compile & Run
RPCGEN $> rpcgen square.x Generated files – square.h, square_svc.c, square_clnt.c Server program $> cc Server.c square_svc.c –o server Client program $> cc Client.c square_clnt.c –o client
25
Outline Overview – Distributed Programming Socket RPC CORBA References
26
CORBA [1/26] Object Management Architecture (OMA)
A standard architecture for distributed programming Developed by the industry consortium OMG CORBA services Fundamental services e.g., object location (naming) Standard service interfaces (horizontal) Basic information system services e.g., transactions, persistence Standard domain interfaces (vertical) Interfaces for special application domains e.g., medical, telecommunication Application objects – not standardized 26
27
CORBA = Common Object Request Broker Architecture
CORBA [2/26] What is CORBA? CORBA = Common Object Request Broker Architecture ORB (Object Request Broker) standard The core of the OMA The goal of CORBA Location transparency Interoperability – platform-independent, language-independent 27
28
Language-independent
CORBA [3/26] CORBA Standard Interface : IDL (Interface Definition Language) Standard Communication Protocol : IIOP (Internet Inter-ORB Protocol) Language-independent service interfaces IDL Interface C++ IRIX HP-UX SmallTalk Alpha NT Ada Visual C++ NT Java Java OS AIX Solaris MVS COBOL ORB = IIOP + IDL 28
29
RPC CORBA CORBA [4/26] RPC vs. CORBA
When a specific function is called, the data types of parameters are fixed Language dependent CORBA The data types of parameters are more flexible by polymorphism Language independent Dynamic method invocation & dispatching 29
30
CORBA [5/26] CORBA History
1.0~1.2 ( ~ ) CORBA object model (Core 92) IDL & mapping from IDL to the C language APIs for interfacing to the ORB Interfaces for the Basic Object Adapter (BOA) and memory management 2.0~2.3 ( ~ ) General Inter-ORB Protocol / Internet Inter-ORB Protocol Portable Object Adapter (POA) Mapping from IDL to Java, Cobol, Ada, Smalltalk, C++ 2.4~3.0 ( ~ ) Java and Internet integration Asynchronous messaging and QoS control Minimum, fault-tolerant, and real-time CORBA 30
31
CORBA [6/26] CORBA Architecture
31
32
CORBA [7/26] Main CORBA Features
Object Request Broker (ORB) Interface Definition Language (IDL) Inter-ORB protocols Object Adapter Stub & Skeleton Interface Repository Dynamic Invocation & Dispatching 32
33
CORBA [8/26] Object Request Broker
Server Client Object Request Broker (ORB) CORBA object bus The key to location transparency The roles of ORB Routing the client’s request and the server’s reply Management of the Interface Repository a distributed database of IDL definitions Client side services for converting remote object references to and from strings Client side dynamic invocation of remote objects Server side resource management – object activation & deactivation invoke & response ORB 33
34
CORBA [9/26] Interface Definition Language
Interface Definition Language (IDL) Object service specification for interoperability Only to define interface signatures (not implementation) The key to programming language transparency Most OO concepts support (e.g., multiple inheritance) C C++ Java … C C++ Java … IDL IDL Client Server ORB (Object Request Broker) 34
35
CORBA [10/26] Inter-ORB Protocols
General Inter-ORB Protocol (GIOP) Specifies a set of message formats and common data representations for communication between ORBs Internet Inter-ORB Protocol (IIOP) Specifies how GIOP messages are exchanged over a TCP/IP network C++ Smalltalk Ada OLE Java Network 35
36
CORBA [11/26] Object Adapter
Serves as glue between object (servant) & ORB The roles of Object Adapter Object registration & management Inter-operable Object Reference (IOR) generation Request dispatching from server-side ORBs to the servants of target objects Basic Object Adapter (BOA) Vendor-specific implementation low portability Replaced by Portable Object Adapter (POA) 36
37
CORBA [12/26] Stub & Skeleton
Stub (client side) Generated from IDL compiler in client’s language Acts as a local proxy for the remote object Marshalls data to be send & unmarshalls result One client stub instance per instance of remote object Skeleton (server side) Generated from IDL compiler in server’s language Unmarshalls request data & dispatch request to servant & marshalls reply data Used by the POA 37
38
CORBA [13/26] Interface Repository
A distributed database of IDL definitions Used for performing operations on objects whose interface is not known at compile time Knowing interfaces of all objects a priori may be impractical Independently developed components Fast changing parts of system Dynamic manipulation Allows CORBA dynamic invocation 38
39
CORBA [14/26] CORBA Invocation
Static Invocation Interface Using pre-compiled static stub High performance Dynamic Invocation Interface Using dynamic invocation High flexibility The interfaces for dynamic invocation & dispatching Dynamic Invocation Interface (DII) Dynamic Skeleton Interface (DSI) 39
40
CORBA [15/26] Dynamic Invocation Interface
Dynamic Invocation Interface (DII) To invoke remote objects without having stubs Generic run-time invocation A generic stub is used DII steps Search & fetch an interface name & method description from an Interface Repository Construct an argument list & a request Remote method invocation 40
41
CORBA [16/26] Dynamic Skeleton Interface
Dynamic Skeleton Interface (DSI) To dispatch request to remote objects without having skeletons Generic run-time invocation A generic skeleton is used 41
42
CORBA [17/26] CORBA Programming
IDL Client developer Server developer IDL compiler Client “stub” Source IDL compiler Server “skeleton” Source Client Program Source Compile & Link (Java, C++, PL1, COBOL, …) Compile & Link (Java, C++, PL1, COBOL…) Client Server 1. Write IDL interface 2. Compile the IDL interface using an IDL compiler in your programming language 3. Write server/client program using generated skeleton/stub 42
43
CORBA [18/26] CORBA Programming Code Example: IDL Interface
Hello.idl interface Hello { string sayHello(in string name); }; CORBA ORB used in this example C++ – omniORB ( Java – Java SDK 43
44
CORBA [19/26] CORBA Programming – using Naming Service Code Example: Hello Server in C++ (1/3)
HelloServer.cpp [1/3] #include <iostream> #include <cstdlib> #include "Hello.hh" using namespace std; class Hello_i : public POA_Hello { public: inline Hello_i() {} virtual ~Hello_i() {} char* sayHello(const char* name); }; char* Hello_i::sayHello(const char* name) { char* buffer = CORBA::string_alloc(256); if (buffer == NULL) return NULL; sprintf(buffer, "Hello, %s!", name); return buffer; } 44
45
CORBA [20/26] CORBA Programming – using Naming Service Code Example: Hello Server in C++ (2/3)
HelloServer.cpp [2/3] int main(int argc, char **argv) { // creates & initializes the ORB CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); // get RootPOA references CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var poa = PortableServer::POA::_narrow(obj); // activate Hello CORBA object Hello_i* myhello = new Hello_i(); PortableServer::ObjectId_var myhelloid = poa->activate_object(myhello); obj = myhello->_this(); CORBA::String_var sior(orb->object_to_string(obj)); cout << (char*) sior << endl; 45
46
CORBA [21/26] CORBA Programming – using Naming Service Code Example: Hello Server in C++ (3/3)
HelloServer.cpp [3/3] // bind object to name service CORBA::Object_var obj2 = orb->resolve_initial_references("NameService"); CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(obj2); CosNaming::Name name; name.length(1); name[0].id = CORBA::string_dup("Hello"); name[0].kind = CORBA::string_dup("Object"); nc->rebind(name, obj); myhello->_remove_ref(); PortableServer::POAManager_var pman = poa->the_POAManager(); pman->activate(); orb->run(); return 0; } 46
47
CORBA [22/26] CORBA Programming – using Naming Service Code Example: Hello Client in Java
HelloClient.java import org.omg.CosNaming.*; import org.omg.CORBA.*; public class HelloClient { public static void main(String args[]) { try { ORB orb = ORB.init(args, null); org.omg.CORBA.Object namingContextObj = orb.resolve_initial_references("NameService"); NamingContext namingContext = NamingContextHelper.narrow(namingContextObj); NameComponent[] path = { new NameComponent("Hello", "Object") }; org.omg.CORBA.Object helloObj = namingContext.resolve(path); Hello helloRef = HelloHelper.narrow(helloObj); String hello = helloRef.sayHello("World"); System.out.println(hello); } catch (Exception e) { e.printStackTrace(); } 47
48
CORBA [23/26] CORBA Programming – using Naming Service Code Example: Makefile for C++ Server Compile
CC = /usr/bin/g++ CPPFLAGS = -g -c LDFLAGS = -g OMNI_HOME = /usr/local OMNI_INCLUDES = -I$(OMNI_HOME)/include OMNI_LIB_DIR = $(OMNI_HOME)/lib OMNIIDL = $(OMNI_HOME)/bin/omniidl INCLUDES = $(OMNI_INCLUDES) LIBS = -lomniORB4 -lomnithread -lomniDynamic4 OBJECTS = HelloSK.o HelloServer.o all Server: $(OBJECTS) $(CC) $(LDFLAGS) -o HelloServer -L$(OMNI_LIB_DIR) $(OBJECTS) $(LIBS) HelloSK.o: HelloSK.cc Hello.hh $(CC) $(CPPFLAGS) $(INCLUDES) HelloSK.cc HelloServer.o: HelloServer.cpp Hello.hh $(CC) $(CPPFLAGS) $(INCLUDES) HelloServer.cpp HelloSK.cc: Hello.idl $(OMNIIDL) -bcxx Hello.idl clean clean_all: rm -fr *.o rm -fr core rm -fr *.hh rm -fr *SK.cc rm -fr HelloServer 48
49
Compile the server code – make using the makefile
CORBA [24/26] CORBA Programming – using Naming Service Code Example: Compile Compile the server code – make using the makefile Compile the client code 49
50
Run C++ name server (omniNames)
CORBA [25/26] CORBA Programming – using Naming Service Code Example: Run Name Server Run C++ name server (omniNames) 50
51
CORBA [26/26] CORBA Programming – using Naming Service Code Example: Run Server & Client Program
Run the server program Run the client program 51
52
Outline Overview – Distributed Programming Socket RPC CORBA References
52
53
References Sams’ Teach Yourself CORBA in 14 Days
The omniORB version 4.1 User’s Guide 53
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.