fraggle-block 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,3 +24,9 @@ A synchronous Ruby client for [Doozer](https://github.com/ha/doozer).
24
24
  => nil
25
25
 
26
26
  See [examples](https://github.com/dylanegan/fraggle-block/tree/master/examples) for more.
27
+
28
+
29
+ # Generate ProtoBuf code
30
+
31
+ BEEFCAKE_NAMESPACE=Fraggle::Block protoc --beefcake_out lib/fraggle/block/ -I . msg.proto
32
+
@@ -20,7 +20,6 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  s.add_dependency "beefcake", "~>0.3"
23
- s.add_dependency "system_timer", "1.0"
24
23
 
25
24
  s.add_development_dependency "turn"
26
25
  end
data/lib/fraggle/block.rb CHANGED
@@ -3,7 +3,7 @@ require 'fraggle/block/response'
3
3
 
4
4
  module Fraggle
5
5
  module Block
6
- DEFAULT_URI = "doozerd:?" + [
6
+ DEFAULT_URI = "doozer:?" + [
7
7
  "ca=127.0.0.1:8046",
8
8
  "ca=127.0.0.1:8041",
9
9
  "ca=127.0.0.1:8042",
@@ -24,7 +24,7 @@ module Fraggle
24
24
 
25
25
  module URI
26
26
  def self.parse(u)
27
- if u =~ /^doozerd:\?(.*)$/
27
+ if u =~ /^doozer:\?(.*)$/
28
28
  parts = $1.split("&")
29
29
  parts.inject([]) do |m, pt|
30
30
  k, v = pt.split("=")
@@ -12,6 +12,9 @@ module Fraggle
12
12
 
13
13
  def initialize(addrs = [])
14
14
  @addrs = addrs
15
+ if not @addrs or @addrs.length == 0
16
+ raise "No doozer servers to connect to"
17
+ end
15
18
  connect
16
19
  end
17
20
 
@@ -40,9 +43,22 @@ module Fraggle
40
43
  send(request).first
41
44
  end
42
45
 
43
- def walk(path, rev = nil)
44
- request = Request.new(:path => path, :rev => rev, :verb => WALK)
45
- send(request)
46
+ def walk(path, rev = nil, offset = 0)
47
+ all_responses = []
48
+ done = false
49
+ while not done
50
+ request = Request.new(:path => path, :rev => rev, :verb => WALK, :offset => offset)
51
+ responses = send(request)
52
+ responses.each do |response|
53
+ if response.err_code == Response::Err::RANGE
54
+ done = true
55
+ break
56
+ end
57
+ all_responses.push response
58
+ offset += 1
59
+ end
60
+ end
61
+ return all_responses
46
62
  end
47
63
 
48
64
  def disconnect
@@ -57,7 +73,7 @@ module Fraggle
57
73
  def connect
58
74
  begin
59
75
  host, port = @addrs.shift.split(':')
60
- @connection = connection_to(host, port)
76
+ @connection = connection_to(host, port.to_i)
61
77
  find_all_of_the_nodes
62
78
  rescue => e
63
79
  retry if @addrs.any?
@@ -70,7 +86,8 @@ module Fraggle
70
86
  end
71
87
 
72
88
  def find_all_of_the_nodes
73
- walk('/ctl/node/*/addr').each do |node|
89
+ response = rev()
90
+ walk('/ctl/node/*/addr', response.rev).each do |node|
74
91
  @addrs << node.value unless @addrs.include? node.value
75
92
  end
76
93
  end
@@ -1,6 +1,5 @@
1
1
  require 'fraggle/block/msg.pb'
2
2
  require 'socket'
3
- require "system_timer"
4
3
 
5
4
  module Fraggle
6
5
  module Block
@@ -18,11 +17,24 @@ module Fraggle
18
17
  end
19
18
 
20
19
  def connect
21
- SystemTimer.timeout_after(10) do
22
- s = TCPSocket.new(@host, @port)
23
- s.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
24
- s
20
+ # http://stackoverflow.com/questions/231647/how-do-i-set-the-socket-timeout-in-ruby
21
+ timeout = 10
22
+ addr = Socket.getaddrinfo(@host, nil)
23
+ sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
24
+
25
+ begin
26
+ sock.connect_nonblock(Socket.pack_sockaddr_in(@port, addr[0][3]))
27
+ rescue Errno::EINPROGRESS
28
+ resp = IO.select(nil, [sock], nil, timeout.to_i)
29
+ if resp.nil?
30
+ raise Errno::ECONNREFUSED
31
+ end
32
+ begin
33
+ sock.connect_nonblock(Socket.pack_sockaddr_in(@port, addr[0][3]))
34
+ rescue Errno::EISCONN
25
35
  end
36
+ end
37
+ sock
26
38
  end
27
39
 
28
40
  def disconnect
@@ -38,14 +50,11 @@ module Fraggle
38
50
 
39
51
  def read
40
52
  responses = []
41
- loop do
42
- head = @sock.read(4)
43
- length = head.unpack("N")[0]
44
- data = @sock.read(length)
45
- response = Response.decode(data)
46
- responses << response if response.valid?
47
- break if response.done?
48
- end
53
+ head = @sock.read(4)
54
+ length = head.unpack("N")[0]
55
+ data = @sock.read(length)
56
+ response = Response.decode(data)
57
+ responses << response if response.valid?
49
58
  responses
50
59
  end
51
60
  end
@@ -1,4 +1,4 @@
1
- ## Generated from msg.proto for proto
1
+ ## Generated from msg.proto for server
2
2
  require "beefcake"
3
3
 
4
4
  module Fraggle
@@ -11,23 +11,21 @@ module Fraggle
11
11
  GET = 1
12
12
  SET = 2
13
13
  DEL = 3
14
- ESET = 4
15
14
  REV = 5
15
+ WAIT = 6
16
16
  NOP = 7
17
- WATCH = 8
18
17
  WALK = 9
19
- CANCEL = 10
20
18
  GETDIR = 14
21
19
  STAT = 16
20
+ ACCESS = 99
22
21
  end
23
22
 
24
- required :tag, :int32, 1
25
- required :verb, Request::Verb, 2
23
+ optional :tag, :int32, 1
24
+ optional :verb, Request::Verb, 2
26
25
  optional :path, :string, 4
27
26
  optional :value, :bytes, 5
28
27
  optional :other_tag, :int32, 6
29
28
  optional :offset, :int32, 7
30
- optional :limit, :int32, 8
31
29
  optional :rev, :int64, 9
32
30
 
33
31
  end
@@ -39,18 +37,19 @@ module Fraggle
39
37
  OTHER = 127
40
38
  TAG_IN_USE = 1
41
39
  UNKNOWN_VERB = 2
42
- REDIRECT = 3
40
+ READONLY = 3
43
41
  TOO_LATE = 4
44
42
  REV_MISMATCH = 5
45
43
  BAD_PATH = 6
46
44
  MISSING_ARG = 7
45
+ RANGE = 8
47
46
  NOTDIR = 20
48
47
  ISDIR = 21
49
48
  NOENT = 22
50
49
  end
51
50
 
52
- required :tag, :int32, 1
53
- required :flags, :int32, 2
51
+ optional :tag, :int32, 1
52
+ optional :flags, :int32, 2
54
53
  optional :rev, :int64, 3
55
54
  optional :path, :string, 5
56
55
  optional :value, :bytes, 6
@@ -1,15 +1,8 @@
1
1
  module Fraggle
2
2
  module Block
3
3
  class Response
4
- VALID = 1
5
- DONE = 2
6
-
7
4
  def valid?
8
- (flags & VALID) > 0
9
- end
10
-
11
- def done?
12
- (flags & DONE) > 0
5
+ true
13
6
  end
14
7
  end
15
8
  end
@@ -1,5 +1,5 @@
1
1
  module Fraggle
2
2
  module Block
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
data/msg.proto ADDED
@@ -0,0 +1,57 @@
1
+ package server;
2
+
3
+ // see doc/proto.md
4
+ message Request {
5
+ optional int32 tag = 1;
6
+
7
+ enum Verb {
8
+ GET = 1;
9
+ SET = 2;
10
+ DEL = 3;
11
+ REV = 5;
12
+ WAIT = 6;
13
+ NOP = 7;
14
+ WALK = 9;
15
+ GETDIR = 14;
16
+ STAT = 16;
17
+ ACCESS = 99;
18
+ }
19
+ optional Verb verb = 2;
20
+
21
+ optional string path = 4;
22
+ optional bytes value = 5;
23
+ optional int32 other_tag = 6;
24
+
25
+ optional int32 offset = 7;
26
+
27
+ optional int64 rev = 9;
28
+ }
29
+
30
+ // see doc/proto.md
31
+ message Response {
32
+ optional int32 tag = 1;
33
+ optional int32 flags = 2;
34
+
35
+ optional int64 rev = 3;
36
+ optional string path = 5;
37
+ optional bytes value = 6;
38
+ optional int32 len = 8;
39
+
40
+ enum Err {
41
+ // don't use value 0
42
+ OTHER = 127;
43
+ TAG_IN_USE = 1;
44
+ UNKNOWN_VERB = 2;
45
+ READONLY = 3;
46
+ TOO_LATE = 4;
47
+ REV_MISMATCH = 5;
48
+ BAD_PATH = 6;
49
+ MISSING_ARG = 7;
50
+ RANGE = 8;
51
+ NOTDIR = 20;
52
+ ISDIR = 21;
53
+ NOENT = 22;
54
+ }
55
+ optional Err err_code = 100;
56
+ optional string err_detail = 101;
57
+ }
metadata CHANGED
@@ -1,78 +1,58 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: fraggle-block
3
- version: !ruby/object:Gem::Version
4
- hash: 27
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Dylan Egan
14
9
  - Blake Mizerany
15
10
  autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
-
19
- date: 2011-04-22 00:00:00 Z
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
13
+ date: 2012-04-10 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
22
16
  name: beefcake
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
25
18
  none: false
26
- requirements:
19
+ requirements:
27
20
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 13
30
- segments:
31
- - 0
32
- - 3
33
- version: "0.3"
21
+ - !ruby/object:Gem::Version
22
+ version: '0.3'
34
23
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: system_timer
38
24
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: !ruby/object:Gem::Requirement
40
26
  none: false
41
- requirements:
42
- - - "="
43
- - !ruby/object:Gem::Version
44
- hash: 15
45
- segments:
46
- - 1
47
- - 0
48
- version: "1.0"
49
- type: :runtime
50
- version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '0.3'
31
+ - !ruby/object:Gem::Dependency
52
32
  name: turn
53
- prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
33
+ requirement: !ruby/object:Gem::Requirement
55
34
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
63
39
  type: :development
64
- version_requirements: *id003
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
65
47
  description: A synchronous Ruby client for Doozer.
66
- email:
48
+ email:
67
49
  - dylanegan@gmail.com
68
50
  - blake.mizerany@gmail.com
69
- executables:
51
+ executables:
70
52
  - fraggle-block
71
53
  extensions: []
72
-
73
54
  extra_rdoc_files: []
74
-
75
- files:
55
+ files:
76
56
  - .gitignore
77
57
  - Gemfile
78
58
  - README.md
@@ -86,43 +66,35 @@ files:
86
66
  - lib/fraggle/block/msg.pb.rb
87
67
  - lib/fraggle/block/response.rb
88
68
  - lib/fraggle/block/version.rb
69
+ - msg.proto
89
70
  - test/client_test.rb
90
71
  - test/connection_test.rb
91
72
  - test/helper.rb
92
73
  homepage: https://github.com/dylanegan/fraggle-block
93
74
  licenses: []
94
-
95
75
  post_install_message:
96
76
  rdoc_options: []
97
-
98
- require_paths:
77
+ require_paths:
99
78
  - lib
100
- required_ruby_version: !ruby/object:Gem::Requirement
79
+ required_ruby_version: !ruby/object:Gem::Requirement
101
80
  none: false
102
- requirements:
103
- - - ">="
104
- - !ruby/object:Gem::Version
105
- hash: 3
106
- segments:
107
- - 0
108
- version: "0"
109
- required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
86
  none: false
111
- requirements:
112
- - - ">="
113
- - !ruby/object:Gem::Version
114
- hash: 3
115
- segments:
116
- - 0
117
- version: "0"
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
118
91
  requirements: []
119
-
120
92
  rubyforge_project: fraggle-block
121
- rubygems_version: 1.7.1
93
+ rubygems_version: 1.8.21
122
94
  signing_key:
123
95
  specification_version: 3
124
96
  summary: A synchronous Ruby client for Doozer.
125
- test_files:
97
+ test_files:
126
98
  - test/client_test.rb
127
99
  - test/connection_test.rb
128
100
  - test/helper.rb