summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Internal.hs
blob: e07698dd0a62f3ed2b3e2ac55c45121969f770d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
-- |
--   Copyright   :  (c) Sam T. 2013
--   License     :  MIT
--   Maintainer  :  pxqr.sta@gmail.com
--   Stability   :  experimental
--   Portability :  portable
--
--   This module implement opaque broadcast message passing. It
--   provides sessions needed by Network.BitTorrent and
--   Network.BitTorrent.Exchange and modules. To hide some internals
--   of this module we detach it from Exchange.
--
--   Note: expose only static data in data field lists, all dynamic
--   data should be modified through standalone functions.
--
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE RecordWildCards       #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances  #-}
module Network.BitTorrent.Internal
       ( Progress(..), startProgress

         -- * Client
       , ClientSession (clientPeerID, allowedExtensions)

       , ThreadCount
       , defaultThreadCount

       , newClient

       , getCurrentProgress
       , getSwarmCount
       , getPeerCount


         -- * Swarm
       , SwarmSession(SwarmSession, torrentMeta, clientSession)
       , getSessionCount
       , newLeacher, newSeeder
       , enterSwarm, leaveSwarm , waitVacancy

         -- * Peer
       , PeerSession(PeerSession, connectedPeerAddr
                    , swarmSession, enabledExtensions
                    )
       , SessionState
       , withPeerSession

         -- ** Exceptions
       , SessionException(..)
       , isSessionException
       , putSessionException

         -- ** Properties
       , bitfield, status
       , emptyBF, fullBF, singletonBF, adjustBF
       , getPieceCount, getClientBF

         -- * Timeouts
       , updateIncoming, updateOutcoming
       ) where

import Control.Applicative
import Control.Concurrent
import Control.Concurrent.STM
import Control.Concurrent.MSem as MSem
import Control.Lens
import Control.Monad.State
import Control.Monad.Reader
import Control.Exception

import Data.IORef
import Data.Default
import Data.Function
import Data.Ord
import Data.Set as S
import Data.Typeable

import Data.Serialize hiding (get)
import Text.PrettyPrint

import Network
import Network.Socket
import Network.Socket.ByteString

import GHC.Event as Ev

import Data.Bitfield as BF
import Data.Torrent
import Network.BitTorrent.Extension
import Network.BitTorrent.Peer
import Network.BitTorrent.Exchange.Protocol as BT
import Network.BitTorrent.Tracker.Protocol as BT

{-----------------------------------------------------------------------
    Progress
-----------------------------------------------------------------------}

-- | 'Progress' contains upload/download/left stats about
--   current client state and used to notify the tracker
--
--   This data is considered as dynamic within one client
--   session. This data also should be shared across client
--   application sessions (e.g. files), otherwise use 'startProgress'
--   to get initial 'Progress'.
--
data Progress = Progress {
    prUploaded   :: !Integer -- ^ Total amount of bytes uploaded.
  , prDownloaded :: !Integer -- ^ Total amount of bytes downloaded.
  , prLeft       :: !Integer -- ^ Total amount of bytes left.
  } deriving (Show, Read, Eq)

-- TODO make lenses

-- | Initial progress is used when there are no session before.
--
--   Please note that tracker might penalize client some way if the do
--   not accumulate progress. If possible and save 'Progress' between
--   client sessions to avoid that.
--
startProgress :: Integer -> Progress
startProgress = Progress 0 0

{-----------------------------------------------------------------------
    Client session
-----------------------------------------------------------------------}

{- NOTE: If we will not restrict number of threads we could end up
with thousands of connected swarm and make no particular progress.

Note also we do not bound number of swarms! This is not optimal
strategy because each swarm might have say 1 thread and we could end
up bounded by the meaningless limit. Bounding global number of p2p
sessions should work better, and simpler.-}

-- | Each client might have a limited number of threads.
type ThreadCount = Int

-- | The number of threads suitable for a typical BT client.
defaultThreadCount :: ThreadCount
defaultThreadCount = 1000

