Skip to content
Browse files

Introduced Cucub::Reactor and Cucub::Channel for VM

  • Loading branch information...
1 parent 073ad9c commit dbcf53baf29ae3d9b113ab18465bc34e03bb667b @krakatoa krakatoa committed Oct 24, 2012
Showing with 104 additions and 5 deletions.
  1. +67 −0 lib/channel.rb
  2. +2 −0 lib/cucub-vm.rb
  3. +27 −0 lib/reactor.rb
  4. +8 −5 lib/vm.rb
View
67 lib/channel.rb
@@ -0,0 +1,67 @@
+module Cucub
+ class Channel
+ def initialize(kind)
+ @kind = kind
+ case @kind
+ when :vm_inner_inbound
+ vm_inner_inbound_start
+ when :vm_inner_outbound
+ vm_inner_outbound_start
+ end
+ end
+
+ def self.vm_inner_inbound
+ return @@vm_inner_inbound if defined?(@@vm_inner_inbound) and @@vm_inner_inbound.is_a? Cucub::Channel
+
+ @@vm_inner_inbound = Cucub::Channel.new(:vm_inner_inbound)
+ end
+
+ def self.vm_inner_outbound
+ return @@vm_inner_outbound if defined?(@@vm_inner_outbound) and @@vm_inner_outbound.is_a? Cucub::Channel
+
+ @@vm_inner_outbound = Cucub::Channel.new(:vm_inner_outbound)
+ end
+
+ def self.channels
+ channels = []
+ channels << @@vm_inner_inbound if defined?(@@vm_inner_inbound)
+ channels << @@vm_inner_outbound if defined?(@@vm_inner_outbound)
+ channels
+ end
+
+ def vm_inner_inbound_start
+ # a ZMQ PUSH-PULL socket which connects to the local server as a 'puller', so workers can consume messages.
+
+ # It works by connecting to an IPC socket. In a future, it might be considered to
+ # enable tcp communication, so workers can be outside the local server.
+
+ $stdout.puts "Connecting to the Inner Inbound (PULL) socket"
+ @socket = MaZMQ::Pull.new
+ @socket.connect :ipc, "/tmp/cucub-inner-inbound.sock"
+ end
+
+ def vm_inner_outbound_start
+ # a ZMQ PUSH-PULL socket which connects to the local server as a 'pusher', so workers can push their messages.
+
+ # It works by connecting to an IPC socket. In a future, it might be considered to
+ # enable tcp communication, so workers can be outside the local server.
+
+ $stdout.puts "Connecting to the Inner Inbound (PUSH) socket"
+ @socket = MaZMQ::Push.new
+ @socket.connect :ipc, "/tmp/cucub-inner-outbound.sock"
+ end
+
+ def close
+ $stdout.puts "Closing #{@kind} socket."
+ @socket.close
+ end
+
+ def self.shutdown!
+ @@vm_inner_inbound.close if defined?(@@vm_inner_inbound)
+ @@vm_inner_outbound.close if defined?(@@vm_inner_outbound)
+
+ MaZMQ.terminate
+ end
+
+ end
+end
View
2 lib/cucub-vm.rb
@@ -2,4 +2,6 @@
require './lib/object'
require './lib/objects_hub'
+require './lib/channel'
+require './lib/reactor'
require './lib/vm'
View
27 lib/reactor.rb
@@ -0,0 +1,27 @@
+require 'singleton'
+
+module Cucub
+ class Reactor
+ include Singleton
+
+ def run
+ EM.epoll
+ EM.run do
+ $stdout.puts "inside reactor"
+ self.init_channels
+ #Cucub::Channel.channels.each {|channel|
+ # $stdout.puts channel.inspect
+ #}
+ end
+ end
+
+ def init_channels
+ @inbound = Cucub::Channel.vm_inner_inbound
+ @outbound = Cucub::Channel.vm_inner_outbound
+ end
+
+ def stop
+ EM.stop
+ end
+ end
+end
View
13 lib/vm.rb
@@ -16,9 +16,8 @@ def start!(vm_opts={})
$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
- EM.run do
- $stdout.puts "inside reactor"
- end
+
+ self.init_reactor
end
def init_classes
@@ -37,9 +36,13 @@ def init_objects(boot_file)
require boot_file
end
+ def init_reactor
+ Cucub::Reactor.instance.run
+ end
+
def shutdown!
- EM.stop
- #@dispatcher.stop
+ Cucub::Channel.shutdown!
+ Cucub::Reactor.instance.stop
end
def config_filepath

0 comments on commit dbcf53b

Please sign in to comment.
Something went wrong with that request. Please try again.