Class: CPEE::Worklist::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/cpee-worklist/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Controller

Returns a new instance of Controller.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/cpee-worklist/controller.rb', line 26

def initialize(opts)
  CPEE::redis_connect(opts,"Main")
  @opts = opts

  @redis = opts[:redis]
  @votes = []

  @activities = Activities.new(opts)
  @activities.unserialize

  CPEE::Persistence::new_static_object(CPEE::Persistence::obj,opts)

  @orgmodels = []
  Dir::glob(File.join(@opts[:top],'orgmodels','*')).each do |g|
    add_orgmodel File.basename(g), File.read(g)
  end

  @callback_keys = {}
  @psredis = @opts[:redis_dyn].call "Callback Response"

  Thread.new do
    @psredis.psubscribe('callback-response:*','callback-end:*') do |on|
      on.pmessage do |pat, what, message|
        if pat == 'callback-response:*' && @callback_keys.has_key?(what[18..-1])
          index = message.index(' ')
          mess = message[index+1..-1]
          instance = message[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
          @callback_keys[what[18..-1]].send(:callback,resp,m['content']['headers'])
        end
        if pat == 'callback-end:*'
          @callback_keys.delete(what[13..-1])
        end
      end
    end
  end
end

Instance Attribute Details

#activitiesObject (readonly)

Returns the value of attribute activities.



24
25
26
# File 'lib/cpee-worklist/controller.rb', line 24

def activities
  @activities
end

#callback_keysObject (readonly)

Returns the value of attribute callback_keys.



24
25
26
# File 'lib/cpee-worklist/controller.rb', line 24

def callback_keys
  @callback_keys
end

#communicationObject (readonly)

Returns the value of attribute communication.



24
25
26
# File 'lib/cpee-worklist/controller.rb', line 24

def communication
  @communication
end

#eventsObject (readonly)

Returns the value of attribute events.



24
25
26
# File 'lib/cpee-worklist/controller.rb', line 24

def events
  @events
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



24
25
26
# File 'lib/cpee-worklist/controller.rb', line 24

def notifications
  @notifications
end

#optsObject (readonly)

Returns the value of attribute opts.



24
25
26
# File 'lib/cpee-worklist/controller.rb', line 24

def opts
  @opts
end

#orgmodelsObject (readonly)

Returns the value of attribute orgmodels.



24
25
26
# File 'lib/cpee-worklist/controller.rb', line 24

def orgmodels
  @orgmodels
end

#votesObject (readonly)

Returns the value of attribute votes.



24
25
26
# File 'lib/cpee-worklist/controller.rb', line 24

def votes
  @votes
end

Instance Method Details

#add_activity(activity) ⇒ Object



152
153
154
155
# File 'lib/cpee-worklist/controller.rb', line 152

def add_activity(activity)
  @activities << activity
  @activities.serialize
end

#add_orgmodel(name, content) ⇒ Object

{{{



98
99
100
101
102
# File 'lib/cpee-worklist/controller.rb', line 98

def add_orgmodel(name,content) #{{{
  FileUtils.mkdir_p(File.join(@opts[:top],'orgmodels'))
  @orgmodels << name unless @orgmodels.include?(name)
  File.write(File.join(@opts[:top],'orgmodels',name), content)
end

#baseObject



90
91
92
# File 'lib/cpee-worklist/controller.rb', line 90

def base
  base_url
end

#base_urlObject



81
82
83
# File 'lib/cpee-worklist/controller.rb', line 81

def base_url
  File.join(@opts[:url],'/')
end

#callback(hw, key, content) ⇒ Object



143
144
145
146
# File 'lib/cpee-worklist/controller.rb', line 143

def callback(hw,key,content)
  CPEE::Message::send(:callback,'activity/content',base,info,uuid,info,content.merge(:key => key),@redis)
  @callback_keys[key] = hw
end

#cancel_callback(key) ⇒ Object



148
149
150
# File 'lib/cpee-worklist/controller.rb', line 148

def cancel_callback(key)
  CPEE::Message::send(:'callback-end',key,base,info,uuid,info,{},@redis)
end

#hostObject



78
79
80
# File 'lib/cpee-worklist/controller.rb', line 78

def host
  @opts[:host]
end

#idObject



72
73
74
# File 'lib/cpee-worklist/controller.rb', line 72

def id
  'worklist'
end

#infoObject



94
95
96
# File 'lib/cpee-worklist/controller.rb', line 94

def info
  'worklist'
end

#instance_idObject



87
88
89
# File 'lib/cpee-worklist/controller.rb', line 87

def instance_id
  @id
end

#instance_urlObject



84
85
86
# File 'lib/cpee-worklist/controller.rb', line 84

def instance_url
  File.join(@opts[:url].to_s,'/')
end

#notify(what, content = {}) ⇒ Object

}}}



104
105
106
# File 'lib/cpee-worklist/controller.rb', line 104

def notify(what,content={})
  CPEE::Message::send(:event,what,base,info,uuid,info,content,@redis)
end

#uuidObject



75
76
77
# File 'lib/cpee-worklist/controller.rb', line 75

def uuid
  @id
end

#vote(what, content = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cpee-worklist/controller.rb', line 108

def vote(what,content={})
  topic, name = what.split('/')
  handler = File.join(topic,'vote',name)
  votes = []
  CPEE::Persistence::extract_handler(id,@opts,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,info,uuid,info,content,@redis)
  end

  if votes.length > 0
    @votes += votes
    psredis = @opts[:redis_dyn].call "Vote"
    collect = []
    psredis.subscribe(votes.map{|e| ['vote-response:' + e.to_s] }.flatten) do |on|
      on.message do |what, message|
        index = message.index(' ')
        mess = message[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