summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Core
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-28 06:28:06 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-28 06:28:06 +0400
commitfc4ed85a313f93b4fb0c46b500ee12c38e94df68 (patch)
tree3eed36a608878fe5d232ab5d84df1e4ffefed24a /src/Network/BitTorrent/Core
parent533068e7ebbf3ae5f15bd7b65312a69ab50052e5 (diff)
Implement AnnounceQuery parsing
Diffstat (limited to 'src/Network/BitTorrent/Core')
-rw-r--r--src/Network/BitTorrent/Core/PeerId.hs18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs
index 2c79739a..a2b03e92 100644
--- a/src/Network/BitTorrent/Core/PeerId.hs
+++ b/src/Network/BitTorrent/Core/PeerId.hs
@@ -15,6 +15,7 @@
15module Network.BitTorrent.Core.PeerId 15module Network.BitTorrent.Core.PeerId
16 ( -- * PeerId 16 ( -- * PeerId
17 PeerId (getPeerId) 17 PeerId (getPeerId)
18 , byteStringToPeerId
18 19
19 -- * Generation 20 -- * Generation
20 , genPeerId 21 , genPeerId
@@ -60,30 +61,37 @@ import Paths_bittorrent (version)
60 61
61import Data.Torrent.Client 62import Data.Torrent.Client
62 63
63-- TODO use unpacked form (length is known statically) 64-- TODO use unpacked Word160 form (length is known statically)
64 65
65-- | Peer identifier is exactly 20 bytes long bytestring. 66-- | Peer identifier is exactly 20 bytes long bytestring.
66newtype PeerId = PeerId { getPeerId :: ByteString } 67newtype PeerId = PeerId { getPeerId :: ByteString }
67 deriving (Show, Eq, Ord, BEncode, ToJSON, FromJSON) 68 deriving (Show, Eq, Ord, BEncode, ToJSON, FromJSON)
68 69
70peerIdLen :: Int
71peerIdLen = 20
72
69instance Serialize PeerId where 73instance Serialize PeerId where
70 put = putByteString . getPeerId 74 put = putByteString . getPeerId
71 get = PeerId <$> getBytes 20 75 get = PeerId <$> getBytes peerIdLen
72 76
73instance URLShow PeerId where 77instance URLShow PeerId where
74 urlShow = BC.unpack . getPeerId 78 urlShow = BC.unpack . getPeerId
75 79
76instance IsString PeerId where 80instance IsString PeerId where
77 fromString str 81 fromString str
78 | BS.length bs == 20 = PeerId bs 82 | BS.length bs == peerIdLen = PeerId bs
79 | otherwise = error $ "Peer id should be 20 bytes long: " 83 | otherwise = error $ "Peer id should be 20 bytes long: " ++ show str
80 ++ show str
81 where 84 where
82 bs = fromString str 85 bs = fromString str
83 86
84instance Pretty PeerId where 87instance Pretty PeerId where
85 pretty = text . BC.unpack . getPeerId 88 pretty = text . BC.unpack . getPeerId
86 89
90byteStringToPeerId :: BS.ByteString -> Maybe PeerId
91byteStringToPeerId bs
92 | BS.length bs == peerIdLen = Just (PeerId bs)
93 | otherwise = Nothing
94
87{----------------------------------------------------------------------- 95{-----------------------------------------------------------------------
88-- Encoding 96-- Encoding
89-----------------------------------------------------------------------} 97-----------------------------------------------------------------------}