Commit 8751f2cb authored by Paul Asmuth's avatar Paul Asmuth
Browse files

basic jetty+websocket integration

parent 89b9eccb
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ organization := "com.paulasmuth"

version := "0.0.1"

mainClass in (Compile, run) := Some("com.fnordmetric.enterprise.Enterprise")
mainClass in (Compile, run) := Some("com.fnordmetric.enterprise.FnordMetric")

scalaSource in Compile <<= baseDirectory(_ / "src")

@@ -16,7 +16,9 @@ scalaVersion := "2.9.1"

resolvers += "Couchbase Maven2 Repo" at "http://files.couchbase.com/maven2"

libraryDependencies += "org.eclipse.jetty" % "jetty-server" % "7.2.2.v20101205"
libraryDependencies += "org.eclipse.jetty" % "jetty-server" % "8.1.8.v20121106"

libraryDependencies += "org.eclipse.jetty" % "jetty-websocket" % "8.1.8.v20121106"

libraryDependencies += "org.scalatest" %% "scalatest" % "2.0.M1" % "test"

+39 −11
Original line number Diff line number Diff line
package com.fnordmetric.enterprise.FnordMetric
// FnordMetric Enterprise
//   (c) 2011-2013 Paul Asmuth <paul@paulasmuth.com>
//
// Licensed under the MIT License (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of
// the License at: http://opensource.org/licenses/MIT

package com.fnordmetric.enterprise

import java.util.Locale
import java.util.Date
import java.text.DateFormat
import java.io.File
import scala.collection.mutable.HashMap;
import scala.collection.mutable.HashMap

object SQLTap{
object FnordMetric {

  val VERSION = "v0.0.1"

  val CONFIG  = HashMap[Symbol,String]()
  var DEFAULTS = HashMap[Symbol, String]()

  var DEFAULTS = HashMap[Symbol, String](
    'http_threads      -> "4",
    'websocket_threads -> "4"
  )

  var debug = false
  var verbose = false
@@ -27,6 +38,12 @@ object SQLTap{
      else if(args(n) == "--http-threads")
        { CONFIG += (('http_threads, args(n+1))); n += 2 }

      else if(args(n) == "--websocket")
        { CONFIG += (('websocket_port, args(n+1))); n += 2 }

      else if(args(n) == "--websocket-threads")
        { CONFIG += (('websocket_threads, args(n+1))); n += 2 }

      else if((args(n) == "-d") || (args(n) == "--debug"))
        { debug = true; n += 1 }

@@ -50,7 +67,15 @@ object SQLTap{
  }

  def boot = try {
    SQLTap.log("Booting...")
    FnordMetric.log("Booting...")

    val websocket_threads = CONFIG('websocket_threads).toInt
    val websocket_port = CONFIG.getOrElse('websocket_port, "0")
      .asInstanceOf[String].toInt

    val websocket = if (websocket_port > 0)
      new HTTPServer(websocket_port, websocket_threads, new WebSocketHandler)

  } catch {
    case e: Exception => exception(e, true)
  }
@@ -67,6 +92,9 @@ object SQLTap{
    println("usage: fnordmetric-server [options]                                            ")
    println("  --http               <port>   start http server on this port                 ")
    println("  --http-threads       <num>    number of http worker-threads (default: 4)     ")
    println("  --websocket          <port>   start websocket server on this port            ")
    println("  --websocket-threads  <num>    number of websocket worker-threads (default: 4)")
    println("  --admin              <port>   start http admin web interface on this port    ")
    println("  -h, --help                    you're reading it...                           ")
    println("  -d, --debug                   debug mode                                     ")
    println("  -v, --verbose                 verbose mode                                   ")
+39 −0
Original line number Diff line number Diff line
// FnordMetric Enterprise
//   (c) 2011-2013 Paul Asmuth <paul@paulasmuth.com>
//
// Licensed under the MIT License (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of
// the License at: http://opensource.org/licenses/MIT

package com.fnordmetric.enterprise

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.handler.AbstractHandler
import org.eclipse.jetty.util.thread.QueuedThreadPool

import java.io.PrintStream
import java.io.OutputStream

class HTTPServer(port: Int, num_threads: Int, handler: AbstractHandler) {
  val server = new Server(port)
  /*server.setThreadPool(pool)
  server.setGracefulShutdown(1000)*/

  server.setHandler(handler)
  without_stderr(_ => server.start())

  FnordMetric.log("Listening on ws://0.0.0.0:" + port)

  def without_stderr(lambda: Unit => Unit) : Unit = {
    val stderr = System.err

    val dummy = new PrintStream(new OutputStream(){
      def write(b: Int) : Unit = ()
    })

    System.setErr(dummy)
    lambda()
    System.setErr(stderr)
  }

}
+20 −0
Original line number Diff line number Diff line
// FnordMetric Enterprise
//   (c) 2011-2013 Paul Asmuth <paul@paulasmuth.com>
//
// Licensed under the MIT License (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of
// the License at: http://opensource.org/licenses/MIT

package com.fnordmetric.enterprise

import org.eclipse.jetty.websocket.WebSocket.Connection

class WebSocket extends org.eclipse.jetty.websocket.WebSocket {

  def onOpen(conn: Connection) =
    FnordMetric.log_debug("[WebSocket] connection opened")

  def onClose(code: Int, message: String) =
    FnordMetric.log_debug("[WebSocket] connection closed")

}
+18 −0
Original line number Diff line number Diff line
// FnordMetric Enterprise
//   (c) 2011-2013 Paul Asmuth <paul@paulasmuth.com>
//
// Licensed under the MIT License (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of
// the License at: http://opensource.org/licenses/MIT

package com.fnordmetric.enterprise

import javax.servlet.http.HttpServletRequest

class WebSocketHandler extends org.eclipse.jetty.websocket.WebSocketHandler {

  def doWebSocketConnect(request: HttpServletRequest, protocol: String) : WebSocket = {
    new WebSocket()
  }

}