diff options
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 21 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 6 | ||||
-rw-r--r-- | src/Network/KRPC/Manager.hs | 20 | ||||
-rw-r--r-- | src/Network/KRPC/Message.hs | 7 |
4 files changed, 51 insertions, 3 deletions
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs index a1934014..e1104cb9 100644 --- a/src/Network/BitTorrent/DHT/Query.hs +++ b/src/Network/BitTorrent/DHT/Query.hs | |||
@@ -72,7 +72,6 @@ import Data.Either | |||
72 | import Data.List as L | 72 | import Data.List as L |
73 | import Data.Monoid | 73 | import Data.Monoid |
74 | import Data.Text as T | 74 | import Data.Text as T |
75 | import Data.BEncode (BValue) | ||
76 | import qualified Data.Set as Set | 75 | import qualified Data.Set as Set |
77 | ;import Data.Set (Set) | 76 | ;import Data.Set (Set) |
78 | import Network | 77 | import Network |
@@ -85,13 +84,20 @@ import Network.KRPC hiding (Options, def) | |||
85 | import Network.KRPC.Message (ReflectedIP(..)) | 84 | import Network.KRPC.Message (ReflectedIP(..)) |
86 | import Network.KRPC.Manager (QueryFailure(..)) | 85 | import Network.KRPC.Manager (QueryFailure(..)) |
87 | import Data.Torrent | 86 | import Data.Torrent |
88 | import Network.BitTorrent.Address | ||
89 | import Network.BitTorrent.DHT.Message | 87 | import Network.BitTorrent.DHT.Message |
90 | import Network.BitTorrent.DHT.Routing as R | 88 | import Network.BitTorrent.DHT.Routing as R |
91 | import Network.BitTorrent.DHT.Session | 89 | import Network.BitTorrent.DHT.Session |
92 | import Control.Concurrent.STM | 90 | import Control.Concurrent.STM |
93 | import qualified Network.BitTorrent.DHT.Search as Search | 91 | import qualified Network.BitTorrent.DHT.Search as Search |
92 | #ifdef VERSION_bencoding | ||
93 | import Network.BitTorrent.Address | ||
94 | import Data.BEncode (BValue) | ||
94 | import Network.DHT.Mainline | 95 | import Network.DHT.Mainline |
96 | #else | ||
97 | import Network.BitTorrent.Address hiding (NodeId) | ||
98 | import Data.ByteString (ByteString) | ||
99 | import Data.Tox | ||
100 | #endif | ||
95 | 101 | ||
96 | {----------------------------------------------------------------------- | 102 | {----------------------------------------------------------------------- |
97 | -- Handlers | 103 | -- Handlers |
@@ -99,12 +105,23 @@ import Network.DHT.Mainline | |||
99 | 105 | ||
100 | nodeHandler :: ( Address ip | 106 | nodeHandler :: ( Address ip |
101 | , KRPC (Query a) (Response b) | 107 | , KRPC (Query a) (Response b) |
108 | #ifdef VERSION_bencoding | ||
102 | , Envelope (Query a) (Response b) ~ BValue ) | 109 | , Envelope (Query a) (Response b) ~ BValue ) |
110 | #else | ||
111 | , Envelope (Query a) (Response b) ~ ByteString ) | ||
112 | #endif | ||
103 | => (NodeAddr ip -> a -> DHT ip b) -> NodeHandler ip | 113 | => (NodeAddr ip -> a -> DHT ip b) -> NodeHandler ip |
114 | #ifdef VERSION_bencoding | ||
104 | nodeHandler action = handler mainline $ \ sockAddr qry -> do | 115 | nodeHandler action = handler mainline $ \ sockAddr qry -> do |
105 | let remoteId = queringNodeId qry | 116 | let remoteId = queringNodeId qry |
106 | read_only = queryIsReadOnly qry | 117 | read_only = queryIsReadOnly qry |
107 | q = queryParams qry | 118 | q = queryParams qry |
119 | #else | ||
120 | nodeHandler action = handler (error "TODO TOX Messaging") $ \ sockAddr qry -> do | ||
121 | let remoteId = msgClient qry | ||
122 | read_only = False | ||
123 | q = msgPayload qry | ||
124 | #endif | ||
108 | case fromSockAddr sockAddr of | 125 | case fromSockAddr sockAddr of |
109 | Nothing -> throwIO BadAddress | 126 | Nothing -> throwIO BadAddress |
110 | Just naddr -> do | 127 | Just naddr -> do |
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs index db8e7cff..20dba595 100644 --- a/src/Network/BitTorrent/DHT/Session.hs +++ b/src/Network/BitTorrent/DHT/Session.hs | |||
@@ -107,7 +107,9 @@ import Data.Torrent as Torrent | |||
107 | import Network.KRPC as KRPC hiding (Options, def) | 107 | import Network.KRPC as KRPC hiding (Options, def) |
108 | import qualified Network.KRPC as KRPC (def) | 108 | import qualified Network.KRPC as KRPC (def) |
109 | import Network.KRPC.Message (KMessageOf) | 109 | import Network.KRPC.Message (KMessageOf) |
110 | #ifdef VERSION_bencoding | ||
110 | import Data.BEncode (BValue) | 111 | import Data.BEncode (BValue) |
112 | #endif | ||
111 | import Network.BitTorrent.Address | 113 | import Network.BitTorrent.Address |
112 | import Network.BitTorrent.DHT.ContactInfo (PeerStore) | 114 | import Network.BitTorrent.DHT.ContactInfo (PeerStore) |
113 | import qualified Network.BitTorrent.DHT.ContactInfo as P | 115 | import qualified Network.BitTorrent.DHT.ContactInfo as P |
@@ -314,7 +316,11 @@ instance MonadLogger (DHT ip) where | |||
314 | logger <- asks loggerFun | 316 | logger <- asks loggerFun |
315 | liftIO $ logger loc src lvl (toLogStr msg) | 317 | liftIO $ logger loc src lvl (toLogStr msg) |
316 | 318 | ||
319 | #ifdef VERSION_bencoding | ||
317 | type NodeHandler ip = Handler (DHT ip) KMessageOf BValue | 320 | type NodeHandler ip = Handler (DHT ip) KMessageOf BValue |
321 | #else | ||
322 | type NodeHandler ip = Handler (DHT ip) KMessageOf ByteString | ||
323 | #endif | ||
318 | 324 | ||
319 | -- | Run DHT session. You /must/ properly close session using | 325 | -- | Run DHT session. You /must/ properly close session using |
320 | -- 'closeNode' function, otherwise socket or other scarce resources may | 326 | -- 'closeNode' function, otherwise socket or other scarce resources may |
diff --git a/src/Network/KRPC/Manager.hs b/src/Network/KRPC/Manager.hs index b1e93101..58ac7674 100644 --- a/src/Network/KRPC/Manager.hs +++ b/src/Network/KRPC/Manager.hs | |||
@@ -151,7 +151,11 @@ data Manager h = Manager | |||
151 | , listenerThread :: !(MVar ThreadId) | 151 | , listenerThread :: !(MVar ThreadId) |
152 | , transactionCounter :: {-# UNPACK #-} !TransactionCounter | 152 | , transactionCounter :: {-# UNPACK #-} !TransactionCounter |
153 | , pendingCalls :: {-# UNPACK #-} !PendingCalls | 153 | , pendingCalls :: {-# UNPACK #-} !PendingCalls |
154 | #ifdef VERSION_bencoding | ||
154 | , handlers :: [Handler h KMessageOf BValue] | 155 | , handlers :: [Handler h KMessageOf BValue] |
156 | #else | ||
157 | , handlers :: [Handler h KMessageOf BC.ByteString] | ||
158 | #endif | ||
155 | } | 159 | } |
156 | 160 | ||
157 | -- | A monad which can perform or handle queries. | 161 | -- | A monad which can perform or handle queries. |
@@ -185,7 +189,11 @@ sockAddrFamily (SockAddrCan _ ) = AF_CAN | |||
185 | -- run 'listen'. | 189 | -- run 'listen'. |
186 | newManager :: Options -- ^ various protocol options; | 190 | newManager :: Options -- ^ various protocol options; |
187 | -> SockAddr -- ^ address to listen on; | 191 | -> SockAddr -- ^ address to listen on; |
192 | #ifdef VERSION_bencoding | ||
188 | -> [Handler h KMessageOf BValue] -- ^ handlers to run on incoming queries. | 193 | -> [Handler h KMessageOf BValue] -- ^ handlers to run on incoming queries. |
194 | #else | ||
195 | -> [Handler h KMessageOf BC.ByteString] -- ^ handlers to run on incoming queries. | ||
196 | #endif | ||
189 | -> IO (Manager h) -- ^ new rpc manager. | 197 | -> IO (Manager h) -- ^ new rpc manager. |
190 | newManager opts @ Options {..} servAddr handlers = do | 198 | newManager opts @ Options {..} servAddr handlers = do |
191 | validateOptions opts | 199 | validateOptions opts |
@@ -218,7 +226,11 @@ isActive Manager {..} = liftIO $ isBound sock | |||
218 | 226 | ||
219 | -- | Normally you should use Control.Monad.Trans.Resource.allocate | 227 | -- | Normally you should use Control.Monad.Trans.Resource.allocate |
220 | -- function. | 228 | -- function. |
229 | #ifdef VERSION_bencoding | ||
221 | withManager :: Options -> SockAddr -> [Handler h KMessageOf BValue] | 230 | withManager :: Options -> SockAddr -> [Handler h KMessageOf BValue] |
231 | #else | ||
232 | withManager :: Options -> SockAddr -> [Handler h KMessageOf BC.ByteString] | ||
233 | #endif | ||
222 | -> (Manager h -> IO a) -> IO a | 234 | -> (Manager h -> IO a) -> IO a |
223 | withManager opts addr hs = bracket (newManager opts addr hs) closeManager | 235 | withManager opts addr hs = bracket (newManager opts addr hs) closeManager |
224 | 236 | ||
@@ -420,14 +432,22 @@ handler msging body = (name, wrapper) | |||
420 | Right a -> Right . seal <$> body addr a | 432 | Right a -> Right . seal <$> body addr a |
421 | 433 | ||
422 | runHandler :: MonadKRPC h m | 434 | runHandler :: MonadKRPC h m |
435 | #ifdef VERSION_bencoding | ||
423 | => HandlerBody h KMessageOf BValue -> SockAddr -> KQuery -> m KResult | 436 | => HandlerBody h KMessageOf BValue -> SockAddr -> KQuery -> m KResult |
437 | #else | ||
438 | => HandlerBody h KMessageOf BC.ByteString -> SockAddr -> KQuery -> m KResult | ||
439 | #endif | ||
424 | runHandler h addr m = Lifted.catches wrapper failbacks | 440 | runHandler h addr m = Lifted.catches wrapper failbacks |
425 | where | 441 | where |
426 | signature = querySignature (queryMethod m) (queryId m) addr | 442 | signature = querySignature (queryMethod m) (queryId m) addr |
427 | 443 | ||
428 | wrapper = do | 444 | wrapper = do |
429 | $(logDebugS) "handler.quered" signature | 445 | $(logDebugS) "handler.quered" signature |
446 | #ifdef VERSION_bencoding | ||
430 | result <- liftHandler (h addr (Q m)) | 447 | result <- liftHandler (h addr (Q m)) |
448 | #else | ||
449 | result <- liftHandler (h addr m) | ||
450 | #endif | ||
431 | 451 | ||
432 | case result of | 452 | case result of |
433 | Left msg -> do | 453 | Left msg -> do |
diff --git a/src/Network/KRPC/Message.hs b/src/Network/KRPC/Message.hs index 19f9fc9e..2f5f6729 100644 --- a/src/Network/KRPC/Message.hs +++ b/src/Network/KRPC/Message.hs | |||
@@ -34,7 +34,9 @@ module Network.KRPC.Message | |||
34 | , unknownMessage | 34 | , unknownMessage |
35 | 35 | ||
36 | -- * Query | 36 | -- * Query |
37 | #ifdef VERSION_bencoding | ||
37 | , KQueryOf(..) | 38 | , KQueryOf(..) |
39 | #endif | ||
38 | , KQuery | 40 | , KQuery |
39 | #ifndef VERSION_bencoding | 41 | #ifndef VERSION_bencoding |
40 | , queryArgs | 42 | , queryArgs |
@@ -44,7 +46,9 @@ module Network.KRPC.Message | |||
44 | , MethodName | 46 | , MethodName |
45 | 47 | ||
46 | -- * Response | 48 | -- * Response |
49 | #ifdef VERSION_bencoding | ||
47 | , KResponseOf(..) | 50 | , KResponseOf(..) |
51 | #endif | ||
48 | , KResponse | 52 | , KResponse |
49 | , ReflectedIP(..) | 53 | , ReflectedIP(..) |
50 | 54 | ||
@@ -357,5 +361,6 @@ instance BEncode KMessage where | |||
357 | <|> E <$> fromBEncode b | 361 | <|> E <$> fromBEncode b |
358 | <|> decodingError "KMessage: unknown message or message tag" | 362 | <|> decodingError "KMessage: unknown message or message tag" |
359 | #else | 363 | #else |
360 | type KMessage = Tox.Message | 364 | type KMessageOf = Tox.Message |
365 | type KMessage = KMessageOf B.ByteString | ||
361 | #endif | 366 | #endif |