@@ -43,7 +43,7 @@ import Simplex.Messaging.Server.MsgStore.Journal
4343import Simplex.Messaging.Server.MsgStore.STM
4444import Simplex.Messaging.Server.MsgStore.Types
4545import Simplex.Messaging.Server.NtfStore
46- import Simplex.Messaging.Server.QueueStore ( QueueRec ( .. ))
46+ import Simplex.Messaging.Server.QueueStore
4747import Simplex.Messaging.Server.QueueStore.STM
4848import Simplex.Messaging.Server.Stats
4949import Simplex.Messaging.Server.StoreLog
@@ -165,27 +165,25 @@ data Env = Env
165165 serverInfo :: ServerInformation ,
166166 server :: Server ,
167167 serverIdentity :: KeyHash ,
168- queueStore :: QueueStore ,
169168 msgStore :: AMsgStore ,
170169 ntfStore :: NtfStore ,
171170 random :: TVar ChaChaDRG ,
172- storeLog :: Maybe (StoreLog 'WriteMode),
173171 tlsServerCreds :: T. Credential ,
174172 httpServerCreds :: Maybe T. Credential ,
175173 serverStats :: ServerStats ,
176174 sockets :: TVar [(ServiceName , SocketState )],
177175 clientSeq :: TVar ClientId ,
178- clients :: TVar (IntMap (Maybe Client )),
176+ clients :: TVar (IntMap (Maybe AClient )),
179177 proxyAgent :: ProxyAgent -- senders served on this proxy
180178 }
181179
182180type family MsgStore s where
183181 MsgStore 'MSMemory = STMMsgStore
184182 MsgStore 'MSJournal = JournalMsgStore
185183
186- data AMsgStore = forall s . MsgStoreClass (MsgStore s ) => AMS (SMSType s ) (MsgStore s )
184+ data AMsgStore = forall s . ( STMQueueStore ( MsgStore s ), MsgStoreClass (MsgStore s ) ) => AMS (SMSType s ) (MsgStore s )
187185
188- data AMsgQueue = forall s . MsgStoreClass (MsgStore s ) => AMQ (SMSType s ) (MsgQueue (MsgStore s ))
186+ data AStoreQueue = forall s . MsgStoreClass (MsgStore s ) => ASQ (SMSType s ) (StoreQueue (MsgStore s ))
189187
190188data AMsgStoreCfg = forall s . MsgStoreClass (MsgStore s ) => AMSC (SMSType s ) (MsgStoreConfig (MsgStore s ))
191189
@@ -197,11 +195,11 @@ type Subscribed = Bool
197195
198196data Server = Server
199197 { subscribedQ :: TQueue (RecipientId , ClientId , Subscribed ),
200- subscribers :: TMap RecipientId (TVar Client ),
198+ subscribers :: TMap RecipientId (TVar AClient ),
201199 ntfSubscribedQ :: TQueue (NotifierId , ClientId , Subscribed ),
202- notifiers :: TMap NotifierId (TVar Client ),
203- subClients :: TVar (IntMap Client ), -- clients with SMP subscriptions
204- ntfSubClients :: TVar (IntMap Client ), -- clients with Ntf subscriptions
200+ notifiers :: TMap NotifierId (TVar AClient ),
201+ subClients :: TVar (IntMap AClient ), -- clients with SMP subscriptions
202+ ntfSubClients :: TVar (IntMap AClient ), -- clients with Ntf subscriptions
205203 pendingSubEvents :: TVar (IntMap (NonEmpty (RecipientId , Subscribed ))),
206204 pendingNtfSubEvents :: TVar (IntMap (NonEmpty (NotifierId , Subscribed ))),
207205 savingLock :: Lock
@@ -213,11 +211,16 @@ newtype ProxyAgent = ProxyAgent
213211
214212type ClientId = Int
215213
216- data Client = Client
214+ data AClient = forall s . MsgStoreClass (MsgStore s ) => AClient (SMSType s ) (Client (MsgStore s ))
215+
216+ clientId' :: AClient -> ClientId
217+ clientId' (AClient _ Client {clientId}) = clientId
218+
219+ data Client s = Client
217220 { clientId :: ClientId ,
218221 subscriptions :: TMap RecipientId Sub ,
219222 ntfSubscriptions :: TMap NotifierId () ,
220- rcvQ :: TBQueue (NonEmpty (Maybe QueueRec , Transmission Cmd )),
223+ rcvQ :: TBQueue (NonEmpty (Maybe ( StoreQueue s , QueueRec ) , Transmission Cmd )),
221224 sndQ :: TBQueue (NonEmpty (Transmission BrokerMsg )),
222225 msgQ :: TBQueue (NonEmpty (Transmission BrokerMsg )),
223226 procThreads :: TVar Int ,
@@ -253,8 +256,8 @@ newServer = do
253256 savingLock <- createLockIO
254257 return Server {subscribedQ, subscribers, ntfSubscribedQ, notifiers, subClients, ntfSubClients, pendingSubEvents, pendingNtfSubEvents, savingLock}
255258
256- newClient :: ClientId -> Natural -> VersionSMP -> ByteString -> SystemTime -> IO Client
257- newClient clientId qSize thVersion sessionId createdAt = do
259+ newClient :: SMSType s -> ClientId -> Natural -> VersionSMP -> ByteString -> SystemTime -> IO ( Client ( MsgStore s ))
260+ newClient _msType clientId qSize thVersion sessionId createdAt = do
258261 subscriptions <- TM. emptyIO
259262 ntfSubscriptions <- TM. emptyIO
260263 rcvQ <- newTBQueueIO qSize
@@ -283,8 +286,7 @@ newEnv :: ServerConfig -> IO Env
283286newEnv config@ ServerConfig {smpCredentials, httpCredentials, storeLogFile, msgStoreType, storeMsgsFile, smpAgentCfg, information, messageExpiration, msgQueueQuota, maxJournalMsgCount, maxJournalStateLines} = do
284287 serverActive <- newTVarIO True
285288 server <- newServer
286- queueStore <- newQueueStore
287- msgStore <- case msgStoreType of
289+ msgStore@ (AMS _ store) <- case msgStoreType of
288290 AMSType SMSMemory -> AMS SMSMemory <$> newMsgStore STMStoreConfig {storePath = storeMsgsFile, quota = msgQueueQuota}
289291 AMSType SMSJournal -> case storeMsgsFile of
290292 Just storePath ->
@@ -293,10 +295,10 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, storeLogFile, msgSt
293295 Nothing -> putStrLn " Error: journal msg store require path in [STORE_LOG], restore_messages" >> exitFailure
294296 ntfStore <- NtfStore <$> TM. emptyIO
295297 random <- C. newRandom
296- storeLog <-
297- forM storeLogFile $ \ f -> do
298- logInfo $ " restoring queues from file " <> T. pack f
299- readWriteQueueStore f queueStore
298+ forM_ storeLogFile $ \ f -> do
299+ logInfo $ " restoring queues from file " <> T. pack f
300+ sl <- readWriteQueueStore f store
301+ setStoreLog store sl
300302 tlsServerCreds <- getCredentials " SMP" smpCredentials
301303 httpServerCreds <- mapM (getCredentials " HTTPS" ) httpCredentials
302304 mapM_ checkHTTPSCredentials httpServerCreds
@@ -307,7 +309,7 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, storeLogFile, msgSt
307309 clientSeq <- newTVarIO 0
308310 clients <- newTVarIO mempty
309311 proxyAgent <- newSMPProxyAgent smpAgentCfg random
310- pure Env {serverActive, config, serverInfo, server, serverIdentity, queueStore, msgStore, ntfStore, random, storeLog , tlsServerCreds, httpServerCreds, serverStats, sockets, clientSeq, clients, proxyAgent}
312+ pure Env {serverActive, config, serverInfo, server, serverIdentity, msgStore, ntfStore, random, tlsServerCreds, httpServerCreds, serverStats, sockets, clientSeq, clients, proxyAgent}
311313 where
312314 getCredentials protocol creds = do
313315 files <- missingCreds
@@ -351,3 +353,6 @@ newSMPProxyAgent :: SMPClientAgentConfig -> TVar ChaChaDRG -> IO ProxyAgent
351353newSMPProxyAgent smpAgentCfg random = do
352354 smpAgent <- newSMPClientAgent smpAgentCfg random
353355 pure ProxyAgent {smpAgent}
356+
357+ readWriteQueueStore :: STMQueueStore s => FilePath -> s -> IO (StoreLog 'WriteMode)
358+ readWriteQueueStore = readWriteStoreLog readQueueStore writeQueueStore
0 commit comments