|
|
|
@ -22,6 +22,7 @@ import Control.Concurrent |
|
|
|
import Control.Exception |
|
|
|
import Control.Exception |
|
|
|
import Control.Monad |
|
|
|
import Control.Monad |
|
|
|
import System.Log.Logger |
|
|
|
import System.Log.Logger |
|
|
|
|
|
|
|
import ATrade.Util |
|
|
|
|
|
|
|
|
|
|
|
newtype OrderIdGenerator = IO OrderId |
|
|
|
newtype OrderIdGenerator = IO OrderId |
|
|
|
type PeerId = B.ByteString |
|
|
|
type PeerId = B.ByteString |
|
|
|
@ -75,10 +76,10 @@ notificationCallback state n = do |
|
|
|
Nothing -> warningM "Broker.Server" "Notification: unknown order" |
|
|
|
Nothing -> warningM "Broker.Server" "Notification: unknown order" |
|
|
|
|
|
|
|
|
|
|
|
where |
|
|
|
where |
|
|
|
addNotification peerId n = atomicModifyIORef' state (\s -> |
|
|
|
addNotification peerId n = atomicMapIORef state (\s -> |
|
|
|
case M.lookup peerId . pendingNotifications $ s of |
|
|
|
case M.lookup peerId . pendingNotifications $ s of |
|
|
|
Just ns -> (s { pendingNotifications = M.insert peerId (n : ns) (pendingNotifications s)}, ()) |
|
|
|
Just ns -> s { pendingNotifications = M.insert peerId (n : ns) (pendingNotifications s)} |
|
|
|
Nothing -> (s { pendingNotifications = M.insert peerId [n] (pendingNotifications s)}, ())) |
|
|
|
Nothing -> s { pendingNotifications = M.insert peerId [n] (pendingNotifications s)}) |
|
|
|
|
|
|
|
|
|
|
|
brokerServerThread state = finally brokerServerThread' cleanup |
|
|
|
brokerServerThread state = finally brokerServerThread' cleanup |
|
|
|
where |
|
|
|
where |
|
|
|
@ -103,9 +104,9 @@ brokerServerThread state = finally brokerServerThread' cleanup |
|
|
|
case findBrokerForAccount (orderAccountId order) bros of |
|
|
|
case findBrokerForAccount (orderAccountId order) bros of |
|
|
|
Just bro -> do |
|
|
|
Just bro -> do |
|
|
|
oid <- nextOrderId |
|
|
|
oid <- nextOrderId |
|
|
|
atomicModifyIORef' state (\s -> (s { |
|
|
|
atomicMapIORef state (\s -> s { |
|
|
|
orderToBroker = M.insert oid bro (orderToBroker s), |
|
|
|
orderToBroker = M.insert oid bro (orderToBroker s), |
|
|
|
orderMap = M.insert oid peerId (orderMap s) }, ())) |
|
|
|
orderMap = M.insert oid peerId (orderMap s) }) |
|
|
|
submitOrder bro order { orderId = oid } |
|
|
|
submitOrder bro order { orderId = oid } |
|
|
|
return $ ResponseOrderSubmitted oid |
|
|
|
return $ ResponseOrderSubmitted oid |
|
|
|
|
|
|
|
|
|
|
|
@ -121,7 +122,7 @@ brokerServerThread state = finally brokerServerThread' cleanup |
|
|
|
maybeNs <- M.lookup peerId . pendingNotifications <$> readIORef state |
|
|
|
maybeNs <- M.lookup peerId . pendingNotifications <$> readIORef state |
|
|
|
case maybeNs of |
|
|
|
case maybeNs of |
|
|
|
Just ns -> do |
|
|
|
Just ns -> do |
|
|
|
atomicModifyIORef' state (\s -> (s { pendingNotifications = M.insert peerId [] (pendingNotifications s)}, ())) |
|
|
|
atomicMapIORef state (\s -> s { pendingNotifications = M.insert peerId [] (pendingNotifications s)}) |
|
|
|
return $ ResponseNotifications ns |
|
|
|
return $ ResponseNotifications ns |
|
|
|
Nothing -> return $ ResponseNotifications [] |
|
|
|
Nothing -> return $ ResponseNotifications [] |
|
|
|
Nothing -> return $ ResponseError "Unable to parse request" |
|
|
|
Nothing -> return $ ResponseError "Unable to parse request" |
|
|
|
|