Skip to content

Instantly share code, notes, and snippets.

@toretore
Forked from Aeon/mtool.rb
Last active August 29, 2015 13:57
Show Gist options
  • Save toretore/9564384 to your computer and use it in GitHub Desktop.
Save toretore/9564384 to your computer and use it in GitHub Desktop.
require 'api/pools.rb'
module Mtool
extend Blather::DSL
#Blather.logger.level = Logger::DEBUG
Blather.logger.level = Logger::INFO
@@faye = nil
@@xmppServerName = 'example.com'
@@xmppMUCUrl = 'conference.example.com'
@@jid = '[email protected]/web'
@@password = 'password'
@@jid_resource = 'monitor'
@@defaultInterface = 'en0'
def self.run(faye)
@@faye = faye
client.run
end
def self.pools
discover 'items', @@xmppMUCUrl, nil do |result|
yield result.items
end
end
def self.machines(pool)
discover 'items', pool, nil do |result|
filtered_machines = result.items.reject do |m|
m.name == 'resource-manager' || m.name == 'monitor'
end
yield filtered_machines
end
end
def self.leave(pool)
leave = Blather::Stanza::Presence::MUC.new
leave.to = "#{pool}/#{@@jid_resource}"
leave.type = 'unavailable'
write_to_stream leave
end
setup @@jid, @@password, @@xmppServerName
when_ready do
puts "Connected ! send messages to #{jid.stripped}."
# listen to client api requests and dispatch them to appropriate controllers
@@faye.subscribe('/api/*') do |message|
puts 'received api message'
puts message.inspect
begin
route = message['channel'].match Regexp.new("/api/(?<module>[^/]*)(?:/(?<id>[^/]*))?")
route_method = route[:id].nil? ? :index : :get
api_klass = "Api/#{route[:module]}".camelize.constantize
handler = api_klass.new(self, @@faye, message)
handler.handle
rescue
binding.pry
puts "Unhandled api call, need to define handler for #{message['channel']}"
end
end
# join all known pools to get presence notifications
pools do |items|
items.each do |pool|
join pool.jid.to_s, @@jid_resource
#puts "Joined room #{pool.jid.to_s}"
end
end
end
end
[:INT, :TERM].each do |sig|
trap(sig) {
print 'Shutting down component...'
APtool.shutdown
puts 'done.'
EM.stop
}
end
EM.run do
faye = Faye::Client.new('http://localhost:9292/faye')
Mtool.run faye
end
class Handler
attr_reader :xmpp, :faye, :message
def initialize(xmpp, faye, message)
@xmpp, @faye, @message = xmpp, faye, message
end
def handle
raise NotImplementedError
end
end
class PoolsHandler < Handler
def handle
xmpp.pools do |pool_items|
puts pool_items.inspect
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment