Windows Application

27
Same Application Domain Windows Application GUI Classes Database 1

description

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 - PowerPoint PPT Presentation

Transcript of Windows Application

Same Application Domain

Windows Application

GUI Classes Database

1

Three-Tier Architecture

GUI(Client)

BusinessLogic

DatabaseHost Logic

Different Application Domains2

Client and Server

• Client

Windows Programs

(Web programs)• Server

Programs over network or Internet

Mainframe, Unix, Linux,

Xray: Windows Server 2008

R2 Enterprise

3

Different Systems

Integer, 2 bytes

Little-Endian

Big-Endian

High Byte Low ByteLow Byte High Byte

4

SOAP (Simple Object Access Protocol)

Heavyweight

Data Conversion

Integer, 2 bytes

Little-Endian

Big-Endian

ASCII characters

High Byte Low ByteLow Byte High Byte

5

.NET Remoting

Both client and server are WindowsLightweightAccessing objects across application domains

6

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

7

• 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

Remoting Objects

8

Example

Solution DBServer

• Project DBHost

• Project DBServerClasses

Solution DBClient

• Project Prog8GUI

• Project FormGrid

9

Creating Program Folder First

UserName_Prog8

10

Solution DBServer

Two Projects

• Project DBHost

Console project

• Project DBServerClasses

Class Library project

11

Project DBServerClasses

‘ Class library shared by both server and clientPublic Class Customers

Inherits MarshalByRefObject ' The object stays in the server domain

Dim ranNum As New Random

...

End Class

Root Name Space: UWPCS3340Assembly Name: DBServerClasses

12

Project DBServerClasses

‘ The class has one functionPublic Class Customers

Public Function CreateCustomer() As String ‘ could be any type

Dim CustomerID As String = ranNum.Next(1000).ToString

Return CustomerID

End FunctionEnd Class

13

Project DBHost

• Console application

• Default unit: Module1

• Use class instead of module

• Assembly name is DBHost.exe.

• Add reference to System.Runtime.Remoting

• Add reference to UWPCS3340

14

Project DBHost

Imports UWPCS3340

Imports System.Runtime.Remoting

Imports System.Runtime.Remoting.Channels

Imports System.Runtime.Remoting.Channels.Tcp

Imports System.Net

15

Public Class Prog8Host Private Shared IPAddress As System.Net.IPAddress

Private Shared IPHEntry As System.Net.IPHostEntry

Public Shared Sub Main() IPHEntry = System.Net.Dns.GetHostEntry(Environment.MachineName)

For Each LocalIP As System.Net.IPAddress In IPHEntry.AddressList

If LocalIP.AddressFamily = Sockets.AddressFamily.InterNetwork Then

IPAddress = LocalIP

Exit For

End If

Next

' Registers channel

ChannelServices.RegisterChannel(New TcpServerChannel(8081), False)

Console.WriteLine("Server " & " at IP address " & IPAddress.ToString)

Console.WriteLine()

Dim Url As String = “Prog8" Console.WriteLine("Registering: " & GetType(Customers).ToString)

Console.WriteLine("URI: " & Url)

RemotingConfiguration.RegisterWellKnownServiceType( _

GetType(Customers), Url, WellKnownObjectMode.Singleton)

Console.WriteLine("Registered: " & GetType(Customers).ToString)

‘ Reads a line to terminate

Console.ReadLine()

End Sub

End Class16

Solution DBClient

17

Project Prog8GUI

• Windows Forms Application

• Add reference to DBServerClasses.dll

• Form – Two radio buttons: Local & Remote– Two buttons: Get ID & Exit– Textbox: txtLog

18

Project Prog8GUIImports UWPCS3340

Dim url As StringDim theCustomer As Customers

Private Sub Form1_Load(...) rdoLocal.Checked = TrueEnd Sub

Private Sub rdoLocal_CheckedChanged(…) rdoLocal.CheckedChanged

If rdoLocal.Checked Then

' Port 49341 may works?

url = "tcp://localhost:8081/Prog8“

Else

' Port 8081 works too!

url = "tcp://" + txtIP.Text + ":8081/Prog8“

' IP address and DNS name both work!

‘ url = "tcp://xray.uwplatt.edu:8081/Prog8“

End If

theCustomer = CType(Activator.GetObject(GetType(Customers), url), Customers)

End Sub

19

Project Prog8GUI

‘ Call class method that is a function returning a stringPrivate 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 TryEnd Sub

20

Project DBServerClasses

‘ Add a functionPublic Class Customers

Public Function PassParams(ByVal count As Integer,

ByVal command As String) As Double

Dim answer As Double

If command = “sqrt” Then

answer = Math.Sqrt(Math.Abs(count)).ToString

ElseIf command = “cube” Then

answer = Math.pow(count, 3).ToString

Else

answer = “invalid command”

End If

Return answer

End Function

End Class

21

Project Prog8GUI

‘ Call class method that is a function with two paramters

Private Sub Button2_Click() Handles Button2.Click

Dim theParams As Double

Try

theParams = theCustomer.PassParams(txtValue.Text, txtCommand.Text)

txtLog.Text &= "The answer: " & 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

22

Project DBServerClasses

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

23

Project DBServerClasses<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 struct

End FunctionEnd Class

24

Project DBServerClasses<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 struct

End FunctionEnd Class

25

Project DBServerClassesPublic 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 struct

End Function

End Class

26

Project DBServerClasses

Public Class Customers

‘ Returns an object (DataTable)

Public Function RefObj() As DataTable

Dim theTable As New DataTable

...

Return theTable

End Function

End Class

27