summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Exchange.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-11 11:01:27 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-11 11:01:27 +0400
commit7c14f44783d4f5e241ce37027dd60fefcc3f5382 (patch)
tree685643cd1a1f836b261b29faba5a7c71fcb2ad46 /src/Network/BitTorrent/Exchange.hs
parent0e3903fa3d486c57504837fd497a3a348793f7fc (diff)
~ Use data-default for default values.
Diffstat (limited to 'src/Network/BitTorrent/Exchange.hs')
-rw-r--r--src/Network/BitTorrent/Exchange.hs71
1 files changed, 46 insertions, 25 deletions
diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs
index 2173cf8b..dda7d304 100644
--- a/src/Network/BitTorrent/Exchange.hs
+++ b/src/Network/BitTorrent/Exchange.hs
@@ -60,10 +60,11 @@ waitMessage se = do
60 Nothing -> waitMessage se 60 Nothing -> waitMessage se
61 Just msg -> do 61 Just msg -> do
62 liftIO $ updateIncoming se 62 liftIO $ updateIncoming se
63 liftIO $ print msg
63 return msg 64 return msg
64 65
65signalMessage :: Message -> PeerSession -> PeerWire () 66signalMessage :: PeerSession -> Message -> PeerWire ()
66signalMessage msg se = do 67signalMessage se msg = do
67 C.yield msg 68 C.yield msg
68 liftIO $ updateOutcoming se 69 liftIO $ updateOutcoming se
69 70
@@ -71,58 +72,75 @@ signalMessage msg se = do
71getPieceCount :: PeerSession -> IO PieceCount 72getPieceCount :: PeerSession -> IO PieceCount
72getPieceCount = undefined 73getPieceCount = undefined
73 74
75canOffer :: PeerSession -> PeerWire Bitfield
76canOffer PeerSession {..} = liftIO $ do
77 pbf <- readIORef $ peerBitfield
78 cbf <- readIORef $ clientBitfield $ swarmSession
79 return $ BF.difference pbf cbf
80
81revise :: PeerSession -> PeerWire ()
82revise se @ PeerSession {..} = do
83 isInteresting <- (not . BF.null) <$> canOffer se
84 SessionStatus {..} <- liftIO $ readIORef peerSessionStatus
85
86 when (isInteresting /= _interested seClientStatus) $
87 signalMessage se $ if isInteresting then Interested else NotInterested
88
89
74nextEvent :: PeerSession -> PeerWire Event 90nextEvent :: PeerSession -> PeerWire Event
75nextEvent se @ PeerSession {..} = waitMessage se >>= diff 91nextEvent se @ PeerSession {..} = waitMessage se >>= go
76 where 92 where
77 -- diff finds difference between 93 go KeepAlive = nextEvent se
78-- diff KeepAlive = nextEvent se 94 go Choke = do
79 diff msg = do
80 liftIO $ print (ppMessage msg)
81 nextEvent se
82
83 handleMessage Choke = do
84 SessionStatus {..} <- liftIO $ readIORef peerSessionStatus 95 SessionStatus {..} <- liftIO $ readIORef peerSessionStatus
85 if psChoking sePeerStatus 96 if _choking sePeerStatus
86 then nextEvent se 97 then nextEvent se
87 else undefined 98 else undefined
88 99
89 handleMessage Unchoke = undefined 100 go Unchoke = do
101 SessionStatus {..} <- liftIO $ readIORef peerSessionStatus
102 if not (_choking sePeerStatus)
103 then nextEvent se
104 else if undefined
105 then undefined
106 else undefined
90 --return $ Available BF.difference 107 --return $ Available BF.difference
91 108
92 handleMessage Interested = return undefined 109 go Interested = return undefined
93 handleMessage NotInterested = return undefined 110 go NotInterested = return undefined
94 handleMessage (Have ix) = do 111
112 go (Have ix) = do
95 pc <- liftIO $ getPieceCount se 113 pc <- liftIO $ getPieceCount se
96 haveMessage $ have ix (haveNone pc) -- TODO singleton 114 haveMessage $ have ix (haveNone pc) -- TODO singleton
97 115
98 handleMessage (Bitfield bf) = undefined 116 go (Bitfield bf) = undefined
99 handleMessage (Request bix) = do 117 go (Request bix) = do
100 undefined 118 undefined
101 119
102 handleMessage msg @ (Piece blk) = undefined 120 go msg @ (Piece blk) = undefined
103 handleMessage msg @ (Port _) 121 go msg @ (Port _)
104 = checkExtension msg ExtDHT $ do 122 = checkExtension msg ExtDHT $ do
105 undefined 123 undefined
106 124
107 handleMessage msg @ HaveAll 125 go msg @ HaveAll
108 = checkExtension msg ExtFast $ do 126 = checkExtension msg ExtFast $ do
109 pc <- liftIO $ getPieceCount se 127 pc <- liftIO $ getPieceCount se
110 haveMessage (haveAll pc) 128 haveMessage (haveAll pc)
111 129
112 handleMessage msg @ HaveNone 130 go msg @ HaveNone
113 = checkExtension msg ExtFast $ do 131 = checkExtension msg ExtFast $ do
114 pc <- liftIO $ getPieceCount se 132 pc <- liftIO $ getPieceCount se
115 haveMessage (haveNone pc) 133 haveMessage (haveNone pc)
116 134
117 handleMessage msg @ (SuggestPiece ix) 135 go msg @ (SuggestPiece ix)
118 = checkExtension msg ExtFast $ do 136 = checkExtension msg ExtFast $ do
119 undefined 137 undefined
120 138
121 handleMessage msg @ (RejectRequest ix) 139 go msg @ (RejectRequest ix)
122 = checkExtension msg ExtFast $ do 140 = checkExtension msg ExtFast $ do
123 undefined 141 undefined
124 142
125 handleMessage msg @ (AllowedFast pix) 143 go msg @ (AllowedFast pix)
126 = checkExtension msg ExtFast $ do 144 = checkExtension msg ExtFast $ do
127 undefined 145 undefined
128 146
@@ -148,7 +166,10 @@ newtype P2P a = P2P {
148 runP2P :: ReaderT PeerSession PeerWire a 166 runP2P :: ReaderT PeerSession PeerWire a
149 } deriving (Monad, MonadReader PeerSession, MonadIO) 167 } deriving (Monad, MonadReader PeerSession, MonadIO)
150 168
151instance MonadState Bitfield P2P where 169instance MonadState SessionStatus P2P where
170 get = asks peerSessionStatus >>= liftIO . readIORef
171 put x = asks peerSessionStatus >>= liftIO . (`writeIORef` x)
172
152 173
153runConduit :: Socket -> Conduit Message IO Message -> IO () 174runConduit :: Socket -> Conduit Message IO Message -> IO ()
154runConduit sock p2p = 175runConduit sock p2p =