Android Question Is it possible to implement transparent proxies in B4A?

Lee Gillie CCP

Active Member
Licensed User
I have developed remote method call library for Windows. Basically you write a service contract interface, and create a generic consumer or provider of the interface. Using transparent proxy the consumer method call is then serialized and sent to the provider (transparently behind the scenes). The provider then deserializes and uses transparent proxy to delivery those method calls to an instance of the contract interface. And of course the method response value (or exception) results in another movement of data over the wire. For the consumer the contract interface object instance is used in the same way as a local class/object to call methods and receive events.

If anyone has published library work similar to this already I have not seen it. I suspect this will be a deep dive into reflection and B4A/Java interop to get to those important proxy invokes. I don't even know if this is possible via B4A classes. I like the "transparent" aspect as it hides so many messy implementation details from the user and hope to be able to leverage that with B4A if possible.

Seeking any wisdom or pointers before digging in here. Forget it, not possible is a valid response.
 

Lee Gillie CCP

Active Member
Licensed User
This may help to clarify my question. This is MS VB.net code to establish a client, transparent proxy on the contract interface, and then to show how easy it is to call remote methods via the transparent proxy...

B4X:
' REMOTING CLIENT EXAMPLE CODE
Public WithEvents DataServiceSubscriber As ServiceSubscriber(Of INMServices) = Nothing
Public WithEvents DataService As INMServices = Nothing

' This object manages proxy and communications
DataServiceSubscriber = New ServiceSubscriber(Of INMServices)

' This creates a transparent proxy for the contract interface
DataService = DirectCast(DataServiceSubscriber.CreateProxy(), INMServices)

' With the above in place connect the subscriber to the server
If DataServiceSubscriber.Connect(srvr, NMDataServiceTCPPort) Then
    DataServiceSubscriber.WaitForConnectionDisconnection(10000)
End If

' Now (as though it were a real class instance) invoke a method on the proxy,
' which actually is executed on the server...  i.e. Register is a method
' defined in the INMServices contract interface. There is no local
' implementation. Rather the invocation is intercepted and put on the wire
' to the server.
DataService.Register(My.Computer.Name, Environment.UserName, ClientClassification.NMManager, My.Application.Info.AssemblyName)
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
This is MS VB.net code to establish a server. This still uses transparent proxy approach to deliver events, but we have a real class instance of the contract interface here.

B4X:
' REMOTING SERVER EXAMPLE

' Likewise for server we have a proxy manager, and a proxy instance
Public WithEvents Publisher As ServicePublisher(Of INMServices)
Public WithEvents NMDataServices As NMServices = Nothing

' Getting the server up and running is straight forward
Publisher = New ServicePublisher(Of INMServices)(NMDataServices)
NMDataServices.Publisher = Publisher
Publisher.AddListenerEndPoint("localhost", NMDataServiceTCPPort)
Publisher.StartListening()


' This instance of the contract interface provides server side implementations
Public Class NMServices
    Implements INMServices
        .
        . Server side methods implemented here
        .

    Public Sub Register( _
    ByVal computer As String, _
    ByVal userName As String, _
    ByVal clientClass As NMServicesKnownLayer.ClientClassification, _
    ByVal application As String) _
    Implements NMServicesKnownLayer.INMServices.Register
              .
              .
              .
    ' This event will be broadcast to all connected clients
    ' It is raised just as you would in a VB class.
    ' This silly example lets all other clients see registrations
    RaiseEvent RegisteredUsersChanged(GetRegisteredUsers())
    ' On the client side, the event handler is coded just as you
    ' would for a local object. No application wiring is needed.

    End Sub

End Class

Proxy magic mostly provided by .NET Framework Activator class.
 
Upvote 0
Top