{- NOTE: basically, client session should contain options which user
app store in configuration files. (related to the protocol) Moreover
it should contain the all client identification info. (e.g. DHT)  -}

-- | Client session is the basic unit of bittorrent network, it has:
--
--     * The /peer ID/ used as unique identifier of the client in
--     network. Obviously, this value is not changed during client
--     session.
--
--     * The number of /protocol extensions/ it might use. This value
--     is static as well, but if you want to dynamically reconfigure
--     the client you might kill the end the current session and
--     create a new with the fresh required extensions.
--
--     * The number of /swarms/ to join, each swarm described by the
--     'SwarmSession'.
--
--  Normally, you would have one client session, however, if we need,
--  in one application we could have many clients with different peer
--  ID's and different enabled extensions at the same time.
--
data ClientSession = ClientSession {
    -- | Used in handshakes and discovery mechanism.
    clientPeerID      :: !PeerID

    -- | Extensions we should try to use. Hovewer some particular peer
    -- might not support some extension, so we keep enabledExtension in
    -- 'PeerSession'.
  , allowedExtensions :: [Extension]

    -- | Semaphor used to bound number of active P2P sessions.
  , activeThreads     :: !(MSem ThreadCount)

    -- | Max number of active connections.
  , maxActive         :: !ThreadCount

    -- | Used to traverse the swarm session.
  , swarmSessions     :: !(TVar (Set SwarmSession))

  , eventManager      :: !EventManager

    -- | Used to keep track global client progress.
  , currentProgress   :: !(TVar  Progress)
  }

instance Eq ClientSession where
  (==) = (==) `on` clientPeerID

instance Ord ClientSession where
  compare = comparing clientPeerID

-- | Get current global progress of the client. This value is usually
-- shown to a user.
getCurrentProgress :: MonadIO m => ClientSession -> m Progress
getCurrentProgress = liftIO . readTVarIO . currentProgress

-- | Get number of swarms client aware of.
getSwarmCount :: MonadIO m => ClientSession -> m SessionCount
getSwarmCount ClientSession {..} = liftIO $
  S.size <$> readTVarIO swarmSessions

-- | Get number of peers the client currently connected to.
getPeerCount :: MonadIO m => ClientSession -> m ThreadCount
getPeerCount ClientSession {..} = liftIO $ do
  unused  <- peekAvail activeThreads
  return (maxActive - unused)

-- | Create a new client session. The data passed to this function are
-- usually loaded from configuration file.
newClient :: SessionCount     -- ^ Maximum count of active P2P Sessions.
          -> [Extension]      -- ^ Extensions allowed to use.
          -> IO ClientSession -- ^ Client with unique peer ID.

newClient n exts = do
  mgr <- Ev.new
  -- TODO kill this thread when leave client
  _   <- forkIO $ loop mgr

  ClientSession
    <$> newPeerID
    <*> pure exts
    <*> MSem.new n
    <*> pure n
    <*> newTVarIO S.empty
    <*> pure mgr
    <*> newTVarIO (startProgress 0)

{-----------------------------------------------------------------------
    Swarm session
-----------------------------------------------------------------------}

-- TODO document P2P sessions bounding
type SessionCount = Int

defSeederConns :: SessionCount
defSeederConns = defaultUnchokeSlots

defLeacherConns :: SessionCount
defLeacherConns = defaultNumWant

-- | Swarm session is
data SwarmSession = SwarmSession {
    torrentMeta       :: !Torrent

    -- |
  , clientSession     :: !ClientSession

    -- | Represent count of peers we _currently_ can connect to in the
    -- swarm. Used to bound number of concurrent threads.
  , vacantPeers       :: !(MSem SessionCount)

    -- | Modify this carefully updating global progress.
  , clientBitfield    :: !(TVar  Bitfield)
  , connectedPeers    :: !(TVar (Set PeerSession))
  }

