B4J Question b4x-cloudkvs-synchronized

Johan Schoeman

Expert
Licensed User
Longtime User
Ok - so I am a dummy with B4J. I have managed to get this sample project of @Erel going (https://www.b4x.com/android/forum/threads/b4x-cloudkvs-synchronized-key-value-store.63536/). The B4J server is running and I can log some info in the B4J server project when sending data from the B4A client project to the B4J server.

From the B4A client I am sending "123456" (as an Int = 123456)

In the B4J log I see the following when I write the "item.ValueField" to the B4J log (received from the B4A client):
B4X:
Task: getuser_u1, User: u1, Key: 47, IP: 192.168.0.104
Task: additem, User: u1, Key: number, IP: 192.168.0.104
going to add an item
UserField = u1
KeyField = number
Time Tick = 1.537106661093E12
mybyte(0) = 120
mybyte(1) = -100
mybyte(2) = 99
mybyte(3) = 118
mybyte(4) = 120
mybyte(5) = -60
mybyte(6) = -56
mybyte(7) = 0
mybyte(8) = 0
mybyte(9) = 3
mybyte(10) = -68
mybyte(11) = 1
mybyte(12) = 39
Task: getuser_u1, User: u1, Key: 2, IP: 192.168.0.104

This is what Sub AddItem looks like where I have added the code to log the byte values of mybyte():
B4X:
Public Sub AddItem(item As Item)
    lock.WriteLock
    Try
        Dim lastId As String = sql.ExecQuerySingleResult2("SELECT max(id) FROM data WHERE user = ?", Array(item.UserField))
        If lastId = Null Then lastId = 0
        Dim id As Long = lastId + 1
        If item.TimeField < DateTime.Now - 3 * DateTime.TicksPerMinute Then
            Log("checking old record")
            'this is an old record. Maybe there is a newer one...
            Dim rs As ResultSet = sql.ExecQuery2("SELECT time, value FROM data WHERE user = ? AND key = ?", Array(item.UserField, item.KeyField))
            If rs.NextRow Then
                Dim currentTime As Long = rs.GetLong("time")
                If currentTime > item.TimeField Then
                    Log("Old record discarded.")
                    item.ValueField = rs.GetBlob("value")
                    item.TimeField = currentTime
                End If
            End If
            rs.Close
        End If
     
        sql.ExecNonQuery2("INSERT OR REPLACE INTO data VALUES (?, ?, ?, ?, ?)",  _
            Array (item.UserField, item.KeyField, item.ValueField, id, Min(item.TimeField, DateTime.Now)))
        Log("UserField = " & item.UserField)
        Log("KeyField = " & item.KeyField)
        Log("Time Tick = " & Min(item.TimeField, DateTime.Now))
        Dim mybyte() As Byte = item.ValueField
        For i = 0 To mybyte.Length - 1
            Log("mybyte(" & i & ") = " & mybyte(i))
        Next
    Catch
        Log(LastException)
    End Try
    lock.WriteRelease
End Sub

Question is - how do I convert the "mybytes()" that was received by the B4J server into the equivalent "int" 123456 in the B4J server (in Sub AddItem)? What do I need to do with the bytes() of item.ValueField to get the "int" received from the B4A client?
 
Last edited:
Top