From 6bb92a610c4874ea3fa37fb15cd55c48f219d6ed Mon Sep 17 00:00:00 2001 From: Sam T Date: Fri, 16 Aug 2013 08:50:08 +0400 Subject: ~ Remove torrent-content modules. --- src/Network/BitTorrent/DHT/Protocol.hs | 2 +- src/Network/BitTorrent/Exchange.hs | 3 +- src/Network/BitTorrent/Exchange/Protocol.hs | 152 ++++------------------------ src/Network/BitTorrent/Sessions.hs | 4 +- src/Network/BitTorrent/Sessions/Types.lhs | 6 +- src/Network/BitTorrent/Tracker.hs | 2 +- src/Network/BitTorrent/Tracker/HTTP.hs | 2 +- src/Network/BitTorrent/Tracker/Protocol.hs | 2 +- src/Network/BitTorrent/Tracker/UDP.hs | 2 +- 9 files changed, 30 insertions(+), 145 deletions(-) (limited to 'src/Network/BitTorrent') diff --git a/src/Network/BitTorrent/DHT/Protocol.hs b/src/Network/BitTorrent/DHT/Protocol.hs index b0100b70..73e5fa68 100644 --- a/src/Network/BitTorrent/DHT/Protocol.hs +++ b/src/Network/BitTorrent/DHT/Protocol.hs @@ -32,7 +32,7 @@ import System.Entropy import Remote.KRPC import Remote.KRPC.Protocol import Data.BEncode -import Data.Torrent +import Data.Torrent.Metainfo import Network.BitTorrent.Peer import Network.BitTorrent.Exchange.Protocol () diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs index 0f1d2833..e81880b4 100644 --- a/src/Network/BitTorrent/Exchange.hs +++ b/src/Network/BitTorrent/Exchange.hs @@ -91,7 +91,8 @@ import Text.PrettyPrint as PP hiding (($$)) import Network -import Data.Bitfield as BF +import Data.Torrent.Block +import Data.Torrent.Bitfield as BF import Network.BitTorrent.Extension import Network.BitTorrent.Exchange.Protocol import Network.BitTorrent.Sessions.Types diff --git a/src/Network/BitTorrent/Exchange/Protocol.hs b/src/Network/BitTorrent/Exchange/Protocol.hs index 00b6795b..3b2472da 100644 --- a/src/Network/BitTorrent/Exchange/Protocol.hs +++ b/src/Network/BitTorrent/Exchange/Protocol.hs @@ -37,16 +37,6 @@ module Network.BitTorrent.Exchange.Protocol , defaultHandshake, defaultBTProtocol, defaultReserved , handshakeMaxSize - -- * Block - , PieceIx, BlockLIx, PieceLIx - , BlockIx(..), ppBlockIx - , Block(..), ppBlock ,blockSize - , pieceIx, blockIx - , blockRange, ixRange, isPiece - - -- ** Defaults - , defaultBlockSize - -- * Regular messages , Message(..) , ppMessage @@ -89,12 +79,28 @@ import Text.PrettyPrint import Network import Network.Socket.ByteString -import Data.Bitfield -import Data.Torrent +import Data.Torrent.Bitfield +import Data.Torrent.Block +import Data.Torrent.Metainfo import Network.BitTorrent.Extension import Network.BitTorrent.Peer +getInt :: S.Get Int +getInt = fromIntegral <$> S.getWord32be +{-# INLINE getInt #-} + +putInt :: S.Putter Int +putInt = S.putWord32be . fromIntegral +{-# INLINE putInt #-} + +getIntB :: B.Get Int +getIntB = fromIntegral <$> B.getWord32be +{-# INLINE getIntB #-} + +putIntB :: Int -> B.Put +putIntB = B.putWord32be . fromIntegral +{-# INLINE putIntB #-} {----------------------------------------------------------------------- Handshake @@ -195,128 +201,6 @@ handshake sock hs = do throwIO $ userError "Handshake info hash do not match." return hs' -{----------------------------------------------------------------------- - Block Index ------------------------------------------------------------------------} - -type BlockLIx = Int -type PieceLIx = Int - - -data BlockIx = BlockIx { - -- | Zero-based piece index. - ixPiece :: {-# UNPACK #-} !PieceLIx - - -- | Zero-based byte offset within the piece. - , ixOffset :: {-# UNPACK #-} !Int - - -- | Block size starting from offset. - , ixLength :: {-# UNPACK #-} !Int - } deriving (Show, Eq) - -$(deriveJSON (L.map toLower . L.dropWhile isLower) ''BlockIx) - -getInt :: S.Get Int -getInt = fromIntegral <$> S.getWord32be -{-# INLINE getInt #-} - -putInt :: S.Putter Int -putInt = S.putWord32be . fromIntegral -{-# INLINE putInt #-} - -getIntB :: B.Get Int -getIntB = fromIntegral <$> B.getWord32be -{-# INLINE getIntB #-} - -putIntB :: Int -> B.Put -putIntB = B.putWord32be . fromIntegral -{-# INLINE putIntB #-} - -instance Serialize BlockIx where - {-# SPECIALIZE instance Serialize BlockIx #-} - get = BlockIx <$> getInt <*> getInt <*> getInt - {-# INLINE get #-} - - put i = do putInt (ixPiece i) - putInt (ixOffset i) - putInt (ixLength i) - {-# INLINE put #-} - -instance Binary BlockIx where - {-# SPECIALIZE instance Binary BlockIx #-} - get = BlockIx <$> getIntB <*> getIntB <*> getIntB - {-# INLINE get #-} - - put BlockIx {..} = do - putIntB ixPiece - putIntB ixOffset - putIntB ixLength - --- | Format block index in human readable form. -ppBlockIx :: BlockIx -> Doc -ppBlockIx BlockIx {..} = - "piece = " <> int ixPiece <> "," <+> - "offset = " <> int ixOffset <> "," <+> - "length = " <> int ixLength - -{----------------------------------------------------------------------- - Block ------------------------------------------------------------------------} - -data Block = Block { - -- | Zero-based piece index. - blkPiece :: {-# UNPACK #-} !PieceLIx - - -- | Zero-based byte offset within the piece. - , blkOffset :: {-# UNPACK #-} !Int - - -- | Payload. - , blkData :: !Lazy.ByteString - } deriving (Show, Eq) - --- | Format block in human readable form. Payload is ommitted. -ppBlock :: Block -> Doc -ppBlock = ppBlockIx . blockIx - -blockSize :: Block -> Int -blockSize blk = fromIntegral (Lazy.length (blkData blk)) -{-# INLINE blockSize #-} - --- | Widely used semi-official block size. -defaultBlockSize :: Int -defaultBlockSize = 16 * 1024 - - -isPiece :: Int -> Block -> Bool -isPiece pieceSize (Block i offset bs) = - offset == 0 - && fromIntegral (Lazy.length bs) == pieceSize - && i >= 0 -{-# INLINE isPiece #-} - -pieceIx :: Int -> Int -> BlockIx -pieceIx i = BlockIx i 0 -{-# INLINE pieceIx #-} - -blockIx :: Block -> BlockIx -blockIx = BlockIx <$> blkPiece <*> blkOffset <*> blockSize - -blockRange :: (Num a, Integral a) => Int -> Block -> (a, a) -blockRange pieceSize blk = (offset, offset + len) - where - offset = fromIntegral pieceSize * fromIntegral (blkPiece blk) - + fromIntegral (blkOffset blk) - len = fromIntegral (Lazy.length (blkData blk)) -{-# INLINE blockRange #-} - -ixRange :: (Num a, Integral a) => Int -> BlockIx -> (a, a) -ixRange pieceSize i = (offset, offset + len) - where - offset = fromIntegral pieceSize * fromIntegral (ixPiece i) - + fromIntegral (ixOffset i) - len = fromIntegral (ixLength i) -{-# INLINE ixRange #-} - {----------------------------------------------------------------------- Regular messages -----------------------------------------------------------------------} diff --git a/src/Network/BitTorrent/Sessions.hs b/src/Network/BitTorrent/Sessions.hs index 1d0d21b4..ba0c60a1 100644 --- a/src/Network/BitTorrent/Sessions.hs +++ b/src/Network/BitTorrent/Sessions.hs @@ -73,8 +73,8 @@ import Network hiding (accept) import Network.BSD import Network.Socket -import Data.Bitfield as BF -import Data.Torrent +import Data.Torrent.Bitfield as BF +import Data.Torrent.Metainfo import Network.BitTorrent.Extension import Network.BitTorrent.Peer import Network.BitTorrent.Sessions.Types diff --git a/src/Network/BitTorrent/Sessions/Types.lhs b/src/Network/BitTorrent/Sessions/Types.lhs index 5571e23a..e62e362f 100644 --- a/src/Network/BitTorrent/Sessions/Types.lhs +++ b/src/Network/BitTorrent/Sessions/Types.lhs @@ -59,8 +59,8 @@ > import Network -> import Data.Bitfield as BF -> import Data.Torrent +> import Data.Torrent.Bitfield as BF +> import Data.Torrent.Metainfo > import Network.BitTorrent.Extension > import Network.BitTorrent.Peer > import Network.BitTorrent.Exchange.Protocol as BT @@ -248,7 +248,7 @@ fresh required extensions. Normally, you would have one client session, however, if we needed, in one application we could have many clients with different peer ID's and different enabled extensions at the same time. - + > -- | > data ClientSession = ClientSession { > -- | Used in handshakes and discovery mechanism. diff --git a/src/Network/BitTorrent/Tracker.hs b/src/Network/BitTorrent/Tracker.hs index e1e6ea71..e98f1e94 100644 --- a/src/Network/BitTorrent/Tracker.hs +++ b/src/Network/BitTorrent/Tracker.hs @@ -39,7 +39,7 @@ import Data.IORef import Network import Network.URI -import Data.Torrent +import Data.Torrent.Metainfo import Network.BitTorrent.Peer import Network.BitTorrent.Sessions.Types import Network.BitTorrent.Tracker.Protocol diff --git a/src/Network/BitTorrent/Tracker/HTTP.hs b/src/Network/BitTorrent/Tracker/HTTP.hs index 0ada154b..f781b847 100644 --- a/src/Network/BitTorrent/Tracker/HTTP.hs +++ b/src/Network/BitTorrent/Tracker/HTTP.hs @@ -30,7 +30,7 @@ import Data.URLEncoded as URL import Network.URI import Network.HTTP -import Data.Torrent +import Data.Torrent.Metainfo import Network.BitTorrent.Tracker.Protocol {----------------------------------------------------------------------- diff --git a/src/Network/BitTorrent/Tracker/Protocol.hs b/src/Network/BitTorrent/Tracker/Protocol.hs index ee395883..c468656f 100644 --- a/src/Network/BitTorrent/Tracker/Protocol.hs +++ b/src/Network/BitTorrent/Tracker/Protocol.hs @@ -44,7 +44,7 @@ import Data.Text (Text) import Data.Text.Encoding import Data.Serialize hiding (Result) import Data.URLEncoded as URL -import Data.Torrent +import Data.Torrent.Metainfo import Network import Network.Socket diff --git a/src/Network/BitTorrent/Tracker/UDP.hs b/src/Network/BitTorrent/Tracker/UDP.hs index 43de7663..13e1298b 100644 --- a/src/Network/BitTorrent/Tracker/UDP.hs +++ b/src/Network/BitTorrent/Tracker/UDP.hs @@ -24,7 +24,7 @@ import Data.Text.Encoding import Network.Socket hiding (Connected) import Network.Socket.ByteString as BS -import Data.Torrent () +import Data.Torrent.Metainfo () import Network.BitTorrent.Tracker.Protocol -- cgit v1.2.3