Class: IRC::Client::Dispatcher::ConnectionDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/failirc/client/dispatcher/connectiondispatcher.rb

Defined Under Namespace

Classes: Connections, Data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher) ⇒ ConnectionDispatcher

Returns a new instance of ConnectionDispatcher.



166
167
168
169
170
171
172
173
174
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 166

def initialize (dispatcher)
    @client     = dispatcher.client
    @dispatcher = dispatcher

    @connections   = Connections.new(client)
    @input         = Data.new(dispatcher)
    @output        = Data.new(dispatcher)
    @disconnecting = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



164
165
166
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 164

def client
  @client
end

#connectionsObject (readonly)

Returns the value of attribute connections.



164
165
166
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 164

def connections
  @connections
end

#disconnectingObject (readonly)

Returns the value of attribute disconnecting.



164
165
166
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 164

def disconnecting
  @disconnecting
end

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



164
165
166
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 164

def dispatcher
  @dispatcher
end

#inputObject (readonly)

Returns the value of attribute input.



164
165
166
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 164

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



164
165
166
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 164

def output
  @output
end

Instance Method Details

#cleanObject



288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 288

def clean
    @disconnecting.each {|data|
        server = data[:server]
        output = data[:output]

        if output.first == :EOC
            output.shift
            handleDisconnection server, output.shift
            @disconnecting.delete(data)
        end
    }
end

#connect(options, config, name = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 184

def connect (options, config, name=nil)
    socket  = nil
    context = nil

    begin
        socket = TCPSocket.new(options[:host], options[:port])
    rescue Errno::ECONNREFUSED
        self.debug "Could not connect to #{options[:host]}/#{options[:port]}."
        return
    end

    if options[:ssl] != 'disabled'
        context = SSLUtils::context(options[:ssl_cert], options[:ssl_key])
    end

    host = socket.peeraddr[2]
    ip   = socket.peeraddr[3]
    port = socket.peeraddr[1]

    self.debug "Connecting to #{host}[#{ip}/#{port}]"

    begin
        if config.attributes['ssl'] != 'disabled'
            ssl = OpenSSL::SSL::SSLSocket.new socket, context

            ssl.connect
            socket = ssl
        end

        @connections.servers[:bySocket][socket] = Server.new(client, socket, config, name)
        @connections.servers[:byName][server(socket).name] = server socket
        @connections.sockets.push(socket)

        if config.attributes['password']
            server(socket).password = config.attributes['password']
        end

        @input[socket]
        
        dispatcher.execute :connect, @connections.servers[:bySocket][socket]
    rescue OpenSSL::SSL::SSLError
        self.debug "Tried to connect to #{host}[#{ip}/#{port}] with SSL but the handshake failed."
        socket.close rescue nil
    rescue Errno::ECONNRESET
        socket.close rescue nil
        self.debug "#{host}[#{ip}/#{port}] connection reset."
    rescue Exception => e
        socket.close rescue nil
        self.debug e
    end
end

#disconnect(server, message) ⇒ Object



283
284
285
286
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 283

def disconnect (server, message)
    @output.push server, :EOC
    @output.push server, message
end

#finalizeObject



385
386
387
388
389
390
391
392
393
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 385

def finalize
    begin
        @connections.sockets.each {|socket|
            disconnect self.server(socket), disconnecting
        }
    rescue Exception => e
        self.debug e
    end
end

#handleObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 301

def handle
    @input.each {|socket|
        if dispatcher.event.handling[socket] || @input.empty?(socket)
            next
        end

        Thread.new {
            begin
                if string = @input.pop(socket)
                    dispatcher.dispatch(:input, server(socket), string)
                end
            rescue Exception => e
                self.debug e
            end
        }
    }
end

#handleDisconnection(server, message) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 373

def handleDisconnection (server, message)
    @dispatcher.execute(:disconnect, server, message) rescue nil

    @input.delete(server.socket)
    @output.delete(server.socket)
    connections.delete(server.socket)

    self.debug "Disconnected from #{server}[#{server.ip}/#{server.port}]"

    server.socket.close rescue nil
end

#read(timeout = 0.1) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 236

def read (timeout=0.1)
    begin
        reading, = IO::select @connections.sockets, nil, nil, timeout
    rescue IOError
        @connections.sockets.each {|socket|
            if socket.closed?
                kill server socket
            end
        }
    rescue Exception => e
        self.debug e
    end

    if !reading
        return
    end

    reading.each {|socket|
        server = self.server socket

        begin
            input = socket.read_nonblock 2048

            if !input || input.empty?
                raise Errno::EPIPE
            end

            input.split(/[\r\n]+/).each {|string|
                @input.push(socket, string)
            }
        rescue IOError
            disconnect server, 'Input/output error'
        rescue Errno::EBADF, Errno::EPIPE, OpenSSL::SSL::SSLError
            disconnect server, 'Client exited'
        rescue Errno::ECONNRESET
            disconnect server, 'Connection reset by peer'
        rescue Errno::ETIMEDOUT
            disconnect server, 'Ping timeout'
        rescue Errno::EHOSTUNREACH
            disconnect server, 'No route to host'
        rescue Errno::EAGAIN, IO::WaitReadable
        rescue Exception => e
            self.debug e
        end
    }
end

#server(identifier) ⇒ Object



395
396
397
398
399
400
401
402
403
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 395

def server (identifier)
    if identifier.is_a?(Server)
        return identifier
    elsif identifier.is_a?(String)
        return @connections.servers[:byName][identifier]
    else
        return @connections.servers[:bySocket][identifier]
    end
end

#serversObject



180
181
182
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 180

def servers
    @connections.servers
end

#socketsObject



176
177
178
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 176

def sockets
    @connections.sockets
end

#write(timeout = 0) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/failirc/client/dispatcher/connectiondispatcher.rb', line 319

def write (timeout=0)
    begin
        none, writing = IO::select nil, @connections.sockets, nil, timeout
    rescue IOError
        @connections.sockets.each {|socket|
            if socket.closed?
                kill server socket
            end
        }
    rescue Exception => e
        self.debug e
    end

    if !writing
        return
    end

    writing.each {|socket|
        if @output.empty?(socket)
            next
        end

        server = self.server socket

        begin
            while !@output.empty?(socket)
                output = @output.first(socket)

                if output == :EOC
                    @output.delete(socket)
                else
                    output.force_encoding 'ASCII-8BIT'
                    socket.write_nonblock "#{output}\r\n"

                    @output.pop(socket)
                end
            end
        rescue IOError
            disconnect server, 'Input/output error'
        rescue Errno::EBADF, Errno::EPIPE, OpenSSL::SSL::SSLError
            disconnect server, 'Client exited'
        rescue Errno::ECONNRESET
            disconnect server, 'Connection reset by peer'
        rescue Errno::ETIMEDOUT
            disconnect server, 'Ping timeout'
        rescue Errno::EHOSTUNREACH
            disconnect server, 'No route to host'
        rescue Errno::EAGAIN, IO::WaitWritable
        rescue Exception => e
            self.debug e
        end
    }
end