Class: Ionian::ManagedSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/ionian/managed_socket.rb

Overview

A socket manager that performs functions like heartbeating and auto-reconnect.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ ManagedSocket

Returns a new instance of ManagedSocket.

Parameters:

  • kwargs (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • :auto_reconnect (Boolean) — default: false

    Automatically reconnect if the socket closes. Must call #close to break the auto-reconnect loop.

Raises:

  • (NotImplementedError)

See Also:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ionian/managed_socket.rb', line 18

def initialize **kwargs
  @auto_reconnect = kwargs.delete(:auto_reconnect) || false
  raise NotImplementedError, ':auto_reconnect must be set true.' unless @auto_reconnect
  @kwargs = kwargs
  
  @match_handlers  = []
  @status_handlers = []
  
  @write_queue = Queue.new
  @write_pipe_rx, @write_pipe_tx = IO.pipe
  @write_pipe_rx.extend Ionian::Extension::IO
end

Instance Attribute Details

#auto_reconnectObject (readonly)

When true, automatically reconnect if the socket closes.



11
12
13
# File 'lib/ionian/managed_socket.rb', line 11

def auto_reconnect
  @auto_reconnect
end

Instance Method Details

#closeObject

Close the socket. Disables :auto_reconnect.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ionian/managed_socket.rb', line 33

def close
  unless @closed == true
    @auto_reconnect = false
    @socket.close if @socket and not @socket.closed?
    @write_pipe_tx.close rescue IOError
    @write_pipe_rx.close rescue IOError
    @write_queue = nil
    
    @closed = true
  end
end

#register_match_handler {|MatchData, self| ... } ⇒ Block Also known as: on_match

Register a block to be called when Extension::IO#run_match receives matched data. Method callbacks can be registered with &object.method(:method).

Yields:

  • (MatchData, self)

Returns:

  • (Block)

    the given block.



84
85
86
87
88
# File 'lib/ionian/managed_socket.rb', line 84

def register_match_handler &block
  @match_handlers << block unless @match_handlers.include? block
  @socket.register_match_handler &block if @socket
  block
end

#register_status_handler {|status, self| ... } ⇒ Block Also known as: on_status_change

Register a block to be called when there is a change in socket status. Method callbacks can be registered with &object.method(:method).

Yields:

  • (status, self)

Returns:

  • (Block)

    a reference to the given block.

Raises:

  • (NotImplementedError)


103
104
105
# File 'lib/ionian/managed_socket.rb', line 103

def register_status_handler &block
  raise NotImplementedError
end

#runObject

Start the event loop. Should be called after the handlers are registered.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ionian/managed_socket.rb', line 47

def run
  @run_thread ||= Thread.new do
    while not @write_pipe_rx.closed?
      begin
        create_socket if (not @socket or @socket.closed?) and not @write_pipe_rx.closed?
        io = ::IO.select([@write_pipe_rx, @socket.fd], nil, nil, nil).first.first
        
        case io
        
        when @write_pipe_rx
          @write_pipe_rx.read_all
          while @socket and not @write_queue.empty?
            @socket.write @write_queue.shift
          end
          
        when @socket.fd
          @socket.read_match
          
        end
      rescue IOError # Far-end socket closed.
        @socket.close if @socket and not @socket.closed?
      end
    end
  end
end

#unregister_match_handler(&block) ⇒ Object

Unregister a block from being called when matched data is received.



93
94
95
96
97
# File 'lib/ionian/managed_socket.rb', line 93

def unregister_match_handler &block
  @match_handlers.delete_if { |o| o == block }
  @socket.unregister_match_handler &block if @socket
  block
end

#unregister_status_handler(&block) ⇒ Object

Unregister a block from being called when there is a change in socket status.

Raises:

  • (NotImplementedError)


110
111
112
# File 'lib/ionian/managed_socket.rb', line 110

def unregister_status_handler &block
  raise NotImplementedError
end

#write(data) ⇒ Object

Write data to the socket.



74
75
76
77
# File 'lib/ionian/managed_socket.rb', line 74

def write data
  @write_queue << data
  @write_pipe_tx.write "\n"
end