2012-02-03
a scala snippet again
val t: Thread = new Thread("Server at " + port) { val dispatch = Map("POST /status/success" -> processSuccess _, "POST /status/failure" -> processFailure _, "POST /queryrunstate" -> processRunQuery _, "GET /comet_request" -> tellThemToShutUp _ ) override def run { while (!Thread.interrupted()) { try { val s = ss.accept val source = Source.fromInputStream(s.getInputStream) val rq = source.getLines.next try { val f = dispatch.keys.find(rq startsWith _).map(dispatch).getOrElse(ignore _) f(rq, s, source) } catch { case se : SocketException => log(se) } finally { s.close } } catch { case ioe: IOException => log(ioe) case ie: InterruptedException => return } } } } def tellThemToShutUp(rq: String, socket: Socket, source: Source) { Response("oh shut up, this is not the web server you are lookign for", "410 Gone") }
Entry tags:
scala snippet
val serverSocket = try {new ServerSocket(port)} catch { case e: java.net.BindException => { println("wtf, several copies? what is it? ") throw new IllegalStateException("this port, " + port + ", is already taken!") } }