Permalink
Browse files

Polished VM lifecycle, Reactor and Channels, and now Actors run OK

  • Loading branch information...
1 parent ec68124 commit 4c18176b7eb351d8e5d8f2700256f70ae76baa74 @krakatoa krakatoa committed Nov 11, 2012
Showing with 47 additions and 30 deletions.
  1. +2 −1 Gemfile
  2. +1 −9 Gemfile.lock
  3. +16 −2 lib/actor.rb
  4. +5 −1 lib/channel.rb
  5. +2 −2 lib/panzmq.rb
  6. +9 −6 lib/reactor.rb
  7. +12 −9 lib/vm.rb
View
@@ -8,7 +8,8 @@ group :cli do
gem "servolux"
end
-gem "ma-zmq", :path => "/home/krakatoa/workspace/al-nattahnam/ma-zmq"
+gem "ffi-rzmq"
+#gem "ma-zmq", :path => "/home/krakatoa/workspace/al-nattahnam/ma-zmq"
gem "cucub-protocol", '0.0.1', :path => "/home/krakatoa/workspace/al-nattahnam/cucub/protocol", :require => 'cucub-protocol'
# Add dependencies to develop your gem here.
View
@@ -5,18 +5,10 @@ PATH
inifile
msgpack
-PATH
- remote: /home/krakatoa/workspace/al-nattahnam/ma-zmq
- specs:
- ma-zmq (0.0.2)
- eventmachine (= 1.0.0.beta.4)
- ffi-rzmq (= 0.9.3)
-
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
- eventmachine (1.0.0.beta.4)
ffi (1.1.5)
ffi-rzmq (0.9.3)
ffi
@@ -54,8 +46,8 @@ PLATFORMS
DEPENDENCIES
bundler (~> 1.2.1)
cucub-protocol (= 0.0.1)!
+ ffi-rzmq
jeweler (~> 1.8.4)
- ma-zmq!
rspec
servolux
simplecov
View
@@ -1,17 +1,25 @@
+require 'fiber'
+
module Cucub
class Actor
def initialize
- state = :alive
+ @state = :alive
set_fiber
+ self
end
def set_fiber
@fiber = Fiber.new { |args, origin|
while alive?
- puts "> at Actor"
+ $stdout.puts "> at Actor"
msg = args.shift
puts msg
+ #if msg == "exit"
+ # self.kill
+ # origin.yield
+ #end
+
# play
origin.transfer
@@ -24,7 +32,13 @@ def wire(msg)
end
def kill
+ $stdout.puts "Suiciding me."
@state = :dead
+ #@fiber.resume
+ end
+
+ def fiber_state
+ @fiber.alive?
end
def alive?
View
@@ -40,6 +40,7 @@ def vm_inner_inbound_start
#@socket.connect :ipc, "/tmp/cucub-inner-inbound.sock"
@socket = PanZMQ::Pull.new
@socket.connect "ipc:///tmp/cucub-inner-inbound.sock"
+ @socket
end
def vm_inner_outbound_start
@@ -53,6 +54,7 @@ def vm_inner_outbound_start
#@socket.connect :ipc, "/tmp/cucub-inner-outbound.sock"
@socket = PanZMQ::Push.new
@socket.connect "ipc:///tmp/cucub-inner-outbound.sock"
+ @socket
end
#def recv_string
@@ -79,10 +81,12 @@ def close
end
def self.shutdown!
+ $stdout.puts "1. Shutting down Inbound Socket"
@@vm_inner_inbound.close if defined?(@@vm_inner_inbound)
+ $stdout.puts "2. Shutting down Outbound Socket"
@@vm_inner_outbound.close if defined?(@@vm_inner_outbound)
- #MaZMQ.terminate
+ $stdout.puts "3. Terminate PanZMQ"
PanZMQ.terminate
end
View
@@ -15,7 +15,7 @@ def self.terminate
class Pull
def initialize
@socket = PanZMQ.context.socket ZMQ::PULL
- #@socket.setsockopt(ZMQ::LINGER, 0)
+ @socket.setsockopt(ZMQ::LINGER, 0)
@messages = []
@alive = true
end
@@ -47,7 +47,7 @@ def close
class Push
def initialize
@socket = PanZMQ.context.socket ZMQ::PUSH
- #@socket.setsockopt(ZMQ::LINGER, 0)
+ @socket.setsockopt(ZMQ::LINGER, 0)
end
def connect(params)
View
@@ -43,17 +43,20 @@ def plug_actor(actor)
# maybe send plugged event ?
end
+ def kill_actors
+ $stdout.puts "4. Kill Actors"
+ @actors.each { |actor| actor.kill }
+ $stdout.puts "5. Killed"
+ end
+
def init_channels
@inbound = Cucub::Channel.vm_inner_inbound
- @outbound = Cucub::Channel.vm_inner_outbound
+ #@outbound = Cucub::Channel.vm_inner_outbound
end
def stop
- $stdout.puts "Stopping gracefully the reactor."
- @running = false
- @actors.each { |actor| actor.kill }
- #PanZMQ.terminate
- #EM.stop
+ Cucub::Channel.shutdown!
+ kill_actors
exit
end
end
View
@@ -7,17 +7,20 @@ class VM
def initialize
#@dispatcher = Cucub::Dispatcher.instance
+ @running = true
end
def start!(vm_opts={})
- @config_filepath = vm_opts[:config]
- self.init_classes
-
- $stdout.puts Cucub::ObjectsHub.instance.objects.inspect
- self.init_objects(vm_opts[:initializer]) # this will go inside worker instance
- $stdout.puts Cucub::ObjectsHub.instance.objects.inspect
-
- self.init_reactor
+ if @running
+ @config_filepath = vm_opts[:config]
+ self.init_classes
+
+ $stdout.puts Cucub::ObjectsHub.instance.objects.inspect
+ self.init_objects(vm_opts[:initializer]) # this will go inside worker instance
+ $stdout.puts Cucub::ObjectsHub.instance.objects.inspect
+
+ self.init_reactor
+ end
end
def init_classes
@@ -41,7 +44,7 @@ def init_reactor
end
def shutdown!
- Cucub::Channel.shutdown!
+ @running = false
Cucub::Reactor.instance.stop
end

0 comments on commit 4c18176

Please sign in to comment.