Systems Prog. & Script. - Heriot Watt Univ 1 Systems Programming & Scripting Lecture 11: The Distributed Object Model
Systems Prog. & Script. - Heriot Watt Univ 2 Introduction The distributed object model is one of executing object- oriented code across a network of machines. It uses the object-based programming model in which objects in different processes communicate through remote method invocation. Central to the distributed object model applications is a middleware component. Middleware is a software component that is based on processes and message passing protocols in a distributed system. The middleware layer uses message exchange protocols such request-reply protocols to provide abstractions on e.g. remote invocations.
Systems Prog. & Script. - Heriot Watt Univ 3 Introduction (cont'd) The middleware layer abstracts over: – Execution site (location transparency), – the communication protocol, – the operating system and – the hardware. See figure in next slide for an illustration of the position of the middleware layer and its relation to other components of a distributed system.
Systems Prog. & Script. - Heriot Watt Univ 4 Middleware Layer Coulouris G., Dollimore J. and Kindberg T., Distributed Systems: Concepts and Design, Pearson Addison and Wesly, 2001 Applications Middleware layers Request reply protocol External data representation Operating System RMI, RPC and events
Systems Prog. & Script. - Heriot Watt Univ 5 Distributed Objects In an object-based system, its state is logically divided into parts with each part associated with a specific object. The distributed object model borrows the basic object-oriented concepts such as: –Object references. –Interfaces (e.g. classes). –Actions, method invocations on objects. –Exceptions. –Garbage collection.
Systems Prog. & Script. - Heriot Watt Univ 6 Distributed Objects (cont'd) A distributed objects system can use a client-server architecture where in this case: –Objects are created and managed by servers. –Clients issue remote method invocations on the servers’ objects to use their services.
Systems Prog. & Script. - Heriot Watt Univ 7 Remote and Local Method Invocations Coulouris G., Dollimore J. and Kindberg T., Distributed Systems: Concepts and Design, Pearson Addison and Wesly, 2001 invocation remote invocation remote local invocation A B C D E F
Systems Prog. & Script. - Heriot Watt Univ 8 Concepts of the Distributed Objects Model Remote Object References: –Extended from the concept of object reference in the object-orientation model where: Any object that can receive a remote method invocation must have a remote object reference. Remote object references can be passed as arguments and results to remote method invocations.
Systems Prog. & Script. - Heriot Watt Univ 9 Concepts of the Distributed Objects Model Remote Interfaces (see figure in next slide): –The class of a remote object implements the methods of its remote interface. –Objects in other processes can only invoke the methods specified in the remote interface. –Remote interfaces do not have constructors, i.e. cannot construct objects directly through interfaces.
Systems Prog. & Script. - Heriot Watt Univ 10 Concepts of the Distributed Objects Model Coulouris G., Dollimore J. and Kindberg T., Distributed Systems: Concepts and Design, Pearson Addison and Wesly, 2001 interface remote m1 m2 m3 m4 m5 m6 Data implementation remote object { of methods
Systems Prog. & Script. - Heriot Watt Univ 11 Concepts of the Distributed Objects Model Garbage Collection: –A typical garbage collector ensures that if there are no references still held to an object, such object is removed and memory is recovered. –A distributed garbage collector not only checks local references to an object but also remote object references from other parts of a distributed system, in order to decide whether to remove an object or not.
Systems Prog. & Script. - Heriot Watt Univ 12 Concepts of the Distributed Objects Model Exceptions: –Remote method invocations can fail due to distribution problem or failure in the execution of the method itself. –Thus, remote method invocations should be able to raise exception on each of the above type of problems occur.
Systems Prog. & Script. - Heriot Watt Univ 13 Implementing RMI A number of objects and modules are required to achieve remote method invocation as shown in the figure on the next slide. This diagram illustrates the operations and components involved when an application-level object A invokes a method in a remote application-level object B. Object A holds a remote object reference for object B. Several modules are required in a remote method invocation including: –Communication modules. –Remote reference modules. –The RMI software including a proxy, a dispatcher and skeleton. The two cooperating communication modules at the client and server implement the request-reply protocol which forms the basis of communication.
Systems Prog. & Script. - Heriot Watt Univ 14 Modules Participating in a Remote Method Invocation Coulouris G., Dollimore J. and Kindberg T., Distributed Systems: Concepts and Design, Pearson Addison and Wesly, 2001 object A object B skeleton Request proxy for B Reply Communication Remote Remote reference Communication module reference module module for B’s class & dispatcher remote client server
Systems Prog. & Script. - Heriot Watt Univ 15 Example: Remote Calculator First we define an interface of the functions that should be provided by the server: public interface ICalc { double Add (double x, double y); double Sub (double x, double y); double Mul (double x, double y); double Div (double x, double y); } Example from “Programming C# 3.0”, Jesse Liberty, O'Reilly. Chapter 19.
Systems Prog. & Script. - Heriot Watt Univ 16 Example: Remote Calculator The server uses remoting libraries: using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http;
Systems Prog. & Script. - Heriot Watt Univ 17 Example: Remote Calculator The main server class: public class Calculator : MarshalByRefObject, ICalc { public Calculator () { Console.WriteLine("Calculator constructor"); } // the four interface functions public double Add (double x, double y) { Console.WriteLine("Add {0} + {1}", x, y); return x+y; }
Systems Prog. & Script. - Heriot Watt Univ 18 Example: Remote Calculator Initialising the server: public class ServerTest { public static void Main() { // create a channel an register HttpChannel chan = new HttpChannel(65102); ChannelServices.RegisterChannel(chan); Type calcType = Type.GetType("CalcServer.Calculator"); // register our well-known type and tell the server // to connect the type to the endpoint "theEndPoint" RemotingConfiguration.RegisterWellKnownServiceType ( calcType, "theEndPoint", WellKnownObjectMode.Singleton );
Systems Prog. & Script. - Heriot Watt Univ 19 Example: Remote Calculator The main client class: public class CalcClient { public static void Main() { // create an http channel HttpChannel chan = new HttpChannel(0); ChannelServices.RegisterChannel(chan); // get my remote object MarshalByRefObject obj = (MarshalByRefObject) RemotingServices.Connect ( typeof(CalcServer.ICalc) // what to get " // where ); try {// cast object to interface CalcServer.ICalc calc = obj as CalcServer.ICalc; // use the interface to call methods double sum = calc.Add(3.0,4.0); Console.WriteLine("3+4 = {0}", sum);
Systems Prog. & Script. - Heriot Watt Univ 20 Example: Remote Calculator Compile the components individually: – gmcs -target:library ICalc.cs – gmcs -pkg:dotnet -reference:ICalc CalcServer.cs – gmcs -pkg:dotnet -reference:ICalc CalcClient.cs Run Server and Client as separate programs: – mono CalcServer.exe – mono CalcClient.exe This will print – 3+4 = 7