Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for yaml marshalling, and the ability for optional mars…

…hallers to be added and used reliably. Optional support for cascading deserialization by un-setting the format option (set to nil)
  • Loading branch information...
commit e4077574c34b29976344f3ff9275c39edda3a7a4 1 parent 9a0b1bf
James Tucker authored
Showing with 38 additions and 15 deletions.
  1. +3 −3 lib/nanite.rb
  2. +35 −12 lib/nanite/marshal.rb
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
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
Please sign in to comment.
Something went wrong with that request. Please try again.