B4J Question Connection Check Timer

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am just trying to work out what the best way in doing the following..

I have many devices that send a UDP message to my B4J app every 30 seconds.

I want to create a connection check feature so I can send a email if it fails to receive 6 messages. (3 minutes).

For example:
A message is sent every 30 seconds.
If it fails to get 6 message in a row to send a email to say connection lost. (I already have the email side of things working as well as the UDP side of things.)

I am logging each connection in a map. ConnectionID and last connection time.

I was thinking of creating a timer to check each any every ConnectionID for the last command time every 10-20 seconds, and comparing the time difference and if it's more than 3 minutes send a email.

Or, the other way I was thinking of using was the CallSubUtils, and calling the sub with the ConnectionID to check the connection in 3 minutes time to see if it has changed.

There could be 200-300 devices that send UDP messages to my B4J app. So using CallSubUtils might run a lot of timers in the background? And using a timer to check all connections at the one time might slow things down? I am not sure if that is a good idea or not.

Anyone have any suggestions on how to check if there has been a connection within 3 minutes ?
 

udg

Expert
Licensed User
Longtime User
One way could be:
a scheduled check every 3 minutes and a global var where you record last time checked (i.e last time the scheduler fired). When a message arrives you simply set True value in a map DeviceID/MessageOk (or in Array where the index is the DeviceID or whatever data structure you find appropriate for your app).
When the scheduler fires, you take note of the current time for the "last time check" var, the missing devices list and finally clear the list/array/map, starting the process again.

udg
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
And using a timer to check all connections at the one time might slow things down?
Lets test it...

B4X:
Sub AppStart (Args() As String)
   Dim m As Map
   m.Initialize
   For i = 1 To 500
     m.Put(i, DateTime.Now - Rnd(0, 5 * DateTime.TicksPerMinute))     
   Next
   For i = 1 To 5
     Dim n As Long = DateTime.Now
     Dim expired As List
     expired.Initialize
     For Each k As Int In m.Keys
       Dim v As Long = m.Get(k)
       If v < DateTime.Now - 3 * DateTime.TicksPerMinute Then expired.Add(k)
     Next
     Log("Took: " & (DateTime.Now - n))
   Next
End Sub

The output is:
Took: 2
Took: 1
Took: 0
Took: 0
Took: 0

So it takes about 1ms (or less) to find it. You will never see any slowdown because of this.
 
Upvote 0
Top