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
76 self.register_plugin('xep_0077') # In-band registration
77 self.register_plugin('xep_0030')
78 self.register_plugin('xep_0059')
79 self.register_plugin('xep_0060') # PubSub
81 self.add_event_handler("session_start", self.start)
82 self.add_event_handler("register", self.register)
83 self.add_event_handler("pubsub_publish", self.handle_omf_message)
88 def _init_parser(self):
89 """ Init the parser depending on the OMF Version
92 self._parser = OMF6Parser()
96 """ Check if the client is ready
101 def start(self, event):
102 """ Send presence to the Xmppp Server. This function is called directly by the sleekXmpp library
107 self._server = "pubsub.%s" % self.boundjid.domain
109 def register(self, iq):
110 """ Register to the Xmppp Server. This function is called directly by the sleekXmpp library
114 msg = " %s already registered!" % self.boundjid
120 resp['register']['username'] = self.boundjid.user
121 resp['register']['password'] = self.password
125 msg = " Account created for %s!" % self.boundjid
127 self._registered = True
129 msg = " Could not register account: %s" % e.iq['error']['text']
132 msg = " No response from server."
135 def unregister(self):
136 """ Unregister from the Xmppp Server.
140 self.plugin['xep_0077'].cancel_registration(
141 ifrom=self.boundjid.full)
142 msg = " Account unregistered for %s!" % self.boundjid
145 msg = " Could not unregister account: %s" % e.iq['error']['text']
148 msg = " No response from server."
152 """ Get all the nodes of the Xmppp Server.
156 result = self['xep_0060'].get_nodes(self._server)
157 for item in result['disco_items']['items']:
158 msg = ' - %s' % str(item)
162 error = traceback.format_exc()
163 msg = 'Could not retrieve node list.\ntraceback:\n%s' % error
166 def subscriptions(self):
167 """ Get all the subscriptions of the Xmppp Server.
171 result = self['xep_0060'].get_subscriptions(self._server)
173 for node in result['node']:
174 msg = ' - %s' % str(node)
178 error = traceback.format_exc()
179 msg = ' Could not retrieve subscriptions.\ntraceback:\n%s' % error
182 def create(self, node):
183 """ Create the topic corresponding to the node
185 :param node: Name of the topic, corresponding to the node (ex : omf.plexus.wlab17)
189 msg = " Create Topic : " + node
192 config = self['xep_0004'].makeForm('submit')
193 config.add_field(var='pubsub#node_type', value='leaf')
194 config.add_field(var='pubsub#notify_retract', value='0')
195 config.add_field(var='pubsub#publish_model', value='open')
196 config.add_field(var='pubsub#persist_items', value='1')
197 config.add_field(var='pubsub#max_items', value='1')
198 config.add_field(var='pubsub#title', value=node)
201 self['xep_0060'].create_node(self._server, node, config = config)
203 #error = traceback.format_exc()
204 #msg = ' Could not create topic: %s\ntraceback:\n%s' % (node, error)
205 msg = 'Could not create the topic : '+node+' . Maybe the topic already exists'
208 def delete(self, node):
209 """ Delete the topic corresponding to the node
211 :param node: Name of the topic, corresponding to the node (ex : omf.plexus.wlab17)
215 # To check if the queue are well empty at the end
216 #print " length of the queue : " + str(self.send_queue.qsize())
217 #print " length of the queue : " + str(self.event_queue.qsize())
219 self['xep_0060'].delete_node(self._server, node)
220 msg = ' Deleted node: %s' % node
223 #error = traceback.format_exc()
224 #msg = ' Could not delete topic: %s\ntraceback:\n%s' % (node, error)
225 msg = 'Could not delete the topic : '+node+' . Maybe It is not the owner of the topic'
228 def publish(self, data, node):
229 """ Publish the data to the corresponding topic
231 :param data: Data that will be published
233 :param node: Name of the topic
238 msg = " Publish to Topic : " + node
241 result = self['xep_0060'].publish(self._server,node,payload=data)
242 # id = result['pubsub']['publish']['item']['id']
243 # print('Published at item id: %s' % id)
245 error = traceback.format_exc()
246 msg = ' Could not publish to: %s\ntraceback:\n%s' % (node, error)
252 :param data: data from which the items will be get back
258 result = self['xep_0060'].get_item(self._server, self.boundjid,
260 for item in result['pubsub']['items']['substanzas']:
261 msg = 'Retrieved item %s: %s' % (item['id'], tostring(item['payload']))
264 error = traceback.format_exc()
265 msg = ' Could not retrieve item %s from topic %s\ntraceback:\n%s' \
266 % (data, self.boundjid, error)
269 def retract(self, data):
272 :param data: data from which the item will be retracted
277 result = self['xep_0060'].retract(self._server, self.boundjid, data)
278 msg = ' Retracted item %s from topic %s' % (data, self.boundjid)
281 error = traceback.format_exc()
282 msg = 'Could not retract item %s from topic %s\ntraceback:\n%s' \
283 % (data, self.boundjid, error)
287 """ Purge the information in the server
291 result = self['xep_0060'].purge(self._server, self.boundjid)
292 msg = ' Purged all items from topic %s' % self.boundjid
295 error = traceback.format_exc()
296 msg = ' Could not purge items from topic %s\ntraceback:\n%s' \
297 % (self.boundjid, error)
300 def subscribe(self, node):
301 """ Subscribe to a topic
303 :param node: Name of the topic
308 result = self['xep_0060'].subscribe(self._server, node)
309 msg = ' Subscribed %s to topic %s' \
310 % (self.boundjid.user, node)
314 error = traceback.format_exc()
315 msg = ' Could not subscribe %s to topic %s\ntraceback:\n%s' \
316 % (self.boundjid.bare, node, error)
319 def unsubscribe(self, node):
320 """ Unsubscribe to a topic
322 :param node: Name of the topic
327 result = self['xep_0060'].unsubscribe(self._server, node)
328 msg = ' Unsubscribed %s from topic %s' % (self.boundjid.bare, node)
331 error = traceback.format_exc()
332 msg = ' Could not unsubscribe %s from topic %s\ntraceback:\n%s' \
333 % (self.boundjid.bare, node, error)
336 def check_mailbox(self, itype, attr):
337 return self._parser.check_mailbox(itype, attr)
340 def handle_omf_message(self, iq):
341 """ Handle published/received message
343 :param iq: Stanzas that is currently published/received
347 self._parser.handle(iq)