Class: Equity::Node

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

Overview

Represents a cluster node.

Direct Known Subclasses

Static

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
20
# 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
  @active          = true
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



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

def address
  @address
end

#client_addressObject (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_portObject (readonly)

Returns the value of attribute client_port.



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

def client_port
  @client_port
end

#counterObject (readonly)

Returns the value of attribute counter.



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

def counter
  @counter
end

#failure_counterObject (readonly)

Returns the value of attribute failure_counter.



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

def failure_counter
  @failure_counter
end

#portObject (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



74
75
76
77
78
79
# File 'lib/equity/node.rb', line 74

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)


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

def connect(client)
  raise(RuntimeError, 'node is already connected') if connected?
  raise(RuntimeError, 'node has been shut down') if shutdown?
  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)


58
59
60
# File 'lib/equity/node.rb', line 58

def connected?
  !!@client
end

#disconnectObject



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

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

#owns_socket?(socket) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/equity/node.rb', line 66

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

#shutdown?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/equity/node.rb', line 85

def shutdown?
  !@active
end

#socketsObject



62
63
64
# File 'lib/equity/node.rb', line 62

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

#stop_accepting!Object



81
82
83
# File 'lib/equity/node.rb', line 81

def stop_accepting!
  @active = false
end

#to_sObject



70
71
72
# File 'lib/equity/node.rb', line 70

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