-- INVARIANT:
--   max_sessions_count - sizeof connectedPeers = value vacantPeers

instance Eq SwarmSession where
  (==) = (==) `on` (tInfoHash . torrentMeta)

instance Ord SwarmSession where
  compare = comparing (tInfoHash . torrentMeta)

getSessionCount :: SwarmSession -> IO SessionCount
getSessionCount SwarmSession {..} = do
  S.size <$> readTVarIO connectedPeers

newSwarmSession :: Int -> Bitfield -> ClientSession -> Torrent
                -> IO SwarmSession
newSwarmSession n bf cs @ ClientSession {..} t @ Torrent {..}
  = SwarmSession <$> pure t
                 <*> pure cs
                 <*> MSem.new n
                 <*> newTVarIO bf
                 <*> newTVarIO S.empty

newSeeder :: ClientSession -> Torrent -> IO SwarmSession
newSeeder cs t @ Torrent {..}
  = newSwarmSession defSeederConns (haveAll (pieceCount tInfo)) cs t

newLeacher :: ClientSession -> Torrent -> IO SwarmSession
newLeacher cs t @ Torrent {..}
  = newSwarmSession defLeacherConns (haveNone (pieceCount tInfo)) cs t

--isLeacher :: SwarmSession -> IO Bool
--isLeacher = undefined

{-
haveDone :: MonadIO m => PieceIx -> SwarmSession -> m ()
haveDone ix =
  liftIO $ atomically $ do
    bf <- readTVar clientBitfield
    writeTVar (have ix bf)
    currentProgress
-}

enterSwarm :: SwarmSession -> IO ()
enterSwarm SwarmSession {..} = do
  MSem.wait (activeThreads clientSession)
  MSem.wait vacantPeers

leaveSwarm :: SwarmSession -> IO ()
leaveSwarm SwarmSession {..} = do
  MSem.signal vacantPeers
  MSem.signal (activeThreads clientSession)

waitVacancy :: SwarmSession -> IO () -> IO ()
waitVacancy se =
  bracket (enterSwarm se) (const (leaveSwarm se))
                  . const

{-----------------------------------------------------------------------
    Peer session
-----------------------------------------------------------------------}

data PeerSession = PeerSession {
    -- | Used as unique 'PeerSession' identifier within one
    -- 'SwarmSession'.
    connectedPeerAddr :: !PeerAddr

  , swarmSession      :: !SwarmSession

    -- | Extensions such that both peer and client support.
  , enabledExtensions :: [Extension]

    -- | To dissconnect from died peers appropriately we should check
    -- if a peer do not sent the KA message within given interval. If
    -- yes, we should throw an exception in 'TimeoutCallback' and
    -- close session between peers.
    --
    -- We should update timeout if we /receive/ any message within
    -- timeout interval to keep connection up.
  , incomingTimeout     :: !TimeoutKey

    -- | To send KA message appropriately we should know when was last
    -- time we sent a message to a peer. To do that we keep registered
    -- timeout in event manager and if we do not sent any message to
    -- the peer within given interval then we send KA message in
    -- 'TimeoutCallback'.
    --
    -- We should update timeout if we /send/ any message within timeout
    -- to avoid reduntant KA messages.
  , outcomingTimeout   :: !TimeoutKey

    -- TODO use dupChan for broadcasting
  , broadcastMessages :: !(Chan   [Message])
  , sessionState      :: !(IORef  SessionState)
  }

data SessionState = SessionState {
    _bitfield :: !Bitfield
  , _status   :: !SessionStatus
  } deriving (Show, Eq)

$(makeLenses ''SessionState)

instance Eq PeerSession where
  (==) = (==) `on` connectedPeerAddr

instance Ord PeerSession where
  compare = comparing connectedPeerAddr

instance (MonadIO m, MonadReader PeerSession m)
      => MonadState SessionState m where
  get    = do
    ref <- asks sessionState
    st <- liftIO (readIORef ref)
    liftIO $ print (completeness (_bitfield st))
    return st

  put !s = asks sessionState >>= \ref -> liftIO $ writeIORef ref s

