Project

General

Profile

Download (2.95 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/>.
#++


module CyborgHood
module DSL
module BotnetTask
def ask(peer, key, cmd, *args)
_add_subtask_using_peer(peer) do |subtask, conv_thread|
conv_thread.call(cmd, *args) do |reply|
case reply[:status]
when :ok
subtask.results = {key => reply[:result]}
when :decline
# TODO: remove this case ???
when :error
subtask.errors << reply[:exception]
end

subtask.finish
end
end
end

def know?(peer, key, cmd)
_add_subtask_using_peer(peer) do |subtask, conv_thread|
conv_thread.exists?(cmd) do |reply|
case reply[:status]
when :ok
subtask.results = {key => reply[:result]}
when :decline
# TODO: remove this case ???
when :error
subtask.errors << reply[:exception]
end

subtask.finish
end
end
end

def _add_subtask_using_peer(peer)
_add_subtask("botnet/peer/#{peer}/out") do |subtask|
logger.debug "Task '#{@name}': Trying to contact peer '#{peer}'"
@bot.contact_peer(peer) do |conv|
if conv
logger.debug "Task '#{@name}': Peer '#{peer}' contacted, starting conversation"

@peer_contacted << peer

# don't use the block call to leave the conversation thread open
conv_thread = conv.thread(@notification_name)

yield(subtask, conv_thread)
else
logger.debug "Task '#{@name}': Could not contact peer '#{peer}'"
subtask.errors << CyberError.new(:unrecoverable, "botnet/client/dsl", "Task '#{@name}': could not contact peer '#{peer}'")
subtask.finish
end
end
end
end

def _setup
super

@peer_contacted = Set.new
end

def _finished
super

cb = Proc.new do |conv|
conv.close_thread(@notification_name)
end

@peer_contacted.each do |peer|
@bot.contact_peer(peer, true, &cb)
end
end
end

Task.class_eval("include BotnetTask")
end
end
(2-2/5)