miu-rpc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa1fe4ff7aa971970bc23988f4494ac764c223e1
4
+ data.tar.gz: e0ca526a0503a3855713f1ec62035d63a7e0e81d
5
+ SHA512:
6
+ metadata.gz: 4447a767035b39d6454714a2a213994481724f06b588fe628a842f746eccf124e47060fa923b39d3ec763f410c7099da0c5e26cf3bc61a762772d138d783c083
7
+ data.tar.gz: 1756d06652d77edbecfdb5b7539f702e094928ae1d22ef8227f2c69f9fc872b2f02cd083591352cfba253e1d45ba4525dd273f33b4a5497552063872ee1789b9
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in miu-rpc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 mashiro
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Miu::RPC
2
+
3
+ Miu RPC extension
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'miu-rpc'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install miu-rpc
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class Handler
23
+ def add(a, b)
24
+ a + b
25
+ end
26
+ end
27
+
28
+ server = Miu::RPC::Server.new 'inproc://myrpcserver', Handler.new
29
+ client = Miu::RPC::Client.new 'inproc://myrpcserver'
30
+
31
+ client.call :add, 1, 2 #=> 3
32
+
33
+ # with future
34
+ f = client.future.call :add, 1, 2
35
+ f.value #=> 3
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => :spec
@@ -0,0 +1,46 @@
1
+ require 'miu/rpc/helpers'
2
+
3
+ module Miu
4
+ module RPC
5
+ class Client
6
+ include Celluloid::ZMQ
7
+
8
+ class Evaluator
9
+ include Saorin::Client::Base
10
+ include Helpers
11
+
12
+ def initialize(socket)
13
+ super :formatter => MessagePack
14
+ @socket = socket
15
+ end
16
+
17
+ def send_request(content)
18
+ @socket.write content
19
+ read_parts(@socket)[0]
20
+ end
21
+ end
22
+
23
+ def initialize(address)
24
+ @socket = DealerSocket.new
25
+ @socket.connect address
26
+ @evaluator = Evaluator.new @socket
27
+ end
28
+
29
+ def close
30
+ @socket.close
31
+ end
32
+
33
+ def call(method, *args)
34
+ @evaluator.call method, *args
35
+ rescue Exception => e
36
+ abort e
37
+ end
38
+
39
+ def notify(method, *args)
40
+ @evaluator.notify method, *args
41
+ rescue Exception => e
42
+ abort e
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ module Miu
2
+ module RPC
3
+ module Helpers
4
+ def read_parts(socket)
5
+ values = []
6
+ loop do
7
+ values << socket.read
8
+ break unless socket.more_parts?
9
+ end
10
+ values
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ require 'miu/rpc/helpers'
2
+
3
+ module Miu
4
+ module RPC
5
+ class Server
6
+ include Celluloid::ZMQ
7
+ include Helpers
8
+
9
+ class Evaluator
10
+ include Saorin::Server::Base
11
+
12
+ def initialize(handler)
13
+ super handler, :formatter => MessagePack
14
+ end
15
+ end
16
+
17
+ def initialize(address, handler)
18
+ @socket = Celluloid::ZMQ::RouterSocket.new
19
+ @socket.bind address
20
+ @evaluator = Evaluator.new handler
21
+
22
+ async.run
23
+ end
24
+
25
+ def close
26
+ @socket.close
27
+ terminate
28
+ end
29
+
30
+ def run
31
+ loop do
32
+ parts = read_parts @socket
33
+ async.handle_message parts
34
+ end
35
+ end
36
+
37
+ def handle_message(parts)
38
+ id, request = parts
39
+ response = @evaluator.process_request request
40
+ @socket.write id, response
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module Miu
2
+ module RPC
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
data/lib/miu/rpc.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'celluloid/zmq'
2
+ require 'saorin/msgpack'
3
+ require 'miu/rpc/version'
4
+ require 'miu/rpc/client'
5
+ require 'miu/rpc/server'
data/miu-rpc.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'miu/rpc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'miu-rpc'
8
+ spec.version = Miu::RPC::VERSION
9
+ spec.authors = ['mashiro']
10
+ spec.email = ['mail@mashiro.org']
11
+ spec.description = %q{miu rpc extension}
12
+ spec.summary = spec.description
13
+ spec.homepage = 'https://github.com/yuijo/miu-rpc'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'miu', '>= 0.2.0'
22
+ spec.add_dependency 'celluloid-zmq', '>= 0.14.0'
23
+ spec.add_dependency 'saorin', '>= 0.5.0'
24
+ spec.add_dependency 'saorin-msgpack', '>= 0.0.2'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec'
27
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Miu::RPC do
4
+ before :all do
5
+ c = Class.new do
6
+ def add(a, b)
7
+ a + b
8
+ end
9
+ end
10
+ address = 'inproc://rspec'
11
+ @server = Miu::RPC::Server.new address, c.new
12
+ @client = Miu::RPC::Client.new address
13
+ end
14
+
15
+ after :all do
16
+ @client.close
17
+ @server.close
18
+ end
19
+
20
+ it { expect(@client.call :add, 1, 2).to eq 3 }
21
+ it { expect { @client.call :sub, 1, 2 }.to raise_error(Saorin::MethodNotFound) }
22
+ end
@@ -0,0 +1,20 @@
1
+ require 'miu/rpc'
2
+ Celluloid.logger = nil
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # Require this file using `require "spec_helper"` to ensure that it is only
7
+ # loaded once.
8
+ #
9
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: miu-rpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mashiro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: miu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: celluloid-zmq
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.14.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.14.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: saorin
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: saorin-msgpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: miu rpc extension
98
+ email:
99
+ - mail@mashiro.org
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/miu/rpc.rb
111
+ - lib/miu/rpc/client.rb
112
+ - lib/miu/rpc/helpers.rb
113
+ - lib/miu/rpc/server.rb
114
+ - lib/miu/rpc/version.rb
115
+ - miu-rpc.gemspec
116
+ - spec/miu/rpc_spec.rb
117
+ - spec/spec_helper.rb
118
+ homepage: https://github.com/yuijo/miu-rpc
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.0.0
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: miu rpc extension
142
+ test_files:
143
+ - spec/miu/rpc_spec.rb
144
+ - spec/spec_helper.rb