data SessionException = PeerDisconnected
                      | ProtocolError Doc
                        deriving (Show, Typeable)

instance Exception SessionException

isSessionException :: Monad m => SessionException -> m ()
isSessionException _ = return ()

putSessionException :: SessionException -> IO ()
putSessionException = print

-- TODO check if it connected yet peer
withPeerSession :: SwarmSession -> PeerAddr
                -> ((Socket, PeerSession) -> IO ())
                -> IO ()

withPeerSession ss @ SwarmSession {..} addr
    = handle isSessionException . bracket openSession closeSession
  where
    openSession = do
      let caps  = encodeExts $ allowedExtensions $ clientSession
      let ihash = tInfoHash torrentMeta
      let pid   = clientPeerID $ clientSession
      let chs   = Handshake defaultBTProtocol caps ihash pid

      sock <- connectToPeer addr
      phs  <- handshake sock chs `onException` close sock

      cbf <- readTVarIO clientBitfield
      sendAll sock (encode (Bitfield cbf))

      let enabled = decodeExts (enabledCaps caps (handshakeCaps phs))
      ps <- PeerSession addr ss enabled
         <$> registerTimeout (eventManager clientSession)
                maxIncomingTime (return ())
         <*> registerTimeout (eventManager clientSession)
                maxOutcomingTime (sendKA sock)
         <*> newChan
         <*> do {
           ; tc <- totalCount <$> readTVarIO clientBitfield
           ; newIORef (SessionState (haveNone tc) def)
           }

      atomically $ modifyTVar' connectedPeers (S.insert ps)

      return (sock, ps)

    closeSession (sock, ps) = do
      atomically $ modifyTVar' connectedPeers (S.delete ps)
      close sock

getPieceCount :: (MonadReader PeerSession m) => m PieceCount
getPieceCount = asks (pieceCount . tInfo . torrentMeta . swarmSession)

emptyBF :: (MonadReader PeerSession m) => m Bitfield
emptyBF = liftM haveNone getPieceCount

fullBF ::  (MonadReader PeerSession m) => m Bitfield
fullBF = liftM haveAll getPieceCount

singletonBF :: (MonadReader PeerSession m) => PieceIx -> m Bitfield
singletonBF i = liftM (BF.singleton i) getPieceCount

adjustBF :: (MonadReader PeerSession m) => Bitfield -> m Bitfield
adjustBF bf = (`adjustSize` bf) `liftM` getPieceCount

getClientBF :: (MonadIO m, MonadReader PeerSession m) => m Bitfield
getClientBF = asks swarmSession >>= liftIO . readTVarIO . clientBitfield

--data Signal =
--nextBroadcast :: P2P (Maybe Signal)
--nextBroadcast =


{-----------------------------------------------------------------------
    Timeouts
-----------------------------------------------------------------------}

sec :: Int
sec = 1000 * 1000

maxIncomingTime :: Int
maxIncomingTime = 120 * sec

maxOutcomingTime :: Int
maxOutcomingTime = 1 * sec

-- | Should be called after we have received any message from a peer.
updateIncoming :: PeerSession -> IO ()
updateIncoming PeerSession {..} = do
  updateTimeout (eventManager (clientSession swarmSession))
    incomingTimeout maxIncomingTime

-- | Should be called before we have send any message to a peer.
updateOutcoming :: PeerSession -> IO ()
updateOutcoming PeerSession {..}  =
  updateTimeout (eventManager (clientSession swarmSession))
    outcomingTimeout maxOutcomingTime

sendKA :: Socket -> IO ()
sendKA sock {- SwarmSession {..} -} = do
  return ()
--  print "I'm sending keep alive."
--  sendAll sock (encode BT.KeepAlive)
--  let mgr = eventManager clientSession
--  updateTimeout mgr
--  print "Done.."