2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 # Julien Tribino <julien.tribino@inria.fr>
21 from nepi.util.logger import Logger
22 from nepi.resources.omf.omf6_parser import OMF6Parser
26 from sleekxmpp.exceptions import IqError, IqTimeout
27 class BaseOMFClient(sleekxmpp.ClientXMPP):
30 print "SleekXMPP is not installed. Without this library, \n" + \
31 " You will be not able to use OMF Resources \n"+ \
32 " If you want to install SleekXmpp : \n"+ \
33 " git clone -b develop git://github.com/fritzy/SleekXMPP.git \n"+ \
35 "sudo python setup.py install\n"
36 class BaseOMFClient(object):
40 import xml.etree.ElementTree as ET
42 # inherit from BaseXmpp and XMLstream classes
43 class OMFClient(sleekxmpp.ClientXMPP, Logger):
45 .. class:: Class Args :
47 :param jid: Jabber Id (= Xmpp Slice + Date)
49 :param password: Jabber Password (= Xmpp Password)
54 This class is an XMPP Client with customized method
58 def __init__(self, jid, password):
61 :param jid: Jabber Id (= Xmpp Slice + Date)
63 :param password: Jabber Password (= Xmpp Password)
68 Logger.__init__(self, "OMFClient")
70 sleekxmpp.ClientXMPP.__init__(self, jid, password)
72 self._registered = False
78 self.register_plugin('xep_0077') # In-band registration
79 self.register_plugin('xep_0030')
80 self.register_plugin('xep_0059')
81 self.register_plugin('xep_0060') # PubSub
83 self.add_event_handler("session_start", self.start)
84 self.add_event_handler("register", self.register)
85 self.add_event_handler("pubsub_publish", self.handle_omf_message)
90 def _init_parser(self):
91 """ Init the parser depending on the OMF Version
94 self._parser = OMF6Parser()
98 """ Check if the client is ready
103 def start(self, event):
104 """ Send presence to the Xmppp Server. This function is called directly by the sleekXmpp library
109 self._server = "pubsub.%s" % self.boundjid.domain
111 def register(self, iq):
112 """ Register to the Xmppp Server. This function is called directly by the sleekXmpp library
116 msg = " %s already registered!" % self.boundjid
122 resp['register']['username'] = self.boundjid.user
123 resp['register']['password'] = self.password
127 msg = " Account created for %s!" % self.boundjid
129 self._registered = True
131 msg = " Could not register account: %s" % e.iq['error']['text']
134 msg = " No response from server."
137 def unregister(self):
138 """ Unregister from the Xmppp Server.
142 self.plugin['xep_0077'].cancel_registration(
143 ifrom=self.boundjid.full)
144 msg = " Account unregistered for %s!" % self.boundjid
147 msg = " Could not unregister account: %s" % e.iq['error']['text']
150 msg = " No response from server."
154 """ Get all the nodes of the Xmppp Server.
158 result = self['xep_0060'].get_nodes(self._server)
159 for item in result['disco_items']['items']:
160 msg = ' - %s' % str(item)
164 error = traceback.format_exc()
165 msg = 'Could not retrieve node list.\ntraceback:\n%s' % error
168 def subscriptions(self):
169 """ Get all the subscriptions of the Xmppp Server.
173 result = self['xep_0060'].get_subscriptions(self._server)
175 for node in result['node']:
176 msg = ' - %s' % str(node)
180 error = traceback.format_exc()
181 msg = ' Could not retrieve subscriptions.\ntraceback:\n%s' % error
184 def create(self, node):
185 """ Create the topic corresponding to the node
187 :param node: Name of the topic, corresponding to the node (ex : omf.plexus.wlab17)
191 msg = " Create Topic : " + node
194 config = self['xep_0004'].makeForm('submit')
195 config.add_field(var='pubsub#node_type', value='leaf')
196 config.add_field(var='pubsub#notify_retract', value='0')
197 config.add_field(var='pubsub#publish_model', value='open')
198 config.add_field(var='pubsub#persist_items', value='1')
199 config.add_field(var='pubsub#max_items', value='1')
200 config.add_field(var='pubsub#title', value=node)
203 self['xep_0060'].create_node(self._server, node, config = config)
205 #error = traceback.format_exc()
206 #msg = ' Could not create topic: %s\ntraceback:\n%s' % (node, error)
207 msg = 'Could not create the topic : '+node+' . Maybe the topic already exists'
210 def delete(self, node):
211 """ Delete the topic corresponding to the node
213 :param node: Name of the topic, corresponding to the node (ex : omf.plexus.wlab17)
217 # To check if the queue are well empty at the end
218 #print " length of the queue : " + str(self.send_queue.qsize())
219 #print " length of the queue : " + str(self.event_queue.qsize())
221 self['xep_0060'].delete_node(self._server, node)
222 msg = ' Deleted node: %s' % node
225 error = traceback.format_exc()
226 msg = ' Could not delete topic: %s\ntraceback:\n%s' % (node, error)
229 def publish(self, data, node):
230 """ Publish the data to the corresponding topic
232 :param data: Data that will be published
234 :param node: Name of the topic
239 msg = " Publish to Topic : " + node
242 result = self['xep_0060'].publish(self._server,node,payload=data)
243 # id = result['pubsub']['publish']['item']['id']
244 # print('Published at item id: %s' % id)
246 error = traceback.format_exc()
247 msg = ' Could not publish to: %s\ntraceback:\n%s' % (node, error)
253 :param data: data from which the items will be get back
259 result = self['xep_0060'].get_item(self._server, self.boundjid,
261 for item in result['pubsub']['items']['substanzas']:
262 msg = 'Retrieved item %s: %s' % (item['id'], tostring(item['payload']))
265 error = traceback.format_exc()
266 msg = ' Could not retrieve item %s from topic %s\ntraceback:\n%s' \
267 % (data, self.boundjid, error)
270 def retract(self, data):
273 :param data: data from which the item will be retracted
278 result = self['xep_0060'].retract(self._server, self.boundjid, data)
279 msg = ' Retracted item %s from topic %s' % (data, self.boundjid)
282 error = traceback.format_exc()
283 msg = 'Could not retract item %s from topic %s\ntraceback:\n%s' \
284 % (data, self.boundjid, error)
288 """ Purge the information in the server
292 result = self['xep_0060'].purge(self._server, self.boundjid)
293 msg = ' Purged all items from topic %s' % self.boundjid
296 error = traceback.format_exc()
297 msg = ' Could not purge items from topic %s\ntraceback:\n%s' \
298 % (self.boundjid, error)
301 def subscribe(self, node):
302 """ Subscribe to a topic
304 :param node: Name of the topic
309 result = self['xep_0060'].subscribe(self._server, node)
310 msg = ' Subscribed %s to topic %s' \
311 % (self.boundjid.user, node)
315 error = traceback.format_exc()
316 msg = ' Could not subscribe %s to topic %s\ntraceback:\n%s' \
317 % (self.boundjid.bare, node, error)
320 def unsubscribe(self, node):
321 """ Unsubscribe to a topic
323 :param node: Name of the topic
328 result = self['xep_0060'].unsubscribe(self._server, node)
329 msg = ' Unsubscribed %s from topic %s' % (self.boundjid.bare, node)
332 error = traceback.format_exc()
333 msg = ' Could not unsubscribe %s from topic %s\ntraceback:\n%s' \
334 % (self.boundjid.bare, node, error)
338 def handle_omf_message(self, iq):
339 """ Handle published/received message
341 :param iq: Stanzas that is currently published/received
345 self._parser.handle(iq)