Project

General

Profile

Download (7.33 KB) Statistics
| Branch: | Tag: | Revision:
#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++

require 'singleton'


module CyborgHood
# the base mixin (not intended to be used directly, but...)
module CyborgServerInterfaceBase
NODE_PATTERN = "((?:\/|(?:\/[a-zA-Z0-9._]+)+[?=]?))"

def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
attr_accessor :exported_methods
attr_accessor :auto_export_public_instance_methods

def export_method(*syms)
syms = [syms] unless syms.is_a? Array
self.exported_methods ||= []
self.exported_methods += syms.collect{|m| m.to_s }
end

def unexport_method(*syms)
syms = [syms] unless syms.is_a? Array
self.exported_methods ||= []
self.exported_methods -= syms.collect{|m| m.to_s }
end

def export_parent_methods
self.export_method *self.superclass.public_instance_methods(false)
end

def is_node?(node)
(node =~ NODE_PATTERN) ? true : false
end
end

def initialize(*args)
super(*args)

@config = Config.instance

self.class.exported_methods ||= []
self.class.auto_export_public_instance_methods = true
end

# convenience method
def is_node?(node)
self.class.is_node?(node)
end

def api_klasses
list = self.class.constants.collect do |c|
cc = self.class.const_get(c)
(cc.class == Class and cc.ancestors.include? CyborgHood::CyborgServerInterfaceBase) ? [c, cc] : nil
end.compact
Hash[list]
end

def api_methods
methods = self.class.exported_methods
methods += self.class.public_instance_methods(false) if self.class.auto_export_public_instance_methods
methods -= ["initialize", "__destroy", "method_missing"]
methods & self.methods
end

def api_container_methods
[]
end

def api_containers
(api_klasses.keys + api_container_methods).sort
end

def api_leafs
(api_methods - api_container_methods).sort
end

def api_nodes
(api_klasses.keys + api_methods).sort
end

def find_node_action(session, node_name)
node_name.gsub!(/^\//, "")
next_node_name, other_nodes_names = node_name.split('/', 2)

next_node_klass = next_node_name.nil? ? self.class : api_klasses[next_node_name]
# inner class or method ?
if next_node_klass.nil?
# method is declared ?
if api_methods.include? next_node_name
# final node ?
if other_nodes_names.blank?
# cannot use method(), as this method may not exist at all (but still
# be usuable through metaprogramming
return lambda do |*args|
r = child_node(next_node_name, session, *args)
# dynamic tree construction: method may return a node
if r.is_a? CyborgHood::CyborgServerInterfaceBase
r.api_nodes
else
r
end
end
end
# not a container, leaving
return unless self.api_container_methods.include? next_node_name
next_node = child_node(next_node_name, session)
else
# unknown method
return
end
else
next_node = next_node_klass.instance
# final node ?
return next_node.method('api_nodes') if other_nodes_names.blank?
end

# search deeper
if next_node.is_a? CyborgHood::CyborgServerInterfaceBase
next_node.find_node_action(session, other_nodes_names)
else
# it is not a node, so there are no children
return
end
end

def child_node(next_node_name, session, *args)
args.unshift session if self.is_a? CyborgHood::CyborgServerStatefulInterface
self.send(next_node_name, *args)
end

def has_node?(cmd)
not find_node_action(nil, cmd).nil?
end

# preliminary incoming message handling
def call(session, cmd, data)
action = find_node_action(session, cmd)
raise "unknown node" if action.nil?

raise "wrong format for arguments" unless data.is_a? Array

begin
action.call(*data)
rescue
logger.debug "node action error message: " + $!
logger.debug "node action error backtrace: " + $!.backtrace.join("\n")
raise "method call failed: " + $!
end
end
end

# structural mixins

module CyborgServerInterface
def self.included(base)
base.class_eval("include CyborgServerInterfaceBase")
base.class_eval("include Singleton")
base.extend(ClassMethods)
end

module ClassMethods
def dynamic_interface(&resource_generator)
class_eval do
class_inheritable_reader :resource_generator

def method_missing(method_name, *args)
node_name = method_name.to_s
if api_methods.include?(node_name)
self.resource_generator.call(node_name)
else
super
end
end
end

write_inheritable_attribute(:resource_generator, resource_generator)
end
end
end

module CyborgServerEmbeddedInterface
def self.included(base)
base.class_eval("include CyborgServerInterfaceBase")
base.export_parent_methods
end
end

module CyborgServerStatefulInterface
def self.included(base)
base.class_eval("include CyborgServerInterfaceBase")
base.class_eval("include Singleton")
base.extend(ClassMethods)
end

module ClassMethods
def stateful_dynamic_interface(resource_key_pattern, &resource_generator)
class_eval do
class_inheritable_reader :resource_key_pattern, :resource_generator

def method_missing(method_name, *args)
session = args.shift

node_name = method_name.to_s
if api_methods.include?(node_name)
resource_key = self.resource_key_pattern.gsub("#NODE#", node_name)
session.store.get(resource_key) { self.resource_generator.call(node_name) }
else
super
end
end
end

write_inheritable_attribute(:resource_key_pattern, resource_key_pattern)
write_inheritable_attribute(:resource_generator, resource_generator)
end
end
end

# additional mixin

module CyborgServerRootInterfaceAddon
PROTOCOL_VERSION = "0.1~"

def self.included(base)
list = self.public_instance_methods(false)
base.class_eval do
export_method list
end
end

def product_name
PRODUCT
end

def product_version
VERSION
end

def protocol_version
PROTOCOL_VERSION
end

def bot_name
@config.bot_name
end
end
end
(2-2/5)