alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (ServerConsole.scala)

This example Scala source code file (ServerConsole.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

bufferedreader, bufferedreader, classnotfoundexception, cursor_home, eofexception, io, long, long, nil, server, server, serverconsole, string, string, unit

The Scala ServerConsole.scala source code

/*
 *  @author Stephane Micheloud
 */

import java.io._

import scala.compat.Platform.currentTime
import scala.remoting.Debug, Debug._

trait ServerConsole extends Thread {
  private val startTime = currentTime

  start()

  private var isTerminated = false

  def terminate() { isTerminated = true }

  protected def loop(block: => Unit) {
    while (!isTerminated) {
      try {
        block
      }
      catch {
        case e: ObjectStreamException =>
          trace("Object stream error ("+e.getMessage+")")
        case e: EOFException =>
          trace("Connection lost")
        case e: ClassNotFoundException =>
          trace("Class not found")
        case e =>
          trace("Server error: "+e)
      }
    }
  }

  override def run() {
    val in = new BufferedReader(new InputStreamReader(System.in))
    var quit = false
    while (!quit) {
      val args = getArgs(in)
      if (args contains "quit")
        quit = true
      if (args contains "cls") {
        println(ERASE_SCREEN)
        println(CURSOR_HOME)
      }
      if (args contains "warning")
        Debug.level = Level.WARNING
      if (args contains "info")
        Debug.level = Level.INFO
      if (args contains "silent")
        Debug.level = Level.SILENT
    }
    terminate()
    println("Server exited ("+mkTimeString(currentTime - startTime)+")")
    exit(0)

  }

  protected def trace(msg: String) {
    Debug.info("[ServerConsole.trace] "+msg)
  }

  private def getArgs(in: BufferedReader): List[String] = {
    print("> ")
    val input = try { in.readLine() } catch { case _ => null }
    if (input != null) (input.trim split "\\s+").toList else Nil
  }

  private def mkTimeString(time: Long): String = {
    def twoDigits(i: Long) = (if (i < 10) "0" else "")+i
    val sec = time / 1000
    val min = sec / 60
    val h = min / 60
    twoDigits(h) +":"+
    twoDigits(min - h * 60)+":"+
    twoDigits(sec - min * 60)
  }

  private val ERASE_SCREEN = "\033[2J"
  private val CURSOR_HOME  = "\033[H"
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala ServerConsole.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.