klomp 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,14 @@
1
1
  Klomp Changes
2
2
  --------------------------------------------------------------------------------
3
3
 
4
+ 1.0.2 (2012/10/10)
5
+ ================================================================================
6
+
7
+ - Frames now respond to `#[]` and `#[]=` (for header access) and `#body=` (for
8
+ setting body after construction)
9
+ - Configurable foreground IO.select timeout through `options['select_timeout']`
10
+ - Add logging of read frames and exceptions when going offline
11
+
4
12
  1.0.1 (2012/10/5)
5
13
  ================================================================================
6
14
 
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "klomp"
5
- s.version = "1.0.1"
5
+ s.version = "1.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Nick Sieger"]
9
- s.date = "2012-10-03"
9
+ s.date = "2012-10-10"
10
10
  s.description = "Klomp is a simple [Stomp] messaging client that keeps your sanity intact.\n\nThe purpose of Klomp is to be the simplest possible Stomp client. No in-memory\nbuffering of outgoing messages, no fanout subscriptions in-process, no\ntransactions, no complicated messaging patterns. Code simple enough so that when\nsomething goes wrong, the problem is obvious.\n\n[Stomp]: http://stomp.github.com/"
11
11
  s.email = ["nick.sieger@livingsocial.com"]
12
12
  s.extra_rdoc_files = ["Manifest.txt"]
@@ -1,5 +1,5 @@
1
1
  class Klomp
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
 
4
4
  class Error < StandardError; end
5
5
 
@@ -28,6 +28,7 @@ class Klomp
28
28
  @options['host'] ||= host
29
29
  @subscriptions = {}
30
30
  @logger = options['logger']
31
+ @select_timeout = options['select_timeout'] || 0.1
31
32
  connect
32
33
  end
33
34
 
@@ -82,7 +83,7 @@ class Klomp
82
83
  @socket = TCPSocket.new *options['server']
83
84
  @socket.set_encoding 'UTF-8'
84
85
  write Frames::Connect.new(options)
85
- frame = read Frames::Connected, 0.1
86
+ frame = read Frames::Connected, @select_timeout
86
87
  log_frame frame if logger
87
88
  raise Error, frame.headers['message'] if frame.error?
88
89
  end
@@ -91,7 +92,7 @@ class Klomp
91
92
  raise Error, "connection closed" if closed?
92
93
  raise Error, "disconnected" unless connected?
93
94
 
94
- rs, ws, = IO.select(nil, [@socket], nil, 0.1)
95
+ rs, ws, = IO.select(nil, [@socket], nil, @select_timeout)
95
96
  raise Error, "connection unavailable for write" unless ws && !ws.empty?
96
97
 
97
98
  @socket.write frame.to_s
@@ -99,19 +100,19 @@ class Klomp
99
100
  frame
100
101
  rescue Error
101
102
  raise
102
- rescue
103
- go_offline
103
+ rescue => e
104
+ go_offline e
104
105
  raise
105
106
  end
106
107
 
107
108
  def read(type, timeout = nil)
108
109
  rs, = IO.select([@socket], nil, nil, timeout)
109
110
  raise Error, "connection unavailable for read" unless rs && !rs.empty?
110
- type.new @socket.gets(FRAME_SEP)
111
+ type.new(@socket.gets(FRAME_SEP)).tap {|frame| log_frame frame if logger }
111
112
  rescue Error
112
113
  raise
113
- rescue
114
- go_offline
114
+ rescue => e
115
+ go_offline e
115
116
  raise
116
117
  end
117
118
 
@@ -122,8 +123,8 @@ class Klomp
122
123
  logger.debug "frame=#{frame.name} #{frame.headers.map{|k,v| k + '=' + v }.join(' ')} body=#{body}"
123
124
  end
124
125
 
125
- def log_exception(ex, level = :error)
126
- logger.send level, "exception=#{ex.class.name} message=#{ex.message.inspect} backtrace[0]=#{ex.backtrace[0]} backtrace[1]=#{ex.backtrace[1]}"
126
+ def log_exception(ex, level = :error, msg_start = '')
127
+ logger.send level, "#{msg_start}exception=#{ex.class.name} message=#{ex.message.inspect} backtrace[0]=#{ex.backtrace[0]} backtrace[1]=#{ex.backtrace[1]}"
127
128
  logger.debug "exception=#{ex.class.name} full_backtrace=" + ex.backtrace.join("\n")
128
129
  end
129
130
 
@@ -131,12 +132,8 @@ class Klomp
131
132
  @closing = true
132
133
  end
133
134
 
134
- def go_offline
135
- if logger
136
- msg = "offline server=#{options['server'].join(':')}"
137
- msg << " exception=#{$!.class.name} message=#{$!.message.inspect}" if $!
138
- logger.warn msg
139
- end
135
+ def go_offline(ex)
136
+ log_exception(ex, :warn, "offline server=#{options['server'].join(':')} ") if logger
140
137
  @socket.close rescue nil
141
138
  @socket = nil
142
139
  Sentinel.new(self)
@@ -3,17 +3,14 @@ class Klomp
3
3
 
4
4
  module Frames
5
5
  class Frame
6
- def name
7
- @name ||= self.class.name.split('::').last.upcase
8
- end
6
+ def name; @name ||= self.class.name.split('::').last.upcase; end
9
7
 
10
- def headers
11
- @headers ||= {}
12
- end
8
+ def headers; @headers ||= {}; end
9
+ def [](key); headers[key]; end
10
+ def []=(key, value); headers[key] = value; end
13
11
 
14
- def body
15
- @body ||= ""
16
- end
12
+ def body; @body ||= ""; end
13
+ def body=(b); @body = b; end
17
14
 
18
15
  def to_s
19
16
  "#{name}\n#{dump_headers}\n#{@body}#{FRAME_SEP}"
@@ -2,9 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe Klomp::Frames do
4
4
 
5
+ Given(:options) { {'login' => 'admin', 'passcode' => 'password', 'host' => '127.0.0.1'} }
6
+
5
7
  context "CONNECT" do
6
8
 
7
- Given(:options) { {'login' => 'admin', 'passcode' => 'password', 'host' => '127.0.0.1'} }
8
9
 
9
10
  When(:connect) { Klomp::Frames::Connect.new(options).to_s }
10
11
 
@@ -20,4 +21,28 @@ describe Klomp::Frames do
20
21
 
21
22
  end
22
23
 
24
+ context "#[] is an alias for #headers" do
25
+
26
+ When(:connect) { Klomp::Frames::Connect.new(options) }
27
+
28
+ When { connect['my-header'] = 'my-value' }
29
+
30
+ Then { connect['login'].should == 'admin' }
31
+
32
+ Then { connect['passcode'].should == 'password' }
33
+
34
+ Then { connect['my-header'].should == 'my-value' }
35
+
36
+ end
37
+
38
+ context "body can be assigned after construction" do
39
+
40
+ Given(:send_frame) { Klomp::Frames::Send.new("/queue/q", "", {}) }
41
+
42
+ When { send_frame.body = "hello" }
43
+
44
+ Then { send_frame.body.should == "hello" }
45
+
46
+ end
47
+
23
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klomp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-05 00:00:00.000000000 Z
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
@@ -224,7 +224,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
224
224
  version: '0'
225
225
  segments:
226
226
  - 0
227
- hash: -2110109472818919447
227
+ hash: 4534787410900240650
228
228
  required_rubygems_version: !ruby/object:Gem::Requirement
229
229
  none: false
230
230
  requirements: