-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ping candidates and remove bad nodes
- Loading branch information
Showing
9 changed files
with
229 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 56 additions & 29 deletions
85
dht/src/main/scala/com/github/torrentdam/bittorrent/dht/Client.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,104 @@ | ||
package com.github.torrentdam.bittorrent.dht | ||
|
||
import cats.effect.kernel.Temporal | ||
import cats.effect.Concurrent | ||
import cats.effect.Resource | ||
import cats.effect.Sync | ||
import cats.effect.std.{Queue, Random} | ||
import cats.effect.{Concurrent, IO, Resource, Sync} | ||
import cats.syntax.all.* | ||
import com.comcast.ip4s.* | ||
import com.github.torrentdam.bittorrent.InfoHash | ||
|
||
import java.net.InetSocketAddress | ||
import org.legogroup.woof.given | ||
import org.legogroup.woof.Logger | ||
import scodec.bits.ByteVector | ||
|
||
trait Client[F[_]] { | ||
trait Client { | ||
|
||
def id: NodeId | ||
|
||
def getPeers(nodeInfo: NodeInfo, infoHash: InfoHash): F[Either[Response.Nodes, Response.Peers]] | ||
def getPeers(nodeInfo: NodeInfo, infoHash: InfoHash): IO[Either[Response.Nodes, Response.Peers]] | ||
|
||
def findNodes(nodeInfo: NodeInfo, target: NodeId): F[Response.Nodes] | ||
def findNodes(nodeInfo: NodeInfo, target: NodeId): IO[Response.Nodes] | ||
|
||
def ping(address: SocketAddress[IpAddress]): F[Response.Ping] | ||
def ping(address: SocketAddress[IpAddress]): IO[Response.Ping] | ||
|
||
def sampleInfoHashes(nodeInfo: NodeInfo, target: NodeId): F[Either[Response.Nodes, Response.SampleInfoHashes]] | ||
def sampleInfoHashes(nodeInfo: NodeInfo, target: NodeId): IO[Either[Response.Nodes, Response.SampleInfoHashes]] | ||
} | ||
|
||
object Client { | ||
|
||
def apply[F[_]]( | ||
def generateTransactionId(using random: Random[IO]): IO[ByteVector] = | ||
val nextChar = random.nextAlphaNumeric | ||
(nextChar, nextChar).mapN((a, b) => ByteVector.encodeAscii(List(a, b).mkString).toOption.get) | ||
|
||
def apply( | ||
selfId: NodeId, | ||
sendQueryMessage: (SocketAddress[IpAddress], Message.QueryMessage) => F[Unit], | ||
receiveResponse: F[(SocketAddress[IpAddress], Either[Message.ErrorMessage, Message.ResponseMessage])], | ||
generateTransactionId: F[ByteVector] | ||
)(using | ||
F: Temporal[F], | ||
logger: Logger[F] | ||
): Resource[F, Client[F]] = { | ||
for { | ||
messageSocket: MessageSocket, | ||
queryHandler: QueryHandler[IO] | ||
)(using Logger[IO], Random[IO]): Resource[IO, Client] = { | ||
for | ||
responses <- Resource.eval { | ||
Queue.unbounded[IO, (SocketAddress[IpAddress], Message.ErrorMessage | Message.ResponseMessage)] | ||
} | ||
requestResponse <- RequestResponse.make( | ||
generateTransactionId, | ||
sendQueryMessage, | ||
receiveResponse | ||
messageSocket.writeMessage, | ||
responses.take | ||
) | ||
} yield new Client[F] { | ||
_ <- | ||
messageSocket.readMessage | ||
.flatMap { | ||
case (a, m: Message.QueryMessage) => | ||
Logger[IO].debug(s"Received $m") >> | ||
queryHandler(a, m.query).flatMap { | ||
case Some(response) => | ||
val responseMessage = Message.ResponseMessage(m.transactionId, response) | ||
Logger[IO].debug(s"Responding with $responseMessage") >> | ||
messageSocket.writeMessage(a, responseMessage) | ||
case None => | ||
Logger[IO].debug(s"No response for $m") | ||
} | ||
case (a, m: Message.ResponseMessage) => responses.offer((a, m)) | ||
case (a, m: Message.ErrorMessage) => responses.offer((a, m)) | ||
} | ||
.recoverWith { case e: Throwable => | ||
Logger[IO].debug(s"Failed to read message: $e") | ||
} | ||
.foreverM | ||
.background | ||
yield new Client { | ||
|
||
def id: NodeId = selfId | ||
|
||
def getPeers( | ||
nodeInfo: NodeInfo, | ||
infoHash: InfoHash | ||
): F[Either[Response.Nodes, Response.Peers]] = | ||
): IO[Either[Response.Nodes, Response.Peers]] = | ||
requestResponse.sendQuery(nodeInfo.address, Query.GetPeers(selfId, infoHash)).flatMap { | ||
case nodes: Response.Nodes => nodes.asLeft.pure | ||
case peers: Response.Peers => peers.asRight.pure | ||
case _ => F.raiseError(InvalidResponse()) | ||
case _ => IO.raiseError(InvalidResponse()) | ||
} | ||
|
||
def findNodes(nodeInfo: NodeInfo, target: NodeId): F[Response.Nodes] = | ||
def findNodes(nodeInfo: NodeInfo, target: NodeId): IO[Response.Nodes] = | ||
requestResponse.sendQuery(nodeInfo.address, Query.FindNode(selfId, target)).flatMap { | ||
case nodes: Response.Nodes => nodes.pure | ||
case _ => Concurrent[F].raiseError(InvalidResponse()) | ||
case _ => IO.raiseError(InvalidResponse()) | ||
} | ||
|
||
def ping(address: SocketAddress[IpAddress]): F[Response.Ping] = | ||
def ping(address: SocketAddress[IpAddress]): IO[Response.Ping] = | ||
requestResponse.sendQuery(address, Query.Ping(selfId)).flatMap { | ||
case ping: Response.Ping => ping.pure | ||
case _ => Concurrent[F].raiseError(InvalidResponse()) | ||
case _ => IO.raiseError(InvalidResponse()) | ||
} | ||
def sampleInfoHashes(nodeInfo: NodeInfo, target: NodeId): F[Either[Response.Nodes, Response.SampleInfoHashes]] = | ||
def sampleInfoHashes(nodeInfo: NodeInfo, target: NodeId): IO[Either[Response.Nodes, Response.SampleInfoHashes]] = | ||
requestResponse.sendQuery(nodeInfo.address, Query.SampleInfoHashes(selfId, target)).flatMap { | ||
case response: Response.SampleInfoHashes => response.asRight[Response.Nodes].pure | ||
case response: Response.Nodes => response.asLeft[Response.SampleInfoHashes].pure | ||
case _ => Concurrent[F].raiseError(InvalidResponse()) | ||
case _ => IO.raiseError(InvalidResponse()) | ||
} | ||
} | ||
} | ||
|
||
case class BootstrapError(message: String) extends Throwable(message) | ||
case class InvalidResponse() extends Throwable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.