diff options
-rw-r--r-- | krpc.cabal | 5 | ||||
-rw-r--r-- | src/Network/KRPC.hs | 8 | ||||
-rw-r--r-- | src/Network/KRPC/Message.hs | 47 |
3 files changed, 28 insertions, 32 deletions
@@ -40,13 +40,10 @@ library | |||
40 | , Network.KRPC.Message | 40 | , Network.KRPC.Message |
41 | build-depends: base == 4.* | 41 | build-depends: base == 4.* |
42 | , bytestring >= 0.10 | 42 | , bytestring >= 0.10 |
43 | |||
44 | , lifted-base >= 0.1.1 | 43 | , lifted-base >= 0.1.1 |
45 | , transformers >= 0.2 | 44 | , transformers >= 0.2 |
46 | , monad-control >= 0.3 | 45 | , monad-control >= 0.3 |
47 | 46 | , bencoding >= 0.4.3 | |
48 | , bencoding == 0.4.* | ||
49 | |||
50 | , network >= 2.3 | 47 | , network >= 2.3 |
51 | 48 | ||
52 | if impl(ghc < 7.6) | 49 | if impl(ghc < 7.6) |
diff --git a/src/Network/KRPC.hs b/src/Network/KRPC.hs index 8e158f48..2c3a1b48 100644 --- a/src/Network/KRPC.hs +++ b/src/Network/KRPC.hs | |||
@@ -208,7 +208,7 @@ call :: forall req resp host. | |||
208 | (MonadBaseControl IO host, MonadIO host, KRPC req resp) | 208 | (MonadBaseControl IO host, MonadIO host, KRPC req resp) |
209 | => SockAddr -> req -> host resp | 209 | => SockAddr -> req -> host resp |
210 | call addr arg = liftIO $ withRemote $ \sock -> do | 210 | call addr arg = liftIO $ withRemote $ \sock -> do |
211 | sendMessage (KQuery name (toBEncode arg)) addr sock | 211 | sendMessage (KQuery (toBEncode arg) name undefined) addr sock |
212 | getResult sock | 212 | getResult sock |
213 | where | 213 | where |
214 | Method name = method :: Method req resp | 214 | Method name = method :: Method req resp |
@@ -231,12 +231,12 @@ handler body = (name, newbody) | |||
231 | Method name = method :: Method req resp | 231 | Method name = method :: Method req resp |
232 | 232 | ||
233 | {-# INLINE newbody #-} | 233 | {-# INLINE newbody #-} |
234 | newbody addr q = | 234 | newbody addr KQuery {..} = |
235 | case fromBEncode (queryArgs q) of | 235 | case fromBEncode queryArgs of |
236 | Left e -> return (Left (ProtocolError (BC.pack e))) | 236 | Left e -> return (Left (ProtocolError (BC.pack e))) |
237 | Right a -> do | 237 | Right a -> do |
238 | r <- body addr a | 238 | r <- body addr a |
239 | return (Right (KResponse (toBEncode r))) | 239 | return (Right (KResponse (toBEncode r) queryId)) |
240 | 240 | ||
241 | sockAddrFamily :: SockAddr -> Family | 241 | sockAddrFamily :: SockAddr -> Family |
242 | sockAddrFamily (SockAddrInet _ _ ) = AF_INET | 242 | sockAddrFamily (SockAddrInet _ _ ) = AF_INET |
diff --git a/src/Network/KRPC/Message.hs b/src/Network/KRPC/Message.hs index 854b733c..1a004c64 100644 --- a/src/Network/KRPC/Message.hs +++ b/src/Network/KRPC/Message.hs | |||
@@ -100,6 +100,8 @@ serverError = ServerError . BC.pack . show | |||
100 | 100 | ||
101 | type MethodName = ByteString | 101 | type MethodName = ByteString |
102 | 102 | ||
103 | type TransactionId = ByteString | ||
104 | |||
103 | -- | Query used to signal that caller want to make procedure call to | 105 | -- | Query used to signal that caller want to make procedure call to |
104 | -- callee and pass arguments in. Therefore query may be only sent from | 106 | -- callee and pass arguments in. Therefore query may be only sent from |
105 | -- client to server but not in the opposite direction. | 107 | -- client to server but not in the opposite direction. |
@@ -109,27 +111,24 @@ type MethodName = ByteString | |||
109 | -- > { "y" : "q", "q" : "<method_name>", "a" : [<arg1>, <arg2>, ...] } | 111 | -- > { "y" : "q", "q" : "<method_name>", "a" : [<arg1>, <arg2>, ...] } |
110 | -- | 112 | -- |
111 | data KQuery = KQuery | 113 | data KQuery = KQuery |
112 | { queryMethod :: !MethodName | 114 | { queryArgs :: !BValue |
113 | , queryArgs :: !BValue | 115 | , queryMethod :: !MethodName |
116 | , queryId :: !TransactionId | ||
114 | } deriving (Show, Read, Eq, Ord, Typeable) | 117 | } deriving (Show, Read, Eq, Ord, Typeable) |
115 | 118 | ||
116 | instance BEncode KQuery where | 119 | instance BEncode KQuery where |
117 | {-# SPECIALIZE instance BEncode KQuery #-} | 120 | toBEncode KQuery {..} = toDict $ |
118 | {-# INLINE toBEncode #-} | 121 | "a" .=! queryArgs |
119 | toBEncode (KQuery m args) = toDict $ | 122 | .: "q" .=! queryMethod |
120 | "a" .=! args | 123 | .: "t" .=! queryId |
121 | .: "q" .=! m | ||
122 | .: "y" .=! ("q" :: ByteString) | 124 | .: "y" .=! ("q" :: ByteString) |
123 | .: endDict | 125 | .: endDict |
126 | {-# INLINE toBEncode #-} | ||
124 | 127 | ||
128 | fromBEncode = fromDict $ do | ||
129 | lookAhead $ match "y" (BString "q") | ||
130 | KQuery <$>! "a" <*>! "q" <*>! "t" | ||
125 | {-# INLINE fromBEncode #-} | 131 | {-# INLINE fromBEncode #-} |
126 | fromBEncode bv @ (BDict d) | ||
127 | | BE.lookup "y" d == Just (BString "q") = (`fromDict` bv) $ do | ||
128 | a <- field (req "a") | ||
129 | q <- field (req "q") | ||
130 | return $! KQuery q a | ||
131 | |||
132 | fromBEncode _ = decodingError "KQuery" | ||
133 | 132 | ||
134 | -- | KResponse used to signal that callee successufully process a | 133 | -- | KResponse used to signal that callee successufully process a |
135 | -- procedure call and to return values from procedure. KResponse should | 134 | -- procedure call and to return values from procedure. KResponse should |
@@ -140,20 +139,20 @@ instance BEncode KQuery where | |||
140 | -- | 139 | -- |
141 | -- > { "y" : "r", "r" : [<val1>, <val2>, ...] } | 140 | -- > { "y" : "r", "r" : [<val1>, <val2>, ...] } |
142 | -- | 141 | -- |
143 | newtype KResponse = KResponse | 142 | data KResponse = KResponse |
144 | { respVals :: BValue | 143 | { respVals :: BValue |
144 | , respId :: TransactionId | ||
145 | } deriving (Show, Read, Eq, Ord, Typeable) | 145 | } deriving (Show, Read, Eq, Ord, Typeable) |
146 | 146 | ||
147 | instance BEncode KResponse where | 147 | instance BEncode KResponse where |
148 | {-# INLINE toBEncode #-} | 148 | toBEncode KResponse {..} = toDict $ |
149 | toBEncode (KResponse vals) = toDict $ | 149 | "r" .=! respVals |
150 | "r" .=! vals | 150 | .: "t" .=! respId |
151 | .: "y" .=! ("r" :: ByteString) | 151 | .: "y" .=! ("r" :: ByteString) |
152 | .: endDict | 152 | .: endDict |
153 | {-# INLINE toBEncode #-} | ||
153 | 154 | ||
154 | {-# INLINE fromBEncode #-} | 155 | fromBEncode = fromDict $ do |
155 | fromBEncode bv @ (BDict d) | 156 | lookAhead $ match "y" (BString "r") |
156 | | BE.lookup "y" d == Just (BString "r") = (`fromDict` bv) $ do | 157 | KResponse <$>! "r" <*>! "t" |
157 | KResponse <$>! "r" | 158 | {-# INLINE fromBEncode #-} \ No newline at end of file |
158 | |||
159 | fromBEncode _ = decodingError "KDict" | ||