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

io.udash.rpc.internals.BroadcastManager.scala Maven / Gradle / Ivy

There is a newer version: 0.2.0-rc.3
Show newest version
package io.udash.rpc.internals

import com.typesafe.scalalogging.LazyLogging
import org.atmosphere.cpr._

private[rpc] object BroadcastManager extends LazyLogging {
  private var factory: BroadcasterFactory = null
  private var metaBroadcaster: MetaBroadcaster = null

  def init(factory: BroadcasterFactory, metaBroadcaster: MetaBroadcaster) = {
    if (this.factory != null || this.metaBroadcaster != null) {
      logger.warn("BroadcastManager is reinitialized! It should not happen!")
    }

    this.factory = factory
    this.metaBroadcaster = metaBroadcaster
  }

  def registerResource(resource: AtmosphereResource, clientId: String): Unit = {
    require(factory != null, "Init manager with BroadcasterFactory first!")

    val clientBroadcaster = factory.lookup[Broadcaster](clientPath(clientId), true)
    clientBroadcaster.addAtmosphereResource(resource)
    clientBroadcaster.setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy.EMPTY_DESTROY)
  }

  def sendToClient(clientId: String, msg: String): Unit = {
    require(factory != null, "Init manager with BroadcasterFactory first!")

    val clientBroadcaster = factory.lookup[Broadcaster](clientPath(clientId), true)
    clientBroadcaster.broadcast(msg)
  }

  def broadcastToAllClients(msg: String): Unit = {
    require(metaBroadcaster != null, "Init manager with MetaBroadcaster first!")

    metaBroadcaster.broadcastTo(clientPath(pathWildcard), msg)
  }

  def broadcast(msg: String): Unit = {
    require(metaBroadcaster != null, "Init manager with MetaBroadcaster first!")

    metaBroadcaster.broadcastTo(clientPath(pathWildcard), msg)
  }

  private def clientPath(clientId: String) = s"/client/$clientId"

  private def pathWildcard = "*"
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy