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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cpee/controller.rb', line 37 def initialize(id,dir,opts) CPEE::redis_connect(opts) @redis = opts[:redis] @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.
58 59 60 |
# File 'lib/cpee/controller.rb', line 58 def attributes @attributes end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
57 58 59 |
# File 'lib/cpee/controller.rb', line 57 def id @id end |
#loop_guard ⇒ Object (readonly)
Returns the value of attribute loop_guard.
59 60 61 |
# File 'lib/cpee/controller.rb', line 59 def loop_guard @loop_guard end |
Instance Method Details
#attributes_translated ⇒ Object
65 66 67 |
# File 'lib/cpee/controller.rb', line 65 def attributes_translated @attributes_helper.translate(attributes,dataelements,endpoints) end |
#base ⇒ Object
81 82 83 |
# File 'lib/cpee/controller.rb', line 81 def base base_url end |
#base_url ⇒ Object
72 73 74 |
# File 'lib/cpee/controller.rb', line 72 def base_url @opts[:url] end |
#callback(hw, key, content) ⇒ Object
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 187 188 189 190 |
# File 'lib/cpee/controller.rb', line 161 def callback(hw,key,content) CPEE::Message::send(:callback,'activity/content',base,@id,uuid,info,content.merge(:key => key),@redis) psredis = @opts[:redis_dyn].call 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
192 193 194 |
# File 'lib/cpee/controller.rb', line 192 def cancel_callback(key) CPEE::Message::send(:'callback-end',key,base,@id,uuid,info,{},@redis) end |
#dataelements ⇒ Object
90 91 92 |
# File 'lib/cpee/controller.rb', line 90 def dataelements @instance.data end |
#endpoints ⇒ Object
87 88 89 |
# File 'lib/cpee/controller.rb', line 87 def endpoints @instance.endpoints end |
#host ⇒ Object
69 70 71 |
# File 'lib/cpee/controller.rb', line 69 def host @opts[:host] end |
#info ⇒ Object
116 117 118 |
# File 'lib/cpee/controller.rb', line 116 def info @attributes['info'] end |
#instance=(inst) ⇒ Object
84 85 86 |
# File 'lib/cpee/controller.rb', line 84 def instance=(inst) @instance = inst end |
#instance_id ⇒ Object
78 79 80 |
# File 'lib/cpee/controller.rb', line 78 def instance_id @id end |
#instance_url ⇒ Object
75 76 77 |
# File 'lib/cpee/controller.rb', line 75 def instance_url File.join(@opts[:url].to_s,@id.to_s) end |
#notify(what, content = {}) ⇒ Object
120 121 122 123 |
# File 'lib/cpee/controller.rb', line 120 def notify(what,content={}) content[:attributes] = attributes_translated CPEE::Message::send(:event,what,base,@id,uuid,info,content,@redis) end |
#start ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/cpee/controller.rb', line 94 def start if vote("state/change", :state => 'running') @thread = @instance.start @thread.join else @thread = @instance.stop @thread.join end end |
#stop ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cpee/controller.rb', line 104 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
61 62 63 |
# File 'lib/cpee/controller.rb', line 61 def uuid @attributes['uuid'] end |
#vote(what, content = {}) ⇒ Object
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 156 157 158 159 |
# File 'lib/cpee/controller.rb', line 125 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[:attributes] = attributes_translated content[:subscription] = client votes << voteid CPEE::Message::send(:vote,what,base,@id,uuid,info,content,@redis) end if votes.length > 0 @votes += votes psredis = @opts[:redis_dyn].call 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 || 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 |