I am using HTTPUtils2 in a service module (CommandEngine) to get commands from a server.
The process is started in an Activity Modules as follows:-
PollForCommands (see below) has a timout of 120 seconds.
The Server looks for Commands in a database. As soon as it finds one it sends the first command. If no commands are found after 110 seconds then it returns "TimedOut".
In JobDone, any commands are processed, then (whether it it succeeded or failed) PollForCommands is called again.
This generally works well. However, it appears that the call is occasionally duplicated so that I have more than one active request. This eventually slows the tablet down until either the tablet or the internet connection becomes unusably slow.
My internet connection is fast and the Commands are typically only 25 bytes. They respond very quickly and, so far, I have only tested one command at a time so it is not a stream of commands causing the problem.
Question: How can I make sure that I always have one and only one request of this type running? (NB I will in the future need to allow other HTTPJobs to be running at the same time).
The process is started in an Activity Modules as follows:-
B4X:
Sub Activity_Create(FirstTime as Boolean)
If FirstTime then
CallSub(CommandEngine,"PollForCommands")
...
end sub
PollForCommands (see below) has a timout of 120 seconds.
The Server looks for Commands in a database. As soon as it finds one it sends the first command. If no commands are found after 110 seconds then it returns "TimedOut".
In JobDone, any commands are processed, then (whether it it succeeded or failed) PollForCommands is called again.
This generally works well. However, it appears that the call is occasionally duplicated so that I have more than one active request. This eventually slows the tablet down until either the tablet or the internet connection becomes unusably slow.
My internet connection is fast and the Commands are typically only 25 bytes. They respond very quickly and, so far, I have only tested one command at a time so it is not a stream of commands causing the problem.
Question: How can I make sure that I always have one and only one request of this type running? (NB I will in the future need to allow other HTTPJobs to be running at the same time).
B4X:
Sub PollForCommands
Dim Job1 As HttpJob
Job1.Initialize("JobCommand",Me)
Job1.PostString(Main.HTTPServer & "command.aspx","Postcode=" & Main.ActivePostcode & "&UserEmail=" & Main.ActiveUserEmail)
Job1.GetRequest.Timeout = 120000
End Sub
Sub JobDone (Job As HttpJob)
Select Job.JobName
Case "JobCommand"
If Job.Success Then
'Check if Timed Out
Dim TimedOut As Boolean
TimedOut = False
If Job.GetString.Length >= 8 Then
If Subs.Left(Job.GetString,8) = "TimedOut" Then
TimedOut = True
End If
End If
If TimedOut Then
CallSub2(PhoneMonitor,"ShowCommands","TimedOut Server...")
Else
CallSub2(PhoneMonitor,"ShowCommands",Job.GetString)
If AutoSendCommands Then CallSub(PhoneMonitor,"btn_RunCommand_Click")
End If
Else
CallSub2(PhoneMonitor,"ShowCommands","TimedOut HTTP...")
End If
PollForCommands
Case Else
ToastMessageShow("Command Engine: Unknown HTTPJob: " & Job.JobName, False)
End Select
Job.Release
End Sub