|
|
|
|
@ -37,6 +37,7 @@ import Text.Printf.TH
@@ -37,6 +37,7 @@ import Text.Printf.TH
|
|
|
|
|
startBot :: Config -> IO () |
|
|
|
|
startBot conf = forever $ do |
|
|
|
|
killMVar <- newEmptyMVar |
|
|
|
|
infoM "ATradeBot" "Starting session" |
|
|
|
|
mbsess <- session |
|
|
|
|
(cHostname conf) |
|
|
|
|
(Just ((\_ -> [scramSha1 (cUsername conf) Nothing (cPassword conf)]), Just "bot")) |
|
|
|
|
@ -50,20 +51,24 @@ startBot conf = forever $ do
@@ -50,20 +51,24 @@ startBot conf = forever $ do
|
|
|
|
|
priceMapRef <- newIORef M.empty |
|
|
|
|
tid2 <- forkIO $ quotesourceThread priceMapRef ctx |
|
|
|
|
tid3 <- forkIO $ handleCommands sess priceMapRef |
|
|
|
|
infoM "ATradeBot" "Waiting for kill MVar" |
|
|
|
|
(threadDelay 1000000) `untilM` (not <$> isEmptyMVar killMVar) |
|
|
|
|
infoM "ATradeBot" "killMVar filled, killing child threads" |
|
|
|
|
killThread tid1 |
|
|
|
|
killThread tid2 |
|
|
|
|
killThread tid3 |
|
|
|
|
infoM "ATradeBot" "Threads killed" |
|
|
|
|
|
|
|
|
|
where |
|
|
|
|
onConnectionClosed' killMVar sess _ = putMVar killMVar () |
|
|
|
|
onConnectionClosed' killMVar sess _ = do |
|
|
|
|
infoM "ATradeBot" "Connection closed" |
|
|
|
|
putMVar killMVar () |
|
|
|
|
|
|
|
|
|
threadSinkThread sess ctx = do |
|
|
|
|
noticeM "ATradeBot.TradeSink" "Started trade sink thread" |
|
|
|
|
withSocket ctx Rep $ \sock -> do |
|
|
|
|
bind sock "tcp://0.0.0.0:5543" |
|
|
|
|
forever $ do |
|
|
|
|
infoM "ATradeBot.TradeSink" "Waiting for incoming packet" |
|
|
|
|
mTrade <- eitherDecode' . BL.fromStrict <$> receive sock |
|
|
|
|
send sock [] $ encodeUtf8 "{ \"response\" : \"ok\" }" |
|
|
|
|
case mTrade of |
|
|
|
|
|