bertclient 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/bertclient.rb +44 -8
  2. metadata +5 -5
data/lib/bertclient.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'openssl'
2
+ require 'zlib'
2
3
  require 'bert'
3
4
  require 'resolv-replace' # TCPSocket
4
5
 
@@ -13,10 +14,14 @@ module BERT
13
14
  class BadHeader < RPCError; end
14
15
  class BadData < RPCError; end
15
16
 
17
+ GZIP_BERP = t[:info, :encoding, [t[:gzip]]]
18
+
16
19
  def initialize(opts={}, &block)
17
20
  @host = opts[:host] || 'localhost'
18
21
  @port = opts[:port] || 9999
19
- @ssl = opts[:ssl] || false
22
+ @gzip = opts[:gzip] || false
23
+ @gzip_threshold = opts[:gzip_threshold] || 1024 # bytes
24
+ @ssl = opts[:ssl] || false
20
25
  @verify_ssl = opts.has_key?(:verify_ssl) ? opts[:verify_ssl] : true
21
26
  @socket = {}
22
27
  execute(&block) if block_given?
@@ -40,7 +45,30 @@ module BERT
40
45
  def cast_or_call(cc, mod, fun, *args)
41
46
  req = t[cc, mod.to_sym, fun.to_sym, args]
42
47
  write_berp(req)
43
- read_berp
48
+ read_response
49
+ end
50
+
51
+ def read_response
52
+ gzip_encoded = false
53
+ response = nil
54
+
55
+ loop do
56
+ response = read_berp
57
+ break unless response[0] == :info
58
+
59
+ # For now we only know how to handle gzip encoding info packets
60
+ if response == GZIP_BERP
61
+ gzip_encoded = true
62
+ else
63
+ raise NotImplementedError, "Only gzip-encoding related info packets are supported in this version of bertclient"
64
+ end
65
+ end
66
+
67
+ if gzip_encoded and response[0] == :gzip
68
+ response = BERT.decode(Zlib::Inflate.inflate(response[1]))
69
+ end
70
+
71
+ response
44
72
  end
45
73
 
46
74
  # See bert-rpc.org for error response mechanisms
@@ -112,15 +140,23 @@ module BERT
112
140
  end
113
141
 
114
142
  # Accepts a Ruby object, converts to a berp and sends through the socket
143
+ # Optionally sends gzip packet:
144
+ #
145
+ # -> {info, encoding, gzip}
146
+ # -> {gzip, GzippedBertEncodedData}
115
147
  def write_berp(obj)
116
- socket.write(Client.create_berp(obj))
148
+ data = BERT.encode(obj)
149
+ if @gzip and data.bytesize > @gzip_threshold
150
+ socket.write(Client.create_berp(BERT.encode(GZIP_BERP)))
151
+
152
+ data = BERT.encode(t[:gzip, Zlib::Deflate.deflate(data)])
153
+ end
154
+ socket.write(Client.create_berp(data))
117
155
  end
118
156
 
119
- # Accepts a Ruby object and returns an encoded berp
120
- def Client.create_berp(obj)
121
- data = BERT.encode(obj)
122
- length = [data.bytesize].pack('N')
123
- "#{length}#{data}"
157
+ # Accepts a string and returns a berp
158
+ def Client.create_berp(data)
159
+ [[data.bytesize].pack('N'), data].join
124
160
  end
125
161
  end
126
162
 
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- version: "0.2"
7
+ - 3
8
+ version: "0.3"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Jared Kuolt
@@ -13,11 +13,11 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-03-24 00:00:00 -07:00
16
+ date: 2010-03-29 00:00:00 -07:00
17
17
  default_executable:
18
18
  dependencies: []
19
19
 
20
- description: BERT::Client is a threadsafe BERT-RPC client with support for persistent connections, ssl, and it currently exposes BERT-RPC's cast and call
20
+ description: BERT::Client is a threadsafe BERT-RPC client with support for persistent connections, ssl, gzip, and it currently exposes BERT-RPC's cast and call
21
21
  email: luckythetourist@gmail.com
22
22
  executables: []
23
23
 
@@ -56,6 +56,6 @@ rubyforge_project: bertclient
56
56
  rubygems_version: 1.3.6
57
57
  signing_key:
58
58
  specification_version: 3
59
- summary: A threadsafe BERT-RPC client with ssl support and persistent connections
59
+ summary: A threadsafe BERT-RPC client with support for ssl, gzip and persistent connections
60
60
  test_files: []
61
61