bitcoin-atm

bitcoin atm for pyc inc.

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

StateWatcher.scala

(1733B)


      1 package inc.pyc.chimera
      2 package lycia
      3 
      4 import inc.pyc.chimera.State
      5 import System._
      6 import akka.actor._
      7 import FSM._
      8 
      9 trait StateWatchHelper {
     10   this: FSM[State, Data] =>
     11     
     12     /**
     13      * State Watcher actor
     14      */
     15     def stateWatcher: ActorRef = Lycia.stateWatcher
     16     
     17     /**
     18      * Subscribes state transitions for state watcher
     19      */
     20     def watchTransitions(refs: ActorRef*) {
     21       refs foreach (_ ! SubscribeTransitionCallBack(stateWatcher))
     22     }
     23     
     24     /**
     25      * Unsubscribes state transitions for state watcher
     26      */
     27     def unwatchTransitions(refs: ActorRef*) {
     28       refs foreach (_ ! UnsubscribeTransitionCallBack(stateWatcher))
     29     }
     30 }
     31 
     32 /**
     33  * Responsible for monitoring state transitions
     34  * for finite state machines in the system. This
     35  * includes so far:
     36  * 
     37  * - Overlord
     38  * - Bill Acceptor (reported by Overlord)
     39  */
     40 class StateWatcher extends Actor {
     41 
     42   //val remote = context.actorSelection(
     43     //  "akka.tcp://actorSystemName@10.0.0.1:2552/user/actorName")
     44       
     45   override def preStart: Unit = {
     46     overlord ! SubscribeTransitionCallBack(self)
     47   }
     48       
     49   def receive = {
     50     case CurrentState(ref, state)  => send(ref, state)
     51     case Transition(ref, _, state) => send(ref, state)
     52   }
     53   
     54   def send(ref: ActorRef, state: Any) {
     55     // remote ! StateReport(name(ref), state.toString)
     56   }
     57   
     58   def name(ref: ActorRef): String = {
     59     if(ref equals overlord) "Overlord"
     60     else if(sender equals overlord) "Bill Acceptor"
     61     else "Unknown"
     62   }
     63   
     64   override def postStop: Unit = {
     65     overlord ! UnsubscribeTransitionCallBack(self)
     66   }
     67 }
     68 
     69 /**
     70  * Message to send to Lycia.
     71  * @param name name of the actor
     72  * @param state name of the state
     73  */
     74 case class StateReport(name: String, state: String)