Revision 4761cd13
Added by Marc Dequènes about 13 years ago
- ID 4761cd134c36a40c0240698f7440ba4b693c1b32
lib/cyborghood/cyborg/botnet/interface.rb | ||
---|---|---|
module CyborgHood
|
||
module DSL
|
||
class ServerApiNode < BaseDSL
|
||
attr_reader :bot, :node_name
|
||
attr_reader :bot, :node_name, :parent_node, :store
|
||
|
||
# needed for testing node existence
|
||
reveal :nil?
|
||
reveal :respond_to?
|
||
|
||
def initialize(bot, parent_node = nil, &block)
|
||
def initialize(bot, parent_node = nil, ldir = nil, &block)
|
||
@bot = bot
|
||
@parent_node = parent_node
|
||
@ldir = ldir
|
||
|
||
# don't call super because we need defered loading
|
||
@blocks = [block]
|
||
|
||
@blocks = []
|
||
add_behavior(&block)
|
||
|
||
@ldir_loaded = false
|
||
|
||
cleanup
|
||
end
|
||
|
||
def add_behavior(&block)
|
||
@blocks << block
|
||
if block_given?
|
||
@blocks << block
|
||
else
|
||
return if @ldir_loaded
|
||
|
||
Dir.glob(File.join(self.lpath, '*.rb')) do |file|
|
||
logger.debug "Interface: loading file '#{file}'"
|
||
@blocks << File.read(file)
|
||
end
|
||
|
||
@ldir_loaded = true
|
||
end
|
||
end
|
||
|
||
# string, array (useful for aliases), or regex
|
||
# TODO: name validation
|
||
def node(match, &block)
|
||
child_node = self.class.new(@bot, self, &block)
|
||
def node(match, ldir = nil, &block)
|
||
child_node = self.class.new(@bot, self, ldir, &block)
|
||
if match.is_a? Array
|
||
match.each{|n| @nodes[n] = child_node}
|
||
else
|
||
... | ... | |
@parent_node.nil?
|
||
end
|
||
|
||
def base_lpath
|
||
File.join(Config::LIB_DIR, 'cyborghood-' + @bot.name.downcase, 'interface')
|
||
end
|
||
|
||
def lpath
|
||
parent_lpath = @parent_node.nil? ? self.base_lpath : @parent_node.lpath
|
||
@ldir.nil? ? parent_lpath : File.join(parent_lpath, @ldir)
|
||
end
|
||
|
||
def load(node_element = '')
|
||
cleanup
|
||
@node_name = node_element
|
||
@blocks.each do |bl|
|
||
instance_eval &bl
|
||
if bl.is_a? String
|
||
instance_eval bl
|
||
else
|
||
instance_eval &bl
|
||
end
|
||
end
|
||
end
|
||
|
||
def find_node(session, node_path)
|
||
# it is a string argument when interface root is called, but a list of node elemnts later
|
||
# node_path is a string argument when interface root node is called, but is a list of node elements later on
|
||
if root?
|
||
logger.debug "[Server API] Looking for node '#{node_path}'"
|
||
node_path = node_path.split("/")
|
||
... | ... | |
def cleanup
|
||
@nodes = {}
|
||
@request_cb = nil
|
||
|
||
# data memorized during walk in the node tree
|
||
if root?
|
||
@store = OpenStruct.new
|
||
else
|
||
@store = @parent_node.store
|
||
end
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
[evol] allow splitting the API interface declaration over multiple files/directories, split MapMaker interface, and added cross-nodes storage during the interface life (as we can't use closures across files)