Class: Equity::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/equity/node.rb

Overview

Represents a cluster node.

Defined Under Namespace

Classes: Static

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port) ⇒ Node

Returns a new instance of Node.



13
14
15
16
17
18
19
# File 'lib/equity/node.rb', line 13

def initialize(address, port)
  @client          = nil
  @counter         = 0
  @failure_counter = 0
  @address         = address.dup
  @port            = port.to_i
end

Instance Attribute Details

#address ⇒ Object (readonly)

Returns the value of attribute address.



8
9
10
# File 'lib/equity/node.rb', line 8

def address
  @address
end

#client_address ⇒ Object (readonly)

Returns the value of attribute client_address.



10
11
12
# File 'lib/equity/node.rb', line 10

def client_address
  @client_address
end

#client_port ⇒ Object (readonly)

Returns the value of attribute client_port.



11
12
13
# File 'lib/equity/node.rb', line 11

def client_port
  @client_port
end

#counter ⇒ Object (readonly)

Returns the value of attribute counter.



6
7
8
# File 'lib/equity/node.rb', line 6

def counter
  @counter
end

#failure_counter ⇒ Object (readonly)

Returns the value of attribute failure_counter.



7
8
9
# File 'lib/equity/node.rb', line 7

def failure_counter
  @failure_counter
end

#port ⇒ Object (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/equity/node.rb', line 9

def port
  @port
end

Class Method Details

.with_socket(socket) ⇒ Object



72
73
74
75
76
77
# File 'lib/equity/node.rb', line 72

def self.with_socket(socket)
  ObjectSpace.each_object(self) do |node|
    return node if node.owns_socket?(socket)
  end
  nil
end

Instance Method Details

#connect(client) ⇒ Object

Raises:

  • (RuntimeError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/equity/node.rb', line 21

def connect(client)
  raise(RuntimeError, 'node is already connected') if connected?
  begin
    @client = client
    @server = TCPSocket.new(@address, @port)
    @server.extend SocketPairing
    @server.pair_with(client)
    @counter += 1
    
    begin
      nameinfo = Socket.getnameinfo(@client.getpeername,
        Socket::NI_NUMERICHOST | Socket::NI_NUMERICSERV)
      @client_address, @client_port = nameinfo
    rescue
      @client_address, @client_port = "???", "???"
    end
    
    @server
  rescue Exception => e
    @client = nil
    @server = nil
    @failure_counter += 1
    raise e
  end
end

#connected? ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/equity/node.rb', line 56

def connected?
  !!@client
end

#disconnect ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/equity/node.rb', line 47

def disconnect
  @client.close
  @server.close
  @client = nil
  @server = nil
  @client_address = nil
  @client_port = nil
end

#owns_socket?(socket) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/equity/node.rb', line 64

def owns_socket?(socket)
  connected? && ((socket == @client) || (socket == @server))
end

#sockets ⇒ Object



60
61
62
# File 'lib/equity/node.rb', line 60

def sockets
  connected? ? [@client, @server] : []
end

#to_s ⇒ Object



68
69
70
# File 'lib/equity/node.rb', line 68

def to_s
  "#{@address}:#{@port}"
end