Skip to content

Instantly share code, notes, and snippets.

@Aeon
Last active August 29, 2015 13:57
Show Gist options
  • Save Aeon/9563985 to your computer and use it in GitHub Desktop.
Save Aeon/9563985 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 = 'monitor@example.com/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
api_klass.send route_method, route[:id], message
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
module Api
module Pools
extend Mtool
def self.index
pools do |pool_items|
puts pool_items.inspect
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment