DanaDanger-equity 0.2 → 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/equity CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  #
3
- # equity 0.2 - simple queueing software load balancer
3
+ # equity 0.3 - simple queueing software load balancer
4
4
  #
5
5
  # Usage: equity [-d] <listen-port> [<node-address>:]<node-port> ...
6
6
  #
@@ -26,7 +26,7 @@ end
26
26
  $debugging = !!ARGV.delete('-d')
27
27
 
28
28
  if ARGV.length < 2
29
- STDERR.puts 'equity 0.2'
29
+ STDERR.puts 'equity 0.3'
30
30
  STDERR.puts 'Usage: equity [-d] <listen-port> [<node-address>:]<node-port> ...'
31
31
  STDERR.print "\n"
32
32
  STDERR.puts 'The -d option turns on debug mode. Equity will remain in the foreground and'
data/bin/equityctl ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # equityctl - management client for equity
4
+ #
5
+ # Usage: equityctl [<host>:]<port> <command>
6
+ #
7
+ # The host defaults to localhost.
8
+ #
9
+ # Command Description
10
+ # status Displays each node's address, port, connection status, and
11
+ # connection counter.
12
+ #
13
+ # Equity uses UDP packets to send control information, so all of UDP's caveats
14
+ # apply to equityctl.
15
+ #
16
+
17
+ require 'equity/controller'
18
+
19
+ # Process arguments.
20
+ if ARGV.length != 2
21
+ STDERR.puts 'Usage: equityctl [<host>:]<port> <command>'
22
+ STDERR.print "\n"
23
+ STDERR.puts 'The host defaults to localhost.'
24
+ STDERR.print "\n"
25
+ STDERR.puts 'Command Description'
26
+ STDERR.puts 'status Displays each node\'s address, port, connection status, and'
27
+ STDERR.puts ' connection counter.'
28
+ exit(64)
29
+ end
30
+
31
+ address, port = ARGV.shift.split(':', 2)
32
+ if port.nil?
33
+ port = address
34
+ address = 'localhost'
35
+ end
36
+
37
+ command = ARGV.shift
38
+
39
+ # Send command.
40
+ class NoResponseError < Exception; end
41
+
42
+ controller = Equity::Controller.new(address, port.to_i)
43
+ begin
44
+ case command
45
+ when 'status'
46
+ nodes = controller.node_status || raise(NoResponseError)
47
+ nodes.each do |node|
48
+ puts "#{node} #{node.connected? ? 'C' : '-'} #{node.counter}"
49
+ end
50
+ else
51
+ STDERR.puts "Unrecognized command: #{command}"
52
+ STDERR.puts 'Run equityctl with no arguments for a list of commands.'
53
+ exit(64)
54
+ end
55
+ rescue NoResponseError
56
+ STDERR.puts "No response from #{address}:#{port}"
57
+ exit(1)
58
+ end
@@ -0,0 +1,58 @@
1
+ require 'socket'
2
+ require 'dl/import'
3
+ require 'equity/manager'
4
+ require 'equity/node'
5
+
6
+ module Equity
7
+ class Controller
8
+ def initialize(host, port)
9
+ @host = host.dup
10
+ @port = port.to_i
11
+ @socket = UDPSocket.new
12
+ end
13
+
14
+ def node_status
15
+ reply = send(Manager::CMD_NODE_STATUS)
16
+ return nil unless reply
17
+
18
+ nodes = []
19
+ count = reply.slice!(0, 1)[0]
20
+ count.times do
21
+ length, connected, counter, port = reply.slice!(0, 9).unpack('nCNn')
22
+ address = reply.slice!(0, length - 9)
23
+ node = Node::Static.new(address, port)
24
+ node.connected = (connected != 0)
25
+ node.counter = counter
26
+ nodes << node
27
+ end
28
+ nodes
29
+ end
30
+
31
+ module Alarm
32
+ extend DL::Importable
33
+ if RUBY_PLATFORM =~ /darwin/
34
+ so_ext = 'dylib'
35
+ else
36
+ so_ext = 'so'
37
+ end
38
+ dlload "libc.#{so_ext}"
39
+ extern "unsigned int alarm(unsigned int)"
40
+ end
41
+
42
+ private
43
+
44
+ def send(message)
45
+ @socket.send(message, 0, @host, @port)
46
+ begin
47
+ trap('ALRM') { raise Errno::EINTR }
48
+ Alarm.alarm(15)
49
+ reply, address = @socket.recvfrom(1024)
50
+ rescue Errno::EINTR
51
+ reply = nil
52
+ end
53
+ Alarm.alarm(0)
54
+ reply
55
+ end
56
+
57
+ end
58
+ end
@@ -31,7 +31,7 @@ module Equity
31
31
  message = [@nodes.length].pack('C')
32
32
  @nodes.each do |node|
33
33
  message += [
34
- 5 + node.address.length,
34
+ 9 + node.address.length,
35
35
  (node.connected? ? 1 : 0),
36
36
  node.counter,
37
37
  node.port
data/lib/equity/node.rb CHANGED
@@ -52,5 +52,29 @@ module Equity
52
52
  def to_s
53
53
  "#{@address}:#{@port}"
54
54
  end
55
+
56
+ class Static < Node
57
+ attr_writer :counter
58
+
59
+ def connected=(bool)
60
+ @client = bool
61
+ end
62
+
63
+ def connect(client)
64
+ raise(RuntimeError, 'static nodes cannot be connected')
65
+ end
66
+
67
+ def disconnect
68
+ raise(RuntimeError, 'static nodes cannot be disconnected')
69
+ end
70
+
71
+ def sockets
72
+ []
73
+ end
74
+
75
+ def owns_socket?(socket)
76
+ false
77
+ end
78
+ end
55
79
  end
56
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: DanaDanger-equity
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - DanaDanger
@@ -17,12 +17,15 @@ description:
17
17
  email:
18
18
  executables:
19
19
  - equity
20
+ - equityctl
20
21
  extensions: []
21
22
 
22
23
  extra_rdoc_files: []
23
24
 
24
25
  files:
25
26
  - bin/equity
27
+ - bin/equityctl
28
+ - lib/equity/controller.rb
26
29
  - lib/equity/manager.rb
27
30
  - lib/equity/node.rb
28
31
  - lib/equity/socket_pairing.rb