Merging the openflow part to the nepi-3-dev branch
[nepi.git] / src / nepi / resources / omf / omf_client.py
index f20132d..05af4f1 100644 (file)
@@ -1,38 +1,56 @@
-"""
-    NEPI, a framework to manage network experiments
-    Copyright (C) 2013 INRIA
-
-    This program is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-"""
+#
+#    NEPI, a framework to manage network experiments
+#    Copyright (C) 2013 INRIA
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: Alina Quereilhac <alina.quereilhac@inria.fr>
+#         Julien Tribino <julien.tribino@inria.fr>
 
 from nepi.util.logger import Logger
 
-import sleekxmpp
-from sleekxmpp.exceptions import IqError, IqTimeout
+try:
+    import sleekxmpp
+    from sleekxmpp.exceptions import IqError, IqTimeout
+    class BaseOMFClient(sleekxmpp.ClientXMPP):
+        pass
+except ImportError:
+    msg = ("SleekXMPP is not installed. Without this library "
+          "you will be not able to use OMF Resources "
+          "if you want to install SleekXmpp: \n"
+          " git clone -b develop git://github.com/fritzy/SleekXMPP.git \n"
+          " cd SleekXMPP \n"
+          " sudo python setup.py install\n")
+
+    logger = Logger("BaseOMFClient")
+    logger.debug(msg)
+
+    class BaseOMFClient(object):
+        pass
+
 import traceback
 import xml.etree.ElementTree as ET
 
-# inherit from BaseXmpp and XMLStream classes
-class OMFClient(sleekxmpp.ClientXMPP, Logger): 
+# inherit from BaseXmpp and XMLstream classes
+class OMFClient(BaseOMFClient, Logger): 
     """
     .. class:: Class Args :
       
         :param jid: Jabber Id (= Xmpp Slice + Date)
-        :type jid: Str
+        :type jid: str
         :param password: Jabber Password (= Xmpp Password)
-        :type password: Str
+        :type password: str
 
     .. note::
 
@@ -44,9 +62,9 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
         """
 
         :param jid: Jabber Id (= Xmpp Slice + Date)
-        :type jid: Str
+        :type jid: str
         :param password: Jabber Password (= Xmpp Password)
-        :type password: Str
+        :type password: str
 
 
         """
@@ -102,7 +120,7 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
             self._registered = True
         except IqError as e:
             msg = " Could not register account: %s" % e.iq['error']['text']
-            selferror(msg)
+            self.error(msg)
         except IqTimeout:
             msg = " No response from server."
             self.error(msg)
@@ -131,7 +149,7 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
             result = self['xep_0060'].get_nodes(self._server)
             for item in result['disco_items']['items']:
                 msg = ' - %s' % str(item)
-                self.info(msg)
+                self.debug(msg)
             return result
         except:
             error = traceback.format_exc()
@@ -147,7 +165,7 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
                 #self.boundjid.full)
             for node in result['node']:
                 msg = ' - %s' % str(node)
-                self.info(msg)
+                self.debug(msg)
             return result
         except:
             error = traceback.format_exc()
@@ -161,7 +179,8 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
         :type node: str
 
         """
-        self.debug(" Create Topic : " + node)
+        msg = " Create Topic : " + node
+        self.info(msg)
    
         config = self['xep_0004'].makeForm('submit')
         config.add_field(var='pubsub#node_type', value='leaf')
@@ -208,7 +227,7 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
         """ 
 
         msg = " Publish to Topic : " + node
-        self.debug(msg)
+        self.info(msg)
         try:
             result = self['xep_0060'].publish(self._server,node,payload=data)
             # id = result['pubsub']['publish']['item']['id']
@@ -231,7 +250,7 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
                 data)
             for item in result['pubsub']['items']['substanzas']:
                 msg = 'Retrieved item %s: %s' % (item['id'], tostring(item['payload']))
-                self.info(msg)
+                self.debug(msg)
         except:
             error = traceback.format_exc()
             msg = ' Could not retrieve item %s from topic %s\ntraceback:\n%s' \
@@ -248,7 +267,7 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
         try:
             result = self['xep_0060'].retract(self._server, self.boundjid, data)
             msg = ' Retracted item %s from topic %s' % (data, self.boundjid)
-            self.info(msg)
+            self.debug(msg)
         except:
             error = traceback.format_exc()
             msg = 'Could not retract item %s from topic %s\ntraceback:\n%s' \
@@ -262,7 +281,7 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
         try:
             result = self['xep_0060'].purge(self._server, self.boundjid)
             msg = ' Purged all items from topic %s' % self.boundjid
-            self.info(msg)
+            self.debug(msg)
         except:
             error = traceback.format_exc()
             msg = ' Could not purge items from topic %s\ntraceback:\n%s' \
@@ -280,8 +299,8 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
             result = self['xep_0060'].subscribe(self._server, node)
             msg = ' Subscribed %s to topic %s' \
                     % (self.boundjid.user, node)
-            self.info(msg)
-            #self.debug(msg)
+            #self.info(msg)
+            self.debug(msg)
         except:
             error = traceback.format_exc()
             msg = ' Could not subscribe %s to topic %s\ntraceback:\n%s' \
@@ -298,7 +317,7 @@ class OMFClient(sleekxmpp.ClientXMPP, Logger):
         try:
             result = self['xep_0060'].unsubscribe(self._server, node)
             msg = ' Unsubscribed %s from topic %s' % (self.boundjid.bare, node)
-            self.info(msg)
+            self.debug(msg)
         except:
             error = traceback.format_exc()
             msg = ' Could not unsubscribe %s from topic %s\ntraceback:\n%s' \