From 5e1a315d37e78cdef476a52206736ec09f8d3b82 Mon Sep 17 00:00:00 2001 From: fluency03 Date: Sun, 29 Apr 2018 15:10:06 +0200 Subject: [PATCH] add GetPeers based on names --- .../blockchain/api/actors/NetworkActor.scala | 11 +++++++---- .../blockchain/api/routes/NetworkRoutes.scala | 14 ++++++++++++-- .../blockchain/api/actors/NetworkActorTest.scala | 10 ++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala b/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala index 41b73fe..bd953f1 100644 --- a/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala +++ b/src/main/scala/com/fluency03/blockchain/api/actors/NetworkActor.scala @@ -12,6 +12,7 @@ import scala.concurrent.Future object NetworkActor { final case object GetNetwork final case object GetPeers + final case class GetPeers(names: Set[String]) final case class CreatePeer(name: String) final case class GetPeer(name: String) final case class DeletePeer(name: String) @@ -34,6 +35,7 @@ class NetworkActor extends ActorSupport { def receive: Receive = { case GetNetwork => onGetNetwork() case GetPeers => onGetPeers() + case GetPeers(names) => onGetPeers(names) case CreatePeer(name) => onCreatePeer(name) case GetPeer(name) => onGetPeer(name) case DeletePeer(name) => onDeletePeer(name) @@ -46,6 +48,11 @@ class NetworkActor extends ActorSupport { (p ? GetPublicKeys).mapTo[Set[String]].map(keys => p.path.name -> keys) })).map(_.toMap).pipeTo(sender()) + private def onGetPeers(names: Set[String]): Unit = Future.sequence(context.children + .filter(p => names.contains(p.path.name)) + .map(p => { (p ? GetPublicKeys).mapTo[Set[String]].map(keys => p.path.name -> keys) }) + ).map(_.toMap).pipeTo(sender()) + private def onCreatePeer(name: String): Unit = if (context.child(name).isDefined) sender() ! FailureMsg(s"Peer $name has been created.") else { @@ -67,8 +74,4 @@ class NetworkActor extends ActorSupport { sender() ! SuccessMsg(s"Peer $name deleted.") } else sender() ! FailureMsg(s"Peer $name does not exist.") - // TODO (Chang): APIs for selecting Peers based on Seq of ids - - - } diff --git a/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala b/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala index 619e680..4abf69c 100644 --- a/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala +++ b/src/main/scala/com/fluency03/blockchain/api/routes/NetworkRoutes.scala @@ -2,6 +2,7 @@ package com.fluency03.blockchain.api.routes import akka.actor.ActorRef import akka.event.Logging +import akka.http.scaladsl.unmarshalling.PredefinedFromStringUnmarshallers.CsvSeq import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route @@ -29,8 +30,17 @@ trait NetworkRoutes extends RoutesSupport { } ~ path(PEERS) { get { - val peers: Future[Map[String, Set[String]]] = (networkActor ? GetPeers).mapTo[Map[String, Set[String]]] - complete(peers) + parameters( 'names.as(CsvSeq[String]) ? ) { names => + if (names.isDefined) { + val peers: Future[Map[String, Set[String]]] = + (networkActor ? GetPeers(names.get.toSet)).mapTo[Map[String, Set[String]]] + complete(peers) + } else { + val peers: Future[Map[String, Set[String]]] = + (networkActor ? GetPeers).mapTo[Map[String, Set[String]]] + complete(peers) + } + } } } ~ pathPrefix(PEER) { diff --git a/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala b/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala index be65646..2e78763 100644 --- a/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala +++ b/src/test/scala/com/fluency03/blockchain/api/actors/NetworkActorTest.scala @@ -26,6 +26,9 @@ class NetworkActorTest extends TestKit(ActorSystem("NetworkActorTest")) with Imp networkActor ! GetPeers expectMsg(Map.empty[String, Set[String]]) + networkActor ! GetPeers(Set("somename")) + expectMsg(Map.empty[String, Set[String]]) + val name = "peer" networkActor ! CreatePeer(name) expectMsg(SuccessMsg(s"Peer $name created.")) @@ -42,6 +45,13 @@ class NetworkActorTest extends TestKit(ActorSystem("NetworkActorTest")) with Imp peers.contains(name) shouldEqual true peers(name).size shouldEqual 1 + networkActor ! GetPeers(Set("somename")) + expectMsg(Map.empty[String, Set[String]]) + + networkActor ! GetPeers(Set(name)) + val peers2 = expectMsgType[Map[String, Set[String]]] + peers2 shouldEqual peers + networkActor ! GetPeer(name) val peerOpt = expectMsgType[Some[Peer]] peerOpt.isDefined shouldEqual true