Table of Contents

27.5.2 Class RpcClient

You can create an object of the class RpcClient (gb.xml.rpc) in this way:

Dim hRpcClient As RpcClient
hRpcClient = New RpcClient(remoteFunction As RpcFunction)  As "EVENT-NAME"

Note that you must have defined all the properties of a (new) RPC function before you can create a new RPC client object.

27.5.2.1 Properties

The class RpcClient has three properties:

PropertyData typeDescription
ModeIntegerSpecifies or sets the mode for the internally used HTTP server. These three constants can be used as values for the Mode property: Const offLine As Integer = 0, Const httpSync As Integer = 1 and Const httpAsync As Integer = 2.
RpcMethodRpcFunctionThis property can only be read and returns an Rpc function.
URLStringThe URL consists of the server's locator with the subsequent port number on which the XML RPC server listens. Example: localhost: 1088

Table 27.5.2.1.1: Properties of the RpcClient class

27.5.2.2 Methods

The class RpcClient has only these two methods:

MethodReturn typeDescription
Function Call (Data As Variant[])VariantReturns the function value of the method call. Data is a variant array with the arguments passed to the server.
Function EvalReply (sCad As String)VariantReturns the data part of an XML-RPC response. This data can be of the following types: integer, float, string, boolean, array or structure.

Table 27.5.2.2.1: Methods of the class RpcClient

27.5.2.3 Client Project 1

For the first project, an XML-RPC-Server is started as the server for the first project → chapter 27.5.1 RPC-Server, which offers its services on a PC locally on port 1088. Port 1088 was chosen because it is above the first standard ports (>1023) and because it is not officially used or reserved. The following four functions can be called by the client via the RPC interface:

Doc
Figure 27.5.2.3.1: Documentation of the add2integer function

Signatur
Figure 27.5.2.3.2: List of the signature of the add2integer function

Funktionen
Figure 27.5.2.3.3.3: List of implemented functions

Run
Figure 27.5.2.3.3.4: Call of the method add2integer

Pay attention: In both projects, the data type and integer area are not checked for the two summands.

The source code for the XML RPC client is specified in full:

' Gambas class file
 
Private $sRPCURL As String
Private hXMLRPCFunction As RpcFunction
Private hXMLRPCClient As RpcClient
Private $aArguments As Variant[]
Private $sRPCFunctionName As String
 
Public Sub Form_Open()
  FMain.Resizable = False
  FMain.Title = "HTTP-SERVICE * RPC-CLIENT"
  txbSumme.ReadOnly = True
' $sRPCURL = "0.0.0.0:1088"
' $sRPCURL = "192.168.2.101:1088"
' $sRPCURL = "127.0.0.1:1088"
  $sRPCURL = "localhost:1088" ' This Port officially not used or reserved
  $sRPCFunctionName = "add2integer"
End
 
Public Sub btnGetResult_Click()
  GetResult()
End
 
Public Sub btnGetInformation_Click()
  GetDoc()
  GetSignatures()
  GetMethods()
End
Public Sub txbSummand1_Change()
  txbSumme.Clear()
End
 
Public Sub txbSummand2_Change()
  txbSumme.Clear()
End
 
Private Sub GetResult()
 
  Dim iResult As Integer
 
  $aArguments = New Variant[]
  hXMLRPCFunction = New RpcFunction($sRPCFunctionName, [XmlRpc.xInteger, XmlRpc.xInteger], XmlRpc.xInteger)
  hXMLRPCClient = New RpcClient(hXMLRPCFunction) As "hXMLRPCClient"
  hXMLRPCClient.URL = "http://" & $sRPCURL
 
  $aArguments.Add(CInt(txbSummand1.Text))
  $aArguments.Add(CInt(txbSummand2.Text))
 
  Try iResult = hXMLRPCClient.Call($aArguments)
  If Not Error Then
     txbSumme.Text = Str(iResult)
  Else
     Message.Error(("Error-Text: ") & Error.Text & gb.NewLine &
                   ("Error-Location:  ") & Error.Where & gb.NewLine &
                   ("URL: '") & $sRPCURL & " '")
  Endif
 
End
 
Private Sub GetDoc()
 
  Dim sResult As String
 
  $aArguments = New Variant[]
  hXMLRPCFunction = New RpcFunction("system.methodHelp", [XmlRpc.xString], XmlRpc.xString)
  hXMLRPCClient = New RpcClient(hXMLRPCFunction) As "hXMLRPCClient"
  hXMLRPCClient.URL = "http://" & $sRPCURL
 
  $aArguments.Add("add2integer")
 
  Try sResult = hXMLRPCClient.Call($aArguments)
  If Not Error Then
     Message.Title = ("Description of the function")
     Message.Info(("Function: ") & $sRPCFunctionName & "<hr>" & sResult)
  Else
     Message.Error(("Error-Text: ") & Error.Text)
  Endif
 
