Project

General

Profile

« Previous | Next » 

Revision e321ca6f

Added by Marc Dequènes over 13 years ago

  • ID e321ca6f2a16f3d882f0c1008a71b3f55cf9e102

[evol] created a DSL for client-side calls (several conditions (calls only at the moment) can be joined together in order to exploit parallel tasks via asynchronous calls)

View differences:

README
- libgettext-ruby1.8
- libdaemons-ruby
- libeventmachine-ruby
- libactivesupport-ruby
- libxmpp4r-ruby (if using XMPP backend)
Postman:
- libtmail-ruby1.8
bin/mapmaker
include CyborgServerInterface
include CyborgServerRootInterfaceAddon
export_method :toto
class Taiste
include CyborgServerInterface
bin/mapmaker_client
include BotNet
def start_work
contact_peer("MapMaker") do |conv|
if conv
logger.info "Yo ! Conversation ready with MapMaker (#{conv.peer_name}) !"
conv.thread("plop") do |conv_thread|
conv_thread.call("/api_version", 2) do |reply|
case reply[:status]
when :ok
puts "Result:"
pp reply[:result]
when :decline
puts "Declined: #{reply[:reason]}"
when :error
exception = reply[:exception]
puts "EXCEPTION: [#{exception.severity} - #{exception.category}] #{exception.message}"
pp exception.inspect
else
puts "???"
end
# to test the closure through the inner instance_eval calls
toto = true
conversation_with "MapMaker" do
on_error do
puts "Halalalala !"
end
thread "super taiste" do
call :ver, "/api_version"
call :zones, "/DNS/Zones" if toto
on_error do
puts "Sniff !"
pp reply
end
on_success do
puts "Yahou !"
pp reply
call :gogogo, "/DNS"
on_error do
pp "Plouf"
pp reply
end
on_success do
pp "Hop!"
pp reply
end
end
else
logger.error "Could not connect to MapMaker"
stop
end
# in contact loop, in order to wait for conversation registration to happen
# or the bot would stop before contact is done
try_stop
#stop_bot :at_once
stop_bot :when_finished
end
end
end
lib/cyborghood/cyborg.rb
EventMachine.next_tick { EventMachine.stop_event_loop }
end
def try_stop
if ready_to_stop?
EventMachine.stop_event_loop
else
EventMachine.next_tick { try_stop }
end
end
# core capabilities
def capabilities
[]
......
def process_system_notification(msg)
end
def try_stop
if ready_to_stop?
EventMachine.stop_event_loop
else
EventMachine.next_tick { try_stop }
end
end
end
autoload :BotNet, 'cyborghood/cyborg/botnet'
lib/cyborghood/cyborg/botnet.rb
require 'cyborghood/cyborg/interface'
require 'cyborghood/cyborg/conversation'
require 'cyborghood/cyborg/botnet_dsl'
require 'set'
module CyborgHood
......
end
end
end
# start of DSL
def conversation_with(peer, &block)
BotnetDSL::Conversation.run_dsl(self, peer, &block)
end
end
module BotNet
lib/cyborghood/cyborg/botnet_dsl.rb
#--
# 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[:error][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)
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

Also available in: Unified diff