redpack-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ # -*- mode: ruby; -*-
2
+
3
+ source "http://rubygems.org"
4
+
5
+
6
+ gem "redis", "~> 2.2.2"
7
+ gem "bson"
8
+ unless RUBY_PLATFORM == 'java' then
9
+ gem "bson_ext"
10
+ end
11
+
12
+
13
+ # Add dependencies to develop your gem here.
14
+ # Include everything needed to run rake, tests, features, etc.
15
+ group :development do
16
+ gem "shoulda", ">= 0"
17
+ gem "rdoc", "~> 3.12"
18
+ gem "bundler", "~> 1.0.0"
19
+ gem "jeweler", "~> 1.8.3"
20
+ # gem "rcov", ">= 0"
21
+ end
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ bson (1.5.2)
5
+ bson_ext (1.5.2)
6
+ bson (= 1.5.2)
7
+ git (1.2.5)
8
+ jeweler (1.8.3)
9
+ bundler (~> 1.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ rdoc
13
+ json (1.6.5)
14
+ rake (0.9.2.2)
15
+ rdoc (3.12)
16
+ json (~> 1.4)
17
+ redis (2.2.2)
18
+ shoulda (2.11.3)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bson
25
+ bson_ext
26
+ bundler (~> 1.0.0)
27
+ jeweler (~> 1.8.3)
28
+ rdoc (~> 3.12)
29
+ redis (~> 2.2.2)
30
+ shoulda
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2012 jonghyouk yun
2
+
3
+
4
+ MIT License
5
+
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining
8
+ a copy of this software and associated documentation files (the
9
+ "Software"), to deal in the Software without restriction, including
10
+ without limitation the rights to use, copy, modify, merge, publish,
11
+ distribute, sublicense, and/or sell copies of the Software, and to
12
+ permit persons to whom the Software is furnished to do so, subject to
13
+ the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be
16
+ included in all copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
26
+
27
+ -- http://en.wikipedia.org/wiki/MIT_License
@@ -0,0 +1,19 @@
1
+ = redpack-ruby
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to redpack-ruby
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 jonghyouk yun. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,75 @@
1
+ # -*- coding: utf-8; mode: ruby; -*-
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "redpack-ruby"
18
+ gem.homepage = "http://rubyforge.org/projects/redpack-ruby/"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Redis + MsgPack RPC Server, Client Library}
21
+ gem.description = %Q{Redis + MsgPack RPC Server, Client Library}
22
+ gem.email = "ageldama@gmail.com"
23
+ gem.authors = ["jonghyouk yun"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ =begin
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ test.rcov_opts << '--exclude "gems/*"'
42
+ end
43
+ =end
44
+
45
+ task :default => :test
46
+
47
+ require 'rdoc/task'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "redpack-ruby #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
57
+
58
+
59
+ task :run do |t|
60
+ dirs = ['ext', 'lib'].select { |dir| File.directory?(dir) }
61
+
62
+ original_load_path = $LOAD_PATH
63
+
64
+ cmd = if File.exist?('Gemfile')
65
+ require 'bundler'
66
+ Bundler.setup(:default)
67
+ end
68
+
69
+ # add the project code directories
70
+ $LOAD_PATH.unshift(*dirs)
71
+
72
+ ARGV[1..-1].each do |i|
73
+ load i
74
+ end
75
+ end
@@ -0,0 +1,12 @@
1
+ # -*- mode: org; coding: utf-8; -*-
2
+
3
+ * DONE simple dispatcher (single-object only)
4
+
5
+ * DONE dotted-name dispatcher (obj.method...)
6
+
7
+ * TODO async-call
8
+
9
+ * TODO fiber? em? based server request-handling?
10
+
11
+ * TODO client-timeout?
12
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,17 @@
1
+
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+
6
+ Bundler.require(:default)
7
+
8
+
9
+
10
+ require 'redpack-ruby/consts'
11
+ require 'redpack-ruby/request-context'
12
+ require 'redpack-ruby/method-dispatchers'
13
+ require 'redpack-ruby/responders'
14
+ require 'redpack-ruby/transcoders'
15
+ require 'redpack-ruby/servers'
16
+ require 'redpack-ruby/clients'
17
+
@@ -0,0 +1,73 @@
1
+
2
+
3
+ module RedPack
4
+
5
+ class SimpleSyncClient
6
+
7
+ attr_accessor :timeout
8
+
9
+ def initialize(redis, name, transcoder, timeout=0)
10
+ @redis = redis
11
+ @queue_name = RedPack::Consts::queue_name(name)
12
+ @response_queue_name = RedPack::Consts::new_response_queue_name(@redis)
13
+ @transcoder = transcoder
14
+ @msg_id_seq = 1
15
+ @timeout = timeout
16
+ end
17
+
18
+ def new_msg_id
19
+ if @msg_id_seq < ((2 << 32) - 2) then
20
+ @msg_id_seq = @msg_id_seq.succ
21
+ return @msg_id_seq
22
+ else
23
+ @msg_id_seq = 1
24
+ return @msg_id_seq
25
+ end
26
+ end
27
+
28
+
29
+ # TODO: forwarding?
30
+
31
+ # TODO: result-async?
32
+
33
+ def call(method, *params)
34
+ msg_id = self.new_msg_id
35
+ v = [RedPack::Consts::REQUEST, msg_id, method, params]
36
+ packed = @transcoder.pack({'data' => v,
37
+ 'return' => @response_queue_name,})
38
+ @redis.multi
39
+ @redis.rpush(@queue_name, packed)
40
+ @redis.exec
41
+ # TODO: refactor...
42
+ while true do
43
+ popped = @redis.blpop(@response_queue_name, @timeout)
44
+ if not popped.nil? then
45
+ unpacked = @transcoder.unpack(popped[1])
46
+ datum = unpacked['data']
47
+ # TODO: assert(unpacked[0] == RedPack::Consts::RESPONSE)
48
+ msg_id = datum[1]
49
+ err = datum[2]
50
+ retval = datum[3]
51
+ if err.nil? then
52
+ return retval
53
+ else
54
+ # FIXME: more-than-this?
55
+ raise Exception.new(err)
56
+ end
57
+ else
58
+ raise Exception.new('pop-timed-out')
59
+ end
60
+ end
61
+ end
62
+
63
+
64
+ def notify(method, *params)
65
+ v = [RedPack::Consts::NOTIFY, method, params]
66
+ return @redis.rpush(@queue_name, @transcoder.pack({'data' => v})) > 0
67
+ end
68
+
69
+
70
+ end
71
+
72
+
73
+ end
@@ -0,0 +1,23 @@
1
+
2
+ module RedPack
3
+
4
+ module Consts
5
+
6
+ REQUEST = 0
7
+ RESPONSE = 1
8
+ NOTIFY = 2
9
+
10
+ def self.queue_name(name)
11
+ return "redpack_request_queue:#{name}"
12
+ end
13
+
14
+ SESSION_ID_KEY = 'redpack_response_queue_index'
15
+
16
+ def self.new_response_queue_name(redis)
17
+ return redis.incr(RedPack::Consts::SESSION_ID_KEY)
18
+ end
19
+
20
+ end
21
+
22
+
23
+ end
@@ -0,0 +1,22 @@
1
+
2
+
3
+
4
+ module RedPack
5
+
6
+
7
+ module MethodDispatcher
8
+
9
+ def dispatch(ctx, method, params)
10
+ raise NotImplementedError.new
11
+ end
12
+
13
+ end
14
+
15
+
16
+
17
+ end
18
+
19
+
20
+
21
+ require 'redpack-ruby/method-dispatchers/simple'
22
+ require 'redpack-ruby/method-dispatchers/dotted'
@@ -0,0 +1,35 @@
1
+ # -*- mode: ruby; coding: utf-8; -*-
2
+
3
+ module RedPack
4
+
5
+ class DottedInstanceDispatcher
6
+ include RedPack::MethodDispatcher
7
+
8
+ def initialize(named_obj_dict, responder,
9
+ sep=/\./,
10
+ dispatch_delegate_klass=RedPack::SimpleInstanceDispatcher)
11
+ #
12
+ @responder = responder
13
+ @targets = {}
14
+ @sep = sep
15
+ named_obj_dict.each do |name, obj|
16
+ @targets[name] = dispatch_delegate_klass.new(obj, @responder)
17
+ end
18
+ end
19
+
20
+ def parse_dotted(method)
21
+ return method.split(@sep)
22
+ end
23
+
24
+ def dispatch(ctx, method, params)
25
+ a = self.parse_dotted(method)
26
+ obj_name, method_name = a[0], a[1]
27
+ if @targets.key? obj_name then
28
+ @targets[obj_name].dispatch(ctx, method_name, params)
29
+ else
30
+ @responder.fail(ctx, "no-such-object-named: #{obj_name} / #{method}")
31
+ end
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,53 @@
1
+
2
+ module RedPack
3
+
4
+ class SimpleHashDispatcher
5
+ include RedPack::MethodDispatcher
6
+
7
+ def initialize(dispatch_table, responder)
8
+ @dispatch_table = dispatch_table
9
+ @responder = responder
10
+ end
11
+
12
+ def dispatch(ctx, method, params)
13
+ if @dispatch_table.key? method then
14
+ begin
15
+ result = @dispatch_table[method].call(*params)
16
+ @responder.ok(ctx, result)
17
+ rescue
18
+ @responder.fail(ctx, $!)
19
+ end
20
+ else
21
+ @responder.fail(ctx, "no-such-dispatchable-key: #{method}")
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+
28
+
29
+ class SimpleInstanceDispatcher
30
+ include RedPack::MethodDispatcher
31
+
32
+ def initialize(target, responder)
33
+ @target = target
34
+ @responder = responder
35
+ end
36
+
37
+ def dispatch(ctx, method, params)
38
+ if @target.respond_to? method then
39
+ begin
40
+ result = @target.send(method, *params)
41
+ @responder.ok(ctx, result)
42
+ rescue
43
+ @responder.fail(ctx, $!)
44
+ end
45
+ else
46
+ @responder.fail(ctx, "no-such-method: #{method} on: #{target}")
47
+ end
48
+ end
49
+ end
50
+
51
+
52
+ end
53
+
@@ -0,0 +1,26 @@
1
+ module RedPack
2
+
3
+ class RequestContext
4
+
5
+ attr_accessor :msg_id
6
+ attr_accessor :response_queue_name
7
+ attr_accessor :request_or_notify
8
+
9
+
10
+ def initialize(response_queue_name, msg_id, request_or_notify)
11
+ @response_queue_name = response_queue_name
12
+ @msg_id = msg_id
13
+ @request_or_notify = request_or_notify
14
+ end
15
+
16
+ def REQUEST?
17
+ return self.request_or_notify == RedPack::Consts::REQUEST
18
+ end
19
+
20
+ def NOTIFY?
21
+ return self.request_or_notify == RedPack::Consts::NOTIFY
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,57 @@
1
+
2
+
3
+ module RedPack
4
+
5
+ module Responder
6
+ def ok(ctx, result)
7
+ end
8
+
9
+ def fail(ctx, err)
10
+ end
11
+
12
+ end
13
+
14
+
15
+
16
+ class NullResponder
17
+ include RedPack::Responder
18
+
19
+ def ok(ctx, result)
20
+ end
21
+
22
+ def fail(ctx, err)
23
+ raise err
24
+ end
25
+ end
26
+
27
+
28
+ class SyncRedisResponder
29
+ include RedPack::Responder
30
+
31
+ def initialize(redis, transcoder)
32
+ @redis = redis
33
+ @transcoder = transcoder
34
+ end
35
+
36
+ def send(response_queue_name, msg_id, err, retval)
37
+ v = {:data => [RedPack::Consts::RESPONSE, msg_id, err, retval]}
38
+ @redis.rpush(response_queue_name, @transcoder.pack(v))
39
+ end
40
+
41
+ def ok(ctx, result)
42
+ if ctx.REQUEST? then
43
+ self.send(ctx.response_queue_name, ctx.msg_id, nil, result)
44
+ end
45
+ end
46
+
47
+ def fail(ctx, err)
48
+ if ctx.REQUEST? then
49
+ self.send(ctx.response_queue_name, ctx.msg_id, err, nil)
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+
56
+
57
+ end
@@ -0,0 +1,44 @@
1
+ module RedPack
2
+
3
+ class Server
4
+ def initialize(redis, name, transcoder, dispatcher)
5
+ @queue_name = RedPack::Consts::queue_name(name)
6
+ @redis = redis
7
+ @transcoder = transcoder
8
+ @dispatcher = dispatcher
9
+ end
10
+
11
+ def listen(timeout=0)
12
+ a = @redis.blpop(@queue_name, timeout)
13
+ unless a.nil? then
14
+ unpacked = @transcoder.unpack(a[1])
15
+ response_queue_name = unpacked['return']
16
+ data = unpacked['data']
17
+ case data[0]
18
+ when RedPack::Consts::REQUEST then
19
+ process_REQUEST(response_queue_name, data[1], data[2], data[3])
20
+ when RedPack::Consts::NOTIFY then
21
+ process_NOTIFY(data[1], data[2])
22
+ when RedPack::Consts::RESPONSE then
23
+ raise Exception.new('plz dont do that...')
24
+ else
25
+ raise Exception.new("unknown code -- #{data[0]}")
26
+ end
27
+ end
28
+ end
29
+
30
+ def process_REQUEST(response_queue_name, msg_id, method, params)
31
+ ctx = RedPack::RequestContext.new(response_queue_name, msg_id, RedPack::Consts::REQUEST)
32
+ @dispatcher.dispatch(ctx, method, params)
33
+ end
34
+
35
+ def process_NOTIFY(method, params)
36
+ ctx = RedPack::RequestContext.new(nil, nil, RedPack::Consts::NOTIFY)
37
+ @dispatcher.dispatch(ctx, method, params)
38
+ end
39
+
40
+ end
41
+
42
+
43
+
44
+ end
@@ -0,0 +1,29 @@
1
+ module RedPack
2
+
3
+ module TransCoder
4
+ def pack(obj)
5
+ end
6
+
7
+ def unpack(bytes)
8
+ end
9
+ end
10
+
11
+
12
+
13
+ require 'bson'
14
+
15
+ class BsonTransCoder
16
+
17
+ include RedPack::TransCoder
18
+
19
+ def pack(obj)
20
+ return BSON.serialize(obj)
21
+ end
22
+
23
+ def unpack(obj)
24
+ return BSON.deserialize(obj)
25
+ end
26
+ end
27
+
28
+
29
+ end
@@ -0,0 +1,83 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "redpack-ruby"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["jonghyouk yun"]
12
+ s.date = "2012-02-11"
13
+ s.description = "Redis + MsgPack RPC Server, Client Library"
14
+ s.email = "ageldama@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "TODOs.txt",
27
+ "VERSION",
28
+ "lib/redpack-ruby.rb",
29
+ "lib/redpack-ruby/clients.rb",
30
+ "lib/redpack-ruby/consts.rb",
31
+ "lib/redpack-ruby/method-dispatchers.rb",
32
+ "lib/redpack-ruby/method-dispatchers/dotted.rb",
33
+ "lib/redpack-ruby/method-dispatchers/simple.rb",
34
+ "lib/redpack-ruby/request-context.rb",
35
+ "lib/redpack-ruby/responders.rb",
36
+ "lib/redpack-ruby/servers.rb",
37
+ "lib/redpack-ruby/transcoders.rb",
38
+ "redpack-ruby.gemspec",
39
+ "snippets/echo-client.rb",
40
+ "snippets/echo-server.rb",
41
+ "test/helper.rb",
42
+ "test/stop-test_redpack-ruby.rb",
43
+ "test/test_dotted-method-dispatcher.rb",
44
+ "test/test_simple-hash-dispatcher.rb",
45
+ "test/test_simple-instance-dispatcher.rb"
46
+ ]
47
+ s.homepage = "http://rubyforge.org/projects/redpack-ruby/"
48
+ s.licenses = ["MIT"]
49
+ s.require_paths = ["lib"]
50
+ s.rubygems_version = "1.8.15"
51
+ s.summary = "Redis + MsgPack RPC Server, Client Library"
52
+
53
+ if s.respond_to? :specification_version then
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
+ s.add_runtime_dependency(%q<redis>, ["~> 2.2.2"])
58
+ s.add_runtime_dependency(%q<bson>, [">= 0"])
59
+ s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
60
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
61
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
62
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
63
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
64
+ else
65
+ s.add_dependency(%q<redis>, ["~> 2.2.2"])
66
+ s.add_dependency(%q<bson>, [">= 0"])
67
+ s.add_dependency(%q<bson_ext>, [">= 0"])
68
+ s.add_dependency(%q<shoulda>, [">= 0"])
69
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
70
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
71
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
72
+ end
73
+ else
74
+ s.add_dependency(%q<redis>, ["~> 2.2.2"])
75
+ s.add_dependency(%q<bson>, [">= 0"])
76
+ s.add_dependency(%q<bson_ext>, [">= 0"])
77
+ s.add_dependency(%q<shoulda>, [">= 0"])
78
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
79
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
80
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
81
+ end
82
+ end
83
+
@@ -0,0 +1,12 @@
1
+
2
+ require 'redpack-ruby'
3
+ require 'redis'
4
+
5
+
6
+ redis = Redis.new
7
+ transcoder = RedPack::BsonTransCoder.new
8
+ client = RedPack::SimpleSyncClient.new(redis, 'echo', transcoder)
9
+
10
+
11
+ puts client.call('echo', 'hi?')
12
+ puts client.notify('echo', 'hi?')
@@ -0,0 +1,24 @@
1
+
2
+ require 'redpack-ruby'
3
+ require 'redis'
4
+
5
+
6
+ redis = Redis.new
7
+ transcoder = RedPack::BsonTransCoder.new
8
+ responder = RedPack::SyncRedisResponder.new(redis, transcoder)
9
+ disps = {
10
+ 'echo' => lambda{|s|
11
+ puts "echo'ed with [#{s}]"
12
+ return "ECHO: [#{s}]"
13
+ },
14
+
15
+ 'greet' => lambda{|s|
16
+ puts "greet'ed with [#{s}]"
17
+ return "HI, [#{s}]"
18
+ },
19
+ }
20
+ dispatcher = RedPack::SimpleHashDispatcher.new(disps, responder)
21
+ server = RedPack::Server.new(redis, 'echo', transcoder, dispatcher)
22
+ while true do
23
+ server.listen
24
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'redpack-ruby'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestRedpackRuby < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class DottedMethodDispatcherTest < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ responder = RedPack::NullResponder.new
8
+ kvs = {
9
+ 'num' => 42,
10
+ 'str' => 'foobar',
11
+ }
12
+ @dispatcher = RedPack::DottedInstanceDispatcher.new(kvs, responder)
13
+ end
14
+
15
+
16
+ def test_calls
17
+ @dispatcher.dispatch(nil, 'num.abs', [])
18
+ assert(true)
19
+ @dispatcher.dispatch(nil, 'num.**', [3])
20
+ assert(true)
21
+ @dispatcher.dispatch(nil, 'str.upcase', [])
22
+ assert(true)
23
+ end
24
+
25
+ end
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+
3
+ class TestSimpleHashDispatcher < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ responder = RedPack::NullResponder.new
8
+ kvs = {
9
+ :add => lambda {|a, b| return a+b},
10
+ :add1 => lambda {|a| return a.succ},
11
+ }
12
+ @dispatcher = RedPack::SimpleHashDispatcher.new(kvs, responder)
13
+ end
14
+
15
+
16
+ def test_calls
17
+ begin
18
+ @dispatcher.dispatch(nil, :add, [1, 2])
19
+ assert(true)
20
+
21
+ @dispatcher.dispatch(nil, :add1, [1])
22
+ assert(true)
23
+
24
+ @dispatcher.dispatch(nil, :mul, [1, 1])
25
+ assert(false, "never-reach")
26
+ rescue
27
+ assert(true)
28
+ end
29
+ end
30
+
31
+
32
+ end
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ class TestSimpleInstanceDispatcher < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ responder = RedPack::NullResponder.new
8
+ @dispatcher = RedPack::SimpleInstanceDispatcher.new(42, responder)
9
+ end
10
+
11
+
12
+ def test_calls
13
+ begin
14
+ @dispatcher.dispatch(nil, 'between?', [30, 50])
15
+ assert(true)
16
+
17
+ @dispatcher.dispatch(nil, 'succ')
18
+ assert(true)
19
+
20
+ @dispatcher.dispatch(nil, '+', [1])
21
+ assert(true)
22
+
23
+ @dispatcher.dispatch(nil, :mul, [1, 1])
24
+ assert(false, "never-reach")
25
+ rescue
26
+ assert(true)
27
+ end
28
+ end
29
+
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redpack-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jonghyouk yun
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: &81401090 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.2.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *81401090
25
+ - !ruby/object:Gem::Dependency
26
+ name: bson
27
+ requirement: &81399840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *81399840
36
+ - !ruby/object:Gem::Dependency
37
+ name: bson_ext
38
+ requirement: &81398610 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *81398610
47
+ - !ruby/object:Gem::Dependency
48
+ name: shoulda
49
+ requirement: &81397940 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *81397940
58
+ - !ruby/object:Gem::Dependency
59
+ name: rdoc
60
+ requirement: &81397380 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '3.12'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *81397380
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: &81396200 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.0.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *81396200
80
+ - !ruby/object:Gem::Dependency
81
+ name: jeweler
82
+ requirement: &81395460 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 1.8.3
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *81395460
91
+ description: Redis + MsgPack RPC Server, Client Library
92
+ email: ageldama@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ files:
99
+ - .document
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ - Rakefile
105
+ - TODOs.txt
106
+ - VERSION
107
+ - lib/redpack-ruby.rb
108
+ - lib/redpack-ruby/clients.rb
109
+ - lib/redpack-ruby/consts.rb
110
+ - lib/redpack-ruby/method-dispatchers.rb
111
+ - lib/redpack-ruby/method-dispatchers/dotted.rb
112
+ - lib/redpack-ruby/method-dispatchers/simple.rb
113
+ - lib/redpack-ruby/request-context.rb
114
+ - lib/redpack-ruby/responders.rb
115
+ - lib/redpack-ruby/servers.rb
116
+ - lib/redpack-ruby/transcoders.rb
117
+ - redpack-ruby.gemspec
118
+ - snippets/echo-client.rb
119
+ - snippets/echo-server.rb
120
+ - test/helper.rb
121
+ - test/stop-test_redpack-ruby.rb
122
+ - test/test_dotted-method-dispatcher.rb
123
+ - test/test_simple-hash-dispatcher.rb
124
+ - test/test_simple-instance-dispatcher.rb
125
+ homepage: http://rubyforge.org/projects/redpack-ruby/
126
+ licenses:
127
+ - MIT
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ segments:
139
+ - 0
140
+ hash: -900924625
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 1.8.15
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Redis + MsgPack RPC Server, Client Library
153
+ test_files: []