Skip to content

Commit

Permalink
add GetPeers based on names
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 29, 2018
1 parent 983993f commit 5e1a315
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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



}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
Expand All @@ -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
Expand Down

0 comments on commit 5e1a315

Please sign in to comment.