diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-05 23:46:58 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-05 23:46:58 +0400 |
commit | abec3de25b40d23627396d3eb98d1af547cf546c (patch) | |
tree | a8db4d54f8f4a33b213288ecadd2e0fcbc9f481a /src | |
parent | 0362306da8401c8fc4d60fbb537c73afb42a250e (diff) |
Lift the requires function to message class
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/Exchange/Message.hs | 62 |
1 files changed, 46 insertions, 16 deletions
diff --git a/src/Network/BitTorrent/Exchange/Message.hs b/src/Network/BitTorrent/Exchange/Message.hs index 8ef9f3da..eccffc77 100644 --- a/src/Network/BitTorrent/Exchange/Message.hs +++ b/src/Network/BitTorrent/Exchange/Message.hs | |||
@@ -48,7 +48,6 @@ module Network.BitTorrent.Exchange.Message | |||
48 | -- * Messages | 48 | -- * Messages |
49 | , Message (..) | 49 | , Message (..) |
50 | , PeerMessage (..) | 50 | , PeerMessage (..) |
51 | , requires | ||
52 | 51 | ||
53 | -- ** Core messages | 52 | -- ** Core messages |
54 | , StatusUpdate (..) | 53 | , StatusUpdate (..) |
@@ -238,12 +237,20 @@ defaultHandshake = Handshake defaultBTProtocol def | |||
238 | -- Regular messages | 237 | -- Regular messages |
239 | -----------------------------------------------------------------------} | 238 | -----------------------------------------------------------------------} |
240 | 239 | ||
240 | -- | Messages which can be sent after handshaking. Minimal complete | ||
241 | -- definition: 'envelop'. | ||
241 | class PeerMessage a where | 242 | class PeerMessage a where |
242 | -- | Construct a message to be /sent/. | 243 | -- | Construct a message to be /sent/. |
243 | envelop :: ExtendedCaps -- ^ The /receiver/ extended capabilities; | 244 | envelop :: ExtendedCaps -- ^ The /receiver/ extended capabilities; |
244 | -> a -- ^ An regular message; | 245 | -> a -- ^ An regular message; |
245 | -> Message -- ^ Enveloped message to sent. | 246 | -> Message -- ^ Enveloped message to sent. |
246 | 247 | ||
248 | -- | Find out the extension this message belong to. Can be used to | ||
249 | -- check if this message is allowed to send\/recv in current | ||
250 | -- session. | ||
251 | requires :: a -> Maybe Extension | ||
252 | requires _ = Nothing | ||
253 | |||
247 | {----------------------------------------------------------------------- | 254 | {----------------------------------------------------------------------- |
248 | -- Status messages | 255 | -- Status messages |
249 | -----------------------------------------------------------------------} | 256 | -----------------------------------------------------------------------} |
@@ -268,6 +275,7 @@ instance Pretty StatusUpdate where | |||
268 | 275 | ||
269 | instance PeerMessage StatusUpdate where | 276 | instance PeerMessage StatusUpdate where |
270 | envelop _ = Status | 277 | envelop _ = Status |
278 | {-# INLINE envelop #-} | ||
271 | 279 | ||
272 | {----------------------------------------------------------------------- | 280 | {----------------------------------------------------------------------- |
273 | -- Available and transfer messages | 281 | -- Available and transfer messages |
@@ -314,15 +322,19 @@ instance Pretty RegularMessage where | |||
314 | 322 | ||
315 | instance PeerMessage RegularMessage where | 323 | instance PeerMessage RegularMessage where |
316 | envelop _ = Regular | 324 | envelop _ = Regular |
325 | {-# INLINE envelop #-} | ||
317 | 326 | ||
318 | instance PeerMessage Bitfield where | 327 | instance PeerMessage Bitfield where |
319 | envelop c = envelop c . Bitfield | 328 | envelop c = envelop c . Bitfield |
329 | {-# INLINE envelop #-} | ||
320 | 330 | ||
321 | instance PeerMessage BlockIx where | 331 | instance PeerMessage BlockIx where |
322 | envelop c = envelop c . Request | 332 | envelop c = envelop c . Request |
333 | {-# INLINE envelop #-} | ||
323 | 334 | ||
324 | instance PeerMessage (Block BL.ByteString) where | 335 | instance PeerMessage (Block BL.ByteString) where |
325 | envelop c = envelop c . Piece | 336 | envelop c = envelop c . Piece |
337 | {-# INLINE envelop #-} | ||
326 | 338 | ||
327 | {----------------------------------------------------------------------- | 339 | {----------------------------------------------------------------------- |
328 | -- Fast messages | 340 | -- Fast messages |
@@ -360,7 +372,11 @@ instance Pretty FastMessage where | |||
360 | pretty (AllowedFast pix) = "Allowed fast" <+> int pix | 372 | pretty (AllowedFast pix) = "Allowed fast" <+> int pix |
361 | 373 | ||
362 | instance PeerMessage FastMessage where | 374 | instance PeerMessage FastMessage where |
363 | envelop _ = Fast | 375 | envelop _ = Fast |
376 | {-# INLINE envelop #-} | ||
377 | |||
378 | requires _ = Just ExtFast | ||
379 | {-# INLINE requires #-} | ||
364 | 380 | ||
365 | {----------------------------------------------------------------------- | 381 | {----------------------------------------------------------------------- |
366 | -- Extended messages | 382 | -- Extended messages |
@@ -474,7 +490,11 @@ instance Pretty ExtendedHandshake where | |||
474 | pretty = PP.text . show | 490 | pretty = PP.text . show |
475 | 491 | ||
476 | instance PeerMessage ExtendedHandshake where | 492 | instance PeerMessage ExtendedHandshake where |
477 | envelop c = envelop c . EHandshake | 493 | envelop c = envelop c . EHandshake |
494 | {-# INLINE envelop #-} | ||
495 | |||
496 | requires _ = Just ExtExtended | ||
497 | {-# INLINE requires #-} | ||
478 | 498 | ||
479 | nullExtendedHandshake :: ExtendedCaps -> ExtendedHandshake | 499 | nullExtendedHandshake :: ExtendedCaps -> ExtendedHandshake |
480 | nullExtendedHandshake caps | 500 | nullExtendedHandshake caps |
@@ -521,7 +541,11 @@ instance Pretty ExtendedMetadata where | |||
521 | pretty (MetadataUnknown bval ) = ppBEncode bval | 541 | pretty (MetadataUnknown bval ) = ppBEncode bval |
522 | 542 | ||
523 | instance PeerMessage ExtendedMetadata where | 543 | instance PeerMessage ExtendedMetadata where |
524 | envelop c = envelop c . EMetadata | 544 | envelop c = envelop c . EMetadata |
545 | {-# INLINE envelop #-} | ||
546 | |||
547 | requires _ = Just ExtExtended | ||
548 | {-# INLINE requires #-} | ||
525 | 549 | ||
526 | -- | For more info see <http://www.bittorrent.org/beps/bep_0010.html> | 550 | -- | For more info see <http://www.bittorrent.org/beps/bep_0010.html> |
527 | data ExtendedMessage | 551 | data ExtendedMessage |
@@ -536,7 +560,11 @@ instance Pretty ExtendedMessage where | |||
536 | pretty (EUnknown mid _) = "Unknown" <+> PP.text (show mid) | 560 | pretty (EUnknown mid _) = "Unknown" <+> PP.text (show mid) |
537 | 561 | ||
538 | instance PeerMessage ExtendedMessage where | 562 | instance PeerMessage ExtendedMessage where |
539 | envelop _ = Extended | 563 | envelop _ = Extended |
564 | {-# INLINE envelop #-} | ||
565 | |||
566 | requires _ = Just ExtExtended | ||
567 | {-# INLINE requires #-} | ||
540 | 568 | ||
541 | {----------------------------------------------------------------------- | 569 | {----------------------------------------------------------------------- |
542 | -- The message datatype | 570 | -- The message datatype |
@@ -579,19 +607,21 @@ instance Pretty Message where | |||
579 | 607 | ||
580 | instance PeerMessage Message where | 608 | instance PeerMessage Message where |
581 | envelop _ = id | 609 | envelop _ = id |
610 | {-# INLINE envelop #-} | ||
611 | |||
612 | requires KeepAlive = Nothing | ||
613 | requires (Status _) = Nothing | ||
614 | requires (Regular _) = Nothing | ||
615 | requires (Port _) = Just ExtDHT | ||
616 | requires (Fast _) = Just ExtFast | ||
617 | requires (Extended _) = Just ExtExtended | ||
582 | 618 | ||
583 | instance PeerMessage PortNumber where | 619 | instance PeerMessage PortNumber where |
584 | envelop _ = Port | 620 | envelop _ = Port |
585 | 621 | {-# INLINE envelop #-} | |
586 | -- | Can be used to check if this message is allowed to send\/recv in | 622 | |
587 | -- current session. | 623 | requires _ = Just ExtDHT |
588 | requires :: Message -> Maybe Extension | 624 | {-# INLINE requires #-} |
589 | requires KeepAlive = Nothing | ||
590 | requires (Status _) = Nothing | ||
591 | requires (Regular _) = Nothing | ||
592 | requires (Port _) = Just ExtDHT | ||
593 | requires (Fast _) = Just ExtFast | ||
594 | requires (Extended _) = Just ExtExtended | ||
595 | 625 | ||
596 | getInt :: S.Get Int | 626 | getInt :: S.Get Int |
597 | getInt = fromIntegral <$> S.getWord32be | 627 | getInt = fromIntegral <$> S.getWord32be |