sinatra-rocketio-linda 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +4 -0
- data/README.md +3 -1
- data/lib/sinatra-rocketio-linda/tuple.rb +4 -36
- data/lib/sinatra-rocketio-linda/tuplespace.rb +4 -115
- data/lib/sinatra/rocketio/linda.rb +2 -0
- data/lib/sinatra/rocketio/linda/version.rb +1 -1
- data/sinatra-rocketio-linda.gemspec +1 -0
- data/test/test_tuple.rb +4 -22
- data/test/test_tuplespace.rb +6 -124
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bd0f0169cf2d5ffbd3ddb33d57c714f2c4cc60c
|
4
|
+
data.tar.gz: 009f53e47103bc7f183cc73c8e957467e097ca61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d6d37b957f7a54c21ce502c157c507cbb22f47954b290463784996c72bae67bc72ed3662d94bb7c923c8bba0faad0d3e7a6e1993acb343115f877d5519f6807
|
7
|
+
data.tar.gz: 2081757894b6210a146fbd42b0b997664076b4cd2d04ce26e76fb1cbcdda6405cf88e04e560cbb05ed1b61d7930701cf3a2b9daeeb516c809b4a6bdbf83e48b4
|
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -26,6 +26,7 @@ Linda is a coordination launguage for parallel programming.
|
|
26
26
|
|
27
27
|
* http://en.wikipedia.org/wiki/Linda_(coordination_language)
|
28
28
|
* http://ja.wikipedia.org/wiki/Linda
|
29
|
+
* https://github.com/shokai/linda-ruby#usage
|
29
30
|
|
30
31
|
|
31
32
|
### TupleSpace
|
@@ -42,6 +43,8 @@ Shared memory on Sinatra.
|
|
42
43
|
- watch( tuple, callback(tuple) )
|
43
44
|
- overwatch written Tuples in the TupleSpace
|
44
45
|
|
46
|
+
see details on https://github.com/shokai/linda-ruby#usage
|
47
|
+
|
45
48
|
|
46
49
|
Usage
|
47
50
|
-----
|
@@ -163,7 +166,6 @@ run test
|
|
163
166
|
|
164
167
|
Contributing
|
165
168
|
------------
|
166
|
-
|
167
169
|
1. Fork it
|
168
170
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
169
171
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
@@ -1,43 +1,11 @@
|
|
1
|
+
require "linda"
|
2
|
+
|
1
3
|
module Sinatra
|
2
4
|
module RocketIO
|
3
5
|
module Linda
|
4
|
-
|
5
|
-
class Tuple
|
6
|
-
attr_reader :data, :type, :expire_at
|
7
|
-
def initialize(data, opts={})
|
8
|
-
unless [Array, Hash].include? data.class
|
9
|
-
raise ArgumentError, 'argument must be instance of Array or Hash'
|
10
|
-
end
|
11
|
-
@data = data
|
12
|
-
@type = data.class
|
13
|
-
if opts[:expire].class == Fixnum and opts[:expire] > 0
|
14
|
-
@expire_at = Time.now + opts[:expire]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def match?(target)
|
19
|
-
raise ArgumentError, 'argument must be instance of Tuple' unless target.kind_of? self.class
|
20
|
-
return false if @type != target.type
|
21
|
-
if @type == Array
|
22
|
-
return false if @data.length > target.data.length
|
23
|
-
@data.each_with_index do |v,i|
|
24
|
-
return false if target.data[i] != v
|
25
|
-
end
|
26
|
-
return true
|
27
|
-
elsif @type == Hash
|
28
|
-
@data.each do |k,v|
|
29
|
-
return false if target.data[k] != v
|
30
|
-
end
|
31
|
-
return true
|
32
|
-
end
|
33
|
-
false
|
34
|
-
end
|
35
|
-
|
36
|
-
def to_s
|
37
|
-
@data.to_s
|
38
|
-
end
|
6
|
+
# use linda gem (https://rubygems.org/gems/linda)
|
7
|
+
class Tuple < ::Linda::Tuple
|
39
8
|
end
|
40
|
-
|
41
9
|
end
|
42
10
|
end
|
43
11
|
end
|
@@ -1,122 +1,11 @@
|
|
1
|
+
require "linda"
|
2
|
+
|
1
3
|
module Sinatra
|
2
4
|
module RocketIO
|
3
5
|
module Linda
|
4
|
-
|
5
|
-
class TupleSpace
|
6
|
-
include Enumerable
|
7
|
-
attr_reader :name
|
8
|
-
|
9
|
-
def initialize(name="__default__")
|
10
|
-
@name = name
|
11
|
-
@tuples = Array.new
|
12
|
-
@callbacks = Array.new
|
13
|
-
end
|
14
|
-
|
15
|
-
def each(&block)
|
16
|
-
@tuples.each do |tp|
|
17
|
-
yield tp
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def remove_callback(callback)
|
22
|
-
@callbacks.delete callback
|
23
|
-
end
|
24
|
-
|
25
|
-
def size
|
26
|
-
@tuples.size
|
27
|
-
end
|
28
|
-
|
29
|
-
DEFAULT_WRITE_OPTIONS = {
|
30
|
-
:expire => 300
|
31
|
-
}
|
32
|
-
|
33
|
-
def write(tuple, opts={})
|
34
|
-
raise ArgumentError, "options must be Hash" unless opts.kind_of? Hash
|
35
|
-
DEFAULT_WRITE_OPTIONS.each do |k,v|
|
36
|
-
opts[k] = v unless opts.include? k
|
37
|
-
end
|
38
|
-
tuple = Tuple.new tuple, opts unless tuple.kind_of? Tuple
|
39
|
-
calleds = []
|
40
|
-
taked = nil
|
41
|
-
@callbacks.each do |callback|
|
42
|
-
next unless callback[:tuple].match? tuple
|
43
|
-
callback[:callback].call tuple
|
44
|
-
calleds.push callback unless callback[:type] == :watch
|
45
|
-
if callback[:type] == :take
|
46
|
-
taked = tuple
|
47
|
-
break
|
48
|
-
end
|
49
|
-
end
|
50
|
-
calleds.each do |called|
|
51
|
-
@callbacks.delete called
|
52
|
-
end
|
53
|
-
@tuples.unshift tuple unless taked
|
54
|
-
tuple
|
55
|
-
end
|
56
|
-
|
57
|
-
def read(tuple, &block)
|
58
|
-
tuple = Tuple.new tuple unless tuple.kind_of? Tuple
|
59
|
-
@tuples.each do |t|
|
60
|
-
if tuple.match? t
|
61
|
-
if block_given?
|
62
|
-
block.call t
|
63
|
-
return
|
64
|
-
else
|
65
|
-
return t
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
if block_given?
|
70
|
-
callback = {:type => :read, :callback => block, :tuple => tuple}
|
71
|
-
@callbacks.push callback
|
72
|
-
return callback
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def take(tuple, &block)
|
77
|
-
tuple = Tuple.new tuple unless tuple.kind_of? Tuple
|
78
|
-
matched_tuple = nil
|
79
|
-
@tuples.each do |t|
|
80
|
-
if tuple.match? t
|
81
|
-
matched_tuple = t
|
82
|
-
break
|
83
|
-
end
|
84
|
-
end
|
85
|
-
if matched_tuple
|
86
|
-
@tuples.delete matched_tuple
|
87
|
-
if block_given?
|
88
|
-
block.call matched_tuple
|
89
|
-
else
|
90
|
-
return matched_tuple
|
91
|
-
end
|
92
|
-
else
|
93
|
-
if block_given?
|
94
|
-
callback = {:type => :take, :callback => block, :tuple => tuple}
|
95
|
-
@callbacks.push callback
|
96
|
-
return callback
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def watch(tuple, &block)
|
102
|
-
raise ArgumentError, "block not given" unless block_given?
|
103
|
-
tuple = Tuple.new tuple unless tuple.kind_of? Tuple
|
104
|
-
callback = {:type => :watch, :callback => block, :tuple => tuple}
|
105
|
-
@callbacks.unshift callback
|
106
|
-
callback
|
107
|
-
end
|
108
|
-
|
109
|
-
def check_expire
|
110
|
-
expires = []
|
111
|
-
self.each do |tuple|
|
112
|
-
expires.push(tuple) if tuple.expire_at and tuple.expire_at < Time.now
|
113
|
-
end
|
114
|
-
expires.each do |tuple|
|
115
|
-
@tuples.delete tuple
|
116
|
-
end
|
117
|
-
end
|
6
|
+
# use on-memory TupleSpace from linda gem (https://rubygems.org/gems/linda)
|
7
|
+
class TupleSpace < ::Linda::TupleSpace
|
118
8
|
end
|
119
|
-
|
120
9
|
end
|
121
10
|
end
|
122
11
|
end
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency "thin"
|
25
25
|
spec.add_development_dependency "haml"
|
26
26
|
|
27
|
+
spec.add_dependency "linda"
|
27
28
|
spec.add_dependency "sinatra-rocketio"
|
28
29
|
spec.add_dependency "event_emitter"
|
29
30
|
spec.add_dependency "sinatra"
|
data/test/test_tuple.rb
CHANGED
@@ -1,29 +1,11 @@
|
|
1
1
|
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
2
|
require 'sinatra-rocketio-linda/tuple'
|
3
|
+
require 'linda/test/tuple'
|
3
4
|
|
4
5
|
class TestTuple < MiniTest::Test
|
5
|
-
|
6
|
-
tuple = Sinatra::RocketIO::Linda::Tuple.new [1,2,3]
|
7
|
-
assert tuple.match? Sinatra::RocketIO::Linda::Tuple.new [1,2,3]
|
8
|
-
assert tuple.match? Sinatra::RocketIO::Linda::Tuple.new [1,2,3,4]
|
9
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new [1,2])
|
10
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new [1,"a",3])
|
11
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new :a => 1, :b => 2)
|
12
|
-
tuple = Sinatra::RocketIO::Linda::Tuple.new ["a","b","c"]
|
13
|
-
assert tuple.match? Sinatra::RocketIO::Linda::Tuple.new ["a","b","c"]
|
14
|
-
assert tuple.match? Sinatra::RocketIO::Linda::Tuple.new ["a","b","c","d","efg",123,"h"]
|
15
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new ["a","b"])
|
16
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new ["a","b",789])
|
17
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new :foo => 1, :bar => 2)
|
18
|
-
end
|
6
|
+
include Linda::Test::Tuple
|
19
7
|
|
20
|
-
def
|
21
|
-
|
22
|
-
assert tuple.match? Sinatra::RocketIO::Linda::Tuple.new :a => 1, :b => 2
|
23
|
-
assert tuple.match? Sinatra::RocketIO::Linda::Tuple.new :a => 1, :b => 2, :c => 3
|
24
|
-
assert tuple.match? Sinatra::RocketIO::Linda::Tuple.new :a => 1, :b => 2, :c => {:foo => "bar"}
|
25
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new :a => 0, :b => 2)
|
26
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new :b => 2, :c => 3)
|
27
|
-
assert !tuple.match?(Sinatra::RocketIO::Linda::Tuple.new [1,2,3])
|
8
|
+
def target_tuple
|
9
|
+
Sinatra::RocketIO::Linda::Tuple
|
28
10
|
end
|
29
11
|
end
|
data/test/test_tuplespace.rb
CHANGED
@@ -1,134 +1,16 @@
|
|
1
1
|
require File.expand_path 'test_helper', File.dirname(__FILE__)
|
2
2
|
require 'sinatra-rocketio-linda/tuple'
|
3
3
|
require 'sinatra-rocketio-linda/tuplespace'
|
4
|
+
require 'linda/test/tuplespace'
|
4
5
|
|
5
6
|
class TestTupleSpace < MiniTest::Test
|
7
|
+
include Linda::Test::TupleSpace
|
6
8
|
|
7
|
-
def
|
8
|
-
|
9
|
+
def target_tuple
|
10
|
+
Sinatra::RocketIO::Linda::Tuple
|
9
11
|
end
|
10
12
|
|
11
|
-
def
|
12
|
-
|
13
|
-
@space.write Sinatra::RocketIO::Linda::Tuple.new [1,2,3]
|
14
|
-
assert_equal @space.size, 1
|
15
|
-
assert_equal @space.read([1,2]).data, [1,2,3]
|
16
|
-
@space.write :a => 1, :b => 2, :c => 999
|
17
|
-
@space.write :a => 1, :b => 2, :c => 3
|
18
|
-
assert_equal @space.size, 3
|
19
|
-
assert_equal @space.read(:a => 1, :c => 999).data, {:a => 1, :b => 2, :c => 999}
|
20
|
-
assert_equal @space.read(:a => 1, :c => 999).data, {:a => 1, :b => 2, :c => 999}
|
21
|
-
assert_equal @space.read(:a => 1).data, {:a => 1, :b => 2, :c => 3}
|
13
|
+
def target_tuplespace
|
14
|
+
Sinatra::RocketIO::Linda::TupleSpace
|
22
15
|
end
|
23
|
-
|
24
|
-
def test_write_read_callback
|
25
|
-
assert_equal @space.size, 0
|
26
|
-
_tuple1 = nil
|
27
|
-
@space.read [1,2] do |tuple|
|
28
|
-
_tuple1 = tuple
|
29
|
-
end
|
30
|
-
_tuple2 = nil
|
31
|
-
@space.read [1,"a"] do |tuple|
|
32
|
-
_tuple2 = tuple
|
33
|
-
end
|
34
|
-
_tuple3 = nil
|
35
|
-
@space.read [1,2,3] do |tuple|
|
36
|
-
_tuple3 = tuple
|
37
|
-
end
|
38
|
-
@space.write [1,2,3]
|
39
|
-
assert_equal _tuple1.data, [1,2,3]
|
40
|
-
assert_equal _tuple2, nil
|
41
|
-
assert_equal _tuple3.data, [1,2,3]
|
42
|
-
assert_equal @space.read([1]).data, [1,2,3]
|
43
|
-
assert_equal @space.size, 1
|
44
|
-
@space.write [1,2,4]
|
45
|
-
assert_equal _tuple1.data, [1,2,3]
|
46
|
-
assert_equal @space.size, 2
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_take
|
50
|
-
assert_equal @space.size, 0
|
51
|
-
1.upto(3) do |i|
|
52
|
-
@space.write [1,2,3,"a"*i]
|
53
|
-
end
|
54
|
-
assert_equal @space.size, 3
|
55
|
-
assert_equal @space.take([1,2,3]).data, [1,2,3,"aaa"]
|
56
|
-
assert_equal @space.size, 2
|
57
|
-
@space.write :a => 1, :b => 2, :c => 3
|
58
|
-
assert_equal @space.size, 3
|
59
|
-
assert_equal @space.take([1,3]), nil
|
60
|
-
assert_equal @space.take(:a => 1, :b => 2, :c => 4), nil
|
61
|
-
assert_equal @space.take([1,2,3]).data, [1,2,3,"aa"]
|
62
|
-
assert_equal @space.size, 2
|
63
|
-
assert_equal @space.take([1,2,3]).data, [1,2,3,"a"]
|
64
|
-
assert_equal @space.size, 1
|
65
|
-
assert_equal @space.take(:b => 2, :a => 1).data, {:a => 1, :b => 2, :c => 3}
|
66
|
-
assert_equal @space.size, 0
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_take_callback
|
70
|
-
assert_equal @space.size, 0
|
71
|
-
_tuple1 = nil
|
72
|
-
@space.take [1,2] do |tuple|
|
73
|
-
_tuple1 = tuple
|
74
|
-
end
|
75
|
-
_tuple2 = nil
|
76
|
-
@space.take [1,"a"] do |tuple|
|
77
|
-
_tuple2 = tuple
|
78
|
-
end
|
79
|
-
_tuple3 = nil
|
80
|
-
@space.read [1,2,3] do |tuple|
|
81
|
-
_tuple3 = tuple
|
82
|
-
end
|
83
|
-
_tuple4 = nil
|
84
|
-
@space.take [1,2,3] do |tuple|
|
85
|
-
_tuple4 = tuple
|
86
|
-
end
|
87
|
-
1.upto(3) do |i|
|
88
|
-
@space.write [1,2,3,"a"*i]
|
89
|
-
end
|
90
|
-
assert_equal @space.size, 1
|
91
|
-
assert_equal _tuple1.data, [1,2,3,"a"]
|
92
|
-
assert_equal _tuple2, nil
|
93
|
-
assert_equal _tuple3.data, [1,2,3,"aa"]
|
94
|
-
assert_equal _tuple4.data, [1,2,3,"aa"]
|
95
|
-
assert_equal @space.take([1]).data, [1,2,3,"aaa"]
|
96
|
-
assert_equal @space.size, 0
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_watch
|
100
|
-
assert_equal @space.size, 0
|
101
|
-
_tuple1 = nil
|
102
|
-
@space.take [1] do |tuple|
|
103
|
-
_tuple1 = tuple
|
104
|
-
end
|
105
|
-
results = []
|
106
|
-
@space.watch [1,2] do |tuple|
|
107
|
-
results << tuple
|
108
|
-
end
|
109
|
-
@space.write [1,2,3]
|
110
|
-
@space.write [1,2,"aa"]
|
111
|
-
@space.write [1,"a",3]
|
112
|
-
assert_equal _tuple1.data, [1,2,3]
|
113
|
-
assert_equal @space.size, 2
|
114
|
-
assert_equal results.size, 2
|
115
|
-
assert_equal results[0].data, [1,2,3]
|
116
|
-
assert_equal results[1].data, [1,2,"aa"]
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_tuple_expire
|
120
|
-
@space.write [1,2,999], :expire => false
|
121
|
-
@space.write [1,2,3], :expire => 3
|
122
|
-
@space.write [1,2,"a","b"], :expire => 2
|
123
|
-
assert_equal @space.size, 3
|
124
|
-
sleep 2
|
125
|
-
@space.check_expire
|
126
|
-
assert_equal @space.size, 2
|
127
|
-
assert_equal @space.take([1,2]).data, [1,2,3]
|
128
|
-
assert_equal @space.size, 1
|
129
|
-
sleep 1
|
130
|
-
assert_equal @space.take([1,2]).data, [1,2,999]
|
131
|
-
assert_equal @space.size, 0
|
132
|
-
end
|
133
|
-
|
134
16
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-rocketio-linda
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sho Hashimoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: linda
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: sinatra-rocketio
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|