caldecott 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,7 +29,7 @@ module Caldecott
29
29
  puts "Starting local server on port #{local_port} to #{tun_url}"
30
30
  end
31
31
 
32
- EM.start_server("localhost", local_port, TcpConnection) do |conn|
32
+ EM.start_server("0.0.0.0", local_port, TcpConnection) do |conn|
33
33
  # avoid races between tunnel setup and incoming local data
34
34
  conn.pause
35
35
 
@@ -11,7 +11,6 @@ module Caldecott
11
11
  def initialize(logger, url, dst_host, dst_port, auth_token)
12
12
  @log, @auth_token = logger, auth_token
13
13
  @closing = false
14
- @retries = 0
15
14
  init_msg = ""
16
15
 
17
16
  # FIXME: why is this optional?
@@ -64,8 +63,8 @@ module Caldecott
64
63
  @onreceive.call(data)
65
64
  end
66
65
 
67
- def start(base_uri, init_msg)
68
- if (@retries += 1) > MAX_RETRIES
66
+ def start(base_uri, init_msg, attempts = MAX_RETRIES)
67
+ if attempts <= 0
69
68
  trigger_on_close
70
69
  return
71
70
  end
@@ -75,14 +74,13 @@ module Caldecott
75
74
  parsed_uri.path = '/tunnels'
76
75
 
77
76
  @log.debug "post #{parsed_uri.to_s}"
78
- req = EM::HttpRequest.new(parsed_uri.to_s).post :body => init_msg, :head => { "Auth-Token" => @auth_token }
77
+ req = EM::HttpRequest.new(parsed_uri.to_s).post :body => init_msg, :head => { "Auth-Token" => @auth_token, "Content-Length" => init_msg.bytesize }
79
78
 
80
79
  req.callback do
81
80
  @log.debug "post #{parsed_uri.to_s} #{req.response_header.status}"
82
81
  unless [200, 201, 204].include?(req.response_header.status)
83
- start(base_uri, init_msg)
82
+ start(base_uri, init_msg, attempts - 1)
84
83
  else
85
- @retries = 0
86
84
  resp = JSON.parse(req.response)
87
85
 
88
86
  parsed_uri.path = resp["path"]
@@ -99,7 +97,7 @@ module Caldecott
99
97
 
100
98
  req.errback do
101
99
  @log.debug "post #{parsed_uri.to_s} error"
102
- start(base_uri, init_msg)
100
+ start(base_uri, init_msg, attempts - 1)
103
101
  end
104
102
 
105
103
  rescue Exception => e
@@ -109,8 +107,8 @@ module Caldecott
109
107
  end
110
108
  end
111
109
 
112
- def stop
113
- if (@retries += 1) > MAX_RETRIES
110
+ def stop(attempts = MAX_RETRIES)
111
+ if attempts <= 0
114
112
  trigger_on_close
115
113
  return
116
114
  end
@@ -122,7 +120,7 @@ module Caldecott
122
120
 
123
121
  req.errback do
124
122
  @log.debug "delete #{@tun_uri} error"
125
- stop
123
+ stop(attempts - 1)
126
124
  end
127
125
 
128
126
  req.callback do
@@ -130,7 +128,7 @@ module Caldecott
130
128
  if [200, 202, 204, 404].include?(req.response_header.status)
131
129
  trigger_on_close
132
130
  else
133
- stop
131
+ stop(attempts - 1)
134
132
  end
135
133
  end
136
134
  end
@@ -138,7 +136,6 @@ module Caldecott
138
136
  class Reader
139
137
  def initialize(log, uri, conn, auth_token)
140
138
  @log, @base_uri, @conn, @auth_token = log, uri, conn, auth_token
141
- @retries = 0
142
139
  @closing = false
143
140
  start
144
141
  end
@@ -147,13 +144,14 @@ module Caldecott
147
144
  @closing = true
148
145
  end
