Class: SCXMLClient::StatefulProtocol

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EM::Protocols::LineText2
Defined in:
lib/MINT-core/manager/scxml_client.rb

Constant Summary collapse

@@host =
nil
@@port =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatefulProtocol

Returns a new instance of StatefulProtocol.



24
25
26
27
28
# File 'lib/MINT-core/manager/scxml_client.rb', line 24

def initialize


  super()
end

Class Method Details

.config(host, port) ⇒ Object



38
39
40
41
# File 'lib/MINT-core/manager/scxml_client.rb', line 38

def self.config(host,port)
  @@host = host
  @@port = port
end

Instance Method Details

#send_active_state(name) ⇒ Object



84
85
86
# File 'lib/MINT-core/manager/scxml_client.rb', line 84

def send_active_state(name)
  send_data "1 #{name}\r\n"
end

#send_inactive_state(name) ⇒ Object



88
89
90
# File 'lib/MINT-core/manager/scxml_client.rb', line 88

def send_inactive_state(name)
  send_data "0 #{name}\r\n"
end

#subscribe_redis(interactor, name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/MINT-core/manager/scxml_client.rb', line 43

def subscribe_redis(interactor,name)
  @interactor = interactor
  @name = name

  redis = EventMachine::Hiredis.connect

  redis.pubsub.psubscribe(@interactor) { |key, message|

    found=MultiJson.decode message
    if @name.nil? or @name.eql? found["name"]
      if found.has_key? "new_states"
        # send new state
        found["new_states"].each { |state|
          p "activated: #{state}"
          send_active_state state }
      end

      current_active_states = found["states"] + found["abstract_states"].split("|")
      if(SCXMLClient.state_store.has_key? key)
        activated_states =  SCXMLClient.state_store[key]



        deactivated_states = activated_states - current_active_states

        # send deactivate states

        deactivated_states.each {|state|
          p "deactivated: #{state}"
          send_inactive_state state
        }

        #save activated states

      end
      SCXMLClient.state_store[key] = current_active_states
    end
  }.callback { p "subscribed to #{@interactor} name #{@name}"}

end

#unbindObject



30
31
32
33
34
35
36
# File 'lib/MINT-core/manager/scxml_client.rb', line 30

def unbind
  puts "#{@@host}: #{@@port}"
   puts "-- disconnected from remote server!"
   puts "-- attempting reconnection"
   #reconnect @@ip, @@port # use reconnect, already provided by E
  EventMachine.add_timer 15, proc { reconnect(@@host,@@port) }
end