1 Advanced Programming Topics - II Objectives: Background Remoting Types of remoting Marshalling Farmatters Channels
2 Background Networking computers share resources and data amongst them in a cohesive manner Today various computer applications use distributed computing in one form or the other File servers, web application servers, print servers Nowadays, the processing power of computers is sufficiently advanced and their use became widespread So, why not harness the power of many personal computers in tandem and using them to process data?
3 Background To harness the computational computer power of several computers, some distributed technologies came into light Common Object Request Broker Architecture CORBA: by Object Management Group (OMG) Remote Method Invocation (RMI): by Sun Microsystems Distributed Component Object Mode ( DCOM): by Microsoft Web services: invented by IBM Global Services
4 Background CORBARMIDCOM Object Implementation Diverse languages can be used as long as the IDL can be mapped to that language. Only Java language can be applied, because the Java Object Serialization usage. Specification is at the binary level. Diverse languages like C++, Java, Delphi, and even COBOL can be used. Client/Server Interface The client stub is called a stub, and the server side is skeleton. The client stub is called a proxy, and the server side is called stub. Object Location and Activation The ORB is used to locate an object, and Object Adapter is used for activation. The object location and activation are on Java Virtual Machine (JVM) Use the Service Control Manager (SCM) to locate and activate an object. On-demand Activation A client can bind to a naming or a trader service to activate a server object by obtaining a server reference. A client can do a lookup() on the remote server object’s URL name to obtain the object reference. A client can do a CoCreateInstance() to activate a server object.
5 DCOM versus Web Services Both DCOM and Web Services are used to achieve distributed application development In DCOM the object will be created both on the client and server side The client side object is called as Proxy and the server side object is called as Stub Once these two are created the proxy and stub interact with each other in sending and receiving the data remotely
6 DCOM versus Web Services In Web Services the object will be created only on the client side When a method is invoked from the client the proxy creates a request SOAP message and opens the http connection Then the request SOAP message is sent to the server through the http connection The Server then receives the request and converts it into the back end object and processes it The server then creates a response message and sends the response back to the client using the http connection The proxy receives it and converts into the required object and sends it to the client for further usage
7 Remoting When developing a distributed application, the underlying framework should provide transparent programming model to the application developer Proxies play major role in this area The proxy object acts as a 'dummy' of the remote object and forward all the method calls to the remote object instance These proxy objects are created at client end The client is unaware of the actual location of the remote object The client simply calls the methods on the proxy object which are then forwarded to the actual object
8 Types of Remoting: MBV There are two ways an object in one application domain can be made available to another application domain The first option involves serializing the object, transport it to the other domain using streams The object is then de-serialized and used at the other side This version is called Marshall by value (MBV) MBV should be considered only if the object does not depend on any data in its original domain
9 Types of Remoting (MBR) The second type of remoting is called Marshall by reference (MBR) In this case, the client communicates with the remotable object through a proxy, but the object remains in its application domain To the client, the proxy appears as if it is the actual object However, for each call made to the proxy, the proxy passes the call to the remote object using a communication channel, obtain a result from the remote object and pass same to the client
10 Remoting: Formatters The communication between the client and remote object involves method parameters and return values This data must be serialized before it is sent across the network The serialization process creates a persistent copy of the data as a sequence of bytes The process of converting these bytes back into the original data structure is called de-serialization Formatters perform the serialization and de- serialization processes
11 Remoting: Formatters Formatters are available in following namespaces: System.Runtime.Serialization.Formatters.Binary System.Runtime.Serialization.Formatters.SOAP As the names suggest Binary formatter deals with data in binary format while SOAP formatter deals with data in XML format
12 Remoting: Channels To communicate with remote object we need a path through which the data transfer can take place This path is called as Channel. There are two types of channels: TCP channels : these channels use TCP for communication TCP channels typically carry data in binary form i.e. they use Binary formatters HTTP Channels : These channels use HTTP for communication. They typically carry SOAP messages i.e. they use SOAP formatters
13 Remoting: Channels TCP channels with Binary formatters are suitable where speed and performance is important HTTP Channels with SOAP formatters are suitable for platform independent or over the web communication The channels are available in following namespaces: System.Runtime.Remoting.Channels.TCP System.Runtime.Remoting.Channels.HTTP
14 How Does Remoting Work? When the client application communicates with the remote object following steps are involved: At Server Side A Channel is established at a specific port which will listen to all client requests Remote object registers itself with remoting framework and declares its presence over the network At Client Site Client establishes a channel at a specific port to talk with remote object Client creates an instance of remote object by calling Getobject or CreateInstance or new method Client gets the proxy for the remote object Client calls remote object methods on the proxy
15 Remoting Server This is an application that a client connects to in order to gain access to the method of the remotable class Such an application must do three things as follows: Create a communication channel: This can be done with either the TcpChannel class of System.Runtime.Remoting.Channels.Tcp namespace, or using the HttpChannel class of System.Runtime.Remoting.Channels.Http namespace Register the communication channel created with the remoting channel services
16 Remoting Server This is done by passing the channel to the static method, RegisterChannel, of the ChannelServices class, which is in the System.Runtime.Remoting.Channels namespace Register the remotable class with the remoting server This is done by using the static method, RegisterWellKnownServiceType of the RemotingConfiguration class, of the System.Runtime.Remoting namespace This method takes three arguments: The type of the remotable class A URI identifier for the class, and Object creation mode
17 Remoting Server Possible modes are: SingleCall and Singleton, both of which are fields of the WellKnownObjectMode class SingleCall means a separate instance of the remotable class will be created for each call to the remotable class Singleton mode means, a single instance will be used for different calls for all clients Singleton is useful if you wish to retain the state across different calls
18 Remoting Client This is the application that is used to access the methods of the remotable class through the remoting server Here again, there are three things that the client class must do to communicate with the remotable class: Create a channel. This must be of the same type as that of the remoting server Register the communication channel created with the remoting channel services Creating an instance of the proxy class. The proxy class is like an alias to the remotable class, so all calls to the remotable class are made through the proxy class Here we have two options: We use the RegisterWellKnownClientType of the RemotingConfiguration class. This takes the type of the remote class and its URI as arguments Alternatively, we can use the getObject method of the Activator class, which is in the System namespace
19 Example: RemotingServer.cs using System; using System.IO; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.HTTP; namespace RemotingServer { public class MyServer : MarshalByRefObject { public MyServer() { } public string GetString(string text) { Console.WriteLine(text); return "Hello " + text; } } public class StartServer { public static void Main(string[] s) { HTTPChannel channel = new HTTPChannel(8000); ChannelServices.RegisterChannel(channel); RemotingServices.RegisterWellKnownType ("remotingserver","RemotingServer.MyServer", "ServerClassURI",WellKnownObjectMode.Singleton) ; Console.WriteLine("Press enter to stop server..."); Console.ReadLine(); } } }
20 Example: RemotingClient.cs using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.HTTP; using RemotingServer; public class RemotingClient { public static void Main(string[] s) { HTTPChannel channel = new HTTPChannel(8001); ChannelServices.RegisterChannel(channel); MyServer server = (MyServer)Activator.GetObject(typeof(MyServer), " Console.WriteLine(server.GetString("Bipin")); } }