Project

General

Profile

Download (3.82 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 'active_support/basic_object'


module CyborgHood
module BotnetDSL
class BaseDSL < ActiveSupport::BasicObject

def self.run_dsl(*args, &block)
dsl = new(*args)
dsl.instance_eval(&block)
dsl.instance_eval do
_start_dsl
end
end
end

class Thread < BaseDSL
attr_reader :name, :conversation, :reply

def initialize(conversation_dsl, conversation, name, reply = nil)
@conversation_dsl = conversation_dsl
@conversation = conversation
@name = name
@reply = reply

@error_cb = nil
@success_cb = nil
@calls = {}
@calls_reply = {}

@final_reply = {
:results => {},
:errors => {}
}
end

def on_error(&callback)
@error_cb = callback
end

def on_success(&callback)
@success_cb = callback
end

def call(key, cmd, *args)
@calls[key] = {
:cmd => cmd,
:args => args
}
end

protected

def _start_dsl
@conversation.thread(@name) do |conv_thread|
@calls.each_pair do |key, data|
conv_thread.call(data[:cmd], *data[:args]) do |reply|
@calls_reply[key] = reply[:status]

case reply[:status]
when :ok
@final_reply[:results][key] = reply[:result]
when :decline
# TODO: remove this case ???
when :error
@final_reply[:errors][key] = reply[:exception]
end

check_finished
end
end
end
end

def check_finished
return unless @calls.size == @calls_reply.size

# process reply in the same thread, but with a new DSL Thread context
cb = @calls_reply.values.index(:error) ? @error_cb : @success_cb
Thread.run_dsl(@conversation_dsl, @conversation, @name, @final_reply, &cb) if cb
end
end

class Conversation < BaseDSL
attr_reader :bot, :peer

def initialize(bot, peer)
@bot = bot
@peer = peer

@threads = {}
@error_cb = nil
@stop_bot = nil
end

def on_error(&callback)
@error_cb = callback
end

def thread(name, &block)
@threads[name] = block
end

def stop_bot(condition)
@stop_bot = condition
end

protected

def _start_dsl
@bot.contact_peer(peer) do |conv|
if conv
@threads.each_pair do |name, block|
Thread.run_dsl(self, conv, name, &block)
end
else
@error_cb.call unless @error_cb.nil?
end

case @stop_bot
when :when_finished
# in contact loop, in order to wait for conversation registration to happen
# or the bot would stop before contact is done
@bot.try_stop
when :at_once
# it won't wait for conversation to finish...
@bot.stop
end
end
end
end
end
end
(2-2/7)