Module: Isomorfeus::Transport::ServerProcessor

Included in:
ServerSocketProcessor
Defined in:
lib/isomorfeus/transport/server_processor.rb

Instance Method Summary collapse

Instance Method Details

#process_request(request, handler_instance_cache, response_agent_array) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/isomorfeus/transport/server_processor.rb', line 6

def process_request(request, handler_instance_cache, response_agent_array)
  if request.key?('request') && request['request'].key?('agent_ids')
    request['request']['agent_ids'].each_key do |agent_id|
      request['request']['agent_ids'][agent_id].each_key do |handler_class_name|
        response_agent = Isomorfeus::Transport::ResponseAgent.new(agent_id, request['request']['agent_ids'][agent_id][handler_class_name])
        response_agent_array << response_agent
        begin
          handler = if handler_instance_cache.key?(handler_class_name)
                      handler_instance_cache[handler_class_name]
                    else
                      handler_class = Isomorfeus.cached_handler_class(handler_class_name) if Isomorfeus.valid_handler_class_name?(handler_class_name)
                      handler_instance_cache[handler_class_name] = handler_class.new if handler_class
                    end
          if handler
            handler.process_request(response_agent)
          else
            response_agent.error = { error: { handler_class_name => 'No such handler!'}}
          end
        rescue Exception => e
          response_agent.error = { response: { error: "#{handler_class_name}: #{e.message}\n#{e.backtrace.join("\n")}" }}
        end
      end
    end
  elsif request.key?('notification')
    begin
      channel = request['notification']['channel']
      channel_class_name = request['notification']['class']
      if Isomorfeus.valid_channel_class_name?(channel_class_name) && channel
        channel_class = Isomorfeus.cached_channel_class(channel_class_name)
        if channel_class && channel_class.valid_channel?(channel)
          if Isomorfeus.current_user.authorized?(channel_class_name, :send_message, channel)
            allow_publish = if channel_class.server_is_processing_messages?(channel)
                              channel_class.server_process_message(request['notification']['message'], channel)
                            else
                              true
                            end
            if allow_publish == true
              Isomorfeus.pub_sub_client.publish("#{channel_class_name}_#{channel}", Oj.dump({ 'notification' => request['notification'] }, mode: :strict))
            else
              response_agent = OpenStruct.new
              response_agent.result = { notification: request['notification'].merge(error: 'Message cancelled!') }
              response_agent_array << response_agent
            end
          else
            response_agent = OpenStruct.new
            response_agent.result = { notification: request['notification'].merge(error: 'Not authorized!') }
            response_agent_array << response_agent
          end
        else
          response_agent = OpenStruct.new
          response_agent.result = { notification: request['notification'].merge(error: "Not a valid channel #{channel} for #{channel_class_name}!") }
          response_agent_array << response_agent
        end
      else
        response_agent = OpenStruct.new
        response_agent.result = { notification: request['notification'].merge(error: "Not a valid Channel class #{channel_class_name}!") }
        response_agent_array << response_agent
      end
    rescue Exception => e
      response_agent = OpenStruct.new
      response_agent.result = { notification: request['notification'].merge(error: "Isomorfeus::Transport::ServerProcessor: #{e.message}\n#{e.backtrace.join("\n")}") }
      response_agent_array << response_agent
    end
  elsif request.key?('subscribe') && request['subscribe'].key?('agent_ids')
    begin
      agent_id = request['subscribe']['agent_ids'].keys.first
      response_agent = Isomorfeus::Transport::ResponseAgent.new(agent_id, request['subscribe']['agent_ids'][agent_id])
      response_agent_array << response_agent
      channel = response_agent.request['channel']
      channel_class_name = response_agent.request['class']
      if Isomorfeus.valid_channel_class_name?(channel_class_name) && channel
        channel_class = Isomorfeus.cached_channel_class(channel_class_name)
        if channel_class && channel_class.valid_channel?(channel)
          if Isomorfeus.current_user.authorized?(channel_class, :subscribe, channel)
            Isomorfeus.pub_sub_client.subscribe("#{channel_class_name}_#{channel}")
            response_agent.agent_result = { success: channel }
          else
            response_agent.error = { error: "Not authorized!"}
          end
        else
          response_agent = OpenStruct.new
          response_agent.result = { response: { error: "Not a valid channel #{channel} for #{channel_class_name}!" }}
          response_agent_array << response_agent
        end
      else
        response_agent.error = { error: "Not a valid Channel class #{channel_class_name}!" }
      end
    rescue Exception => e
      response_agent.error = { error: "Isomorfeus::Transport::ServerProcessor: #{e.message}\n#{e.backtrace.join("\n")}" }
    end
  elsif request.key?('unsubscribe') && request['unsubscribe'].key?('agent_ids')
    begin
      agent_id = request['unsubscribe']['agent_ids'].keys.first
      response_agent = Isomorfeus::Transport::ResponseAgent.new(agent_id, request['unsubscribe']['agent_ids'][agent_id])
      response_agent_array << response_agent
      channel = response_agent.request['channel']
      channel_class_name = response_agent.request['class']
      if Isomorfeus.valid_channel_class_name?(channel_class_name) && channel
        channel_class = Isomorfeus.cached_channel_class(channel_class_name)
        if channel_class && channel_class.valid_channel?(channel)
          if Isomorfeus.current_user.authorized?(channel_class, :unsubscribe, channel)
            Isomorfeus.pub_sub_client.unsubscribe("#{channel_class_name}_#{channel}")
            response_agent.agent_result = { success: channel }
          else
            response_agent.error = { error: "Not authorized!"}
          end
        else
          response_agent = OpenStruct.new
          response_agent.result = { response: { error: "Not a valid channel #{channel} for #{channel_class_name}!" }}
          response_agent_array << response_agent
        end
      else
        response_agent.error = { error: "Not a valid Channel class #{channel_class_name}!" }
      end
    rescue Exception => e
      response_agent.error = { error: "Isomorfeus::Transport::ServerProcessor: #{e.message}\n#{e.backtrace.join("\n")}" }
    end
  else
    response_agent.error = { error: "No such thing!" }
  end
end