marshal_socket 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ # MarshalSocket
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'marshal_socket'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install marshal_socket
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,72 @@
1
+ require 'marshal_socket/version'
2
+
3
+ require 'socket'
4
+
5
+ module MarshalSocket
6
+
7
+ module Socket
8
+ def dump(obj)
9
+ write_lock.synchronize do
10
+ Marshal.dump([:obj, obj], self)
11
+ end
12
+ end
13
+
14
+ def load
15
+ read_lock.synchronize do
16
+ Marshal.load(self)
17
+ end
18
+ end
19
+
20
+ def bye
21
+ write_lock.synchronize do
22
+ Marshal.dump([:bye], self)
23
+ end
24
+ end
25
+
26
+ def write_lock
27
+ @write_lock = Mutex.new
28
+ end
29
+
30
+ def read_lock
31
+ @read_lock = Mutex.new
32
+ end
33
+
34
+ def receive_objects(&block)
35
+ run = true
36
+
37
+ while run do
38
+ begin
39
+ msg_type, obj = load
40
+ rescue EOFError
41
+ run = false
42
+ next
43
+ end
44
+
45
+ case msg_type
46
+ when :bye
47
+ run = false
48
+ close
49
+ when :obj
50
+ block.call(self, obj)
51
+ else
52
+ $stderr.puts "unknown message type #{msg_type.inspect}"
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
59
+
60
+ class TCPServer < ::TCPServer
61
+ def accept
62
+ super.tap do |socket|
63
+ socket.extend(Socket)
64
+ end
65
+ end
66
+ end
67
+
68
+ class TCPSocket < ::TCPSocket
69
+ include Socket
70
+ end
71
+
72
+ end
@@ -0,0 +1,3 @@
1
+ module MarshalSocket
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+
5
+ require 'marshal_socket'
6
+
7
+ require 'minitest/autorun'
@@ -0,0 +1,60 @@
1
+ require_relative 'helper'
2
+
3
+ describe MarshalSocket::TCPServer do
4
+ before do
5
+ @server = MarshalSocket::TCPServer.new(0)
6
+ _, @port = @server.addr
7
+ end
8
+
9
+ describe '#accept' do
10
+ it 'returns sockets with the MarshalSocket::Socket modules mixed in' do
11
+ client_thread = Thread.new do
12
+ client = MarshalSocket::TCPSocket.new('localhost', @port)
13
+ end
14
+
15
+ socket = @server.accept
16
+
17
+ assert socket.respond_to?(:dump)
18
+ assert socket.respond_to?(:load)
19
+ end
20
+ end
21
+
22
+ it 'should work' do
23
+ received = []
24
+
25
+ client_thread = Thread.new do
26
+ client = MarshalSocket::TCPSocket.new('localhost', @port)
27
+
28
+ client.dump(1)
29
+ client.dump('string')
30
+ client.dump(:symbol)
31
+ client.dump(true)
32
+ client.dump(['array'])
33
+ client.dump(:hash => true)
34
+
35
+ client.bye
36
+
37
+ client.receive_objects do |sock, obj|
38
+ received << obj
39
+ end
40
+ end
41
+
42
+ server_socket = @server.accept
43
+ server_thread = Thread.new do
44
+ server_socket.receive_objects do |sock, obj|
45
+ sock.dump(obj)
46
+ end
47
+ end
48
+
49
+ client_thread.join
50
+ server_thread.join
51
+
52
+ received.size.must_equal 6
53
+ received[0].must_equal 1
54
+ received[1].must_equal 'string'
55
+ received[2].must_equal :symbol
56
+ received[3].must_equal true
57
+ received[4].must_equal ['array']
58
+ received[5].must_equal({:hash => true})
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marshal_socket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mick Staugaard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A dead simple way to pass ruby object on a socket
47
+ email:
48
+ - mick@staugaard.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/marshal_socket/version.rb
54
+ - lib/marshal_socket.rb
55
+ - test/helper.rb
56
+ - test/server_test.rb
57
+ - README.md
58
+ homepage: ''
59
+ licenses: []
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ segments:
71
+ - 0
72
+ hash: -2128519956354470041
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ segments:
80
+ - 0
81
+ hash: -2128519956354470041
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A dead simple way to pass ruby object on a socket
88
+ test_files:
89
+ - test/helper.rb
90
+ - test/server_test.rb