End
 
Private Sub GetSignatures()
 
  Dim aResult As New RpcArray
  Dim sMessage As String
  Dim iIndex As Integer
 
  $aArguments = New Variant[]
  hXMLRPCFunction = New RpcFunction("system.methodSignature", [XmlRpc.xString], XmlRpc.xArray)
  hXMLRPCClient = New RpcClient(hXMLRPCFunction) As "hXMLRPCClient"
  hXMLRPCClient.URL = "http://" & $sRPCURL
 
  $aArguments.Add("add2integer")
 
  Try aResult = hXMLRPCClient.Call($aArguments)
  If Not Error Then
     Message.Title = ("List of data types")
     sMessage = ("List of data types of the function")
     sMessage &= "<font color='red'>" & $sRPCFunctionName & "</font>"
     sMessage &= "<hr>"
     For iIndex = 1 To aResult.Count
       If iIndex = 1 Then
          sMessage &= ("Function value ") & " = " & aResult[iIndex - 1] & gb.NewLine
       Else
          If iIndex = aResult.Count Then
             sMessage &= "Argument " & Str(iIndex - 1) & " = " & aResult[iIndex - 1]
          Else
             sMessage &= "Argument " & Str(iIndex - 1) & " = " & aResult[iIndex - 1] & gb.NewLine
          Endif
       Endif
     Next
     Message.Info(sMessage)
  Else
     Message.Error(("Error-Text: ") & Error.Text)
  Endif
End
 
Private Sub GetMethods()
 
  Dim aResult As New RpcArray
  Dim sMessage As String
  Dim iIndex As Integer
 
  $aArguments = New Variant[]
  hXMLRPCFunction = New RpcFunction("system.listMethods", [], XmlRpc.xArray)
  hXMLRPCClient = New RpcClient(hXMLRPCFunction) As "hXMLRPCClient"
  hXMLRPCClient.URL = "http://" & $sRPCURL
 
  Try aResult = hXMLRPCClient.Call($aArguments)
  If Not Error Then
     Message.Title = ("List of funktions")
     sMessage = ("Functions on the RPC server") & "<hr>"
     For iIndex = 1 To aResult.Count
       sMessage &= aResult[iIndex - 1] & gb.NewLine
     Next
     Message.Info(sMessage)
  Else
     Message.Error(("Error-Text: ") & Error.Text)
  Endif
 
End

27.5.2.4 Client Project 2

In contrast to the first project, an XML-RPC-Server is only started if the PHP file xmlrpc.server.php is called in the web folder ~/public_html → chapter 27.5.1 and 24.13. Therefore, the source code for Client2 differs from Client1 from Project 1 mainly in the different diction of the URL. As a function, add2integer is implemented as in the first project.

' Gambas class file
 
Private $sRPCURL As String
 
Public Sub Form_Open()
  FMain.Resizable = False
  FMain.Title = "WEBSERVICE * RPC-CLIENT"
  txbSumme.ReadOnly = True
  $sRPCURL = "localhost/~" & System.User.Name &/ "xmlrpc.server.php"
End
 
Public Sub btnGetResult_Click()
  GetResult()
End
 
Public Sub txbSummand1_Change()
  txbSumme.Clear()
End
 
Public Sub txbSummand2_Change()
  txbSumme.Clear()
End
 
Private Sub GetResult()
 
  Dim hXMLRPCClient As RpcClient
  Dim hXMLRPCFunction As RpcFunction
  Dim aArguments As New Variant[]
  Dim iResult As Integer
 
  hXMLRPCFunction = New RpcFunction("add2integer", [XmlRpc.xInteger, XmlRpc.xInteger], XmlRpc.xInteger)
 
  hXMLRPCClient = New RpcClient(hXMLRPCFunction) As "hXMLRPCClient"
  hXMLRPCClient.URL = "http://" & $sRPCURL
 
  aArguments.Add(CInt(txbSummand1.Text))
  aArguments.Add(CInt(txbSummand2.Text))
 
  Try iResult = hXMLRPCClient.Call(aArguments)
  If Not Error Then
     txbSumme.Text = Str(iResult)
  Else
     Message.Error("Error-Text:   " & Error.Text & gb.NewLine &
                   "Error-Location:    " & Error.Where & gb.NewLine &
                   "URL:   '" & $sRPCURL & " '")
  Endif
 
End

The result of the algebraic addition will probably not surprise you:


Figure 27.5.2.4.1: Calling the add2integer function implemented on an RPC server (PHP)

Download