From fda80933f98f6998ac872ab617026ecf06e4768c Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 5 Dec 2013 04:58:26 +0400 Subject: Rename ClientInfo datatype to Fingerprint --- bittorrent.cabal | 4 +-- src/Network/BitTorrent/Core/Fingerprint.hs | 32 +++++++++++------------ src/Network/BitTorrent/Core/PeerAddr.hs | 2 +- src/Network/BitTorrent/Core/PeerId.hs | 20 +++++++------- src/Network/BitTorrent/Exchange/Message.hs | 2 +- tests/Data/Torrent/ClientSpec.hs | 33 ------------------------ tests/Network/BitTorrent/Core/FingerprintSpec.hs | 33 ++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 63 deletions(-) delete mode 100644 tests/Data/Torrent/ClientSpec.hs create mode 100644 tests/Network/BitTorrent/Core/FingerprintSpec.hs diff --git a/bittorrent.cabal b/bittorrent.cabal index afd9edb0..6743e2e4 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal @@ -151,14 +151,14 @@ test-suite spec type: exitcode-stdio-1.0 hs-source-dirs: tests main-is: Spec.hs - other-modules: Data.Torrent.ClientSpec - Data.Torrent.InfoHashSpec + other-modules: Data.Torrent.InfoHashSpec Data.Torrent.LayoutSpec Data.Torrent.MagnetSpec Data.Torrent.MetainfoSpec Data.Torrent.ProgressSpec Network.BitTorrent.CoreSpec Network.BitTorrent.Core.PeerIdSpec + Network.BitTorrent.Core.FingerprintSpec Network.BitTorrent.Tracker.MessageSpec Network.BitTorrent.Tracker.RPC.HTTPSpec Network.BitTorrent.Tracker.RPC.UDPSpec diff --git a/src/Network/BitTorrent/Core/Fingerprint.hs b/src/Network/BitTorrent/Core/Fingerprint.hs index e2fbb777..a81edd8b 100644 --- a/src/Network/BitTorrent/Core/Fingerprint.hs +++ b/src/Network/BitTorrent/Core/Fingerprint.hs @@ -5,7 +5,7 @@ -- Stability : experimental -- Portability : portable -- --- 'ClientInfo' is used to identify the client implementation and +-- 'Fingerprint' is used to identify the client implementation and -- version which also contained in 'Peer'. For exsample first 6 -- bytes of peer id of this this library are @-HS0100-@ while for -- mainline we have @M4-3-6--@. We could extract this info and @@ -23,8 +23,8 @@ {-# OPTIONS -fno-warn-orphans #-} module Network.BitTorrent.Core.Fingerprint ( ClientImpl (..) - , ClientInfo (..) - , libClientInfo + , Fingerprint (..) + , libFingerprint ) where import Data.Default @@ -162,33 +162,33 @@ instance Pretty Version where -- | The all sensible infomation that can be obtained from a peer -- identifier or torrent /createdBy/ field. -data ClientInfo = ClientInfo { - ciImpl :: ClientImpl +data Fingerprint = Fingerprint + { ciImpl :: ClientImpl , ciVersion :: Version } deriving (Show, Eq, Ord) -- | Unrecognized client implementation. -instance Default ClientInfo where - def = ClientInfo def def +instance Default Fingerprint where + def = Fingerprint def def {-# INLINE def #-} -- | Example: @\"BitComet-1.2\" == ClientInfo IBitComet (Version [1, 2] [])@ -instance IsString ClientInfo where +instance IsString Fingerprint where fromString str - | _ : ver <- _ver = ClientInfo (fromString impl) (fromString ver) + | _ : ver <- _ver = Fingerprint (fromString impl) (fromString ver) | otherwise = error $ "fromString: invalid client info string" ++ str where (impl, _ver) = L.span ((/=) '-') str -instance Pretty ClientInfo where - pretty ClientInfo {..} = pretty ciImpl <+> "version" <+> pretty ciVersion +instance Pretty Fingerprint where + pretty Fingerprint {..} = pretty ciImpl <+> "version" <+> pretty ciVersion --- | Client info of this (the bittorrent library) package. Normally, --- applications should introduce its own idenitifiers, otherwise they --- can use 'libClientInfo' value. +-- | Fingerprint of this (the bittorrent library) package. Normally, +-- applications should introduce its own fingerprints, otherwise they +-- can use 'libFingerprint' value. -- -libClientInfo :: ClientInfo -libClientInfo = ClientInfo IlibHSbittorrent version +libFingerprint :: Fingerprint +libFingerprint = Fingerprint IlibHSbittorrent version {----------------------------------------------------------------------- -- For torrent file diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index 81754e5e..5173c4fc 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs @@ -89,7 +89,7 @@ instance Serialize PeerAddr where instance Pretty PeerAddr where pretty p @ PeerAddr {..} - | Just pid <- peerID = pretty (clientInfo pid) <+> "at" <+> paddr + | Just pid <- peerID = pretty (fingerprint pid) <+> "at" <+> paddr | otherwise = paddr where paddr = text (show (peerSockAddr p)) diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs index 8deb854a..f30308d4 100644 --- a/src/Network/BitTorrent/Core/PeerId.hs +++ b/src/Network/BitTorrent/Core/PeerId.hs @@ -30,7 +30,7 @@ module Network.BitTorrent.Core.PeerId , defaultVersionNumber -- * Decoding - , clientInfo + , fingerprint ) where import Control.Applicative @@ -175,7 +175,7 @@ defaultClientId = "HS" -- package. Version is taken from .cabal file. defaultVersionNumber :: ByteString defaultVersionNumber = BS.take 4 $ BC.pack $ foldMap show $ - versionBranch $ ciVersion libClientInfo + versionBranch $ ciVersion libFingerprint {----------------------------------------------------------------------- -- Generation @@ -294,23 +294,23 @@ parseImpl = f . BC.unpack -- peer id uses unknown coding style then client info returned is -- 'def'. -- -clientInfo :: PeerId -> ClientInfo -clientInfo pid = either (const def) id $ runGet getCI (getPeerId pid) +fingerprint :: PeerId -> Fingerprint +fingerprint pid = either (const def) id $ runGet getCI (getPeerId pid) where getCI = do leading <- BS.w2c <$> getWord8 case leading of - '-' -> ClientInfo <$> getAzureusImpl <*> getAzureusVersion - 'M' -> ClientInfo <$> pure IMainline <*> getMainlineVersion - 'e' -> ClientInfo <$> getBitCometImpl <*> getBitCometVersion - 'F' -> ClientInfo <$> getBitCometImpl <*> getBitCometVersion + '-' -> Fingerprint <$> getAzureusImpl <*> getAzureusVersion + 'M' -> Fingerprint <$> pure IMainline <*> getMainlineVersion + 'e' -> Fingerprint <$> getBitCometImpl <*> getBitCometVersion + 'F' -> Fingerprint <$> getBitCometImpl <*> getBitCometVersion c -> do c1 <- w2c <$> lookAhead getWord8 if c1 == 'P' then do _ <- getWord8 - ClientInfo <$> pure IOpera <*> getOperaVersion - else ClientInfo <$> pure (getShadowImpl c) <*> getShadowVersion + Fingerprint <$> pure IOpera <*> getOperaVersion + else Fingerprint <$> pure (getShadowImpl c) <*> getShadowVersion getMainlineVersion = do str <- BC.unpack <$> getByteString 7 diff --git a/src/Network/BitTorrent/Exchange/Message.hs b/src/Network/BitTorrent/Exchange/Message.hs index 8a88b761..85ad76d6 100644 --- a/src/Network/BitTorrent/Exchange/Message.hs +++ b/src/Network/BitTorrent/Exchange/Message.hs @@ -203,7 +203,7 @@ instance Serialize Handshake where instance Pretty Handshake where pretty Handshake {..} - = text (BC.unpack hsProtocol) <+> pretty (clientInfo hsPeerId) + = text (BC.unpack hsProtocol) <+> pretty (fingerprint hsPeerId) -- | Get handshake message size in bytes from the length of protocol -- string. diff --git a/tests/Data/Torrent/ClientSpec.hs b/tests/Data/Torrent/ClientSpec.hs deleted file mode 100644 index 4bc881c3..00000000 --- a/tests/Data/Torrent/ClientSpec.hs +++ /dev/null @@ -1,33 +0,0 @@ --- | see -module Data.Torrent.ClientSpec (spec) where -import Test.Hspec -import Network.BitTorrent.Core.PeerId - -spec :: Spec -spec = do - describe "client info" $ do - it "decode mainline encoded peer id" $ do - clientInfo "M4-3-6--xxxxxxxxxxxx" `shouldBe` "Mainline-4.3.6" - clientInfo "M4-20-8-xxxxxxxxxxxx" `shouldBe` "Mainline-4.20.8" - - it "decode azureus encoded peer id" $ do - clientInfo "-AZ2060-xxxxxxxxxxxx" `shouldBe` "Azureus-2060" - clientInfo "-BS0000-xxxxxxxxxxxx" `shouldBe` "BTSlave-0" - - it "decode Shad0w style peer id" $ do - clientInfo "S58B-----xxxxxxxxxxx" `shouldBe` "Shadow-5.8.11" - clientInfo "T58B-----xxxxxxxxxxx" `shouldBe` "BitTornado-5.8.11" - - it "decode bitcomet style peer id" $ do - clientInfo "exbc01xxxxxxxxxxxxxx" `shouldBe` "BitComet-48.49" - clientInfo "FUTB01xxxxxxxxxxxxxx" `shouldBe` "BitComet-48.49" - clientInfo "exbc01LORDxxxxxxxxxx" `shouldBe` "BitLord-48.49" - - it "decode opera style peer id" $ do - clientInfo "OP0123xxxxxxxxxxxxxx" `shouldBe` "Opera-123" - - it "decode ML donkey style peer id" $ do - clientInfo "-ML2.7.2-xxxxxxxxxxx" `shouldBe` "MLdonkey-0" - --- TODO XBT, Bits on Wheels, Queen Bee, BitTyrant, TorrenTopia, --- BitSpirit, Rufus, G3 Torrent, FlashGet \ No newline at end of file diff --git a/tests/Network/BitTorrent/Core/FingerprintSpec.hs b/tests/Network/BitTorrent/Core/FingerprintSpec.hs new file mode 100644 index 00000000..df62442a --- /dev/null +++ b/tests/Network/BitTorrent/Core/FingerprintSpec.hs @@ -0,0 +1,33 @@ +-- | see +module Network.BitTorrent.Core.FingerprintSpec (spec) where +import Test.Hspec +import Network.BitTorrent.Core.PeerId + +spec :: Spec +spec = do + describe "client info" $ do + it "decode mainline encoded peer id" $ do + fingerprint "M4-3-6--xxxxxxxxxxxx" `shouldBe` "Mainline-4.3.6" + fingerprint "M4-20-8-xxxxxxxxxxxx" `shouldBe` "Mainline-4.20.8" + + it "decode azureus encoded peer id" $ do + fingerprint "-AZ2060-xxxxxxxxxxxx" `shouldBe` "Azureus-2060" + fingerprint "-BS0000-xxxxxxxxxxxx" `shouldBe` "BTSlave-0" + + it "decode Shad0w style peer id" $ do + fingerprint "S58B-----xxxxxxxxxxx" `shouldBe` "Shadow-5.8.11" + fingerprint "T58B-----xxxxxxxxxxx" `shouldBe` "BitTornado-5.8.11" + + it "decode bitcomet style peer id" $ do + fingerprint "exbc01xxxxxxxxxxxxxx" `shouldBe` "BitComet-48.49" + fingerprint "FUTB01xxxxxxxxxxxxxx" `shouldBe` "BitComet-48.49" + fingerprint "exbc01LORDxxxxxxxxxx" `shouldBe` "BitLord-48.49" + + it "decode opera style peer id" $ do + fingerprint "OP0123xxxxxxxxxxxxxx" `shouldBe` "Opera-123" + + it "decode ML donkey style peer id" $ do + fingerprint "-ML2.7.2-xxxxxxxxxxx" `shouldBe` "MLdonkey-0" + +-- TODO XBT, Bits on Wheels, Queen Bee, BitTyrant, TorrenTopia, +-- BitSpirit, Rufus, G3 Torrent, FlashGet \ No newline at end of file -- cgit v1.2.3