Class: Smartware::PubSubClient

Inherits:
Object
  • Object
show all
Defined in:
lib/smartware/pub_sub_client.rb

Defined Under Namespace

Classes: Message, PubSubHandler

Constant Summary collapse

@@static_client =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "localhost", port = 6100) ⇒ PubSubClient

Returns a new instance of PubSubClient.



74
75
76
77
78
79
# File 'lib/smartware/pub_sub_client.rb', line 74

def initialize(host = "localhost", port = 6100)
  @host = host
  @port = port
  @reconnect_timer = nil
  @connection = nil
end

Instance Attribute Details

#receiverObject

Returns the value of attribute receiver.



72
73
74
# File 'lib/smartware/pub_sub_client.rb', line 72

def receiver
  @receiver
end

Class Method Details

.create_static_clientObject



62
63
64
# File 'lib/smartware/pub_sub_client.rb', line 62

def self.create_static_client
  @@static_client = PubSubClient.new
end

.destroy_static_clientObject



66
67
68
69
70
# File 'lib/smartware/pub_sub_client.rb', line 66

def self.destroy_static_client
  return if @@static_client.nil?
  @@static_client.stop
  @@static_client = nil
end

Instance Method Details

#brokenObject



90
91
92
93
# File 'lib/smartware/pub_sub_client.rb', line 90

def broken
  @connection = nil
  @reconnect_timer = EventMachine.add_timer 1, &method(:attempt)
end

#receive(data) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/smartware/pub_sub_client.rb', line 95

def receive(data)
  message = Message.new data["reliable_id"],
                        data["key"],
                        data["args"],
                        ->(id) do

    @connection.send_record command: "acknowlege", id: id
  end

  @receiver.call message
end

#startObject



81
82
83
# File 'lib/smartware/pub_sub_client.rb', line 81

def start
  attempt if @connection.nil? && @reconnect_timer.nil?
end

#stopObject



85
86
87
88
# File 'lib/smartware/pub_sub_client.rb', line 85

def stop
  @connection.close_connection unless @connection.nil?
  EventMachine.cancel_timer @reconnect_timer unless @reconnect_timer.nil?
end