Project

General

Profile

Download (3.67 KB) Statistics
| Branch: | Tag: | Revision:
#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009-2011 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 'cyborghood'
require 'eventmachine'
require 'cyborghood/cyborg/dsl'


module CyborgHood
module TaskAspect
def task(name, &block)
DSL::Task.new(self, name, &block)
end

def schedule_task(callback, &task)
EventMachine.defer(task, callback)
end
end

class Cyborg
include I18nTranslation
bindtextdomain("cyborghood", {:path => Config::L10N_DIR, :charset => "UTF-8"})

include TaskAspect

attr_reader :name, :services

def initialize(services = nil)
@services = services

@name = self.class.name.split("::").last
@config = Config.instance

# setup logs
unless @config.log.nil?
logger.output_level(@config.log.console_level) unless @config.log.console_level.nil?
unless @config.log.path.nil?
if File.directory? @config.log.path
logger.log_to_file(File.join(@config.log.path, "ch_#{self.class.human_name}.log"))
else
logger.fatal "Log path does not exist or is not a directory, exiting"
exit 1
end
end
end

@channels = {}

setup

logger.info "Bot '#{self.human_name}' loaded"
end

# #^.*/system$# categories are reserved
# but you may enhance or break the system as you wish
def get_channel(name)
return @channels[name] if @channels.has_key? name
@channels[name] = EventMachine::Channel.new
end

def drop_channel(name)
@channels.delete(name)
end

def setup
@system_notification_name = 'global/system'
@system_notification = get_channel(@system_notification_name)
@system_notification_processing = @system_notification.subscribe do |msg|
process_system_notification(msg)
end

EventMachine.error_handler do |e|
logger.error "Reactor error: " + e.to_s
logger.debug e.backtrace.join("\n")
end
end

def run
logger.info "Bot starting"
EventMachine.run do
start_work
end
rescue EventMachine::ConnectionNotBound
logger.error "Reactor error: Internal error (EventMachine::ConnectionNotBound)"
end

def start_work
# use "aspects" Modules to define behaviors
end

def ask_to_stop
logger.info "Bot was asked to stop..."
yield if block_given?
try_stop
end

def ready_to_stop?
DSL::Task.idle?
end

def stop
logger.info "Bot stopping"
EventMachine.next_tick { EventMachine.stop_event_loop }
end

def try_stop
if ready_to_stop?
@system_notification.unsubscribe(@system_notification_processing)
@system_notification_processing = nil
drop_channel(@system_notification_name)

stop
else
EventMachine.next_tick { try_stop }
end
end

# core capabilities
def capabilities
[]
end

protected

def process_system_notification(msg)
end
end

autoload :BotNet, 'cyborghood/cyborg/botnet'
end
(2-2/4)