pb_actor 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 769f131d2a68aac7e498d83a94e10698b93baa9b
4
- data.tar.gz: 3fa73cbcab4e355beb9ee81c0c930722476797d5
3
+ metadata.gz: 3d0cf1ea9d8a2f2a20ad8f90ef852e74182c42ca
4
+ data.tar.gz: 8077c6562b7ff8c7f1cc60aea93884f907415dfb
5
5
  SHA512:
6
- metadata.gz: 1afb16fa5de4f6e2b504f02391505e3089285b0d32f2ba4a607a0f22b7c2a926fc6be8f7535ad9967ec00af2f2a7901ba14d016257361b5909fd98d115956c5c
7
- data.tar.gz: bc702fb0db8595fe9fa424ddda87e92a5987aaf88a62a20a9a3a11ad34122a1fbbedee729a51edc2a4888cee9963a3d5546fa078400f61a1bec30a69874b26fc
6
+ metadata.gz: a812dc4a3616d5cbd13fb03ebafc5dba50b9cacd2611ecda4a95cecf61c43363895182af8453cfbc0aff01140432aa1bf8b5e6381e7bfb36e229aa146f3b7c9e
7
+ data.tar.gz: 2fe7ccd11918ed2f751130e3f957dd89dc08cd377fff7d86e5c8a8a2c3671aa4a5e35d2106edbd513fe838acd8e81a35d7075d0acce8515a50c895c5e42227c4
@@ -0,0 +1,9 @@
1
+ module PbActor
2
+ class AsyncProxy < BasicProxy
3
+ def method_missing method, *args, &blk
4
+ super
5
+ Message.send [:async_method_call, nil, method, *args], @wr
6
+ nil
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'timeout'
2
+
3
+ module PbActor
4
+ class BasicProxy
5
+ def initialize origin, pid, wr, rd
6
+ @origin, @pid, @wr, @rd = origin, pid, wr, rd
7
+ end
8
+
9
+ def alive?
10
+ # Have any other way to check a process status?
11
+ !timeout(0.001){@rd.eof?}
12
+ rescue Timeout::Error => e
13
+ true
14
+ end
15
+
16
+ def method_missing method, *args, &blk
17
+ raise ArgumentError, 'actor not support block' if blk
18
+ raise DeadActorError, 'dead actor call' unless alive?
19
+ end
20
+
21
+ def to_s
22
+ "#{self.class}(#{@origin.class})"
23
+ end
24
+
25
+ undef send, public_send
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ module PbActor
2
+ class Future
3
+ def initialize id, wr, rd
4
+ @id = id
5
+ @wr= wr
6
+ @rd = rd
7
+ end
8
+
9
+ def value
10
+ loop do
11
+ Message.send [:future_value_get, @id], @wr
12
+ type, value = Message.recv @rd
13
+ if type == :future_value
14
+ break value
15
+ else
16
+ sleep 0.01
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ require 'pb_actor/future'
2
+ require 'securerandom'
3
+
4
+ module PbActor
5
+ class FutureProxy < BasicProxy
6
+ def method_missing method, *args, &blk
7
+ super
8
+ id = SecureRandom.uuid
9
+ Message.send [:future_method_call, id, method, *args], @wr
10
+ Future.new id, @wr, @rd
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module PbActor
2
+ module Message
3
+ class << self
4
+ def send msg, wr
5
+ Marshal.dump(msg, wr)
6
+ rescue Errno::EPIPE => e
7
+ raise DeadActorError, 'dead actor call'
8
+ end
9
+
10
+ def recv rd
11
+ Marshal.load rd
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,52 +1,9 @@
1
- require 'timeout'
2
- require 'securerandom'
1
+ require 'pb_actor/message'
2
+ require 'pb_actor/basic_proxy'
3
+ require 'pb_actor/async_proxy'
4
+ require 'pb_actor/future_proxy'
3
5
 
4
6
  module PbActor
