class EasyWamp::WampServer

Constants

PROTOCOL_VERSION
TYPE_ID_CALL
TYPE_ID_CALLERROR
TYPE_ID_CALLRESULT
TYPE_ID_EVENT
TYPE_ID_PREFIX
TYPE_ID_PUBLISH
TYPE_ID_SUBSCRIBE
TYPE_ID_UNSUBSCRIBE
TYPE_ID_WELCOME

Public Class Methods

add_prefix(prefix, value) click to toggle source
# File lib/easy_wamp/wamp.rb, line 75
def self.add_prefix(prefix, value)
  @@prefix[prefix] = value
end
call_callback(action, *args) click to toggle source
# 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) { |get_client_ws(e)| ... } click to toggle source
# 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) click to toggle source
# File lib/easy_wamp/wamp.rb, line 33
def self.expand(curi)
  @@prefix[curi.split(':')[0]] || curi
end
get_api_call(uri) click to toggle source
# 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) click to toggle source
# File lib/easy_wamp/wamp.rb, line 67
def self.get_client_id(ws)
  @@clients[ws]
end
get_client_ws(id) click to toggle source
# File lib/easy_wamp/wamp.rb, line 71
def self.get_client_ws(id)
  @@clients.key(id)
end
handle_close(ws) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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=[]) click to toggle source
# 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) click to toggle source
# File lib/easy_wamp/wamp.rb, line 25
def self.register_callback(callback, &block)
  @@call_back[callback] = block
end
register_new_client(ws, id) click to toggle source
# File lib/easy_wamp/wamp.rb, line 59
def self.register_new_client(ws, id)
  @@clients[ws] = id
end
remove_client(ws) click to toggle source
# File lib/easy_wamp/wamp.rb, line 63
def self.remove_client(ws)
  @@clients.delete(ws)
end
send_error(ws, id, type, desc, details) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_id() click to toggle source
# File lib/easy_wamp/wamp.rb, line 55
def self.session_id
  SecureRandom.uuid
end
start_service(host, port, api) click to toggle source
# 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) click to toggle source
# 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
thread() click to toggle source
# File lib/easy_wamp/wamp.rb, line 21
def self.thread()
  return @@thread
end
unsubscribe(uri, client) click to toggle source
# 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