|
|
|
|
@ -1,7 +1,8 @@
@@ -1,7 +1,8 @@
|
|
|
|
|
|
|
|
|
|
module ATrade.QuoteSource.Server ( |
|
|
|
|
startQuoteSourceServer, |
|
|
|
|
stopQuoteSourceServer |
|
|
|
|
stopQuoteSourceServer, |
|
|
|
|
QuoteSourceServerData(..) |
|
|
|
|
) where |
|
|
|
|
|
|
|
|
|
import ATrade.Types |
|
|
|
|
@ -10,6 +11,7 @@ import Control.Concurrent hiding (readChan, writeChan)
@@ -10,6 +11,7 @@ import Control.Concurrent hiding (readChan, writeChan)
|
|
|
|
|
import Control.Exception |
|
|
|
|
import Control.Monad |
|
|
|
|
import qualified Data.Text as T |
|
|
|
|
import qualified Data.ByteString.Char8 as B8 |
|
|
|
|
import qualified Data.ByteString.Lazy as BL |
|
|
|
|
import Data.List.NonEmpty hiding (map) |
|
|
|
|
import System.Log.Logger |
|
|
|
|
@ -18,11 +20,14 @@ import System.ZMQ4
@@ -18,11 +20,14 @@ import System.ZMQ4
|
|
|
|
|
data QuoteSourceServer = QuoteSourceServerState { |
|
|
|
|
ctx :: Context, |
|
|
|
|
outSocket :: Socket Pub, |
|
|
|
|
tickChannel :: BoundedChan (Maybe Tick), |
|
|
|
|
tickChannel :: BoundedChan QuoteSourceServerData, |
|
|
|
|
completionMvar :: MVar (), |
|
|
|
|
serverThreadId :: ThreadId |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data QuoteSourceServerData = QSSTick Tick | QSSHeartbeat | QSSKill |
|
|
|
|
deriving (Show, Eq) |
|
|
|
|
|
|
|
|
|
serverThread :: QuoteSourceServer -> IO () |
|
|
|
|
serverThread state = do |
|
|
|
|
finally serverThread' cleanup |
|
|
|
|
@ -33,14 +38,17 @@ serverThread state = do
@@ -33,14 +38,17 @@ serverThread state = do
|
|
|
|
|
putMVar (completionMvar state) () |
|
|
|
|
|
|
|
|
|
serverThread' = do |
|
|
|
|
maybeTick <- readChan $ tickChannel state |
|
|
|
|
case maybeTick of |
|
|
|
|
Nothing -> return () |
|
|
|
|
Just tick -> do |
|
|
|
|
qssdata <- readChan $ tickChannel state |
|
|
|
|
case qssdata of |
|
|
|
|
QSSKill -> return () |
|
|
|
|
QSSHeartbeat -> do |
|
|
|
|
send (outSocket state) [] $ B8.pack "SYSTEM#HEARTBEAT" |
|
|
|
|
serverThread' |
|
|
|
|
QSSTick tick -> do |
|
|
|
|
sendMulti (outSocket state) $ fromList . map BL.toStrict $ serializeTick tick |
|
|
|
|
serverThread' |
|
|
|
|
|
|
|
|
|
startQuoteSourceServer :: BoundedChan (Maybe Tick) -> Context -> T.Text -> IO QuoteSourceServer |
|
|
|
|
startQuoteSourceServer :: BoundedChan QuoteSourceServerData -> Context -> T.Text -> IO QuoteSourceServer |
|
|
|
|
startQuoteSourceServer chan c ep = do |
|
|
|
|
sock <- socket c Pub |
|
|
|
|
bind sock $ T.unpack ep |
|
|
|
|
@ -57,5 +65,5 @@ startQuoteSourceServer chan c ep = do
@@ -57,5 +65,5 @@ startQuoteSourceServer chan c ep = do
|
|
|
|
|
return $ state { serverThreadId = stid } |
|
|
|
|
|
|
|
|
|
stopQuoteSourceServer :: QuoteSourceServer -> IO () |
|
|
|
|
stopQuoteSourceServer server = writeChan (tickChannel server) Nothing >> readMVar (completionMvar server) |
|
|
|
|
stopQuoteSourceServer server = writeChan (tickChannel server) QSSKill >> readMVar (completionMvar server) |
|
|
|
|
|
|
|
|
|
|