Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of git@github.com:ezmobius/nanite

* 'master' of git@github.com:ezmobius/nanite:
  test added to dispatcher_spec for default actor action
  added default index option for actors
  Need yaml
  Added support for yaml marshalling, and the ability for optional marshallers to be added and used reliably. Optional support for cascading deserialization by un-setting the format option (set to nil)
  • Loading branch information...
commit 66c2b22d26e6514acf8e7c5174e21624f1f86ed2 2 parents 22796d0 + 2f4dcc6
Ezra Zygmuntowicz authored
6 lib/nanite.rb
View
@@ -1,6 +1,9 @@
require 'rubygems'
require 'amqp'
require 'mq'
+require 'json'
+require 'logger'
+
$:.unshift File.dirname(__FILE__)
require 'extlib'
require 'nanite/packets'
@@ -13,9 +16,6 @@
require 'nanite/marshal'
require 'nanite/console'
require 'nanite/agent'
-require 'json'
-require 'logger'
-
module Nanite
2  lib/nanite/agent.rb
View
@@ -1,3 +1,5 @@
+require 'yaml'
+
module Nanite
class Agent
attr_reader :identity, :format, :status_proc, :results, :root, :log_dir, :vhost, :file_root, :files, :host
2  lib/nanite/dispatcher.rb
View
@@ -46,7 +46,7 @@ def dispatch_request(req)
_, prefix, meth = req.type.split('/')
begin
actor = @actors[prefix]
- res = actor.send(meth, req.payload)
+ res = actor.send((meth.nil? ? "index" : meth), req.payload)
rescue Exception => e
res = "#{e.class.name}: #{e.message}\n #{e.backtrace.join("\n ")}"
end
47 lib/nanite/marshal.rb
View
@@ -1,24 +1,47 @@
+require 'json'
+require 'yaml'
+
module Nanite
- # Data transferred in nanites cluster must be serialized, right?
- # Nanite currently supports native Ruby marshalling and JSON to
- # interoperate with AMQP clients written in other languages.
class Agent
+ class DeserializationFailed < StandardError
+ attr_accessor :packet, :serializers
+ def initialize(packet, serializers)
+ @packet, @serializers = packet, serializers
+ super("Could not deserialize #{packet.inspect} using #{serializers}")
+ end
+ end
- def dump_packet(packet)
- if format == :json
- packet.to_json
+ def serializer
+ @serializer ||= case format
+ when :json
+ JSON
+ when :marshal
+ Marshal
+ when :yaml
+ YAML
else
- Marshal.dump(packet)
+ Marshal
end
end
+ def dump_packet(packet)
+ serializer.dump packet
+ end
+
def load_packet(packet)
- if packet[0] == 4
- Marshal.load(packet)
- else
- JSON.parse(packet)
+ serializer.load packet
+ rescue
+ cascade_deserializers(packet) unless format
+ end
+
+ Supported = [Marshal, JSON, YAML]
+ def cascade_deserializers(packet)
+ Supported.find do |s|
+ o = s.load(packet) rescue nil
+ return o if o
end
+ raise DeserializationFailed.new(packet, Supported)
end
end
-end
+end
15 spec/dispatcher_spec.rb
View
@@ -6,7 +6,11 @@
class Foo < Nanite::Actor
- expose :bar
+ expose :bar, :index
+
+ def index(payload)
+ bar(payload)
+ end
def bar(payload)
['hello', payload]
@@ -36,9 +40,16 @@ def bar(payload)
res.token.should == req.token
end
+ it "should dispatch a request for default action" do
+ req = Nanite::Request.new('/foo', 'payload', 'from', '0xdeadbeef', 'reply_to')
+ res = @dispatcher.dispatch_request(req)
+ res.should be_kind_of Nanite::Result
+ res.token.should == req.token
+ end
+
it "should know about all services" do
@dispatcher.register(Foo.new)
- @dispatcher.all_services.should == ['/foo/bar']
+ @dispatcher.all_services.should == ['/foo/bar', '/foo/index']
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.