zeromq-haskell 0.4 → 0.4.1
raw patch · 5 files changed
+134/−77 lines, 5 files
Files
- README +0/−60
- README.md +63/−0
- src/System/ZMQ.hs +59/−14
- src/System/ZMQ/Base.hsc +10/−1
- zeromq-haskell.cabal +2/−2
− README
@@ -1,60 +0,0 @@-This library provides Haskell bindings to zeromq (http://zeromq.org).---· Current status--Version 0.4 - This software currently has *beta* status, i.e. it had-seen limited testing. Changes to its API may still happen.--This software was developed and tested on Linux 2.6.35 with GHC-6.12.3-using zeromq-2.0.8.---· Installation--As usual for Haskell packages this software is installed best via Cabal-(http://www.haskell.org/cabal). In addition to GHC it depends on 0MQ of course.---· Usage--The API mostly follows 0MQ's. Public functions are:--- init-- term-- socket-- close-- setOption-- subscribe-- unsubscribe-- bind-- connect-- send-- send'-- receive-- poll--One difference to 0MQ's API is that sockets are parameterized types, i.e. there-is not one socket type but when creating a socket the desired socket type has-to be specified, e.g. 'Pair' and the resulting socket is of type 'Socket Pair'.-This additional type information is used to ensure that only options applicable-to the socket type can be set, hence ZMQ_SUBSCRIBE and ZMQ_UNSUBSCRIBE which -only apply to ZMQ_SUB sockets have their own functions ('subscribe' and-'unsubscribe') which can only be used with sockets of type 'Socket Sub'.--Other differences are mostly for convenience. Also one does not deal directly-with 0MQ messages, instead these are created internally as needed.---· Examples--The test folder contains some simple tests mostly mimicking the ones that come-with 0MQ.---· Bugs--If you find any bugs or other shortcomings I would greatly appreciate a bug-report, preferably via http://github.com/twittner/zeromq-haskell/issues or e-mail-to toralf.wittner@gmail.com-
+ README.md view
@@ -0,0 +1,63 @@+This library provides Haskell bindings to zeromq (http://zeromq.org).++Current status+--------------++Version 0.4.1 - This software currently has *beta* status, i.e. it had+seen limited testing. Changes to its API may still happen.++This software was developed and tested on Linux 2.6.35 with GHC-6.12.3+using zeromq-2.0.9.++Installation+------------++As usual for Haskell packages this software is installed best via Cabal+(http://www.haskell.org/cabal). In addition to GHC it depends on 0MQ of course.++Usage+-----++The API mostly follows 0MQ's. Public functions are:++- `init`+- `term`+- `socket`+- `close`+- `setOption`+- `getOption`+- `subscribe`+- `unsubscribe`+- `bind`+- `connect`+- `send`+- `send'`+- `receive`+- `moreToReceive`+- `poll`++One difference to 0MQ's API is that sockets are parameterized types, i.e. there+is not one socket type but when creating a socket the desired socket type has+to be specified, e.g. `Pair` and the resulting socket is of type `Socket Pair`.+This additional type information is used to ensure that only options applicable+to the socket type can be set, hence `ZMQ_SUBSCRIBE` and `ZMQ_UNSUBSCRIBE` which +only apply to `ZMQ_SUB` sockets have their own functions (`subscribe` and+`unsubscribe`) which can only be used with sockets of type `Socket Sub`.++Other differences are mostly for convenience. Also one does not deal directly+with 0MQ messages, instead these are created internally as needed.++Examples+--------++The test folder contains some simple tests mostly mimicking the ones that come+with 0MQ.+++Bugs+----++If you find any bugs or other shortcomings I would greatly appreciate a bug+report, preferably via http://github.com/twittner/zeromq-haskell/issues or e-mail+to toralf.wittner@gmail.com+
src/System/ZMQ.hs view
@@ -39,10 +39,10 @@ init, term,- socket, close, setOption,+ getOption, System.ZMQ.subscribe, System.ZMQ.unsubscribe, bind,@@ -50,9 +50,8 @@ send, send', receive,-+ moreToReceive, poll,- device ) where@@ -68,7 +67,7 @@ import Foreign import Foreign.C.Error import Foreign.C.String-import Foreign.C.Types (CInt, CShort)+import Foreign.C.Types (CInt, CShort, CSize) import qualified Data.ByteString as SB import qualified Data.ByteString.Lazy as LB import qualified Data.ByteString.Unsafe as UB@@ -281,9 +280,10 @@ -- | The events to wait for in poll (cf. man zmq_poll) data PollEvent =- In -- ^ ZMQ_POLLIN (incoming messages)- | Out -- ^ ZMQ_POLLOUT (outgoing messages, i.e. at least 1 byte can be written)- | InOut -- ^ ZMQ_POLLIN | ZMQ_POLLOUT+ In -- ^ ZMQ_POLLIN (incoming messages)+ | Out -- ^ ZMQ_POLLOUT (outgoing messages, i.e. at least 1 byte can be written)+ | InOut -- ^ ZMQ_POLLIN | ZMQ_POLLOUT+ | Native -- ^ ZMQ_POLLERR deriving (Eq, Ord, Show) -- | Type representing a descriptor, poll is waiting for@@ -337,6 +337,21 @@ setOption s (SendBuf o) = setIntOpt s sendBuf o setOption s (ReceiveBuf o) = setIntOpt s receiveBuf o +-- | Get the given socket option by passing in some dummy value of+-- that option. The actual value will be returned. Please note that+-- there are certain combatibility constraints w.r.t the socket+-- type (cf. man zmq_setsockopt).+getOption :: Socket a -> SocketOption -> IO SocketOption+getOption s (HighWM _) = HighWM <$> getIntOpt s highWM+getOption s (Swap _) = Swap <$> getIntOpt s swap+getOption s (Affinity _) = Affinity <$> getIntOpt s affinity+getOption s (Identity _) = Identity <$> getStrOpt s identity+getOption s (Rate _) = Rate <$> getIntOpt s rate+getOption s (RecoveryIVL _) = RecoveryIVL <$> getIntOpt s recoveryIVL+getOption s (McastLoop _) = McastLoop <$> getIntOpt s mcastLoop+getOption s (SendBuf _) = SendBuf <$> getIntOpt s sendBuf+getOption s (ReceiveBuf _) = ReceiveBuf <$> getIntOpt s receiveBuf+ -- | Subscribe Socket to given subscription. subscribe :: SubsType a => Socket a -> String -> IO () subscribe s = setStrOpt s B.subscribe@@ -345,6 +360,12 @@ unsubscribe :: SubsType a => Socket a -> String -> IO () unsubscribe s = setStrOpt s B.unsubscribe +-- | Equivalent of ZMQ_RCVMORE, i.e. returns True if a multi-part+-- message currently being read has more parts to follow, otherwise+-- False.+moreToReceive :: Socket a -> IO Bool+moreToReceive s = getBoolOpt s receiveMore+ -- | Bind the socket to the given address (zmq_bind) bind :: Socket a -> String -> IO () bind (Socket s) str = throwErrnoIfMinus1_ "bind" $@@ -408,14 +429,16 @@ newPoll _ f r = F (Fd f) (fromJust r) fromEvent :: PollEvent -> CShort- fromEvent In = fromIntegral . pollVal $ pollIn- fromEvent Out = fromIntegral . pollVal $ pollOut- fromEvent InOut = fromIntegral . pollVal $ pollInOut+ fromEvent In = fromIntegral . pollVal $ pollIn+ fromEvent Out = fromIntegral . pollVal $ pollOut+ fromEvent InOut = fromIntegral . pollVal $ pollInOut+ fromEvent Native = fromIntegral . pollVal $ pollerr toEvent :: CShort -> Maybe PollEvent toEvent e | e == (fromIntegral . pollVal $ pollIn) = Just In | e == (fromIntegral . pollVal $ pollOut) = Just Out | e == (fromIntegral . pollVal $ pollInOut) = Just InOut+ | e == (fromIntegral . pollVal $ pollerr) = Just Native | otherwise = Nothing -- | Launch a ZeroMQ device (zmq_device).@@ -472,16 +495,38 @@ return (Message ptr) setIntOpt :: (Storable b, Integral b) => Socket a -> ZMQOption -> b -> IO ()-setIntOpt (Socket s) (ZMQOption o) i = throwErrnoIfMinus1_ "setIntOpt" $- bracket (newStablePtr i) freeStablePtr $ \ptr ->+setIntOpt (Socket s) (ZMQOption o) i =+ throwErrnoIfMinus1_ "setIntOpt" $ with i $ \ptr -> c_zmq_setsockopt s (fromIntegral o)- (castStablePtrToPtr ptr)+ (castPtr ptr) (fromIntegral . sizeOf $ i) setStrOpt :: Socket a -> ZMQOption -> String -> IO () setStrOpt (Socket s) (ZMQOption o) str = throwErrnoIfMinus1_ "setStrOpt" $ withCStringLen str $ \(cstr, len) ->- c_zmq_setsockopt s (fromIntegral o) (castPtr cstr) (fromIntegral len)+ c_zmq_setsockopt s (fromIntegral o)+ (castPtr cstr)+ (fromIntegral len)++getBoolOpt :: Socket a -> ZMQOption -> IO Bool+getBoolOpt s o = ((1 :: Int64) ==) <$> getIntOpt s o++getIntOpt :: (Storable b, Integral b) => Socket a -> ZMQOption -> IO b+getIntOpt (Socket s) (ZMQOption o) = do+ let i = 0+ bracket (new i) free $ \iptr ->+ bracket (new (fromIntegral . sizeOf $ i :: CSize)) free $ \jptr -> do+ throwErrnoIfMinus1_ "integralOpt" $+ c_zmq_getsockopt s (fromIntegral o) (castPtr iptr) jptr+ peek iptr++getStrOpt :: Socket a -> ZMQOption -> IO String+getStrOpt (Socket s) (ZMQOption o) =+ bracket (mallocBytes 255) free $ \bPtr ->+ bracket (new (255 :: CSize)) free $ \sPtr -> do+ throwErrnoIfMinus1_ "getStrOpt" $+ c_zmq_getsockopt s (fromIntegral o) (castPtr bPtr) sPtr+ peek sPtr >>= \len -> peekCStringLen (bPtr, fromIntegral len) toZMQFlag :: Flag -> ZMQFlag toZMQFlag NoBlock = noBlock
src/System/ZMQ/Base.hsc view
@@ -83,7 +83,8 @@ recoveryIVL = ZMQ_RECOVERY_IVL, mcastLoop = ZMQ_MCAST_LOOP, sendBuf = ZMQ_SNDBUF,- receiveBuf = ZMQ_RCVBUF+ receiveBuf = ZMQ_RCVBUF,+ receiveMore = ZMQ_RCVMORE } newtype ZMQFlag = ZMQFlag { flagVal :: CInt } deriving (Eq, Ord)@@ -98,6 +99,7 @@ #{enum ZMQPollEvent, ZMQPollEvent, pollIn = ZMQ_POLLIN, pollOut = ZMQ_POLLOUT,+ pollerr = ZMQ_POLLERR, pollInOut = ZMQ_POLLIN | ZMQ_POLLOUT } @@ -147,6 +149,13 @@ -> CInt -- option -> Ptr () -- option value -> CSize -- option value size+ -> IO CInt++foreign import ccall unsafe "zmq.h zmq_getsockopt"+ c_zmq_getsockopt :: ZMQSocket+ -> CInt -- option+ -> Ptr () -- option value+ -> Ptr CSize -- option value size ptr -> IO CInt foreign import ccall unsafe "zmq.h zmq_bind"
zeromq-haskell.cabal view
@@ -1,5 +1,5 @@ name: zeromq-haskell-version: 0.4+version: 0.4.1 synopsis: bindings to zeromq description: Bindings to zeromq (http://zeromq.org) category: System, FFI@@ -13,7 +13,7 @@ tested-With: GHC == 6.12.3 cabal-version: >= 1.6.0 build-type: Simple-extra-source-files: README, AUTHORS, test/*.hs, test/perf/*.hs+extra-source-files: README.md, AUTHORS, test/*.hs, test/perf/*.hs library exposed-modules: System.ZMQ