#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
#End Region
Sub Process_Globals
'the ports should be opened on firewall for both incoming & outgoing ports
'port of the broker
Private PORT As Int = 5000
'port for rdc
Private RDC_PORT As Int = 4001
'server IP addres
Private SERVER_IP As String = "XXX.XXX.129.17"
Type DBResult (Tag As Object, Columns As Map, Rows As List)
Type DBCommand (Name As String, Parameters() As Object)
Private const rdcLink As String = $"http://${SERVER_IP}:${RDC_PORT}/rdc"$
Private fx As JFX
Private MainForm As Form
Private client As Socket
Private SERVER As ServerSocket
Private astream As AsyncStreams
Private txtLogs As TextArea
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
Form1.Title = $"HTTP Test - ${PORT}/${RDC_PORT}"$
txtLogs.Text = ""
ListenForClients
'**********************************************
End Sub
Sub ListenForClients
Log("ListenForClients started...")
SERVER.Initialize(PORT, "server")
Log("Server Initialized: " & SERVER.GetMyIP & "-" & PORT)
Do While True
SERVER.Listen
Log("Server Listening for connections...")
Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
NewConnection(NewSocket)
End If
Sleep(1000) 'prevent a busy loop if there is an error
Loop
End Sub
Sub NewConnection (NewSocket As Socket)
CloseExistingConnection
client = NewSocket
astream.Initialize(client.InputStream, client.OutputStream, "astream")
Log("Data Received...")
End Sub
Sub CloseExistingConnection
If astream.IsInitialized Then
astream.Close
End If
If client.IsInitialized And client.Connected Then
client.Close
End If
End Sub
Sub astream_Terminated
astream_Error
End Sub
Sub astream_Error
End Sub
Sub AStream_NewData (Buffer() As Byte)
Dim myDeviceID As String
Dim MyStr As String
Dim OAOD As String = Chr(13) & Chr(10)
Dim resp As String = "HTTP/1.1 200 OK" & OAOD & "Content-Length: 0" & OAOD & OAOD
'
MyStr = BytesToString(Buffer,0,Buffer.Length,"UTF-8")
MyStr = MyStr.trim
astream.Write(resp.GetBytes("UTF8"))
astream.SendAllAndClose
Log("Got: " & MyStr)
Log("Len Received: " & MyStr.Length)
'***************************
If MyStr.IndexOf("TRACK") > -1 Then
Log("Got Track")
myDeviceID = MyStr.SubString(MyStr.Length - 5)
Log ("Tracking ID: " & myDeviceID)
getTracking(myDeviceID)
End If
'***************************
End Sub
Sub getTracking(myDeviceID As String)
'sql.get_tracking=SELECT tracking FROM devices WHERE deviceid=?
Dim myTracking As String
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("get_tracking", Array(myDeviceID))
Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
If j.Success Then
req.HandleJobAsync(j, "req")
Wait For (req) req_Result(res As DBResult)
For Each row() As Object In res.Rows
myTracking = row(res.Columns.Get("tracking"))
Log("Tracking: " & myTracking)
Next
Else
Log("ERROR: " & j.ErrorMessage)
End If
j.Release
End Sub
Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
Dim cmd As DBCommand
cmd.Initialize
cmd.Name = Name
If Parameters <> Null Then cmd.Parameters = Parameters
Return cmd
End Sub
Sub CreateRequest As DBRequestManager
Dim req As DBRequestManager
req.Initialize(Me, rdcLink)
Return req
End Sub
Private Sub btnClear_Click
txtLogs.Text = ""
End Sub