Browse Source

Refactoring

master
Denis Tereshkin 9 years ago
parent
commit
ed15e2b055
  1. 1
      libatrade.cabal
  2. 13
      src/ATrade/Broker/Server.hs
  3. 9
      src/ATrade/Util.hs
  4. 5
      test/TestBrokerServer.hs

1
libatrade.cabal

@ -20,6 +20,7 @@ library
, ATrade.QuoteSource.Server , ATrade.QuoteSource.Server
, ATrade.Broker.Protocol , ATrade.Broker.Protocol
, ATrade.Broker.Server , ATrade.Broker.Server
, ATrade.Util
build-depends: base >= 4.7 && < 5 build-depends: base >= 4.7 && < 5
, Decimal , Decimal
, time , time

13
src/ATrade/Broker/Server.hs

@ -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"

9
src/ATrade/Util.hs

@ -0,0 +1,9 @@
module ATrade.Util (
atomicMapIORef
) where
import Data.IORef
atomicMapIORef :: IORef a -> (a -> a) -> IO ()
atomicMapIORef ioref f = atomicModifyIORef' ioref (\s -> (f s, ()))

5
test/TestBrokerServer.hs

@ -14,6 +14,7 @@ import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy as BL
import ATrade.Broker.Server import ATrade.Broker.Server
import ATrade.Broker.Protocol import ATrade.Broker.Protocol
import ATrade.Util
import qualified Data.Text as T import qualified Data.Text as T
import Control.Monad import Control.Monad
import Control.Monad.Loops import Control.Monad.Loops
@ -39,7 +40,7 @@ data MockBrokerState = MockBrokerState {
mockSubmitOrder :: IORef MockBrokerState -> Order -> IO () mockSubmitOrder :: IORef MockBrokerState -> Order -> IO ()
mockSubmitOrder state order = do mockSubmitOrder state order = do
atomicModifyIORef' state (\s -> (s { orders = submittedOrder : orders s }, ())) atomicMapIORef state (\s -> s { orders = submittedOrder : orders s })
maybeCb <- notificationCallback <$> readIORef state maybeCb <- notificationCallback <$> readIORef state
case maybeCb of case maybeCb of
Just cb -> cb $ OrderNotification (orderId order) Submitted Just cb -> cb $ OrderNotification (orderId order) Submitted
@ -67,7 +68,7 @@ mkMockBroker accs = do
return (BrokerInterface { return (BrokerInterface {
accounts = accs, accounts = accs,
setNotificationCallback = \cb -> atomicModifyIORef' state (\s -> (s { notificationCallback = cb }, ())), setNotificationCallback = \cb -> atomicMapIORef state (\s -> s { notificationCallback = cb }),
submitOrder = mockSubmitOrder state, submitOrder = mockSubmitOrder state,
cancelOrder = mockCancelOrder state, cancelOrder = mockCancelOrder state,
stopBroker = mockStopBroker state stopBroker = mockStopBroker state

Loading…
Cancel
Save