149
146
 
150
- def start(seq = 1)
151
- if (@retries += 1) > MAX_RETRIES
147
+ def start(seq = 1, attempts = MAX_RETRIES)
148
+ return if @closing
149
+
150
+ if attempts <= 0
152
151
  @conn.trigger_on_close
153
152
  return
154
153
  end
155
154
 
156
- return if @closing
157
155
  uri = "#{@base_uri}/#{seq}"
158
156
  @log.debug "get #{uri}"
159
157
  req = EM::HttpRequest.new(uri).get :timeout => 0, :head => { "Auth-Token" => @auth_token }
@@ -168,7 +166,6 @@ module Caldecott
168
166
  case req.response_header.status
169
167
  when 200
170
168
  @conn.trigger_on_receive(req.response)
171
- @retries = 0
172
169
  start(seq + 1)
173
170
  when 404
174
171
  @conn.trigger_on_close
@@ -182,51 +179,58 @@ module Caldecott
182
179
  class Writer
183
180
  def initialize(log, uri, conn, auth_token)
184
181
  @log, @uri, @conn, @auth_token = log, uri, conn, auth_token
185
- @retries = 0
186
182
  @seq, @write_buffer = 1, ""
187
183
  @closing = @writing = false
188
184
  end
189
185
 
190
186
  def send_data(data)
191
187
  @write_buffer << data
192
- send_data_buffered
188
+ send_data_buffered unless @writing
193
189
  end
194
190
 
195
191
  def close
196
192
  @closing = true
197
193
  end
198
194
 
199
- def send_data_buffered
200
- if (@retries += 1) > MAX_RETRIES
195
+ private
196
+
197
+ def send_data_buffered(attempts = MAX_RETRIES)
198
+ return if @closing
199
+
200
+ @writing = true
201
+
202
+ @log.debug "attempts left: #{attempts}"
203
+ if attempts <= 0
201
204
  @conn.trigger_on_close
202
205
  return
203
206
  end
204
207
 
205
- return if @closing
206
- data, @write_buffer = @write_buffer, "" unless @writing
208
+ data, @write_buffer = @write_buffer, ""
207
209
 
208
- @writing = true
209
210
  uri = "#{@uri}/#{@seq}"
210
211
  @log.debug "put #{uri}"
211
- req = EM::HttpRequest.new(uri).put :body => data, :head => { "Auth-Token" => @auth_token }
212
+ req = EM::HttpRequest.new(uri).put :body => data, :head => { "Auth-Token" => @auth_token, "Content-Length" => data.bytesize }
212
213
 
213
214
  req.errback do
214
215
  @log.debug "put #{uri} error"
215
- send_data_buffered
216
+ send_data_buffered(attempts - 1)
216
217
  end
217
218
 
218
219
  req.callback do
219
220
  @log.debug "put #{uri} #{req.response_header.status}"
220
221
  case req.response_header.status
221
222
  when 200, 202, 204
222
- @writing = false
223
223
  @seq += 1
224
- @retries = 0
225
- send_data_buffered unless @write_buffer.empty?
224
+
225
+ if @write_buffer.empty?
226
+ @writing = false
227
+ else
228
+ send_data_buffered
229
+ end
226
230
  when 404
227
231
  @conn.trigger_on_close
228
232
  else
229
- send_data_buffered
233
+ send_data_buffered(attempts - 1)
230
234
  end
231
235
  end
232
236
  end
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2009-2011 VMware, Inc.
2
2
 
3
3
  module Caldecott
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
data/lib/caldecott.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # Copyright (c) 2009-2011 VMware, Inc.
2
2
 
3
3
  require 'rubygems'
4
- require 'bundler'
5
4
 
6
5
  require 'caldecott/client'
7
6
  require 'caldecott/server'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: caldecott
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - VMware
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-11-08 00:00:00 -08:00
13
+ date: 2012-01-10 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency