em-berp-protocol 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/README.md +8 -0
- data/em-berp-protocol.gemspec +1 -0
- data/lib/em-berp-protocol/version.rb +1 -1
- data/lib/em-berp-protocol.rb +16 -1
- data/tests/test-helper.rb +4 -0
- data/tests/test_em-berp-protocol.rb +1 -4
- data/tests/test_em-berp-protocol_failure_handling.rb +36 -0
- metadata +27 -2
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
script: "rake test"
|
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[![Build Status](https://secure.travis-ci.org/wjessop/em-berp-protocol.png?branch=master)](https://travis-ci.org/wjessop/em-berp-protocol)
|
2
|
+
|
1
3
|
# EventMachine::Protocols::BerpProtocol
|
2
4
|
|
3
5
|
EventMachine connection protocol for sending and receiving BERP messages.
|
@@ -27,6 +29,12 @@ module RubyServer
|
|
27
29
|
def receive_object(obj)
|
28
30
|
send_object({'Oh yeah' => obj})
|
29
31
|
end
|
32
|
+
|
33
|
+
# Optional - specify a receive_error method to get notifications of problems parsing bert data
|
34
|
+
# When this happens em-berp-protocol closes the client connection
|
35
|
+
def receive_error(error_string)
|
36
|
+
puts error
|
37
|
+
end
|
30
38
|
end
|
31
39
|
```
|
32
40
|
|
data/em-berp-protocol.gemspec
CHANGED
data/lib/em-berp-protocol.rb
CHANGED
@@ -10,7 +10,14 @@ module EventMachine
|
|
10
10
|
while @buf.size >= 4
|
11
11
|
if @buf.size >= 4+(size = @buf.unpack('N').first)
|
12
12
|
@buf.slice!(0,4)
|
13
|
-
|
13
|
+
begin
|
14
|
+
slice = @buf.slice!(0,size)
|
15
|
+
receive_object(BERT.decode(slice))
|
16
|
+
rescue TypeError => e
|
17
|
+
handle_bert_error("TypeError: #{e}")
|
18
|
+
rescue EOFError => e
|
19
|
+
handle_bert_error("EOFError: #{e} with data #{slice.inspect}")
|
20
|
+
end
|
14
21
|
else
|
15
22
|
break
|
16
23
|
end
|
@@ -27,6 +34,14 @@ module EventMachine
|
|
27
34
|
data = BERT.encode(obj)
|
28
35
|
send_data([data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*'))
|
29
36
|
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def handle_bert_error(message)
|
41
|
+
remote_port, *remote_ip = get_peername[2,6].unpack("nC4")
|
42
|
+
receive_error("#{message} from client #{remote_ip.join('.')}:#{remote_port}") if respond_to? :receive_error
|
43
|
+
close_connection
|
44
|
+
end
|
30
45
|
end
|
31
46
|
end
|
32
47
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test-helper'
|
2
|
+
|
3
|
+
class TestBerpFailureHandling < Test::Unit::TestCase
|
4
|
+
module Server
|
5
|
+
include EventMachine::Protocols::BerpProtocol
|
6
|
+
|
7
|
+
def receive_error(error)
|
8
|
+
$error = error
|
9
|
+
EM.stop
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_receiving_invalid_magic_value
|
14
|
+
EM.run {
|
15
|
+
EM.start_server "127.0.0.1", 53654, Server
|
16
|
+
assert_nothing_raised do
|
17
|
+
s = TCPSocket.new 'localhost', 53654
|
18
|
+
s.write [1, -3].pack("Nc") # Length 1, magic number -1
|
19
|
+
s.close
|
20
|
+
end
|
21
|
+
}
|
22
|
+
assert $error.match(/\ATypeError: Invalid magic value for BERT string from client 127\.0\.0\.1:\d+\z/)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_unexpected_eof_in_data_stream
|
26
|
+
EM.run {
|
27
|
+
EM.start_server "127.0.0.1", 53654, Server
|
28
|
+
assert_nothing_raised do
|
29
|
+
s = TCPSocket.new '127.0.0.1', 53654
|
30
|
+
s.write [18, -125, 104, 2, 107, 0, 12, 99, 97, 112, 97, 98, 105, 108, 105, 116, 105, 101, 115].pack("Nc*")
|
31
|
+
s.close
|
32
|
+
end
|
33
|
+
}
|
34
|
+
assert $error.match(/\AEOFError: Unexpected end of BERT stream with data "\\x83h\\x02k\\x00\\fcapabilities" from client 127\.0\.0\.1:\d+\z/)
|
35
|
+
end
|
36
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-berp-protocol
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
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:
|
12
|
+
date: 2013-02-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bert
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
description: EventMachine connection protocol for sending and receiving BERP messages
|
47
63
|
email:
|
48
64
|
- will@willj.net
|
@@ -51,6 +67,7 @@ extensions: []
|
|
51
67
|
extra_rdoc_files: []
|
52
68
|
files:
|
53
69
|
- .gitignore
|
70
|
+
- .travis.yml
|
54
71
|
- Gemfile
|
55
72
|
- LICENSE.txt
|
56
73
|
- README.md
|
@@ -58,7 +75,9 @@ files:
|
|
58
75
|
- em-berp-protocol.gemspec
|
59
76
|
- lib/em-berp-protocol.rb
|
60
77
|
- lib/em-berp-protocol/version.rb
|
78
|
+
- tests/test-helper.rb
|
61
79
|
- tests/test_em-berp-protocol.rb
|
80
|
+
- tests/test_em-berp-protocol_failure_handling.rb
|
62
81
|
homepage: https://github.com/wjessop/em-berp-protocol
|
63
82
|
licenses: []
|
64
83
|
post_install_message:
|
@@ -71,12 +90,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
90
|
- - ! '>='
|
72
91
|
- !ruby/object:Gem::Version
|
73
92
|
version: '0'
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
hash: 2809755693066062168
|
74
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
97
|
none: false
|
76
98
|
requirements:
|
77
99
|
- - ! '>='
|
78
100
|
- !ruby/object:Gem::Version
|
79
101
|
version: '0'
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
hash: 2809755693066062168
|
80
105
|
requirements: []
|
81
106
|
rubyforge_project:
|
82
107
|
rubygems_version: 1.8.24
|