diff options
Diffstat (limited to 'src/Network/BitTorrent/Core/PeerId.hs')
-rw-r--r-- | src/Network/BitTorrent/Core/PeerId.hs | 18 |
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 @@ | |||
15 | module Network.BitTorrent.Core.PeerId | 15 | module 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 | ||
61 | import Data.Torrent.Client | 62 | import 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. |
66 | newtype PeerId = PeerId { getPeerId :: ByteString } | 67 | newtype PeerId = PeerId { getPeerId :: ByteString } |
67 | deriving (Show, Eq, Ord, BEncode, ToJSON, FromJSON) | 68 | deriving (Show, Eq, Ord, BEncode, ToJSON, FromJSON) |
68 | 69 | ||
70 | peerIdLen :: Int | ||
71 | peerIdLen = 20 | ||
72 | |||
69 | instance Serialize PeerId where | 73 | instance Serialize PeerId where |
70 | put = putByteString . getPeerId | 74 | put = putByteString . getPeerId |
71 | get = PeerId <$> getBytes 20 | 75 | get = PeerId <$> getBytes peerIdLen |
72 | 76 | ||
73 | instance URLShow PeerId where | 77 | instance URLShow PeerId where |
74 | urlShow = BC.unpack . getPeerId | 78 | urlShow = BC.unpack . getPeerId |
75 | 79 | ||
76 | instance IsString PeerId where | 80 | instance 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 | ||
84 | instance Pretty PeerId where | 87 | instance Pretty PeerId where |
85 | pretty = text . BC.unpack . getPeerId | 88 | pretty = text . BC.unpack . getPeerId |
86 | 89 | ||
90 | byteStringToPeerId :: BS.ByteString -> Maybe PeerId | ||
91 | byteStringToPeerId bs | ||
92 | | BS.length bs == peerIdLen = Just (PeerId bs) | ||
93 | | otherwise = Nothing | ||
94 | |||
87 | {----------------------------------------------------------------------- | 95 | {----------------------------------------------------------------------- |
88 | -- Encoding | 96 | -- Encoding |
89 | -----------------------------------------------------------------------} | 97 | -----------------------------------------------------------------------} |