540 likes | 758 Views
Distributed Object-Oriented Programming (1) – Socket, RPC, CORBA. SNU iDB Lab. Taewhi Lee. May 2nd, 2007. Outline. Overview – Distributed Programming Socket RPC CORBA References. Overview – Distributed Programming [1/2] Paradigm Shift. Service-Oriented Architecture.
E N D
Distributed Object-Oriented Programming(1) – Socket, RPC, CORBA SNU iDB Lab. Taewhi Lee May 2nd, 2007
Outline • Overview – Distributed Programming • Socket • RPC • CORBA • References
Overview – Distributed Programming [1/2]Paradigm Shift Service-Oriented Architecture Distributed Computing SOA DC – client/server are tightly coupled SOA – everything is decoupled 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 CORBA RMI Structured Programming OOP SP – Complexity of system modeling Difficulty in code change/extension OOP – Natural object modeling Reusability by inheritance Flexibility by polymorphism Distributed Structural Model RPC Basic Inter-Process Communication Socket
Overview – Distributed Programming [2/2]Comparison of the Paradigms [Java Web Services Architecture, McGovern]
Outline • Overview – Distributed Programming • Socket • RPC • CORBA • References
Socket [1/8]What is Socket? • Socket • Interface for network access • Originated from the ARPA network in 1971 Socket = Internet address + port number Only one receiver / Multiple senders per port
Socket [2/8]Characteristics of Socket • Characteristics • 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
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)
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
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
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 theInputStream & OutputStream 8) Communicate 6) Get InputStream & OutputStream from the socket 9) Close Socket 7) Communicate using theInputStream & OutputStream 9) Close Socket
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)
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(“127.0.0.1”, 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)
Outline • Overview – Distributed Programming • Socket • RPC • CORBA • References
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’
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!
RPC [3/10]RPC Architecture Client Stub Network Server Stub Network Interface Client Server Parameter Parameter Network Interface B’ B A A’ Result Result
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
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
RPC [6/10]Interface Definition • IDL (Interface Definition Language) • Defines the interface of server programe.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 #
RPC [7/10]RPC Programming (Sun RPC) HP RPC IDL square.x HP RPCGEN Interface Compiler rpcgensquare.x Header File square.h square_clnt.c square_svc.c Server Stub Client Stub Client Program Server Program Client.c Server.c HP RPC Runtime Library C Compiler C Compiler Client Process Server Process client server 21
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 ) ; }
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); }
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
Outline • Overview – Distributed Programming • Socket • RPC • CORBA • References
CORBA [1/26]Object Management Architecture (OMA) A standard architecture for distributed programming Developed by the industry consortium OMG • CORBA services • Fundamental servicese.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
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
CORBA [3/26]CORBA Standard Interface : IDL (Interface Definition Language) Standard Communication Protocol : IIOP (Internet Inter-ORB Protocol) Solaris C++ MVS COBOL HP-UX SmallTalk Alpha NT Ada ORB = IIOP + IDL Language-independent service interfaces Visual C++ NT Java Java OS C++ AIX C++ IRIX IDL Interface
CORBA [4/26]RPC vs. CORBA RPC 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
CORBA [5/26]CORBA History 1.0~1.2 (1991.10 ~ ) 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 (1996.8 ~ ) 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 (2000.10 ~ ) Java and Internet integration Asynchronous messaging and QoS control Minimum, fault-tolerant, and real-time CORBA
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
CORBA [8/26]Object Request Broker 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 Server Client invoke & response ORB
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)
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 Network C++ Smalltalk Ada OLE Java . . . .
CORBA [11/26]Object Adapter 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)
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
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
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)
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
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
IDL compiler IDL compiler Client Program Source Server “skeleton” Source Client “stub” Source Client Server CORBA [17/26]CORBA Programming IDL Server developer Client developer Compile & Link (Java, C++, PL1, COBOL…) Compile & Link (Java, C++, PL1, COBOL, …) 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
CORBA [18/26]CORBA ProgrammingCode Example: IDL Interface • Hello.idl interface Hello { string sayHello(in string name); }; • CORBA ORB used in this example • C++ – omniORB (http://omniorb.sourceforge.net) • Java – Java SDK
CORBA [19/26]CORBA Programming – using Naming ServiceCode 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; }
CORBA [20/26]CORBA Programming – using Naming ServiceCode 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;
CORBA [21/26]CORBA Programming – using Naming ServiceCode 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; }
CORBA [22/26]CORBA Programming – using Naming ServiceCode 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(); } } }
CORBA [23/26]CORBA Programming – using Naming ServiceCode Example: Makefile for C++ Server Compile • Makefile 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
CORBA [24/26]CORBA Programming – using Naming ServiceCode Example: Compile • Compile the server code – make using the makefile • Compile the client code
CORBA [25/26]CORBA Programming – using Naming ServiceCode Example: Run Name Server • Run C++ name server (omniNames)