Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Control.Applicative as a parser. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Foreign/Erlang/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Foreign.Erlang.Network (
, handshakeS
) where

import Control.Applicative ((<$>))
import Control.Concurrent (threadDelay)
import Control.Exception (assert, bracketOnError)
import Control.Monad (liftM)
Expand Down Expand Up @@ -68,15 +69,18 @@ erlDigest cookie challenge = let
[(n, _)] = readHex . show . md5 . B.pack $ cookie ++ show challenge
in toNetwork 16 n

packLen = fromIntegral . B.length

packn, packN :: B.ByteString -> Put
packn msg = putn (fromIntegral . B.length $ msg) >> putLazyByteString msg
packN msg = putN (fromIntegral . B.length $ msg) >> putLazyByteString msg
packn msg = putn (packLen msg) >> putLazyByteString msg
packN msg = putN (packLen msg) >> putLazyByteString msg


sendMessage :: (B.ByteString -> Put) -> (B.ByteString -> IO ()) -> B.ByteString -> IO ()
sendMessage pack out = out . runPut . pack

recvMessage :: Int -> (Int -> IO B.ByteString) -> IO B.ByteString
recvMessage hdrlen inf = (liftM (unpack hdrlen) $ inf hdrlen) >>= inf
recvMessage hdrlen inf = (unpack hdrlen <$> inf hdrlen) >>= inf
where
unpack 2 = runGet getn
unpack 4 = runGet getN
Expand Down
50 changes: 17 additions & 33 deletions src/Foreign/Erlang/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ module Foreign.Erlang.Types (
, tag
) where

import Control.Applicative ((<$>), (<*>))
import Control.Exception (assert)
import Control.Monad (forM, liftM)
import Control.Monad (forM, liftM, (=<<))
import Data.Binary
import Data.Binary.Get
import Data.Binary.Put
Expand Down Expand Up @@ -145,44 +146,27 @@ putErl (ErlNewRef node creation id) = do
mapM_ putWord8 id

getErl = do
tag <- liftM chr getC
tag <- chr <$> getC
case tag of
'a' -> liftM ErlInt getC
'b' -> liftM ErlInt getN
'd' -> getn >>= liftM ErlAtom . getA
'e' -> do
node <- getErl
id <- getN
creation <- getC
return $ ErlRef node id creation
'f' -> do
node <- getErl
id <- getN
creation <- getC
return $ ErlPort node id creation
'g' -> do
node <- getErl
id <- getN
serial <- getN
creation <- getC
return $ ErlPid node id serial creation
'h' -> getC >>= \len -> liftM ErlTuple $ forM [1..len] (const getErl)
'i' -> getN >>= \len -> liftM ErlTuple $ forM [1..len] (const getErl)
'a' -> ErlInt <$> getC
'b' -> ErlInt <$> getN
'd' -> ErlAtom <$> (getA =<< getn)
'e' -> ErlRef <$> getErl <*> getN <*> getC
'f' -> ErlPort <$> getErl <*> getN <*> getC
'g' -> ErlPid <$> getErl <*> getN <*> getN <*> getC
'h' -> ErlTuple <$> (getC >>= getCount getErl)
'i' -> ErlTuple <$> (getN >>= getCount getErl)
'j' -> return ErlNull
'k' -> getn >>= liftM ErlString . getA
'k' -> ErlString <$> (getA =<< getn)
'l' -> do
len <- getN
list <- liftM ErlList $ forM [1..len] (const getErl)
list <- ErlList <$> (getN >>= getCount getErl)
null <- getErl
assert (null == ErlNull) $ return list
'm' -> getN >>= liftM ErlBinary . geta
'r' -> do
len <- getn
node <- getErl
creation <- getC
id <- forM [1..4*len] (const getWord8)
return $ ErlNewRef node creation id
'm' -> ErlBinary <$> (geta =<< getN)
'r' -> ErlNewRef <$> getErl <*> getC <*> (getn >>= getCount getWord8)
x -> error [x]
where
getCount x len = forM [1..len] (const x)

tag = putC . ord

Expand Down