All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hyperscala.web.site.Temporal.scala Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package org.hyperscala.web.site

import org.powerscala.Updatable
import org.powerscala.concurrent.Time._
import org.powerscala.log.Logging

/**
 * @author Matt Hicks 
 */
trait Temporal extends Updatable with Logging with Disposable {
  val created = System.currentTimeMillis()

  private var lastCheckIn = created
  private var _disposed = false

  final def lifetime = fromMillis(System.currentTimeMillis() - created)
  final def stale = fromMillis(System.currentTimeMillis() - lastCheckIn)
  final def disposed = _disposed

  def timeout: Double

  protected[web] def checkIn() = lastCheckIn = System.currentTimeMillis()

  override def update(delta: Double) {
    val elapsed = stale
    if (elapsed > timeout) {    // Temporal has timed out
      debug("%s (%s) has timed out; Elapsed: %s, Timeout: %s".format(getClass.getSimpleName, this, elapsed, timeout))
      _disposed = true
      dispose()
    } else if (disposed) {
      throw new RuntimeException("%s (%s) has already been disposed, it cannot be updated anymore!".format(getClass.getSimpleName, this))
    } else {
      super.update(delta)
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy