Class: Smartware::PubSubServer::PubSubConnection

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::Protocols::LineProtocol
Defined in:
lib/smartware/pub_sub_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ PubSubConnection

Returns a new instance of PubSubConnection.



8
9
10
# File 'lib/smartware/pub_sub_server.rb', line 8

def initialize(server)
  @server = server
end

Instance Method Details

#post_initObject



12
13
14
# File 'lib/smartware/pub_sub_server.rb', line 12

def post_init
  @server.add_connection self
end

#publish_event(key, *args) ⇒ Object



37
38
39
# File 'lib/smartware/pub_sub_server.rb', line 37

def publish_event(key, *args)
  send_data(JSON.dump({ key: key, args: args }) + "\r\n")
end

#publish_reliable_event(id, key, *args) ⇒ Object



41
42
43
# File 'lib/smartware/pub_sub_server.rb', line 41

def publish_reliable_event(id, key, *args)
  send_data(JSON.dump({ reliable_id: id, key: key, args: args }) + "\r\n")
end

#receive_line(line) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/smartware/pub_sub_server.rb', line 20

def receive_line(line)
  begin
    data = JSON.load line

    case data["command"]
    when "acknowlege"
      @server.acknowlege_reliable data["id"]

    else
      raise "unsupported command #{data["command"]}"
    end
  rescue => e
    Logging.logger.warn e.to_s
    e.backtrace.each { |line| Logging.logger.warn line }
  end
end

#unbindObject



16
17
18
# File 'lib/smartware/pub_sub_server.rb', line 16

def unbind
  @server.remove_connection self
end