Saturday, 1 October 2011

Describe in detail Basic of SAO architecture of Remoting?


Remoting has at least three sections :
  Common Interface which will be shared between them.
  Server.
  Client.
Here is an explanation of the process:
  First important section is the common interface between Server and Client. For sample project interface
is very simple with only two methods : SetValue and GetValue. 

Public Interface InterFaceRemoting
Sub SetValue(ByVal value As String)
Function GetValue() As String
End Interface
  Second important section is the server. In this sample server is using HTTP channel and the server object is singleton.

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Imports InterFaceRemoting
Public Class RemotingServer Inherits MarshalByRefObject
Implements InterFaceRemoting.InterFaceRemoting
Private strData As String
Public Function GetValue() As String Implements
InterFaceRemoting.InterFaceRemoting.GetValue
Return strData
End Function
Sub New()
strData = "testing.."
End Sub
Public Sub SetValue(ByVal value As String) Implements
InterFaceRemoting.InterFaceRemoting.SetValue
strData = value
End Sub
End Class
Module ModuleRemotingStartUp
Sub Main()
Dim objHttpChannel As HttpChannel
Console.WriteLine("Server Started...")
objHttpChannel = New HttpChannel(1234)
ChannelServices.RegisterChannel(objHttpChannel)
RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingServer),
"RemoteObject", WellKnownObjectMode.Singleton)
Console.WriteLine("Server registered and listening waiting for clients...")
Console.ReadLine()
End Sub
End Module

In the code above, Channel object is created and registered. Server then hosts the object so that client can connect to it. This is the time when we specify what mode the server object will be created i.e. Singleton or SingleCall. This is done by the following below given code. Note in sample we are hosting the server object in singleton mode that means that the same object
will be shared between all clients. Also note the server object is implementing "InterFaceRemoting" and inheriting from "MarshalByRefObject".
RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemotingServer),
"RemoteObject", WellKnownObjectMode.Singleton)
  In the last section the client will connect to this hosted remoting object.

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Imports InterFaceRemoting
Module ModuleStartClient
Sub Main()
Dim objHttpChannel As New HttpChannel
Dim objRemoting As InterFaceRemoting.InterFaceRemoting
ChannelServices.RegisterChannel(objHttpChannel)
objRemoting =
CType(Activator.GetObject(GetType(InterFaceRemoting.InterFaceRemoting),
"http://localhost:1234/RemoteObject"),
InterFaceRemoting.InterFaceRemoting)
Console.WriteLine("Referenced the main object.... Now displaying Data")
Console.WriteLine("Value on server :- " & objRemoting.GetValue.ToString())
Console.WriteLine("Press enter to Terminate")
Console.ReadLine()
End Sub
End Module
  Finally you need to run server and then client.

No comments:

Post a Comment