B4J Code Snippet Handle custom packet data

This is a simple way to handle custom packet data without having to use loads of if... then or switch statements.

B4X:
...
Dim packet As String ' data from http socket perhaps or user input
' imaginary data
packet = "read:c:/temp/fred.txt"
Dim code As String = packet.SubString2(0,packet.IndexOf(":"))
Try
asJO(Me).RunMethod("_"& code , Array(packet.SubString(packet.IndexOf(":")+1)))
Catch
Log("no such sub found")
End Try
...

Sub asJO(o As JavaObject) As JavaObject
Return o
End Sub

Sub read(s As String)
' read the file c:/temp/fred.txt here
Log("magic it got here without any if ... then or switch statements")
Log(s)
End Sub

All you have to do is add more sub's like read as you add more 'codes' to the data header.

Hope its useful to someone.
 
Top