22{-# LANGUAGE RecordWildCards #-}
33{-# LANGUAGE TypeOperators #-}
44
5- module Share.Web.UCM.SyncV3.Impl where
5+ module Share.Web.UCM.SyncV3.Impl ( server ) where
66
77import Control.Lens hiding ((.=) )
88import Control.Monad.Except (runExceptT )
@@ -28,6 +28,7 @@ import Unison.SyncV3.Types
2828import Unison.Util.Websockets (Queues (.. ), withQueues )
2929import UnliftIO qualified
3030import UnliftIO.STM
31+ import Unison.Debug qualified as Debug
3132
3233-- Amount of entities to buffer from the network into the send/recv queues.
3334sendBufferSize :: Natural
@@ -36,10 +37,10 @@ sendBufferSize = 100
3637recvBufferSize :: Natural
3738recvBufferSize = 100
3839
39- data StreamInitInfo = StreamInitInfo
40+ -- data StreamInitInfo = StreamInitInfo
4041
41- streamSettings :: StreamInitInfo
42- streamSettings = StreamInitInfo
42+ -- streamSettings :: StreamInitInfo
43+ -- streamSettings = StreamInitInfo
4344
4445server :: Maybe UserId -> SyncV3. Routes WebAppServer
4546server mayUserId =
@@ -49,6 +50,7 @@ server mayUserId =
4950
5051downloadEntitiesImpl :: Maybe UserId -> WS. Connection -> WebApp ()
5152downloadEntitiesImpl _mayCallerUserId conn = do
53+ Debug. debugLogM Debug. Temp " Got connection"
5254 -- Auth is currently done via HashJWTs
5355 _authZReceipt <- AuthZ. checkDownloadFromUserCodebase
5456 doSyncEmitter shareEmitter conn
@@ -70,13 +72,18 @@ doSyncEmitter emitterImpl conn = do
7072 sendBufferSize
7173 conn
7274 \ (q@ Queues {receive}) -> handleErr q $ do
75+ Debug. debugLogM Debug. Temp " Got queues"
7376 let recvM :: ExceptT SyncError m (FromReceiverMessage HashJWT Hash32 )
7477 recvM = do
7578 result <- liftIO $ atomically receive
79+ Debug. debugM Debug. Temp " Received: " result
7680 case result of
7781 Msg msg -> pure msg
7882 Err err -> throwError err
83+
84+ Debug. debugLogM Debug. Temp " Waiting for init message"
7985 initMsg <- recvM
86+ Debug. debugM Debug. Temp " Got init: " initMsg
8087 syncState <- case initMsg of
8188 ReceiverInitStream initMsg -> lift $ initialize initMsg
8289 other -> throwError $ InitializationError (" Expected ReceiverInitStream message, got: " <> tShow other)
@@ -93,7 +100,17 @@ doSyncEmitter emitterImpl conn = do
93100 Right r -> pure r
94101
95102initialize :: InitMsg ah -> m (SyncState sh hash )
96- initialize = undefined
103+ initialize InitMsg {initMsgClientVersion, initMsgBranchRef, initMsgRootCausal, initMsgRequestedDepth} = do
104+ let initialCausalHash = hashjwtHash initMsgRootCausal
105+ validRequestsVar <- newTVarIO Set. empty
106+ requestedEntitiesVar <- newTVarIO (Set. singleton initialCausalHash)
107+ entitiesAlreadySentVar <- newTVarIO Set. empty
108+ pure $ SyncState
109+ { codebase = PG. codebaseEnv,
110+ validRequestsVar,
111+ requestedEntitiesVar,
112+ entitiesAlreadySentVar
113+ }
97114
98115data SyncState sh hash = SyncState
99116 { codebase :: CodebaseEnv ,
0 commit comments