summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Network/BitTorrent/DHT/Query.hs21
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs6
-rw-r--r--src/Network/KRPC/Manager.hs20
-rw-r--r--src/Network/KRPC/Message.hs7
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
72import Data.List as L 72import Data.List as L
73import Data.Monoid 73import Data.Monoid
74import Data.Text as T 74import Data.Text as T
75import Data.BEncode (BValue)
76import qualified Data.Set as Set 75import qualified Data.Set as Set
77 ;import Data.Set (Set) 76 ;import Data.Set (Set)
78import Network 77import Network
@@ -85,13 +84,20 @@ import Network.KRPC hiding (Options, def)
85import Network.KRPC.Message (ReflectedIP(..)) 84import Network.KRPC.Message (ReflectedIP(..))
86import Network.KRPC.Manager (QueryFailure(..)) 85import Network.KRPC.Manager (QueryFailure(..))
87import Data.Torrent 86import Data.Torrent
88import Network.BitTorrent.Address
89import Network.BitTorrent.DHT.Message 87import Network.BitTorrent.DHT.Message
90import Network.BitTorrent.DHT.Routing as R 88import Network.BitTorrent.DHT.Routing as R
91import Network.BitTorrent.DHT.Session 89import Network.BitTorrent.DHT.Session
92import Control.Concurrent.STM 90import Control.Concurrent.STM
93import qualified Network.BitTorrent.DHT.Search as Search 91import qualified Network.BitTorrent.DHT.Search as Search
92#ifdef VERSION_bencoding
93import Network.BitTorrent.Address
94import Data.BEncode (BValue)
94import Network.DHT.Mainline 95import Network.DHT.Mainline
96#else
97import Network.BitTorrent.Address hiding (NodeId)
98import Data.ByteString (ByteString)
99import Data.Tox
100#endif
95 101
96{----------------------------------------------------------------------- 102{-----------------------------------------------------------------------
97-- Handlers 103-- Handlers
@@ -99,12 +105,23 @@ import Network.DHT.Mainline
99 105
100nodeHandler :: ( Address ip 106nodeHandler :: ( 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
104nodeHandler action = handler mainline $ \ sockAddr qry -> do 115nodeHandler 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
120nodeHandler 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
107import Network.KRPC as KRPC hiding (Options, def) 107import Network.KRPC as KRPC hiding (Options, def)
108import qualified Network.KRPC as KRPC (def) 108import qualified Network.KRPC as KRPC (def)
109import Network.KRPC.Message (KMessageOf) 109import Network.KRPC.Message (KMessageOf)
110#ifdef VERSION_bencoding
110import Data.BEncode (BValue) 111import Data.BEncode (BValue)
112#endif
111import Network.BitTorrent.Address 113import Network.BitTorrent.Address
112import Network.BitTorrent.DHT.ContactInfo (PeerStore) 114import Network.BitTorrent.DHT.ContactInfo (PeerStore)
113import qualified Network.BitTorrent.DHT.ContactInfo as P 115import 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
317type NodeHandler ip = Handler (DHT ip) KMessageOf BValue 320type NodeHandler ip = Handler (DHT ip) KMessageOf BValue
321#else
322type 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'.
186newManager :: Options -- ^ various protocol options; 190newManager :: 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.
190newManager opts @ Options {..} servAddr handlers = do 198newManager 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
221withManager :: Options -> SockAddr -> [Handler h KMessageOf BValue] 230withManager :: Options -> SockAddr -> [Handler h KMessageOf BValue]
231#else
232withManager :: Options -> SockAddr -> [Handler h KMessageOf BC.ByteString]
233#endif
222 -> (Manager h -> IO a) -> IO a 234 -> (Manager h -> IO a) -> IO a
223withManager opts addr hs = bracket (newManager opts addr hs) closeManager 235withManager 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
422runHandler :: MonadKRPC h m 434runHandler :: 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
424runHandler h addr m = Lifted.catches wrapper failbacks 440runHandler 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
360type KMessage = Tox.Message 364type KMessageOf = Tox.Message
365type KMessage = KMessageOf B.ByteString
361#endif 366#endif