Android Question HTTP Push by Polling

lip

Active Member
Licensed User
Longtime User
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:-

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
 

lip

Active Member
Licensed User
Longtime User
Add some log messages to JobDone and PollForCommands subs so it will be easier to know what's happening.

JobDone should only fire once for each job.

How do you see that there are multiple requests?

The ShowCommands function shows the reply and datetime.now. Initially it shows a TimeOutServer response every 110 seconds. After a few minutes or sometimes hours, a second set of replies starts showing up. The second set has a random offset from the initial set, but they are at 110 second intervals. The server logs also show the second request. Could it be that my Activity Module is being dumped by Android then restarting, with FirstTime true again?
 
Upvote 0

lip

Active Member
Licensed User
Longtime User
Add some log messages to JobDone and PollForCommands subs so it will be easier to know what's happening.

JobDone should only fire once for each job.

How do you see that there are multiple requests?

I have tried watching it in Debug with log messages but the debug tends to loose its connection before the problem happens.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Could it be that my Activity Module is being dumped by Android then restarting, with FirstTime true again?
No. The process can be killed and later recreated. However in such case the previous requests will not affect the new process.

You can run your app in release mode in USB debug mode to see the logs.
 
Upvote 0
Top