diff options
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/ClientInfo.hs')
-rw-r--r-- | src/Network/BitTorrent/PeerWire/ClientInfo.hs | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/Network/BitTorrent/PeerWire/ClientInfo.hs b/src/Network/BitTorrent/PeerWire/ClientInfo.hs index e2da4d3b..629c883f 100644 --- a/src/Network/BitTorrent/PeerWire/ClientInfo.hs +++ b/src/Network/BitTorrent/PeerWire/ClientInfo.hs | |||
@@ -9,13 +9,14 @@ | |||
9 | -- implementation that can be later printed in human frienly | 9 | -- implementation that can be later printed in human frienly |
10 | -- form. Useful for debugging and logging. | 10 | -- form. Useful for debugging and logging. |
11 | -- | 11 | -- |
12 | -- See http://bittorrent.org/beps/bep_0020.html for more information. | 12 | -- > See http://bittorrent.org/beps/bep_0020.html for more information. |
13 | -- | 13 | -- |
14 | {-# LANGUAGE OverloadedStrings #-} | 14 | {-# LANGUAGE OverloadedStrings #-} |
15 | module Network.BitTorrent.PeerWire.ClientInfo | 15 | module Network.BitTorrent.PeerWire.ClientInfo |
16 | ( ClientInfo(..), ClientVersion, ClientImpl | 16 | ( ClientInfo(..), clientInfo, ppClientInfo, unknownClient |
17 | , clientInfo | 17 | |
18 | , ppClientInfo, ppClientVersion, ppClientImpl | 18 | , ClientVersion, ppClientVersion |
19 | , ClientImpl(..), ppClientImpl | ||
19 | 20 | ||
20 | -- , mkEnumTyDef, mkPars, nameMap | 21 | -- , mkEnumTyDef, mkPars, nameMap |
21 | ) where | 22 | ) where |
@@ -29,6 +30,7 @@ import Data.Serialize.Get | |||
29 | import Network.BitTorrent.PeerID | 30 | import Network.BitTorrent.PeerID |
30 | 31 | ||
31 | 32 | ||
33 | -- | All known client versions. | ||
32 | data ClientImpl = | 34 | data ClientImpl = |
33 | IUnknown | 35 | IUnknown |
34 | | IAres | 36 | | IAres |
@@ -89,7 +91,7 @@ data ClientImpl = | |||
89 | | IXanTorrent | 91 | | IXanTorrent |
90 | | IXtorrent | 92 | | IXtorrent |
91 | | IZipTorrent | 93 | | IZipTorrent |
92 | deriving Show | 94 | deriving (Show, Eq, Ord) |
93 | 95 | ||
94 | parseImpl :: ByteString -> ClientImpl | 96 | parseImpl :: ByteString -> ClientImpl |
95 | parseImpl = f . BC.unpack | 97 | parseImpl = f . BC.unpack |
@@ -156,6 +158,7 @@ parseImpl = f . BC.unpack | |||
156 | f "ZT" = IZipTorrent | 158 | f "ZT" = IZipTorrent |
157 | f _ = IUnknown | 159 | f _ = IUnknown |
158 | 160 | ||
161 | -- | Format client implementation info in human readable form. | ||
159 | ppClientImpl :: ClientImpl -> String | 162 | ppClientImpl :: ClientImpl -> String |
160 | ppClientImpl = tail . show | 163 | ppClientImpl = tail . show |
161 | 164 | ||
@@ -166,6 +169,7 @@ unknownImpl = IUnknown | |||
166 | 169 | ||
167 | type ClientVersion = ByteString | 170 | type ClientVersion = ByteString |
168 | 171 | ||
172 | -- | Format client implementation version in human readable form. | ||
169 | ppClientVersion :: ClientVersion -> String | 173 | ppClientVersion :: ClientVersion -> String |
170 | ppClientVersion = BC.unpack | 174 | ppClientVersion = BC.unpack |
171 | 175 | ||
@@ -173,19 +177,26 @@ unknownVersion :: ClientVersion | |||
173 | unknownVersion = "0000" | 177 | unknownVersion = "0000" |
174 | 178 | ||
175 | 179 | ||
176 | 180 | -- | All useful infomation that can be obtained from a peer | |
181 | -- identifier. | ||
177 | data ClientInfo = ClientInfo { | 182 | data ClientInfo = ClientInfo { |
178 | ciImpl :: ClientImpl | 183 | ciImpl :: ClientImpl |
179 | , ciVersion :: ClientVersion | 184 | , ciVersion :: ClientVersion |
180 | } deriving Show | 185 | } deriving (Show, Eq, Ord) |
181 | 186 | ||
187 | -- | Format client implementation in human readable form. | ||
182 | ppClientInfo :: ClientInfo -> String | 188 | ppClientInfo :: ClientInfo -> String |
183 | ppClientInfo ci = ppClientImpl (ciImpl ci) ++ " version " | 189 | ppClientInfo ci = ppClientImpl (ciImpl ci) ++ " version " |
184 | ++ ppClientVersion (ciVersion ci) | 190 | ++ ppClientVersion (ciVersion ci) |
185 | 191 | ||
192 | -- | Unrecognized client implementation. | ||
186 | unknownClient :: ClientInfo | 193 | unknownClient :: ClientInfo |
187 | unknownClient = ClientInfo unknownImpl unknownVersion | 194 | unknownClient = ClientInfo unknownImpl unknownVersion |
188 | 195 | ||
196 | -- | Tries to extract meaningful information from peer ID bytes. If | ||
197 | -- peer id uses unknown coding style then client info returned is | ||
198 | -- 'unknownClient'. | ||
199 | -- | ||
189 | clientInfo :: PeerID -> ClientInfo | 200 | clientInfo :: PeerID -> ClientInfo |
190 | clientInfo pid = either (const unknownClient) id $ runGet getCI (getPeerID pid) | 201 | clientInfo pid = either (const unknownClient) id $ runGet getCI (getPeerID pid) |
191 | where -- TODO other styles | 202 | where -- TODO other styles |