summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Tracker
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Tracker')
-rw-r--r--src/Network/BitTorrent/Tracker/Session.hs33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/Network/BitTorrent/Tracker/Session.hs b/src/Network/BitTorrent/Tracker/Session.hs
index b5c05116..33a46898 100644
--- a/src/Network/BitTorrent/Tracker/Session.hs
+++ b/src/Network/BitTorrent/Tracker/Session.hs
@@ -59,26 +59,30 @@ data LastScrape = LastScrape
59 , seedersCount :: Maybe Int 59 , seedersCount :: Maybe Int
60 } deriving (Show, Eq) 60 } deriving (Show, Eq)
61 61
62-- | Tracker session starts with scrape unknown.
62instance Default LastScrape where 63instance Default LastScrape where
63 def = LastScrape Nothing Nothing 64 def = LastScrape Nothing Nothing
64 65
65 66-- | Status of this client.
66data Status 67data Status
67 = Running 68 = Running -- ^ This client is announced and listenning for incoming
68 | Paused 69 -- connections.
69 deriving (Show, Eq) 70 | Paused -- ^ This client does not expecting incoming connections.
71 deriving (Show, Eq, Bounded, Enum)
70 72
73-- | Client starting in the paused state.
71instance Default Status where 74instance Default Status where
72 def = Paused 75 def = Paused
73 76
77-- | Client status after event announce succeed.
74nextStatus :: Maybe Event -> Status 78nextStatus :: Maybe Event -> Status
75nextStatus Nothing = Running 79nextStatus Nothing = Running
76nextStatus (Just Started ) = Running 80nextStatus (Just Started ) = Running
77nextStatus (Just Stopped ) = Paused 81nextStatus (Just Stopped ) = Paused
78nextStatus (Just Completed) = Running 82nextStatus (Just Completed) = Running
79 83
84-- | Do we need to notify this /specific/ tracker?
80needNotify :: Maybe Event -> Maybe Status -> Bool 85needNotify :: Maybe Event -> Maybe Status -> Bool
81-- we always send _regular_ announce requests (for e.g. to get more peers);
82needNotify Nothing _ = True 86needNotify Nothing _ = True
83needNotify (Just Started) Nothing = True 87needNotify (Just Started) Nothing = True
84needNotify (Just Stopped) Nothing = False 88needNotify (Just Stopped) Nothing = False
@@ -102,10 +106,10 @@ data TrackerEntry = TrackerEntry
102 -- | Used to notify 'Stopped' and 'Completed' events. 106 -- | Used to notify 'Stopped' and 'Completed' events.
103 , statusSent :: !(Maybe Status) 107 , statusSent :: !(Maybe Status)
104 108
105 -- | 109 -- | Can be used to retrieve peer set.
106 , peersCache :: Cached [PeerAddr IP] 110 , peersCache :: Cached [PeerAddr IP]
107 111
108 -- | May be used to show brief swarm stats in client GUI. 112 -- | Cay be used to show brief swarm stats in client GUI.
109 , scrapeCache :: Cached LastScrape 113 , scrapeCache :: Cached LastScrape
110 } 114 }
111 115
@@ -118,12 +122,21 @@ nullEntry uri = TrackerEntry uri Nothing def def
118 122
119-- | Multitracker session. 123-- | Multitracker session.
120data Session = Session 124data Session = Session
121 { infohash :: !InfoHash 125 { -- | Infohash to announce at each 'announce' request.
126 infohash :: !InfoHash
127
128 -- | Status of this client is used to filter duplicated
129 -- notifications, for e.g. we don't want to notify a tracker with
130 -- ['Stopped', 'Stopped'], the last should be ignored.
122 , currentStatus :: !(MVar Status) 131 , currentStatus :: !(MVar Status)
132
133 -- | A set of single-tracker sessions. Any request to a tracker
134 -- must take a lock.
123 , trackers :: !(MVar (TrackerList TrackerEntry)) 135 , trackers :: !(MVar (TrackerList TrackerEntry))
124 } 136 }
125 137
126-- Just Started 138-- | Create a new multitracker session in paused state. To start
139-- announcing client presence use 'notify'.
127newSession :: InfoHash -> TrackerList URI -> IO Session 140newSession :: InfoHash -> TrackerList URI -> IO Session
128newSession ih origUris = do 141newSession ih origUris = do
129 uris <- shuffleTiers origUris 142 uris <- shuffleTiers origUris
@@ -131,7 +144,7 @@ newSession ih origUris = do
131 entries <- newMVar (fmap nullEntry uris) 144 entries <- newMVar (fmap nullEntry uris)
132 return (Session ih status entries) 145 return (Session ih status entries)
133 146
134-- Just Stopped 147-- | Release scarce resources associated with the given session.
135closeSession :: Session -> IO () 148closeSession :: Session -> IO ()
136closeSession _ = return () 149closeSession _ = return ()
137 150