Class: Rubarb::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/rubarb/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, api, insecure_methods = Default::INSECURE_METHODS) ⇒ Connection

Returns a new instance of Connection.



113
114
115
116
117
118
119
120
# File 'lib/rubarb/connection.rb', line 113

def initialize(host, port, api, insecure_methods=Default::INSECURE_METHODS)
  @host = host
  @port = port
  @api = api
  @msg_id_generator = Id.new
  @errbacks = []
  @insecure_methods = insecure_methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/rubarb/connection.rb', line 147

def method_missing(method, * args, & block)
  EventMachine::schedule do
    begin
      @remote_connection.remote_call(method, args, & block)
    rescue Exception => e
      @remote_connection.call_errbacks(e)
    end
  end
end

Instance Attribute Details

#msg_id_generatorObject (readonly)

Returns the value of attribute msg_id_generator.



111
112
113
# File 'lib/rubarb/connection.rb', line 111

def msg_id_generator
  @msg_id_generator
end

#remote_connectionObject (readonly)

Returns the value of attribute remote_connection.



110
111
112
# File 'lib/rubarb/connection.rb', line 110

def remote_connection
  @remote_connection
end

Instance Method Details

#errback(&block) ⇒ Object



122
123
124
# File 'lib/rubarb/connection.rb', line 122

def errback & block
  @errbacks << block if block
end

#start(&block) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rubarb/connection.rb', line 126

def start & block
  EventMachine::schedule do
    begin
      EventMachine::connect(@host, @port, OutgoingHandler) do |connection|
        connection.host = @host
        connection.port = @port
        connection.on_connection = block
        connection.api = @api
        connection.errbacks = @errbacks
        connection.msg_id_generator = @msg_id_generator
        connection.insecure_methods = @insecure_methods
        @remote_connection = connection
      end
    rescue Exception => e
      @errbacks.each do |errback|
        errback.call(e)
      end
    end
  end
end

#stop(&callback) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rubarb/connection.rb', line 157

def stop(& callback)
  EventMachine::schedule do
    if @remote_connection
      EventMachine::next_tick do
        @remote_connection.close_connection
        callback.call(true) if callback
      end
    else
      callback.call(false) if callback
    end
  end
end