5
- class DeadActorError < StandardError
6
- end
7
-
8
- module Message
9
- class << self
10
- def send msg, wr
11
- Marshal.dump(msg, wr)
12
- rescue Errno::EPIPE => e
13
- raise DeadActorError, 'dead actor call'
14
- end
15
-
16
- def recv rd
17
- Marshal.load rd
18
- end
19
- end
20
- end
21
-
22
- class BasicProxy
23
- def initialize origin, pid, wr, rd
24
- @origin, @pid, @wr, @rd = origin, pid, wr, rd
25
- end
26
-
27
- def alive?
28
- begin
29
- # Have any other way to check a process status?
30
- timeout(0.001){Process.wait}
31
- rescue Timeout::Error => e
32
- end
33
- Process.kill(0, @pid) == 1
34
- rescue Errno::ESRCH, Errno::ECHILD => e
35
- false
36
- end
37
-
38
- def method_missing method, *args, &blk
39
- raise ArgumentError, 'actor not support block' if blk
40
- raise DeadActorError, 'dead actor call' unless alive?
41
- end
42
-
43
- def to_s
44
- "#{self.class}(#{@origin.class})"
45
- end
46
-
47
- undef send, public_send
48
- end
49
-
50
7
  class Proxy < BasicProxy
51
8
  def initialize origin
52
9
  @origin = origin
@@ -90,11 +47,11 @@ module PbActor
90
47
  end
91
48
 
92
49
  def async
93
- AsyncProxy.new @origin, @pid, @wr, @rd
50
+ @async ||= AsyncProxy.new @origin, @pid, @wr, @rd
94
51
  end
95
52
 
96
53
  def future
97
- FutureProxy.new @origin, @pid, @wr, @rd
54
+ @future ||= FutureProxy.new @origin, @pid, @wr, @rd
98
55
  end
99
56
 
100
57
  def terminate
@@ -109,41 +66,4 @@ module PbActor
109
66
  nil
110
67
  end
111
68
  end
112
-
113
- class AsyncProxy < BasicProxy
114
- def method_missing method, *args, &blk
115
- super
116
- Message.send [:async_method_call, nil, method, *args], @wr
117
- nil
118
- end
119
- end
120
-
121
- class FutureProxy < BasicProxy
122
- def method_missing method, *args, &blk
123
- super
124
- id = SecureRandom.uuid
125
- Message.send [:future_method_call, id, method, *args], @wr
126
- Future.new id, @wr, @rd
127
- end
128
- end
129
-
130
- class Future
131
- def initialize id, wr, rd
132
- @id = id
133
- @wr= wr
134
- @rd = rd
135
- end
136
-
137
- def value
138
- loop do
139
- Message.send [:future_value_get, @id], @wr
140
- type, value = Message.recv @rd
141
- if type == :future_value
142
- break value
143
- else
144
- sleep 0.01
145
- end
146
- end
147
- end
148
- end
149
69
  end
@@ -1,3 +1,3 @@
1
1
  module PbActor
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/pb_actor.rb CHANGED
@@ -1,11 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require "pb_actor/version"
3
- require "pb_actor/proxy"
4
3
 
5
4
  module PbActor
6
5
  class << self
7
6
  def included base
8
- base.send :include, InstanceMethods
9
7
  base.send :extend, ClassMethods
10
8
  end
11
9
  end
@@ -19,6 +17,8 @@ module PbActor
19
17
  end
20
18
  end
21
19
 
22
- module InstanceMethods
20
+ class DeadActorError < StandardError
23
21
  end
24
22
  end
23
+
24
+ require "pb_actor/proxy"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pb_actor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - jjy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-10 00:00:00.000000000 Z
11
+ date: 2013-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -51,6 +51,11 @@ files:
51
51
  - README.md
52
52
  - Rakefile
53
53
  - lib/pb_actor.rb
54
+ - lib/pb_actor/async_proxy.rb
55
+ - lib/pb_actor/basic_proxy.rb
56
+ - lib/pb_actor/future.rb
57
+ - lib/pb_actor/future_proxy.rb
58
+ - lib/pb_actor/message.rb
54
59
  - lib/pb_actor/proxy.rb
55
60
  - lib/pb_actor/version.rb
56
61
  - pb_actor.gemspec
@@ -76,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
81
  version: '0'
77
82
  requirements: []
78
83
  rubyforge_project:
79
- rubygems_version: 2.0.5
84
+ rubygems_version: 2.0.3
80
85
  signing_key:
81
86
  specification_version: 4
82
87
  summary: Just for fun.