diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-14 11:06:31 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-14 11:06:31 +0400 |
commit | 07ac8270807140fac201b7c973e12f924ca4b36b (patch) | |
tree | ecab362addc9c7fd07ae59d9a0f14fd9bd0c24ee | |
parent | 1fb701938aba43797124d2975c15f936ac71409a (diff) |
~ Minor changes.
-rw-r--r-- | src/Network/BitTorrent.hs | 9 | ||||
-rw-r--r-- | src/Network/BitTorrent/Exchange.hs | 9 | ||||
-rw-r--r-- | src/Network/BitTorrent/Peer.hs | 33 |
3 files changed, 40 insertions, 11 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs index 24d78e85..ce9f0149 100644 --- a/src/Network/BitTorrent.hs +++ b/src/Network/BitTorrent.hs | |||
@@ -45,6 +45,7 @@ import Network.BitTorrent.Exchange | |||
45 | import Network.BitTorrent.Exchange.Protocol | 45 | import Network.BitTorrent.Exchange.Protocol |
46 | import Network.BitTorrent.Tracker | 46 | import Network.BitTorrent.Tracker |
47 | import Network.BitTorrent.Extension | 47 | import Network.BitTorrent.Extension |
48 | import Network.BitTorrent.Peer | ||
48 | 49 | ||
49 | 50 | ||
50 | defaultClient :: IO ClientSession | 51 | defaultClient :: IO ClientSession |
@@ -55,7 +56,7 @@ defaultClient = newClient defaultThreadCount defaultExtensions | |||
55 | 56 | ||
56 | discover :: SwarmSession -> P2P () -> IO () | 57 | discover :: SwarmSession -> P2P () -> IO () |
57 | discover swarm action = do | 58 | discover swarm action = do |
58 | port <- listener swarm action | 59 | port <- forkListener (error "discover") |
59 | 60 | ||
60 | let conn = TConnection (tAnnounce (torrentMeta swarm)) | 61 | let conn = TConnection (tAnnounce (torrentMeta swarm)) |
61 | (tInfoHash (torrentMeta swarm)) | 62 | (tInfoHash (torrentMeta swarm)) |
@@ -69,9 +70,3 @@ discover swarm action = do | |||
69 | addr <- getPeerAddr tses | 70 | addr <- getPeerAddr tses |
70 | spawnP2P swarm addr $ do | 71 | spawnP2P swarm addr $ do |
71 | action | 72 | action |
72 | |||
73 | listener :: SwarmSession -> P2P () -> IO PortNumber | ||
74 | listener _ _ = do | ||
75 | -- TODO: | ||
76 | -- forkIO loop | ||
77 | return 10000 | ||
diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs index 2d0393c0..862611f9 100644 --- a/src/Network/BitTorrent/Exchange.hs +++ b/src/Network/BitTorrent/Exchange.hs | |||
@@ -102,7 +102,7 @@ runSession se addr p2p = | |||
102 | isIOException _ = return () | 102 | isIOException _ = return () |
103 | 103 | ||
104 | -- | Run P2P session in the current thread. Normally you don't need this | 104 | -- | Run P2P session in the current thread. Normally you don't need this |
105 | -- function in client application. | 105 | -- function in client application, except for debugging. |
106 | runP2P :: SwarmSession -> PeerAddr -> P2P () -> IO () | 106 | runP2P :: SwarmSession -> PeerAddr -> P2P () -> IO () |
107 | runP2P se addr p2p = waitVacancy se $ runSession se addr p2p | 107 | runP2P se addr p2p = waitVacancy se $ runSession se addr p2p |
108 | 108 | ||
@@ -115,6 +115,13 @@ spawnP2P se addr p2p = do | |||
115 | forkIO $ do | 115 | forkIO $ do |
116 | runSession se addr p2p `finally` leaveSwarm se | 116 | runSession se addr p2p `finally` leaveSwarm se |
117 | 117 | ||
118 | -- TODO unify this all using PeerConnection | ||
119 | {- | ||
120 | listenP2P :: SwarmSession -> P2P () -> IO PortNumber | ||
121 | listenP2P _ _ = undefined | ||
122 | |||
123 | chainP2P :: SwarmSession -> PeerConnection -> P2P () -> IO () | ||
124 | -} | ||
118 | {----------------------------------------------------------------------- | 125 | {----------------------------------------------------------------------- |
119 | Exceptions | 126 | Exceptions |
120 | -----------------------------------------------------------------------} | 127 | -----------------------------------------------------------------------} |
diff --git a/src/Network/BitTorrent/Peer.hs b/src/Network/BitTorrent/Peer.hs index 9aa924d3..41630c6d 100644 --- a/src/Network/BitTorrent/Peer.hs +++ b/src/Network/BitTorrent/Peer.hs | |||
@@ -44,12 +44,14 @@ module Network.BitTorrent.Peer | |||
44 | 44 | ||
45 | -- ** Generation | 45 | -- ** Generation |
46 | , newPeerID, timestampByteString | 46 | , newPeerID, timestampByteString |
47 | |||
47 | -- ** Extra | 48 | -- ** Extra |
48 | , byteStringPadded | 49 | , byteStringPadded |
49 | 50 | ||
50 | -- * Peer address | 51 | -- * Peer address |
51 | , PeerAddr(..) | 52 | , PeerAddr(..) |
52 | , peerSockAddr, connectToPeer | 53 | , peerSockAddr |
54 | , connectToPeer, forkListener | ||
53 | , ppPeer | 55 | , ppPeer |
54 | 56 | ||
55 | -- * Client version detection | 57 | -- * Client version detection |
@@ -66,6 +68,8 @@ module Network.BitTorrent.Peer | |||
66 | 68 | ||
67 | 69 | ||
68 | import Control.Applicative | 70 | import Control.Applicative |
71 | import Control.Concurrent | ||
72 | import Control.Exception | ||
69 | import Data.BEncode | 73 | import Data.BEncode |
70 | import Data.Bits | 74 | import Data.Bits |
71 | import Data.Word | 75 | import Data.Word |
@@ -84,7 +88,7 @@ import Data.Time.Format (formatTime) | |||
84 | import Text.PrettyPrint (text, Doc, (<+>)) | 88 | import Text.PrettyPrint (text, Doc, (<+>)) |
85 | import System.Locale (defaultTimeLocale) | 89 | import System.Locale (defaultTimeLocale) |
86 | 90 | ||
87 | import Network | 91 | import Network hiding (accept) |
88 | import Network.Socket | 92 | import Network.Socket |
89 | 93 | ||
90 | 94 | ||
@@ -489,7 +493,8 @@ data PeerAddr = PeerAddr { | |||
489 | , peerIP :: HostAddress | 493 | , peerIP :: HostAddress |
490 | , peerPort :: PortNumber | 494 | , peerPort :: PortNumber |
491 | } deriving (Show, Eq, Ord) | 495 | } deriving (Show, Eq, Ord) |
492 | -- TODO verify semantic of ord and eq instances | 496 | |
497 | -- TODO check semantic of ord and eq instances | ||
493 | 498 | ||
494 | instance BEncodable PortNumber where | 499 | instance BEncodable PortNumber where |
495 | toBEncode = toBEncode . fromEnum | 500 | toBEncode = toBEncode . fromEnum |
@@ -535,6 +540,28 @@ connectToPeer p = do | |||
535 | connect sock (peerSockAddr p) | 540 | connect sock (peerSockAddr p) |
536 | return sock | 541 | return sock |
537 | 542 | ||
543 | |||
544 | forkListener :: ((PeerAddr, Socket) -> IO ()) -> IO PortNumber | ||
545 | forkListener action = do | ||
546 | sock <- socket AF_INET Stream defaultProtocol | ||
547 | bindSocket sock (SockAddrInet 0 0) | ||
548 | listen sock 1 | ||
549 | addr <- getSocketName sock | ||
550 | case addr of | ||
551 | SockAddrInet port _ -> do | ||
552 | forkIO (loop sock) | ||
553 | return port | ||
554 | _ -> do | ||
555 | throwIO $ userError "listener: impossible happened" | ||
556 | where | ||
557 | loop sock = do | ||
558 | (conn, addr) <- accept sock | ||
559 | case addr of | ||
560 | SockAddrInet port host -> | ||
561 | action (PeerAddr Nothing host port, conn) | ||
562 | _ -> return () | ||
563 | loop sock | ||
564 | |||
538 | -- | Pretty print peer address in human readable form. | 565 | -- | Pretty print peer address in human readable form. |
539 | ppPeer :: PeerAddr -> Doc | 566 | ppPeer :: PeerAddr -> Doc |
540 | ppPeer p @ PeerAddr {..} = case peerID of | 567 | ppPeer p @ PeerAddr {..} = case peerID of |