Acquire Remote Object from Remote Object Factory : Remote Factories « Network Remote « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Network Remote » Remote FactoriesScreenshots 
Acquire Remote Object from Remote Object Factory

///////////////////////////////////general.vb
// Compile: vbc /target:library  general.vb
Imports System

Public Interface IRemoteObject
    Sub setValue(ByVal newval As Integer)
    Function getValue() As Integer
End Interface

Public Interface IRemoteFactory
    Overloads Function getNewInstance() As IRemoteObject
    Overloads Function getNewInstance(ByVal initvalue As IntegerAs IRemoteObject
End Interface


Public Class MyRemoteObject
    Inherits MarshalByRefObject
    Implements IRemoteObject

    Private myvalue As Integer

    Public Sub New()
    End Sub 'New

    Public Sub New(ByVal startvalue As Integer)
        myvalue = startvalue
    End Sub

    Public Sub setValue(ByVal newval As Integer_
    Implements IRemoteObject.setValue
        myvalue = newval
    End Sub

    Public Function getValue() As Integer _
    Implements IRemoteObject.getValue
        Return myvalue
    End Function

End Class

Public Class MyRemoteFactory
    Inherits MarshalByRefObject
    Implements IRemoteFactory

    Public Sub New()
    End Sub

    Public Function getNewInstance() As IRemoteObject _
    Implements IRemoteFactory.getNewInstance
        Return New MyRemoteObject()
    End Function

    Public Function getNewInstance(ByVal initvalue As IntegerAs IRemoteObject _
    Implements IRemoteFactory.getNewInstance
        Return New MyRemoteObject(initvalue)
    End Function


End Class
///////////////////////////////////test.vb
// Compile: vbc /t:exe /r:general.dll test.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels

Module Client

    Sub Main()
        Dim channel As New HttpChannel()
        ChannelServices.RegisterChannel(channel,false)

        Console.WriteLine("Client.Main(): Creating factory")
        Dim fact As IRemoteFactory = CType(Activator.GetObject_
            GetType(IRemoteFactory), _
            "http://localhost:1234/factory.soap"), _
            IRemoteFactory)

        Console.WriteLine("Client.Main(): Acquiring first object from factory")
        Dim obj1 As IRemoteObject = fact.getNewInstance()
        obj1.setValue(42)

        Console.WriteLine("Client.Main(): Acquiring second object from factory")
        Dim obj2 As IRemoteObject = fact.getNewInstance(47)

        Console.WriteLine("Obj1.getValue(): {0}", obj1.getValue())
        Console.WriteLine("Obj2.getValue(): {0}", obj2.getValue())

        Console.ReadLine()
    End Sub

End Module

///////////////////////////////////server.vb
// vbc /target:exe  /r:general.dll server.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels




Module ServerStartup

    Sub Main()

        Dim chnl As New HttpChannel(1234)
        ChannelServices.RegisterChannel(chnl,false)

        RemotingConfiguration.RegisterWellKnownServiceType_
            GetType(MyRemoteFactory), _
            "factory.soap", _
            WellKnownObjectMode.Singleton)

        Console.WriteLine("ServerStartup.Main(): Server started")

        
        Console.ReadLine()

    End Sub
End Module



           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.