Permalink
Browse files

Add a method to close client in a controlled way.

  • Loading branch information...
1 parent 47fb495 commit 0b897c80e2095141cc569d761dbe7ebd18fab583 @jonifreeman committed Jun 6, 2009
Showing with 23 additions and 11 deletions.
  1. +17 −8 src/main/scala/spmd/connection.scala
  2. +6 −3 src/main/scala/spmd/net_adm.scala
@@ -16,7 +16,12 @@ object Connection {
Response.from(in)
}
- def close = socket.close
+ /** Closes the connection in a controlled way. Server will not execute exitHandler.
+ */
+ def close = {
+ send(List(Attr("_close", "now")))
+ socket.close
+ }
}
trait Server {
@@ -43,15 +48,19 @@ object Connection {
try {
while (true) {
val req = Request.from(in, clientAddress)
- val action = actions.orElse(notFound)
- val res = action(req)
- out.write(res.toString)
- out.flush
+ req match {
+ case Request(_, List(Attr("_close", "now"))) =>
+ clientSocket.close
+ return
+ case _ =>
+ val action = actions.orElse(notFound)
+ val res = action(req)
+ out.write(res.toString)
+ out.flush
+ }
}
} catch {
- case e: IOException => // ok, client closed the connection
- } finally {
- exitHandler(clientAddress)
+ case t => exitHandler(clientAddress)
}
}
}
@@ -80,12 +80,15 @@ object NetAdm extends scala.actors.Actor {
private def connectionEstablished(node: Node) = monitoredNodes.values.contains(node)
private def addrOf(node: Node) = (for ((a, n) <- monitoredNodes if n == node) yield a).toList.head
- // FIXME this request should be closed
- private def requestConnectionFrom(monitoredNode: Node): Address =
- new Client(monitoredNode.address, monitoredNode.monitorPort).send(Console.node.toAttrs) match {
+ private def requestConnectionFrom(monitoredNode: Node): Address = {
+ val client = new Client(monitoredNode.address, monitoredNode.monitorPort)
+ val addr = client.send(Console.node.toAttrs) match {
case Response(List(List(Attr("address", a), Attr("port", p)))) => Address(a, p.toInt)
case Response(x) => error(x.toString)
}
+ client.close
+ addr
+ }
override val port = Console.node.monitorPort

0 comments on commit 0b897c8

Please sign in to comment.