iOS Question [B4X] SNTP class to B4i

gregchao

Member
Licensed User
Longtime User
Erel mentioned that it would be an easy port to B4i. I am not sure how to do it. Can someone help me?
 

gregchao

Member
Licensed User
Longtime User
Here is the link:
 
Upvote 0

gregchao

Member
Licensed User
Longtime User
How do I implement ShiftLeftLong routine in B4i? I am trying to convert this code:

B4X:
Private Sub ShiftLeftLong(N As Long, Count As Int) As Long
    Return jo.RunMethod("shiftLeft", Array(N, Count))
End Sub

#If JAVA
public long shiftLeft (long n, int c) {
    return n << c;
}
#End If
 
Upvote 0

gregchao

Member
Licensed User
Longtime User
I am getting the wrong answer... any suggestions? Sorry, I don't understand the code so I do not know how to fix.

B4X:
Sub Class_Globals
    Private OFFSET_1900_TO_1970 As Long = 2208988800
    'Private jo As JavaObject
    Private no As NativeObject
    Private NtpTime, RoundTripTime, NtpTimeReference As Long 'ignore
End Sub

Public Sub Initialize
    'jo = Me
    no = Me
End Sub

Public Sub UpdateOffsets As ResumableSub
    Dim us As UDPSocket
    us.Initialize("us", 0, 8192)
    Dim RequestData(48) As Byte
    Dim raf As RandomAccessFile
    raf.Initialize3(RequestData, False)
    Dim time As Long = DateTime.Now
    Dim seconds As Long = time / 1000
    Dim milliseconds As Long = time - seconds * 1000
    seconds = seconds + OFFSET_1900_TO_1970
    raf.WriteByte(27, 0)
    raf.WriteInt(seconds, 40)
    Dim fraction As Long = milliseconds * 4294967
    raf.WriteInt(fraction, raf.CurrentPosition)
    raf.WriteByte(Rnd(0, 256), 47)
    Dim packet As UDPPacket
    packet.Initialize(RequestData, "0.pool.ntp.org", 123)
    us.Send(packet)
    Wait For us_PacketArrived (packet As UDPPacket)
    raf.Initialize3(packet.Data, False)
    Dim responseTime As Long = DateTime.Now
    Dim originateTime As Long = ReadTimeStamp(raf, 24)
    Dim receiveTime As Long = ReadTimeStamp(raf, 32)
    Dim transmitTime As Long = ReadTimeStamp(raf, 40)
    RoundTripTime = responseTime - time
    Dim clockOffset As Long = (receiveTime - originateTime + transmitTime - responseTime) / 2
    NtpTime = responseTime + clockOffset
    NtpTimeReference = responseTime
    us.Close
    Return True
End Sub

Public Sub getNow As Long
    If NtpTime = 0 Then
        Log("Time not available.")
        Return DateTime.Now
    End If
    Return NtpTime + DateTime.Now - NtpTimeReference
End Sub

Private Sub ReadTimeStamp (raf As RandomAccessFile, Offset As Int) As Long
    Dim seconds As Long = Read32(raf, Offset)
    Dim fraction As Long = Read32(raf, Offset + 4)
    Return ((seconds - OFFSET_1900_TO_1970) * 1000) + ((fraction * 1000) / 4294967296)
End Sub

Private Sub Read32(raf As RandomAccessFile, Offset As Int) As Long
    Dim i0 As Int = raf.ReadUnsignedByte(Offset)
    Dim i1 As Int = raf.ReadUnsignedByte(raf.CurrentPosition)
    Dim i2 As Int = raf.ReadUnsignedByte(raf.CurrentPosition)
    Dim i3 As Int = raf.ReadUnsignedByte(raf.CurrentPosition)
    Return ShiftLeftLong(i0, 24) + ShiftLeftLong(i1, 16) + ShiftLeftLong(i2, 8) + i3
End Sub

Private Sub ShiftLeftLong(N As Long, Count As Int) As Long
    Return Bit.ShiftLeft(N, Count)
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0

gregchao

Member
Licensed User
Longtime User
Thank you. Works well. Can you tell me how the code maps "sntp.now" to the sub "getnow" in the class?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Where is this code?
in the link provided above. It is a B4X Class (.bas file). No compiled library.
You need to add it to your project using Project->Add existing modules
 
Last edited:
Upvote 0

gregchao

Member
Licensed User
Longtime User
This is the original question "Can you tell me how the code maps "sntp.now" to the sub "getnow" in the class?" Somehow, you can call "sntp.now" and it runs the "getnow" sub in the code. I was wondering where this is defined.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This is the original question "Can you tell me how the code maps "sntp.now" to the sub "getnow" in the class?"
It is answered in the post following your question.
 
Upvote 0

gregchao

Member
Licensed User
Longtime User
OK, I understand.

Public Sub getNow As Long

is the mapped to "now". The "get" is a keyword and not included in call. So you call "sntp.now" instead of "sntp.getnow"
 
Upvote 0
Top