Class: EasyWamp::WampServer

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_wamp/wamp.rb

Constant Summary collapse

PROTOCOL_VERSION =
1
TYPE_ID_WELCOME =
0
TYPE_ID_PREFIX =
1
TYPE_ID_CALL =
2
TYPE_ID_CALLRESULT =
3
TYPE_ID_CALLERROR =
4
TYPE_ID_SUBSCRIBE =
5
TYPE_ID_UNSUBSCRIBE =
6
TYPE_ID_PUBLISH =
7
TYPE_ID_EVENT =
8
@@prefix =
{}
@@events =
{}
@@clients =
{}
@@thread =
nil
@@call_back =
{}

Class Method Summary collapse

Class Method Details

.add_prefix(prefix, value) ⇒ Object



75
76
77
# File 'lib/easy_wamp/wamp.rb', line 75

def self.add_prefix(prefix, value)
  @@prefix[prefix] = value
end

.call_callback(action, *args) ⇒ Object



29
30
31
# File 'lib/easy_wamp/wamp.rb', line 29

def self.call_callback(action, *args)
  @@call_back[action].call(*args) if @@call_back[action]
end

.each_registered(uri, bad, good) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/easy_wamp/wamp.rb', line 79

def self.each_registered(uri, bad, good)
  @@events[uri].each do |e|
    yield(get_client_ws(e)) if get_client_ws(e) && 
                               !bad.include?(e) && 
                               (good == nil || good.empty? || good.include?(e))
  end if(@@events[uri])
end

.expand(curi) ⇒ Object



33
34
35
# File 'lib/easy_wamp/wamp.rb', line 33

def self.expand(curi)
  @@prefix[curi.split(':')[0]] || curi
end

.get_api_call(uri) ⇒ Object



37
38
39
40
# File 'lib/easy_wamp/wamp.rb', line 37

def self.get_api_call(uri)
  uri = expand(uri)
  uri.include?('#') ? uri.split('#')[1] : uri
end

.get_client_id(ws) ⇒ Object



67
68
69
# File 'lib/easy_wamp/wamp.rb', line 67

def self.get_client_id(ws)
  @@clients[ws]
end

.get_client_ws(id) ⇒ Object



71
72
73
# File 'lib/easy_wamp/wamp.rb', line 71

def self.get_client_ws(id)
  @@clients.key(id)
end

.handle_close(ws) ⇒ Object



128
129
130
131
# File 'lib/easy_wamp/wamp.rb', line 128

def self.handle_close(ws)
  remove_client(ws)
  call_callback(:on_close)
end

.handle_msg(ws, msg) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/easy_wamp/wamp.rb', line 133

def self.handle_msg(ws, msg)
  client_id = get_client_id(ws)
  msg = JSON.parse(msg)
  case msg[0]
    when TYPE_ID_PREFIX
      add_prefix(msg[1], msg[2])
    when TYPE_ID_CALL
      begin
        send_result(ws, msg[1], @@api.send(get_api_call(msg[2]), *msg[3..-1]))
      rescue Exception => e
        send_error(ws, msg[1], e.class.name, e.to_s, e.backtrace.join("\n"))
      end
    when TYPE_ID_SUBSCRIBE
      subscribe(msg[1], client_id)
    when TYPE_ID_UNSUBSCRIBE
      unsubscribe(msg[1], client_id)
    when TYPE_ID_PUBLISH
      publish_event(*msg[1..-1])
  end
end

.handle_open(ws) ⇒ Object



121
122
123
124
125
126
# File 'lib/easy_wamp/wamp.rb', line 121

def self.handle_open(ws)
  id = session_id()
  register_new_client(ws, id)
  send_welcome(ws, id)
  call_callback(:on_open)
end

.publish_event(uri, event, ex_list = [], inc_list = []) ⇒ Object



114
115
116
117
118
119
# File 'lib/easy_wamp/wamp.rb', line 114

def self.publish_event(uri, event, ex_list=[], inc_list=[])
  uri = expand(uri)
  each_registered(uri, ex_list, inc_list) do |ws|
    send_event(ws, uri, event)
  end
end

.register_callback(callback, &block) ⇒ Object



25
26
27
# File 'lib/easy_wamp/wamp.rb', line 25

def self.register_callback(callback, &block)
  @@call_back[callback] = block
end

.register_new_client(ws, id) ⇒ Object



59
60
61
# File 'lib/easy_wamp/wamp.rb', line 59

def self.register_new_client(ws, id)
  @@clients[ws] = id
end

.remove_client(ws) ⇒ Object



63
64
65
# File 'lib/easy_wamp/wamp.rb', line 63

def self.remove_client(ws)
  @@clients.delete(ws)
end

.send_error(ws, id, type, desc, details) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/easy_wamp/wamp.rb', line 98

def self.send_error(ws, id, type, desc, details)
  send_msg(ws, [TYPE_ID_CALLERROR,
                id,
                "#{@host}/error##{type}",
                desc,
                details])
end

.send_event(ws, uri, event) ⇒ Object



110
111
112
# File 'lib/easy_wamp/wamp.rb', line 110

def self.send_event(ws, uri, event)
  send_msg(ws, [TYPE_ID_EVENT, uri, event])
end

.send_msg(ws, msg) ⇒ Object



87
88
89
# File 'lib/easy_wamp/wamp.rb', line 87

def self.send_msg(ws, msg)
  ws.send(msg.to_json)
end

.send_result(ws, id, result) ⇒ Object



106
107
108
# File 'lib/easy_wamp/wamp.rb', line 106

def self.send_result(ws, id, result)
  send_msg(ws, [TYPE_ID_CALLRESULT, id, result])
end

.send_welcome(ws, id) ⇒ Object



91
92
93
94
95
96
# File 'lib/easy_wamp/wamp.rb', line 91

def self.send_welcome(ws, id)
  send_msg(ws, [TYPE_ID_WELCOME,
                id,
                PROTOCOL_VERSION,
                "EasyWampServer/#{EasyWamp::VERSION}"])
end

.session_idObject



55
56
57
# File 'lib/easy_wamp/wamp.rb', line 55

def self.session_id
  SecureRandom.uuid
end

.start_service(host, port, api) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/easy_wamp/wamp.rb', line 154

def self.start_service(host, port, api)
  @@api = api
  @@host = host
  @@port = port
  
  @@thread = Thread.new do
    EM.run do
      WebSocket::EventMachine::Server.start(:host => host, :port => port) do |ws|         
        ws.onopen do
          handle_open(ws)
        end
        ws.onmessage do |msg, type|
          begin
            handle_msg(ws, msg)
          rescue Exception => e
            puts "Error Handling Message: #{e.to_s}"
          end
        end
        ws.onclose do
          handle_close(ws)
        end
      end
    end
  end
end

.subscribe(uri, client) ⇒ Object



42
43
44
45
46
47
# File 'lib/easy_wamp/wamp.rb', line 42

def self.subscribe(uri, client)
  uri = expand(uri)
  @@events[uri] ||= Set.new
  @@events[uri].add(client)
  call_callback(:on_subscribe, uri)
end

.threadObject



21
22
23
# File 'lib/easy_wamp/wamp.rb', line 21

def self.thread()
  return @@thread
end

.unsubscribe(uri, client) ⇒ Object



49
50
51
52
53
# File 'lib/easy_wamp/wamp.rb', line 49

def self.unsubscribe(uri, client)
  uri = expand(uri)
  @@events[uri] ||= Set.new
  @@events[uri].delete(client)
end