From: Ramakrishnan Muthukrishnan Date: Sun, 19 Jun 2016 06:56:55 +0000 (+0530) Subject: refactor: remove peerid from Peer datatype X-Git-Url: https://git.rkrishnan.org/vdrive/%22file:/frontends/status?a=commitdiff_plain;h=9beb0fb9814b33725f6adfa5adabb3225a54277b;p=functorrent.git refactor: remove peerid from Peer datatype --- diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 5d987e8..63aaa5a 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -20,8 +20,7 @@ {-# LANGUAGE OverloadedStrings #-} module FuncTorrent.Peer - (Peer(..), - PieceMap, + (PieceMap, handlePeerMsgs ) where @@ -53,7 +52,7 @@ havePiece pm index = dlstate (pm ! index) == Have connectToPeer :: Peer -> IO Handle -connectToPeer (Peer _ ip port) = do +connectToPeer (Peer ip port) = do h <- connectTo ip (PortNumber (fromIntegral port)) hSetBuffering h LineBuffering return h diff --git a/src/FuncTorrent/PeerMsgs.hs b/src/FuncTorrent/PeerMsgs.hs index cfefce1..ff3b435 100644 --- a/src/FuncTorrent/PeerMsgs.hs +++ b/src/FuncTorrent/PeerMsgs.hs @@ -44,8 +44,8 @@ import Data.Binary.Get (getWord32be, getWord16be, getWord8, runGet) import FuncTorrent.Utils (toIP, toPort) --- | Peer is a PeerID, IP address, port tuple -data Peer = Peer ID IP Port +-- | Peer is a IP address, port tuple +data Peer = Peer IP Port deriving (Show, Eq) type ID = String @@ -148,5 +148,5 @@ bsToInt :: ByteString -> Int bsToInt x = fromIntegral (runGet getWord32be (fromChunks (return x))) makePeer :: ByteString -> Peer -makePeer peer = Peer "" (toIP ip') (toPort port') +makePeer peer = Peer (toIP ip') (toPort port') where (ip', port') = splitAt 4 peer diff --git a/src/FuncTorrent/Server.hs b/src/FuncTorrent/Server.hs index 4c3ab94..3827aa2 100644 --- a/src/FuncTorrent/Server.hs +++ b/src/FuncTorrent/Server.hs @@ -27,7 +27,8 @@ import Network (withSocketsDo, listenOn, accept, Socket, PortID ( PortNumber )) import System.IO (hSetBuffering, BufferMode ( NoBuffering )) import FuncTorrent.Metainfo (Metainfo) -import FuncTorrent.Peer (handlePeerMsgs, Peer(..), PieceMap) +import FuncTorrent.Peer (handlePeerMsgs, PieceMap) +import FuncTorrent.PeerMsgs (Peer(..)) import qualified FuncTorrent.FileSystem as FS (MsgChannel) -- server is listening on any port from 6881 - 6889 @@ -41,6 +42,6 @@ start = withSocketsDo $ do run :: Socket -> String -> Metainfo -> PieceMap -> FS.MsgChannel -> IO () run listenSock peerid m pieceMap c = forever $ do (handle, ip, port) <- accept listenSock - let peer = Peer "" ip (fromIntegral port) + let peer = Peer ip (fromIntegral port) hSetBuffering handle NoBuffering forkIO $ handlePeerMsgs peer peerid m pieceMap False c diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs index 8f6a5cc..8090feb 100644 --- a/src/FuncTorrent/Tracker.hs +++ b/src/FuncTorrent/Tracker.hs @@ -38,7 +38,7 @@ import qualified FuncTorrent.Tracker.Udp as UT (trackerLoop) import FuncTorrent.Tracker.Types (TState(..), TrackerEventState(..), TrackerProtocol(..), TrackerMsg(..)) import FuncTorrent.Utils (Port, toPort, getHostname) import qualified FuncTorrent.FileSystem as FS (MsgChannel) -import FuncTorrent.Peer (Peer) +import FuncTorrent.PeerMsgs (Peer) type MsgChannel = Chan TrackerMsg diff --git a/src/FuncTorrent/Tracker/Http.hs b/src/FuncTorrent/Tracker/Http.hs index f911b2f..9edf3fc 100644 --- a/src/FuncTorrent/Tracker/Http.hs +++ b/src/FuncTorrent/Tracker/Http.hs @@ -23,16 +23,15 @@ module FuncTorrent.Tracker.Http (trackerLoop ) where -import Prelude hiding (lookup, splitAt) +import Prelude hiding (lookup) import Control.Concurrent (threadDelay) import Control.Concurrent.MVar (readMVar, putMVar, isEmptyMVar, swapMVar) import Control.Monad (forever, void) import qualified Data.ByteString.Base16 as B16 (encode) import Data.ByteString (ByteString) -import Data.ByteString.Char8 as BC (pack, unpack, splitAt) +import Data.ByteString.Char8 as BC (pack, unpack) import Data.Char (chr) -import Data.List (intercalate) import Data.Map as M (lookup) import Network (PortNumber) import Network.HTTP.Base (urlEncode) @@ -41,7 +40,7 @@ import qualified FuncTorrent.Bencode as Benc import FuncTorrent.Bencode (BVal(..)) import qualified FuncTorrent.FileSystem as FS (MsgChannel, Stats(..), getStats) import FuncTorrent.Network (sendGetRequest) -import FuncTorrent.PeerMsgs (Peer(..), makePeer) +import FuncTorrent.PeerMsgs (makePeer) import FuncTorrent.Utils (splitN, IP, Port) import FuncTorrent.Tracker.Types(TState(..), TrackerResponse(..)) @@ -75,10 +74,9 @@ mkArgs port peer_id up down left' infoHash = trackerLoop :: String -> PortNumber -> String -> ByteString -> FS.MsgChannel -> TState -> IO () trackerLoop url sport peerId infohash fschan tstate = forever $ do - st' <- FS.getStats fschan - st <- readMVar st' - let up = FS.bytesRead st - down = FS.bytesWritten st + st <- readMVar <$> FS.getStats fschan + up <- fmap FS.bytesRead st + down <- fmap FS.bytesWritten st resp <- sendGetRequest url $ mkArgs sport peerId up down (left tstate) infohash case Benc.decode resp of Left e -> diff --git a/src/FuncTorrent/Tracker/Types.hs b/src/FuncTorrent/Tracker/Types.hs index 2165b08..3adcacc 100644 --- a/src/FuncTorrent/Tracker/Types.hs +++ b/src/FuncTorrent/Tracker/Types.hs @@ -29,7 +29,7 @@ module FuncTorrent.Tracker.Types import Data.ByteString (ByteString) import Control.Concurrent.MVar (MVar) -import FuncTorrent.Peer (Peer(..)) +import FuncTorrent.PeerMsgs (Peer) data TrackerProtocol = Http | Udp diff --git a/src/FuncTorrent/Tracker/Udp.hs b/src/FuncTorrent/Tracker/Udp.hs index fe4d9e0..5e98879 100644 --- a/src/FuncTorrent/Tracker/Udp.hs +++ b/src/FuncTorrent/Tracker/Udp.hs @@ -39,7 +39,7 @@ import Network.Socket.ByteString (sendTo, recvFrom) import System.Random (randomIO) import System.Timeout (timeout) -import FuncTorrent.Peer (Peer(..)) +import FuncTorrent.PeerMsgs (Peer(..)) import FuncTorrent.Tracker.Types (TrackerEventState(..), TState(..)) import FuncTorrent.Utils (IP, Port, toIP, toPort, getHostname, getPort) import qualified FuncTorrent.FileSystem as FS (MsgChannel, Stats(..), getStats) @@ -200,7 +200,7 @@ getIPPortPairs = do ip <- toIP <$> getByteString 4 port <- toPort <$> getByteString 2 ipportpairs <- getIPPortPairs - return $ (Peer "" ip port) : ipportpairs + return $ (Peer ip port) : ipportpairs startSession :: String -> Port -> IO UDPTrackerHandle startSession host port = do @@ -216,7 +216,7 @@ closeSession (UDPTrackerHandle s _ _) = close s trackerLoop :: String -> Port -> String -> ByteString -> FS.MsgChannel -> TState -> IO () trackerLoop url sport peerId infohash fschan tstate = forever $ do - st <- fmap readMVar $ FS.getStats fschan + st <- readMVar <$> FS.getStats fschan up <- fmap FS.bytesRead st down <- fmap FS.bytesWritten st handle <- startSession host port