|
|
|
@ -8,8 +8,6 @@ module ATrade.QuoteSource.Server ( |
|
|
|
import ATrade.Types |
|
|
|
import ATrade.Types |
|
|
|
import Control.Concurrent.BoundedChan |
|
|
|
import Control.Concurrent.BoundedChan |
|
|
|
import Control.Concurrent hiding (readChan, writeChan) |
|
|
|
import Control.Concurrent hiding (readChan, writeChan) |
|
|
|
import Control.Concurrent.STM |
|
|
|
|
|
|
|
import Control.Concurrent.STM.TBQueue |
|
|
|
|
|
|
|
import Control.Exception |
|
|
|
import Control.Exception |
|
|
|
import Control.Monad |
|
|
|
import Control.Monad |
|
|
|
import qualified Data.Text as T |
|
|
|
import qualified Data.Text as T |
|
|
|
@ -22,7 +20,7 @@ import System.ZMQ4 |
|
|
|
data QuoteSourceServer = QuoteSourceServerState { |
|
|
|
data QuoteSourceServer = QuoteSourceServerState { |
|
|
|
ctx :: Context, |
|
|
|
ctx :: Context, |
|
|
|
outSocket :: Socket Pub, |
|
|
|
outSocket :: Socket Pub, |
|
|
|
tickChannel :: TBQueue QuoteSourceServerData, |
|
|
|
tickChannel :: BoundedChan QuoteSourceServerData, |
|
|
|
completionMvar :: MVar (), |
|
|
|
completionMvar :: MVar (), |
|
|
|
serverThreadId :: ThreadId, |
|
|
|
serverThreadId :: ThreadId, |
|
|
|
heartbeatThreadId :: ThreadId |
|
|
|
heartbeatThreadId :: ThreadId |
|
|
|
@ -41,7 +39,7 @@ serverThread state = do |
|
|
|
putMVar (completionMvar state) () |
|
|
|
putMVar (completionMvar state) () |
|
|
|
|
|
|
|
|
|
|
|
serverThread' = do |
|
|
|
serverThread' = do |
|
|
|
qssdata <- atomically $ readTBQueue $ tickChannel state |
|
|
|
qssdata <- readChan $ tickChannel state |
|
|
|
case qssdata of |
|
|
|
case qssdata of |
|
|
|
QSSKill -> return () |
|
|
|
QSSKill -> return () |
|
|
|
QSSHeartbeat -> do |
|
|
|
QSSHeartbeat -> do |
|
|
|
@ -51,14 +49,14 @@ serverThread state = do |
|
|
|
sendMulti (outSocket state) $ fromList . map BL.toStrict $ serializeTick tick |
|
|
|
sendMulti (outSocket state) $ fromList . map BL.toStrict $ serializeTick tick |
|
|
|
serverThread' |
|
|
|
serverThread' |
|
|
|
|
|
|
|
|
|
|
|
startQuoteSourceServer :: TBQueue QuoteSourceServerData -> Context -> T.Text -> IO QuoteSourceServer |
|
|
|
startQuoteSourceServer :: BoundedChan QuoteSourceServerData -> Context -> T.Text -> IO QuoteSourceServer |
|
|
|
startQuoteSourceServer chan c ep = do |
|
|
|
startQuoteSourceServer chan c ep = do |
|
|
|
sock <- socket c Pub |
|
|
|
sock <- socket c Pub |
|
|
|
bind sock $ T.unpack ep |
|
|
|
bind sock $ T.unpack ep |
|
|
|
tid <- myThreadId |
|
|
|
tid <- myThreadId |
|
|
|
hbTid <- forkIO $ forever $ do |
|
|
|
hbTid <- forkIO $ forever $ do |
|
|
|
threadDelay 1000000 |
|
|
|
threadDelay 1000000 |
|
|
|
atomically $ writeTBQueue chan QSSHeartbeat |
|
|
|
writeChan chan QSSHeartbeat |
|
|
|
|
|
|
|
|
|
|
|
mv <- newEmptyMVar |
|
|
|
mv <- newEmptyMVar |
|
|
|
let state = QuoteSourceServerState { |
|
|
|
let state = QuoteSourceServerState { |
|
|
|
@ -69,9 +67,9 @@ startQuoteSourceServer chan c ep = do |
|
|
|
serverThreadId = tid, |
|
|
|
serverThreadId = tid, |
|
|
|
heartbeatThreadId = hbTid |
|
|
|
heartbeatThreadId = hbTid |
|
|
|
} |
|
|
|
} |
|
|
|
stid <- forkIO $ serverThread state |
|
|
|
stid <- forkOS $ serverThread state |
|
|
|
return $ state { serverThreadId = stid } |
|
|
|
return $ state { serverThreadId = stid } |
|
|
|
|
|
|
|
|
|
|
|
stopQuoteSourceServer :: QuoteSourceServer -> IO () |
|
|
|
stopQuoteSourceServer :: QuoteSourceServer -> IO () |
|
|
|
stopQuoteSourceServer server = killThread (heartbeatThreadId server) >> atomically (writeTBQueue (tickChannel server) QSSKill) >> readMVar (completionMvar server) |
|
|
|
stopQuoteSourceServer server = killThread (heartbeatThreadId server) >> (writeChan (tickChannel server) QSSKill) >> readMVar (completionMvar server) |
|
|
|
|
|
|
|
|
|
|
|
|