diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-05-03 09:36:02 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-05-03 09:36:02 +0400 |
commit | 6ac50d6e0323094ea0fd6ff37520698b94fa9d41 (patch) | |
tree | d3af77c8a496aed6733f070759b9142f59edd85a /src/Network/BitTorrent/Peer.hs | |
parent | f53531b65171e3b64d733aa0ab8c7fed71325464 (diff) |
~ Reorganize modules.
All peer location & identification & information stuff should be placed
in Network.BitTorrent.Peer now.
Diffstat (limited to 'src/Network/BitTorrent/Peer.hs')
-rw-r--r-- | src/Network/BitTorrent/Peer.hs | 52 |
1 files changed, 6 insertions, 46 deletions
diff --git a/src/Network/BitTorrent/Peer.hs b/src/Network/BitTorrent/Peer.hs index e16329d2..ff32feb5 100644 --- a/src/Network/BitTorrent/Peer.hs +++ b/src/Network/BitTorrent/Peer.hs | |||
@@ -5,52 +5,12 @@ | |||
5 | -- Stability : experimental | 5 | -- Stability : experimental |
6 | -- Portability : non-portable | 6 | -- Portability : non-portable |
7 | -- | 7 | -- |
8 | -- Just convenient reexports for peer related modules. | ||
9 | -- | ||
8 | module Network.BitTorrent.Peer | 10 | module Network.BitTorrent.Peer |
9 | ( Peer(..) | 11 | ( module P |
10 | , peerSockAddr, connectToPeer | ||
11 | , ppPeer | ||
12 | ) where | 12 | ) where |
13 | 13 | ||
14 | import Control.Applicative | 14 | import Network.BitTorrent.Peer.Addr as P |
15 | import Data.Word | 15 | import Network.BitTorrent.Peer.ClientInfo as P |
16 | import Data.Bits | 16 | import Network.BitTorrent.Peer.ID as P \ No newline at end of file |
17 | import Network | ||
18 | import Network.Socket | ||
19 | |||
20 | import Network.BitTorrent.PeerID | ||
21 | import Network.BitTorrent.PeerWire.ClientInfo | ||
22 | |||
23 | |||
24 | data Peer = Peer { | ||
25 | peerID :: Maybe PeerID | ||
26 | , peerIP :: HostAddress | ||
27 | , peerPort :: PortNumber | ||
28 | } deriving Show | ||
29 | |||
30 | -- TODO make platform independent, clarify htonl | ||
31 | -- | Convert peer info from tracker response to socket address. | ||
32 | -- Used for establish connection between peers. | ||
33 | -- | ||
34 | peerSockAddr :: Peer -> SockAddr | ||
35 | peerSockAddr = SockAddrInet <$> (g . peerPort) <*> (htonl . peerIP) | ||
36 | where | ||
37 | htonl :: Word32 -> Word32 | ||
38 | htonl d = | ||
39 | ((d .&. 0xff) `shiftL` 24) .|. | ||
40 | (((d `shiftR` 8 ) .&. 0xff) `shiftL` 16) .|. | ||
41 | (((d `shiftR` 16) .&. 0xff) `shiftL` 8) .|. | ||
42 | ((d `shiftR` 24) .&. 0xff) | ||
43 | |||
44 | g :: PortNumber -> PortNumber | ||
45 | g = id | ||
46 | |||
47 | -- | Tries to connect to peer using reasonable default parameters. | ||
48 | connectToPeer :: Peer -> IO Socket | ||
49 | connectToPeer p = do | ||
50 | sock <- socket AF_INET Stream Network.Socket.defaultProtocol | ||
51 | connect sock (peerSockAddr p) | ||
52 | return sock | ||
53 | |||
54 | ppPeer :: Peer -> String | ||
55 | ppPeer p = maybe "" (++ " at ") ((ppClientInfo . clientInfo) <$> peerID p) | ||
56 | ++ show (peerSockAddr p) | ||