Skip to content
Browse files

Improved fallbacking from ActionSpecs to ObjectSpecs

  • Loading branch information...
1 parent 71abfc6 commit 6f1eac2324bd3e0b32a55be0d26e727b16898b93 @krakatoa krakatoa committed Sep 24, 2012
View
52 lib/protocol/action_specification.rb
@@ -1,6 +1,56 @@
module Cucub
module Protocol
- class ActionSpecification
+ class ActionSpecification < Specification
+ attr_reader :class_name, :action_name
+ attr_reader :from, :respond_to, :read_by
+
+ def initialize(class_name, action_name)
+ @class_name = class_name
+ @action_name = action_name
+
+ super()
+ end
+
+ def object_specification
+ Cucub::Protocol::Loader.instance.specifications[@class_name]
+ end
+
+ def from=(from)
+ @from = from
+ end
+
+ def respond_to=(respond_to)
+ @respond_to = respond_to
+ end
+
+ def read_by=(read_by)
+ @read_by = read_by
+ end
+
+ def uses_box
+ @uses_box || object_specification.uses_box
+ end
+
+ def uses_mailbox
+ @uses_mailbox || object_specification.uses_mailbox
+ end
+
+ def uses_board
+ @uses_board || object_specification.uses_board
+ end
+
+ def serialize
+ @serialize || object_specification.serialize
+ end
+
+ def compression
+ @compression || :none # gzip
+ end
+
+ def encryption
+ @encryption || :none #
+ end
+
end
end
end
View
28 lib/protocol/loader.rb
@@ -1,10 +1,18 @@
+require 'singleton'
+
module Cucub
module Protocol
class Loader
- def initialize(filepath)
+ include Singleton
+
+ def set_path(filepath)
@filepath = filepath
end
+ def specifications
+ @current_specifications
+ end
+
def parse
basefile = IniFile.load(@filepath)
@@ -14,8 +22,14 @@ def parse
object_spec = parse_object_specification(section, basefile[section])
current_specifications.add_object_specification(object_spec)
}
+
+ basefile.sections.select { |x| x.include?("#") }.each { |section|
+ class_name, action_name = section.split("#")
+ action_spec = parse_action_specification(class_name, action_name, basefile[section])
+ current_specifications[class_name].add_action_specification(action_spec)
+ }
- current_specifications
+ @current_specifications = current_specifications
end
def parse_object_specification(class_name, section)
@@ -26,6 +40,16 @@ def parse_object_specification(class_name, section)
}
object_specification
end
+
+ def parse_action_specification(class_name, action_name, section)
+ action_specification = Cucub::Protocol::ActionSpecification.new(class_name, action_name)
+
+ section.each_pair { |key, value|
+ action_specification.send("#{key}=".to_sym, value)
+ }
+ action_specification
+ end
+
end
end
end
View
35 lib/protocol/object_specification.rb
@@ -1,12 +1,15 @@
module Cucub
module Protocol
class ObjectSpecification < Specification
+ attr_reader :class_name
attr_reader :role
def initialize(class_name)
@class_name = class_name
@role = :both # both receiver emitter
+
+ @action_specifications = []
super()
end
@@ -16,6 +19,38 @@ def role=(role)
raise "invalid role!" if ![:both, :receiver, :emitter].include?(role)
@role = role
end
+
+ def uses_box
+ @uses_box || false
+ end
+
+ def uses_mailbox
+ @uses_mailbox || false
+ end
+
+ def uses_board
+ @uses_board || false
+ end
+
+ def serialize
+ @serialize || :none # msgpack
+ end
+
+ def compression
+ @compression || :none # gzip
+ end
+
+ def encryption
+ @encryption || :none #
+ end
+
+ def add_action_specification(action_spec)
+ @action_specifications.push action_spec
+ end
+
+ def action_specifications
+ @action_specifications
+ end
end
end
View
10 lib/protocol/specification.rb
@@ -1,16 +1,8 @@
module Cucub
module Protocol
class Specification
- attr_reader :uses_box, :uses_mailbox, :uses_board, :serialize, :compression, :encryption
def initialize
- @uses_box = false
- @uses_mailbox = false
- @uses_board = false
-
- @serialize = :none # msgpack
- @compression = :none # gzip
- @encryption = :none #
end
def uses_box=(uses_box)
@@ -67,7 +59,7 @@ def uses_board=(uses_board)
def serialize=(serialization)
serialization = serialization.to_sym
raise "invalid serialization type!" if ![:none, :msgpack].include?(serialization)
- @serialization = serialization
+ @serialize = serialization
end
def compression=(compression)
View
11 lib/protocol/specification_set.rb
@@ -12,6 +12,17 @@ def add_object_specification(obj_spec)
def object_specifications
@object_specifications
end
+
+ def [](class_name)
+ # TODO there should be a method to facilitate search in to spec_set for a class
+ object_specification = @object_specifications.select {|spec| spec.class_name == class_name }.first
+
+ if object_specification
+ return object_specification
+ else
+ raise "class was not defined!"
+ end
+ end
end
end
end
View
17 spec/mock/protocol.ini
@@ -1,11 +1,26 @@
[engine]
role = receiver
uses_box = true
+ uses_board = true
+ serialize = msgpack
+ compression = gzip
; cp = 1425
[engine#create_vps]
- test = "blo"
+ from = core
+ respond_to = class+object
+ serialize = none
+ read_by = core
[agent]
uses_mailbox = true
role = emitter
+
+[agent#metric]
+ serialize = none
+
+[agent#adelantar_metric]
+ from = trigger
+; respond_to = broadcast
+ respond_to = box
+ read_by = monitor
View
14 spec/protocol_spec.rb
@@ -7,9 +7,19 @@
describe "#parse" do
it "should return a valid SpecificationSet" do
- @loader = Cucub::Protocol::Loader.new(@filepath + "/mock/protocol.ini")
+ @loader = Cucub::Protocol::Loader.instance
+ @loader.set_path(@filepath + "/mock/protocol.ini")
specification_set = @loader.parse
- puts specification_set.object_specifications.inspect
+ specification_set.object_specifications.each {|obj_spec|
+ puts "object: #{obj_spec.class_name}"
+ puts "serialize: #{obj_spec.serialize}"
+ obj_spec.action_specifications.each {|act|
+ puts "\taction: #{act.action_name}"
+ puts "\tserialize: #{act.serialize}"
+ }
+ puts "\n"
+ #puts obj_spec.inspect
+ }
end
end

0 comments on commit 6f1eac2

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