Commits

Pavel Kozlov committed b04faad

fix error after sending to wrong address

Comments (0)

Files changed (6)

qcp-p2p-sim/src/main/scala/qcp/p2p/gui/Simulator.scala

   }
 
   val system = ActorSystem()
-  val client = system.actorOf(Props(new Client(textArea)), "client")
   val server = system.actorOf(Props(new Server(textArea)), "server")
 
   val bindPortLabel = new Label("Port")
       case ButtonClicked(`sendButton`) =>
         val address = new InetSocketAddress(hostnameField.text, portField.text.toInt)
         val message = messageField.text
-        client ! Client.SendTo(message, address)
+        system.actorOf(Props(new Client(message, address, textArea)))
         val time = timeFormat.format(Calendar.getInstance().getTime)
         textArea append s"$time: $address <= $message\n"
 

qcp-p2p-sim/src/main/scala/qcp/p2p/impl/Client.scala

 import java.net._
 import scala.swing.Publisher
 
-class Client(publisher: Publisher) extends Actor with ActorLogging {
+class Client(message: String, address: InetSocketAddress, publisher: Publisher) extends Actor with ActorLogging {
 
   import context.system
 
-  override def receive = disconnected
+  log.info("Sending message '{}' to address {}", message, address)
+  IO(Tcp) ! Connect(address)
 
-  val disconnected: Receive = {
-
-    case Client.SendTo(message, address) =>
-      log.info("Sending message '{}' to address {}", message, address)
-      IO(Tcp) ! Connect(address)
-      context become connected(message)
-
-  }
-
-  def connected(message: String): Receive = {
+  override def receive = {
 
     case Connected(remote, local) =>
       log.info("Connected, remote={}, local={}", remote, local)
       val handler = context.actorOf(Props(new ClientHandler(message, connection, publisher)))
       connection ! Register(handler)
 
-    case c: ConnectionClosed => context become disconnected
-  }
+    case c: ConnectionClosed =>
+      log.info(s"Connection closed $c")
+      context stop self
 
-}
-
-object Client {
-
-  case class SendTo(message: String, address: InetSocketAddress)
+  }
 
 }

qcp-p2p-sim/src/main/scala/qcp/p2p/impl/ClientHandler.scala

       connection ! c
 
     case c: ConnectionClosed =>
-      log.info(s"Closed $c")
       context.parent ! c
-      context stop self
   }
 
   def buffering(buffer: ByteString, size: Int): Receive = {

qcp-p2p-sim/src/main/scala/qcp/p2p/impl/Receiver.scala

   }
 
 }
-
-object Receiver {
-
-  def props(address: InetSocketAddress, publisher: Publisher) = Props(new Receiver(address, publisher))
-
-}

qcp-p2p-sim/src/main/scala/qcp/p2p/impl/Server.scala

 
     case c@Connected(remote, local) =>
       log.info(s"Connected remote=$remote, local=$local")
-      val handler = context.actorOf(ServerHandler.props(sender(), remote, publisher))
+      val connection = sender()
+      val handler = context.actorOf(Props(new ServerHandler(connection, remote, publisher)))
       sender ! Register(handler)
 
-    case c: ConnectionClosed => log.info(s"Connection closed $c")
-
     case Unbind => socket ! Unbind
 
     case Unbound =>

qcp-p2p-sim/src/main/scala/qcp/p2p/impl/ServerHandler.scala

 
 class ServerHandler(connection: ActorRef, address: InetSocketAddress, publisher: Publisher) extends Actor with ActorLogging {
 
-  val receiver = context.actorOf(Receiver.props(address, publisher))
+  val receiver = context.actorOf(Props(new Receiver(address, publisher)))
 
   override def receive = {
     case Received(data) =>
       } else context.become(buffering(bs, size))
   }
 
-
-}
-
-object ServerHandler {
-  def props(connection: ActorRef, address: InetSocketAddress, publisher: Publisher) = Props(new ServerHandler(connection, address, publisher))
 }