Properly parse command replies
This commit is contained in:
parent
0986095850
commit
1d53c7a1d8
2 changed files with 22 additions and 18 deletions
|
|
@ -63,6 +63,7 @@ import Control.Concurrent.Async
|
||||||
import Control.Concurrent.STM
|
import Control.Concurrent.STM
|
||||||
import Control.Monad.Trans.State
|
import Control.Monad.Trans.State
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
|
import Data.Aeson.Types
|
||||||
import qualified Data.ByteString.Lazy as BS
|
import qualified Data.ByteString.Lazy as BS
|
||||||
import qualified Data.HashMap.Strict as HM
|
import qualified Data.HashMap.Strict as HM
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
|
|
@ -188,14 +189,17 @@ sendThread euphCon qRecv con = do
|
||||||
-}
|
-}
|
||||||
|
|
||||||
data PacketInfo = PacketInfo
|
data PacketInfo = PacketInfo
|
||||||
{ infoPacketID :: Maybe PacketID
|
{ infoPacketID :: Maybe PacketID
|
||||||
, infoServerError :: Maybe T.Text
|
, infoData :: Either T.Text Value
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
instance FromJSON PacketInfo where
|
instance FromJSON PacketInfo where
|
||||||
parseJSON = withObject "packet" $ \o -> do
|
parseJSON = withObject "packet" $ \o -> do
|
||||||
infoPacketID <- o .:? "id"
|
infoPacketID <- o .:? "id"
|
||||||
infoServerError <- o .:? "error"
|
packetData <- o .:? "data"
|
||||||
|
infoData <- case packetData of
|
||||||
|
Nothing -> Left <$> o .: "error"
|
||||||
|
Just d -> return $ Right d
|
||||||
return PacketInfo{..}
|
return PacketInfo{..}
|
||||||
|
|
||||||
-- TODO: Swap for HashMap?
|
-- TODO: Swap for HashMap?
|
||||||
|
|
@ -217,16 +221,16 @@ processPacket qEvent bs = do
|
||||||
PacketInfo{..} <- decode bs
|
PacketInfo{..} <- decode bs
|
||||||
replyID <- infoPacketID
|
replyID <- infoPacketID
|
||||||
replyMVar <- M.lookup replyID replies
|
replyMVar <- M.lookup replyID replies
|
||||||
return (replyID, replyMVar, infoServerError)
|
return (replyID, replyMVar, infoData)
|
||||||
-- ... and then write the appropriate result into the MVar.
|
-- ... and then write the appropriate result into the MVar.
|
||||||
whenJust result $ \(replyID, ReplyMVar var, serverError) -> do
|
whenJust result $ \(replyID, ReplyMVar var, infoData) -> do
|
||||||
modify (M.delete replyID)
|
modify (M.delete replyID)
|
||||||
case serverError of
|
case infoData of
|
||||||
Just e -> liftIO $ putMVar var (Left (EuphServerError e))
|
Left e -> liftIO $ putMVar var (Left (EuphServerError e))
|
||||||
Nothing ->
|
Right d ->
|
||||||
case decode bs of
|
case parseEither parseJSON d of
|
||||||
Nothing -> liftIO $ putMVar var (Left EuphParse)
|
Left e -> liftIO $ putMVar var (Left $ EuphParse e bs)
|
||||||
Just r -> liftIO $ putMVar var (Right r)
|
Right r -> liftIO $ putMVar var (Right r)
|
||||||
|
|
||||||
processRecv :: RecvQueue -> EventQueue -> StateT Awaiting IO ()
|
processRecv :: RecvQueue -> EventQueue -> StateT Awaiting IO ()
|
||||||
processRecv qRecv qEvent = do
|
processRecv qRecv qEvent = do
|
||||||
|
|
@ -436,14 +440,14 @@ data EuphException = EuphClosed
|
||||||
-- ^ Disconnected from server while waiting for the reply.
|
-- ^ Disconnected from server while waiting for the reply.
|
||||||
| EuphServerError T.Text
|
| EuphServerError T.Text
|
||||||
-- ^ The server replied with an error.
|
-- ^ The server replied with an error.
|
||||||
| EuphParse
|
| EuphParse String BS.ByteString
|
||||||
-- ^ Could not parse the server's reply correctly.
|
-- ^ Could not parse the server's reply correctly.
|
||||||
|
|
||||||
instance Show EuphException where
|
instance Show EuphException where
|
||||||
show EuphClosed = "Connection already closed"
|
show EuphClosed = "Connection already closed"
|
||||||
show EuphDisconnected = "Disconnected from server"
|
show EuphDisconnected = "Disconnected from server"
|
||||||
show (EuphServerError t) = "Server error: " ++ T.unpack t
|
show (EuphServerError t) = "Server error: " ++ T.unpack t
|
||||||
show EuphParse = "Parsing failed"
|
show (EuphParse e bs) = "Parsing failed: " ++ e ++ " - packet was " ++ show bs
|
||||||
|
|
||||||
instance Exception EuphException
|
instance Exception EuphException
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ data Message = Message
|
||||||
-- ^ The unix timestamp of when the message was posted
|
-- ^ The unix timestamp of when the message was posted
|
||||||
, msgSender :: SessionView
|
, msgSender :: SessionView
|
||||||
-- ^ The view of the sender's session
|
-- ^ The view of the sender's session
|
||||||
, msgContent :: String
|
, msgContent :: T.Text
|
||||||
-- ^ The content of the message (client-defined)
|
-- ^ The content of the message (client-defined)
|
||||||
, msgEdited :: Maybe UTCTime
|
, msgEdited :: Maybe UTCTime
|
||||||
-- ^ The unix timestamp of when the message was last edited
|
-- ^ The unix timestamp of when the message was last edited
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue