diff options
-rw-r--r-- | src/Network/BitTorrent/Tracker/Session.hs | 33 |
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. | ||
62 | instance Default LastScrape where | 63 | instance Default LastScrape where |
63 | def = LastScrape Nothing Nothing | 64 | def = LastScrape Nothing Nothing |
64 | 65 | ||
65 | 66 | -- | Status of this client. | |
66 | data Status | 67 | data 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. | ||
71 | instance Default Status where | 74 | instance Default Status where |
72 | def = Paused | 75 | def = Paused |
73 | 76 | ||
77 | -- | Client status after event announce succeed. | ||
74 | nextStatus :: Maybe Event -> Status | 78 | nextStatus :: Maybe Event -> Status |
75 | nextStatus Nothing = Running | 79 | nextStatus Nothing = Running |
76 | nextStatus (Just Started ) = Running | 80 | nextStatus (Just Started ) = Running |
77 | nextStatus (Just Stopped ) = Paused | 81 | nextStatus (Just Stopped ) = Paused |
78 | nextStatus (Just Completed) = Running | 82 | nextStatus (Just Completed) = Running |
79 | 83 | ||
84 | -- | Do we need to notify this /specific/ tracker? | ||
80 | needNotify :: Maybe Event -> Maybe Status -> Bool | 85 | needNotify :: Maybe Event -> Maybe Status -> Bool |
81 | -- we always send _regular_ announce requests (for e.g. to get more peers); | ||
82 | needNotify Nothing _ = True | 86 | needNotify Nothing _ = True |
83 | needNotify (Just Started) Nothing = True | 87 | needNotify (Just Started) Nothing = True |
84 | needNotify (Just Stopped) Nothing = False | 88 | needNotify (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. |
120 | data Session = Session | 124 | data 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'. | ||
127 | newSession :: InfoHash -> TrackerList URI -> IO Session | 140 | newSession :: InfoHash -> TrackerList URI -> IO Session |
128 | newSession ih origUris = do | 141 | newSession 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. |
135 | closeSession :: Session -> IO () | 148 | closeSession :: Session -> IO () |
136 | closeSession _ = return () | 149 | closeSession _ = return () |
137 | 150 | ||