bitcoin-atm

bitcoin atm for pyc inc.

git clone https://9o.is/git/bitcoin-atm.git

StateWatcher.scala

(1414B)


      1 package inc.pyc.chimera
      2 package snippet
      3 
      4 import System._
      5 import akka.actor._
      6 import FSM._
      7 
      8 object StateWatcher {
      9   val watcher = System.system.actorOf(Props[StateWatcher], "ClientWatcher")
     10 }
     11 
     12 /**
     13  * Snippet to update the machine's data.
     14  */
     15 class DataManager extends ChimeraEventRegister {
     16   val topic = Topic dataUpdate
     17 }
     18 
     19 /**
     20  * Snippet to update the machine's state.
     21  */
     22 class StateManager extends ChimeraEventRegister {
     23   val topic = Topic stateUpdate
     24   
     25   override val postSubscribe = () => {    
     26     overlord ! SubscribeTransitionCallBack(StateWatcher.watcher)
     27   }
     28 }
     29 
     30 /**
     31  * Message to send to client comet.
     32  */
     33 case class StateUpdate(from: Option[String], to: String)
     34 
     35 /**
     36  * Watches the state of Overlord to update
     37  * the client.
     38  */
     39 class StateWatcher extends Actor with ActorLogging {
     40         
     41   def receive = {
     42     case DataReply(data) =>
     43       sendData(data)
     44       
     45     case CurrentState(_, state) =>
     46       requestData
     47       sendState(None, state.toString)
     48       
     49     case Transition(_, oldState, state) =>
     50       requestData
     51       sendState(Some(oldState.toString), state.toString)
     52   }
     53   
     54   def sendState(from: Option[String], to: String) {
     55     client.updateState(StateUpdate(from, to))
     56   }
     57   
     58   def sendData[T >: Data](data: T) {
     59     client.updateData(data)
     60   }
     61   
     62   def requestData = overlord ! DataRequest
     63   
     64   override def postStop: Unit = {
     65     overlord ! UnsubscribeTransitionCallBack(self)
     66   }
     67 }