root/lib/cyborghood/cyborg/botnet/dsl.rb @ 51556af0
e321ca6f | Marc Dequènes (Duck) | #--
|
|
# CyborgHood, a distributed system management software.
|
|||
364e4a96 | Marc Dequènes (Duck) | # Copyright (c) 2009-2011 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
e321ca6f | Marc Dequènes (Duck) | #
|
|
# 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
|
|||
d7ae0ab2 | Marc Dequènes (Duck) | module DSL
|
|
848944dc | Marc Dequènes (Duck) | module BotnetTask
|
|
def ask(peer, key, cmd, *args)
|
|||
14dd8318 | Marc Dequènes (Duck) | _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
|
|||
51556af0 | Marc Dequènes (Duck) | subtask.errors += reply[:exceptions]
|
|
14dd8318 | Marc Dequènes (Duck) | 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
|
|||
51556af0 | Marc Dequènes (Duck) | subtask.errors += reply[:exceptions]
|
|
14dd8318 | Marc Dequènes (Duck) | end
|
|
subtask.finish
|
|||
end
|
|||
end
|
|||
end
|
|||
def _add_subtask_using_peer(peer)
|
|||
848944dc | Marc Dequènes (Duck) | _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"
|
|||
429ebb9c | Marc Dequènes (Duck) | @peer_contacted << peer
|
|
848944dc | Marc Dequènes (Duck) | # don't use the block call to leave the conversation thread open
|
|
conv_thread = conv.thread(@notification_name)
|
|||
5a8ebc1c | Marc Dequènes (Duck) | ||
14dd8318 | Marc Dequènes (Duck) | yield(subtask, conv_thread)
|
|
848944dc | Marc Dequènes (Duck) | 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
|
|||
fd4ccbdd | Marc Dequènes (Duck) | end
|
|
67e8f239 | Marc Dequènes (Duck) | end
|
|
end
|
|||
end
|
|||
e321ca6f | Marc Dequènes (Duck) | ||
429ebb9c | Marc Dequènes (Duck) | 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
|
|||
e321ca6f | Marc Dequènes (Duck) | end
|
|
429ebb9c | Marc Dequènes (Duck) | ||
Task.class_eval("include BotnetTask")
|
|||
e321ca6f | Marc Dequènes (Duck) | end
|
|
end
|