Class: CPEE::Controller
- Inherits:
-
Object
- Object
- CPEE::Controller
- Defined in:
- lib/cpee/controller.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#loop_guard ⇒ Object
readonly
Returns the value of attribute loop_guard.
Instance Method Summary collapse
- #attributes_translated ⇒ Object
- #base ⇒ Object
- #base_url ⇒ Object
- #callback(hw, key, content) ⇒ Object
- #cancel_callback(key) ⇒ Object
- #dataelements ⇒ Object
- #endpoints ⇒ Object
- #host ⇒ Object
- #info ⇒ Object
-
#initialize(id, dir, opts) ⇒ Controller
constructor
A new instance of Controller.
- #instance=(inst) ⇒ Object
- #instance_id ⇒ Object
- #instance_url ⇒ Object
- #notify(what, content = {}) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #uuid ⇒ Object
- #vote(what, content = {}) ⇒ Object
Constructor Details
#initialize(id, dir, opts) ⇒ Controller
Returns a new instance of Controller.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cpee/controller.rb', line 36 def initialize(id,dir,opts) @redis = Redis.new(path: opts[:redis_path], db: opts[:redis_db]) @votes = [] @id = id @attributes = {} @redis.keys("instance:#{id}/attributes/*").each do |key| @attributes[File.basename(key)] = @redis.get(key) end @attributes_helper = AttributesHelper.new @thread = nil @opts = opts @instance = nil @loop_guard = {} end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
55 56 57 |
# File 'lib/cpee/controller.rb', line 55 def attributes @attributes end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
54 55 56 |
# File 'lib/cpee/controller.rb', line 54 def id @id end |
#loop_guard ⇒ Object (readonly)
Returns the value of attribute loop_guard.
56 57 58 |
# File 'lib/cpee/controller.rb', line 56 def loop_guard @loop_guard end |
Instance Method Details
#attributes_translated ⇒ Object
62 63 64 |
# File 'lib/cpee/controller.rb', line 62 def attributes_translated @attributes_helper.translate(attributes,dataelements,endpoints) end |
#base ⇒ Object
78 79 80 |
# File 'lib/cpee/controller.rb', line 78 def base base_url end |
#base_url ⇒ Object
69 70 71 |
# File 'lib/cpee/controller.rb', line 69 def base_url @opts[:url] end |
#callback(hw, key, content) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/cpee/controller.rb', line 157 def callback(hw,key,content) CPEE::Message::send(:callback,'activity/content',base,@id,uuid,info,content.merge(:key => key),@redis) psredis = Redis.new(path: @opts[:redis_path], db: @opts[:redis_db]) response = nil Thread.new do psredis.subscribe('callback-response:' + key, 'callback-end:' + key) do |on| on. do |what, | if what == 'callback-response:' + key index = .index(' ') mess = [index+1..-1] instance = [0...index] m = JSON.parse(mess) resp = [] m['content']['values'].each do |e| if e[1][0] == 'simple' resp << Riddl::Parameter::Simple.new(e[0],e[1][1]) elsif e[1][0] == 'complex' resp << Riddl::Parameter::Complex.new(e[0],e[1][1],File.open(e[1][2])) end end hw.send(:callback,resp,m['content']['headers']) end if what == 'callback-end:' + key psredis.unsubscribe end end end end end |
#cancel_callback(key) ⇒ Object
188 189 190 |
# File 'lib/cpee/controller.rb', line 188 def cancel_callback(key) CPEE::Message::send(:'callback-end',key,base,@id,uuid,info,{},@redis) end |
#dataelements ⇒ Object
87 88 89 |
# File 'lib/cpee/controller.rb', line 87 def dataelements @instance.data end |
#endpoints ⇒ Object
84 85 86 |
# File 'lib/cpee/controller.rb', line 84 def endpoints @instance.endpoints end |
#host ⇒ Object
66 67 68 |
# File 'lib/cpee/controller.rb', line 66 def host @opts[:host] end |
#info ⇒ Object
113 114 115 |
# File 'lib/cpee/controller.rb', line 113 def info @attributes['info'] end |
#instance=(inst) ⇒ Object
81 82 83 |
# File 'lib/cpee/controller.rb', line 81 def instance=(inst) @instance = inst end |
#instance_id ⇒ Object
75 76 77 |
# File 'lib/cpee/controller.rb', line 75 def instance_id @id end |
#instance_url ⇒ Object
72 73 74 |
# File 'lib/cpee/controller.rb', line 72 def instance_url File.join(@opts[:url].to_s,@id.to_s) end |
#notify(what, content = {}) ⇒ Object
117 118 119 120 |
# File 'lib/cpee/controller.rb', line 117 def notify(what,content={}) content[:attributes] = attributes_translated CPEE::Message::send(:event,what,base,@id,uuid,info,content,@redis) end |
#start ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/cpee/controller.rb', line 91 def start if vote("state/change") @thread = @instance.start @thread.join else @thread = @instance.stop @thread.join end end |
#stop ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/cpee/controller.rb', line 101 def stop ### tell the instance to stop @instance.stop ### end all votes or it will not work Thread.new do # doing stuff in trap context is a nono. but in a thread its fine :-) @votes.each do |key| CPEE::Message::send(:'vote-response',key,base,@id,uuid,info,true,@redis) end end @thread.join if !@thread.nil? && @thread.alive? end |
#uuid ⇒ Object
58 59 60 |
# File 'lib/cpee/controller.rb', line 58 def uuid @attributes['uuid'] end |
#vote(what, content = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/cpee/controller.rb', line 122 def vote(what,content={}) topic, name = what.split('/') handler = File.join(topic,'vote',name) votes = [] @redis.smembers("instance:#{id}/handlers/#{handler}").each do |client| voteid = Digest::MD5.hexdigest(Kernel::rand().to_s) content[:key] = voteid content[:subscription] = client votes << voteid CPEE::Message::send(:vote,what,base,@id,uuid,info,content,@redis) end if votes.length > 0 @votes += votes psredis = Redis.new(path: @opts[:redis_path], db: @opts[:redis_db]) collect = [] psredis.subscribe(votes.map{|e| ['vote-response:' + e.to_s, 'vote-end:' + e.to_s] }.flatten) do |on| on. do |what, | index = .index(' ') mess = [index+1..-1] m = JSON.parse(mess) collect << (m['content'] == 'true' || false) @votes.delete m['name'] cancel_callback m['name'] if collect.length >= votes.length psredis.unsubscribe end end end !collect.include?(false) else true end end |