210 likes | 304 Views
Windows Application. Same Application Domain. Database. GUI. Classes. Modules. Thr e e-Tier Architecture. GUI. Business Logic. Database. Host. Logic. Different Application Domains. Client and Server. Client Windows Programs (normally) Server
E N D
Windows Application Same Application Domain Database GUI Classes Modules
Three-Tier Architecture GUI Business Logic Database Host Logic Different Application Domains
Client and Server • Client Windows Programs (normally) • Server Programs over network or Internet (normally) Mainframe, Unix, Linux, Gamma: Windows 2005 Server
SOAP (Simple Object Access Protocol) Different Systems Heavyweight Data Conversion Integer, 2 bytes Little-Endian Big-Endian ASCII characters
.NET Remoting Both client and server are Windows Lightweight Accessing objects across application domains
Basic Concepts • Channel TCP and HTTP • Ports • Proxy Used on the client side to make calls into the remote object .NET handles it for us • Register object with the Remoting subsystem Class, Url, Singleton/SingleCall
Remoting Objects • Singleton (wellknown) One object for all requests Used in most cases • SingleCall (wellknown) One object for each request • Activated Available only for the client • Serializable Copied over between machines
Example Server Solution • Project MyServer • Project MyObjects Client Solution • Project Prog7 • Project FormGrid
Project MyObjects ‘ Class library shared by both server and client Public Class Customers Inherits MarshalByRefObject ' The object stays in the server domain Dim ranNum As New Random ... End Class Root Name Space: UWPCS3340
Project MyObjects Public Class Customers ... Public Sub New() Debug("Created") End Sub Protected Overrides Sub Finalize() Debug("Finalized") MyBase.Finalize() End Sub Protected Sub Debug(ByVal buf As String) Console.WriteLine("[" & Me.GetHashCode & "] " & buf) End Sub Public Function CreateCustomer() As String ‘ could be any type Dim CustomerID As String = ranNum.Next(1000).ToString & _ ": from host " & IPAddress.ToString Debug("Created Customer #" & CustomerID) Return CustomerID End Function End Class
Project MyServer ‘ Console application ‘ May need to add references to the following namespace ‘ add reference to System.Runtime.Remoting Imports UWPCS3340 Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Tcp
Project MyServer Module Module1 Dim IPAddress As System.Net.IPAddress Dim theHost As String Dim IPHEntry As System.Net.IPHostEntry Sub Main() theHost = System.Net.Dns.GetHostName IPHEntry = System.Net.Dns.GetHostEntry(theHost) IPAddress = IPHEntry.AddressList(0) ' Registers channel ChannelServices.RegisterChannel(New TcpServerChannel(8081), False) Console.WriteLine("Server " & theHost & " at IP address " & IPAddress.ToString) Console.WriteLine() Dim Url As String = "MyUrl" Console.WriteLine("Registering: " & GetType(Customers).ToString) Console.WriteLine("URI: " & Url) RemotingConfiguration.RegisterWellKnownServiceType( _ GetType(Customers), "MyUrl", WellKnownObjectMode.Singleton) Console.WriteLine("Registered: " & GetType(Customers).ToString) ‘ Reads a line to terminate Console.ReadLine() End Sub End Module
Project MyClient ‘ Windows Application ‘ Add reference to MyObjects.dll Imports UWPCS3340 Dim url As String Dim theCustomer As MyObjects.Customers Private Sub Form1_Load(...) rdoLocal.Checked = True End Sub Private Sub rdoLocal_CheckedChanged(…) rdoLocal.CheckedChanged If rdoLocal.Checked Then ' Port 49341 works! url = "tcp://localhost:49341/MyUrl“ ElseIf rdoGamma.Checked Then ' Port 8081 works too! url = "tcp://137.104.21.2:8081/MyUrl“ ' IP address and DNS name both work! ‘ url = "tcp://gamma.uwplatt.edu:8081/MyUrl“ End If theCustomer = CType(Activator.GetObject(GetType(Customers), url), Customers) End Sub
Project MyClient ‘ Call class method that is a function returning a string Private Sub Button1_Click() Handles Button1.Click Dim theID As String Try theID = theCustomer.CreateCustomer() txtLog.Text &= "The customer ID: " & theID & vbCrLf txtLog.SelectionStart = txtLog.Text.Length - 1 txtLog.ScrollToCaret() Catch ex As Exception MsgBox(ex.Message) Thread.Sleep(5000) Exit Sub End Try End Sub
Project MyObjects Public Class Customers ... Public Function PassParams(ByVal count As Integer, ByVal command As String) As String Dim answer As String answer = Math.Sqrt(Math.Abs(count)).ToString & ";" & command & _ ";IP Address: " & IPAddress.ToString Return answer End Function End Class
Project MyClient ‘ Call class method that is a function with two paramters Private Sub Button2_Click() Handles Button2.Click Dim theParams As String Try theParams = theCustomer.PassParams(2, "Bid") txtLog.Text &= "The customer ID: " & theParams & vbCrLf txtLog.SelectionStart = txtLog.Text.Length - 1 txtLog.ScrollToCaret() Catch ex As Exception MsgBox(ex.Message) Thread.Sleep(5000) Exit Sub End Try End Sub
Project MyObjects Public Class Customers ... ‘ Returns an array Public Function getArray() As String() Dim theArray(3) As String theArray(0) = "SA" theArray(1) = "HK" theArray(2) = "DQ" theArray(3) = "CJ" Return theArray End Function End Class
Project MyObjects <Serializable()> _ Public Structure theMessage Dim singleVaye As Single Dim intValue As Integer Dim strValue As String End Structure Public Class Customers ‘ Returns a structure Public Function getStruct () As theMessage Dim struct As theMessage struct.singleVaye = 90.5 struct.intValue = 45 struct.strValue = "Structure“ Return theArray End Function End Class
Project MyObjects <Serializable()> _ Public Structure theMessage ... Dim theArray() As String ‘ Cannot specify size. Pointer. End Structure Public Class Customers ‘ Returns a structure with an array Public Function getStruct () As theMessage Dim struct As theMessage Dim theArray(3) As String ... theArray(0) = "SA" theArray(1) = "HK" theArray(2) = "DQ" theArray(3) = "CJ" struct.theArray = theArray Return theArray End Function End Class
Project MyObjects Public Class Customers ‘ Reference parameters Public Function RefParams(ByRef x As Integer) As theMessage Dim struct As theMessage Dim theArray(4) As String x += 1 ... theArray(0) = "SA" theArray(1) = "HK" theArray(2) = "DQ" theArray(3) = "CJ" struct.theArray = theArray Return theArray End Function End Class
Project MyObjects Public Class Customers ‘ Returns an object (DataTable) Public Function RefObj() As DataTable Dim theTable As New DataTable ... Return theTable End Function End Class