Android Question HTTP Push by Polling

Discussion in 'Android Questions' started by lip, Jun 10, 2015.

  1. lip

    lip Member Licensed 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:-

    Code:
    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).

    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. lip

    lip Member Licensed User

    The HTTP Jobs are created by Sub PollForCommands (Included in my code above?)
     
  4. lip

    lip Member Licensed User

    PolForCommands is only called in two places: (a) When FirstTime = True in the Activity Module and (b) by JobDone.
     
  5. Erel

    Erel Administrator Staff Member Licensed 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?
     
  6. lip

    lip Member Licensed User

    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?
     
  7. lip

    lip Member Licensed User

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

    Erel Administrator Staff Member Licensed User

    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.
     
Loading...