summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-11 09:40:25 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-11 09:40:25 +0400
commitb083c7ea2e9b262ba23deb00cb063819978adb92 (patch)
treea4dcda1df040191c9179a9917cbd5c480aaab10f
parent8e4d3bc8f70f10a45ef86a1a3ec3403d1a78d41c (diff)
Add cost estimatator for WireFailure
-rw-r--r--src/Network/BitTorrent/Exchange/Wire.hs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Exchange/Wire.hs b/src/Network/BitTorrent/Exchange/Wire.hs
index 9b83590a..8eca5120 100644
--- a/src/Network/BitTorrent/Exchange/Wire.hs
+++ b/src/Network/BitTorrent/Exchange/Wire.hs
@@ -18,6 +18,7 @@ module Network.BitTorrent.Exchange.Wire
18 , ChannelSide (..) 18 , ChannelSide (..)
19 , ProtocolError (..) 19 , ProtocolError (..)
20 , WireFailure (..) 20 , WireFailure (..)
21 , peerPenalty
21 , isWireFailure 22 , isWireFailure
22 , disconnectPeer 23 , disconnectPeer
23 24
@@ -158,6 +159,16 @@ data ProtocolError
158instance Pretty ProtocolError where 159instance Pretty ProtocolError where
159 pretty = PP.text . show 160 pretty = PP.text . show
160 161
162errorPenalty :: ProtocolError -> Int
163errorPenalty (InvalidProtocol _) = 1
164errorPenalty (UnexpectedProtocol _) = 1
165errorPenalty (UnexpectedTopic _) = 1
166errorPenalty (UnexpectedPeerId _) = 1
167errorPenalty (UnknownTopic _) = 0
168errorPenalty (HandshakeRefused ) = 1
169errorPenalty (BitfieldAlreadySent _) = 1
170errorPenalty (DisallowedMessage _ _) = 1
171
161-- | Exceptions used to interrupt the current P2P session. 172-- | Exceptions used to interrupt the current P2P session.
162data WireFailure 173data WireFailure
163 -- | Force termination of wire connection. 174 -- | Force termination of wire connection.
@@ -189,6 +200,16 @@ instance Exception WireFailure
189instance Pretty WireFailure where 200instance Pretty WireFailure where
190 pretty = PP.text . show 201 pretty = PP.text . show
191 202
203-- TODO
204-- data Penalty = Ban | Penalty Int
205
206peerPenalty :: WireFailure -> Int
207peerPenalty DisconnectPeer = 0
208peerPenalty PeerDisconnected = 0
209peerPenalty (DecodingError _) = 1
210peerPenalty (ProtocolError e) = errorPenalty e
211peerPenalty (FloodDetected _) = 1
212
192-- | Do nothing with exception, used with 'handle' or 'try'. 213-- | Do nothing with exception, used with 'handle' or 'try'.
193isWireFailure :: Monad m => WireFailure -> m () 214isWireFailure :: Monad m => WireFailure -> m ()
194isWireFailure _ = return () 215isWireFailure _ = return ()