Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 929fbbb

Browse files
committed
[CDEC-439] Alex's review
1 parent 1793ff4 commit 929fbbb

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

networking/src/Ntp/Client.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ spawnNtpClient settings ncStatus = do
268268

269269
addrs <- catMaybes <$> traverse resolveNtpHost (ntpServers settings)
270270
when (null addrs) $ throwM NoHostResolved
271+
-- TODO
272+
-- we should start listening for requests when we send something, since
273+
-- we're not expecting anything to come unless we send something. This
274+
-- way we could simplify the client and remove `ncState` mutable cell.
271275
startReceive cli
272276
`concurrently_` sendLoop cli addrs
273277
`concurrently_` logInfo "started"

networking/src/Ntp/Packet.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module Ntp.Packet
1313
) where
1414

1515

16-
import Control.Lens (each, (^..))
1716
import Control.Monad (replicateM_)
1817
import Data.Binary (Binary (..))
1918
import Data.Binary.Get (getInt8, getWord32be, getWord8, skip)
@@ -41,9 +40,17 @@ instance Binary NtpPacket where
4140

4241
replicateM_ 5 $ putWord32be 0
4342

44-
mapM_ putWord32be $ realMcsToNtp ntpOriginTime ^.. each
45-
mapM_ putWord32be $ realMcsToNtp ntpReceivedTime ^.. each
46-
mapM_ putWord32be $ realMcsToNtp ntpTransmitTime ^.. each
43+
let (osec, ofrac) = realMcsToNtp ntpOriginTime
44+
putWord32be osec
45+
putWord32be ofrac
46+
47+
let (rsec, rfrac) = realMcsToNtp ntpReceivedTime
48+
putWord32be rsec
49+
putWord32be rfrac
50+
51+
let (tsec, tfrac) = realMcsToNtp ntpTransmitTime
52+
putWord32be tsec
53+
putWord32be tfrac
4754

4855
get = do
4956
ntpParams <- getWord8
@@ -84,7 +91,9 @@ ntpToRealMcs sec frac =
8491
let -- microseconds
8592
secMicro :: Integer
8693
secMicro = (fromIntegral sec - ntpTimestampDelta) * 1000000
87-
-- Each fraction resolves to 232 pico seconds (`232 ≈ 10000000/4294`)
94+
-- We divide 1 second into 2 ^ 32 parts, giving 2.3283064365386963e-10
95+
-- as the quantum. A picosecond is 10e-12 of a second, so this is 232
96+
-- picoseconds or `1/4294` of a millisecond.
8897
-- ref: https://tools.ietf.org/html/rfc5905#section-6
8998
fracMicro :: Integer
9099
fracMicro = (fromIntegral frac) `div` 4294

0 commit comments

Comments
 (0)