Class: Workling::Clients::XmppClient

Inherits:
BrokerBase show all
Defined in:
lib/workling/clients/xmpp_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BrokerBase

#dispatch

Methods inherited from Base

#dispatch, installed?, #logger

Class Method Details

.loadObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/workling/clients/xmpp_client.rb', line 45

def self.load
  begin
    gem "xmpp4r"
    require 'xmpp4r'
    require "xmpp4r/pubsub"
    require "xmpp4r/pubsub/helper/servicehelper.rb"
    require "xmpp4r/pubsub/helper/nodebrowser.rb"
    require "xmpp4r/pubsub/helper/nodehelper.rb"
  rescue Exception => e
    raise WorklingError.new("Couldnt load the XMPP library. check that you have the xmpp4r gem installed")
  end
end

Instance Method Details

#closeObject

disconnect from the server



73
74
75
# File 'lib/workling/clients/xmpp_client.rb', line 73

def close
  @client.close
end

#connectObject

starts the client.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/workling/clients/xmpp_client.rb', line 59

def connect
  begin
    @client = Jabber::Client.new Workling.config[:jabber_id]
    @client.connect Workling.config[:jabber_server]
    @client.auth Workling.config[:jabber_password]
    @client.send Jabber::Presence.new.set_type(:available)
    @pubsub = Jabber::PubSub::ServiceHelper.new(@client, Workling.config[:jabber_service])
    unsubscribe_from_all                # make sure there are no open subscriptions, could cause multiple delivery of notifications, as they are persistent
  rescue
    raise WorklingError.new("couldn't connect to the jabber server")
  end
end

#request(key, value) ⇒ Object



98
99
100
# File 'lib/workling/clients/xmpp_client.rb', line 98

def request(key, value)
  @pubsub.publish_item_to(key, value)
end

#retrieve(key) ⇒ Object

request and retrieve work



94
95
96
# File 'lib/workling/clients/xmpp_client.rb', line 94

def retrieve(key)
  @pubsub.get_items_from(key, 1)
end

#subscribe(key) ⇒ Object

subscribe to a queue



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/workling/clients/xmpp_client.rb', line 78

def subscribe(key)
  @pubsub.subscribe_to(key)

  # filter out the subscription notification message that was generated by subscribing to the node
  @pubsub.get_subscriptions_from_all_nodes()

  @pubsub.add_event_callback do |event|
    event.payload.each do |e|
      e.children.each do |child|
        yield Hash.from_xml(child.children.first.to_s) if child.name == 'item'
      end
    end
  end
end