-- Thread code handling incoming messages
-- This thread parses the messages before sending them to "center"; we will have several threads so there's no
-- performance problem.
socketIncoming :: Socket -> Chan CenterRequestOrResponse -> IO ()
socketIncoming sock chanToCenter = do
    (contents,_addr) <- NBS.recvFrom sock 16384
    case parseAnswer (BS.fromStrict contents) of
        Right msg -> writeChan chanToCenter (BrokerCenterResponse msg) >> socketIncoming sock chanToCenter
        Left err -> putStrLn ("ERR : Discarded message because of: " ++ err) >> socketIncoming sock chanToCenter -- discard

-- Thread code handling outgoing messages
socketIncoming :: Socket -> Chan CenterRequestOrResponse -> IO ()
socketIncoming sock chanToCenter = do
    (contents,_addr) <- NBS.recvFrom sock 16384
    case parseAnswer (BS.fromStrict contents) of
        Right msg -> writeChan chanToCenter (BrokerCenterResponse msg) >> socketIncoming sock chanToCenter
        Left err -> putStrLn ("ERR : Discarded message because of: " ++ err) >> socketIncoming sock chanToCenter -- discard

-- Thread code handling outgoing messages
socketIncoming sock chanToCenter = do
    (contents,_addr) <- NBS.recvFrom sock 16384
    case parseAnswer (BS.fromStrict contents) of
        Right msg -> writeChan chanToCenter (BrokerCenterResponse msg) >> socketIncoming sock chanToCenter
        Left err -> putStrLn ("ERR : Discarded message because of: " ++ err) >> socketIncoming sock chanToCenter -- discard

-- Thread code handling outgoing messages