From: Ramakrishnan Muthukrishnan Date: Sat, 11 Jun 2016 16:45:36 +0000 (+0530) Subject: misc cleanups in Http tracker X-Git-Url: https://git.rkrishnan.org/vdrive/%22news.html/class-simplejson.JSONDecoder-index.html?a=commitdiff_plain;h=f669c4670f61f71b7ba19368324a2a8a70c09723;p=functorrent.git misc cleanups in Http tracker --- diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs index 0f13b65..815d081 100644 --- a/src/FuncTorrent/Tracker.hs +++ b/src/FuncTorrent/Tracker.hs @@ -1,23 +1,23 @@ -{-# LANGUAGE OverloadedStrings #-} {- -Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan - -This file is part of FuncTorrent. - -FuncTorrent is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 3 of the License, or -(at your option) any later version. - -FuncTorrent is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with FuncTorrent; if not, see --} + - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan + - + - This file is part of FuncTorrent. + - + - FuncTorrent is free software; you can redistribute it and/or modify + - it under the terms of the GNU General Public License as published by + - the Free Software Foundation; either version 3 of the License, or + - (at your option) any later version. + - + - FuncTorrent is distributed in the hope that it will be useful, + - but WITHOUT ANY WARRANTY; without even the implied warranty of + - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + - GNU General Public License for more details. + - + - You should have received a copy of the GNU General Public License + - along with FuncTorrent; if not, see + -} +{-# LANGUAGE OverloadedStrings #-} module FuncTorrent.Tracker (runTracker , getConnectedPeers @@ -51,7 +51,7 @@ runTracker msgChannel fsChan infohash port peerId announceList sz = do , connectedPeers = ps , left = sz } turl = head announceList - case (getTrackerType turl) of + case getTrackerType turl of Http -> do _ <- forkIO $ trackerLoop turl port peerId infohash fsChan initialTState runStateT (msgHandler msgChannel) initialTState diff --git a/src/FuncTorrent/Tracker/Http.hs b/src/FuncTorrent/Tracker/Http.hs index 0cb36b9..5caefd6 100644 --- a/src/FuncTorrent/Tracker/Http.hs +++ b/src/FuncTorrent/Tracker/Http.hs @@ -1,22 +1,23 @@ -{-# LANGUAGE OverloadedStrings #-} {- -Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan - -This file is part of FuncTorrent. - -FuncTorrent is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 3 of the License, or -(at your option) any later version. + - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan + - + - This file is part of FuncTorrent. + - + - FuncTorrent is free software; you can redistribute it and/or modify + - it under the terms of the GNU General Public License as published by + - the Free Software Foundation; either version 3 of the License, or + - (at your option) any later version. + - + - FuncTorrent is distributed in the hope that it will be useful, + - but WITHOUT ANY WARRANTY; without even the implied warranty of + - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + - GNU General Public License for more details. + - + - You should have received a copy of the GNU General Public License + - along with FuncTorrent; if not, see + -} -FuncTorrent is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with FuncTorrent; if not, see --} +{-# LANGUAGE OverloadedStrings #-} module FuncTorrent.Tracker.Http (trackerLoop @@ -25,7 +26,7 @@ module FuncTorrent.Tracker.Http import Prelude hiding (lookup, splitAt) import Control.Concurrent (threadDelay) -import Control.Concurrent.MVar (readMVar, putMVar) +import Control.Concurrent.MVar (readMVar, putMVar, isEmptyMVar, swapMVar) import Control.Monad (forever) import qualified Data.ByteString.Base16 as B16 (encode) import Data.ByteString (ByteString) @@ -80,14 +81,21 @@ trackerLoop url port peerId infohash fschan tstate = forever $ do down = FS.bytesWritten st resp <- sendGetRequest url $ mkArgs port peerId up down (left tstate) infohash case Benc.decode resp of - Left e -> return () -- $ pack (show e) + Left e -> do + return () -- $ pack (show e) Right trackerInfo -> case parseTrackerResponse trackerInfo of Left e -> return () -- e Right tresp -> do _ <- threadDelay $ fromIntegral (interval tresp) - _ <- putMVar (connectedPeers tstate) (peers tresp) - return () -- trackerLoop port peerId st + ps <- isEmptyMVar $ connectedPeers tstate + if ps + then do + _ <- putMVar (connectedPeers tstate) (peers tresp) + return () + else do + _ <- swapMVar (connectedPeers tstate) (peers tresp) + return () parseTrackerResponse :: BVal -> Either ByteString TrackerResponse parseTrackerResponse resp = diff --git a/src/FuncTorrent/Tracker/Types.hs b/src/FuncTorrent/Tracker/Types.hs index 173e925..cc86e78 100644 --- a/src/FuncTorrent/Tracker/Types.hs +++ b/src/FuncTorrent/Tracker/Types.hs @@ -28,6 +28,7 @@ module FuncTorrent.Tracker.Types , Port ) where +import Data.ByteString (ByteString) import Control.Concurrent.MVar (MVar) import FuncTorrent.Peer (Peer(..)) @@ -42,10 +43,11 @@ data TrackerProtocol = Http data TrackerEventState = None | Started - | Stopped | Completed + | Error ByteString deriving (Show, Eq) -data TrackerMsg = GetStatusMsg + +data TrackerMsg = GetStatusMsg TrackerEventState | GetConnectedPeersMsg (MVar [Peer]) data TState = TState { left :: Integer