summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-14 11:06:31 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-14 11:06:31 +0400
commit07ac8270807140fac201b7c973e12f924ca4b36b (patch)
treeecab362addc9c7fd07ae59d9a0f14fd9bd0c24ee
parent1fb701938aba43797124d2975c15f936ac71409a (diff)
~ Minor changes.
-rw-r--r--src/Network/BitTorrent.hs9
-rw-r--r--src/Network/BitTorrent/Exchange.hs9
-rw-r--r--src/Network/BitTorrent/Peer.hs33
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
45import Network.BitTorrent.Exchange.Protocol 45import Network.BitTorrent.Exchange.Protocol
46import Network.BitTorrent.Tracker 46import Network.BitTorrent.Tracker
47import Network.BitTorrent.Extension 47import Network.BitTorrent.Extension
48import Network.BitTorrent.Peer
48 49
49 50
50defaultClient :: IO ClientSession 51defaultClient :: IO ClientSession
@@ -55,7 +56,7 @@ defaultClient = newClient defaultThreadCount defaultExtensions
55 56
56discover :: SwarmSession -> P2P () -> IO () 57discover :: SwarmSession -> P2P () -> IO ()
57discover swarm action = do 58discover 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
73listener :: SwarmSession -> P2P () -> IO PortNumber
74listener _ _ = 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.
106runP2P :: SwarmSession -> PeerAddr -> P2P () -> IO () 106runP2P :: SwarmSession -> PeerAddr -> P2P () -> IO ()
107runP2P se addr p2p = waitVacancy se $ runSession se addr p2p 107runP2P 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{-
120listenP2P :: SwarmSession -> P2P () -> IO PortNumber
121listenP2P _ _ = undefined
122
123chainP2P :: 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
68import Control.Applicative 70import Control.Applicative
71import Control.Concurrent
72import Control.Exception
69import Data.BEncode 73import Data.BEncode
70import Data.Bits 74import Data.Bits
71import Data.Word 75import Data.Word
@@ -84,7 +88,7 @@ import Data.Time.Format (formatTime)
84import Text.PrettyPrint (text, Doc, (<+>)) 88import Text.PrettyPrint (text, Doc, (<+>))
85import System.Locale (defaultTimeLocale) 89import System.Locale (defaultTimeLocale)
86 90
87import Network 91import Network hiding (accept)
88import Network.Socket 92import 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
494instance BEncodable PortNumber where 499instance 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
544forkListener :: ((PeerAddr, Socket) -> IO ()) -> IO PortNumber
545forkListener 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.
539ppPeer :: PeerAddr -> Doc 566ppPeer :: PeerAddr -> Doc
540ppPeer p @ PeerAddr {..} = case peerID of 567ppPeer p @ PeerAddr {..} = case peerID of