Making runId as sub folder optional for the Collector RM
[nepi.git] / src / nepi / resources / omf / omf_api.py
index 3316f01..edd1397 100644 (file)
@@ -29,8 +29,6 @@ from nepi.util.logger import Logger
 from nepi.resources.omf.omf_client import OMFClient
 from nepi.resources.omf.messages_5_4 import MessageHandler
 
-from nepi.util.timefuncs import tsfromat 
-
 class OMFAPI(Logger):
     """
     .. class:: Class Args :
@@ -48,10 +46,13 @@ class OMFAPI(Logger):
 
     .. note::
 
-       This class is the implementation of an OMF 5.4 API. Since the version 5.4.1, the Topic Architecture start with OMF_5.4 instead of OMF used for OMF5.3
+       This class is the implementation of an OMF 5.4 API. 
+       Since the version 5.4.1, the Topic Architecture start with OMF_5.4 
+       instead of OMF used for OMF5.3
 
     """
-    def __init__(self, slice, host, port, password, xmpp_root = None):
+    def __init__(self, slice, host, port, password, xmpp_root = None, 
+            exp_id = None):
         """
     
         :param slice: Xmpp Slice
@@ -67,11 +68,8 @@ class OMFAPI(Logger):
 
         """
         super(OMFAPI, self).__init__("OMFAPI")
-        
-        date = tsfromat()
-        tz = -time.altzone if time.daylight != 0 else -time.timezone
-        date += "%+06.2f" % (tz / 3600) # timezone difference is in seconds
-        self._user = "%s-%s" % (slice, date)
+        self._exp_id = exp_id 
+        self._user = "%s-%s" % (slice, self._exp_id)
         self._slice = slice
         self._host = host
         self._port = port
@@ -81,6 +79,7 @@ class OMFAPI(Logger):
 
         # OMF xmpp client
         self._client = None
+
         # message handler
         self._message = None
 
@@ -133,7 +132,8 @@ class OMFAPI(Logger):
         """ Publish New Experiment Message
 
         """
-        address = "/%s/%s/%s/%s" % (self._host, self._xmpp_root, self._slice, self._user)
+        address = "/%s/%s/%s/%s" % (self._host, self._xmpp_root, self._slice,
+                self._user)
         #print address
         payload = self._message.newexp_function(self._user, address)
         slice_sid = "/%s/%s" % (self._xmpp_root, self._slice)
@@ -160,7 +160,8 @@ class OMFAPI(Logger):
         :type hostname: str
 
         """
-        return "/%s/%s/%s/%s" % (self._xmpp_root, self._slice, self._user, hostname)
+        return "/%s/%s/%s/%s" % (self._xmpp_root, self._slice, self._user, 
+                hostname)
 
     def _host_resource_id(self, hostname):
         """ Return the Topic Name as /xmpp_root/slice/resources/hostname
@@ -201,7 +202,8 @@ class OMFAPI(Logger):
         self._client.delete(xmpp_node)
 
     def enroll_host(self, hostname):
-        """ Create and Subscribe to the session topic and the resources corresponding to the hostname
+        """ Create and Subscribe to the session topic and the resources
+            corresponding to the hostname
 
         :param hostname: Full hrn of the node
         :type hostname: str
@@ -227,22 +229,43 @@ class OMFAPI(Logger):
 
         :param hostname: Full hrn of the node
         :type hostname: str
-        :param attribute: Attribute that need to be configured (often written as /net/wX/attribute, with X the interface number)
+        :param attribute: Attribute that need to be configured (
+            often written as /net/wX/attribute, with X the interface number)
         :type attribute: str
         :param value: Value of the attribute
         :type value: str
 
         """
+        
         payload = self._message.configure_function(hostname, value, attribute)
         xmpp_node =  self._host_session_id(hostname)
         self._client.publish(payload, xmpp_node)
 
+    
+    def send_stdin(self, hostname, value, app_id):
+        """ Send to the stdin of the application the value
+
+        :param hostname: Full hrn of the node
+        :type hostname: str
+        :param appid: Application Id (Any id that represents in a unique 
+            way the application)
+        :type appid: str
+        :param value: parameter to execute in the stdin of the application
+        :type value: str
+
+        """
+        payload = self._message.stdin_function(hostname, value, app_id)
+        xmpp_node =  self._host_session_id(hostname)
+        self._client.publish(payload, xmpp_node)
+
+
     def execute(self, hostname, app_id, arguments, path, env):
         """ Execute command on the node
 
         :param hostname: Full hrn of the node
         :type hostname: str
-        :param app_id: Application Id (Any id that represents in a unique way the application)
+        :param app_id: Application Id (Any id that represents in a unique 
+            way the application)
         :type app_id: str
         :param arguments: Arguments of the application
         :type arguments: str
@@ -252,7 +275,8 @@ class OMFAPI(Logger):
         :type env: str
 
         """
-        payload = self._message.execute_function(hostname, app_id, arguments, path, env)
+        payload = self._message.execute_function(hostname, app_id, arguments, 
+                path, env)
         xmpp_node =  self._host_session_id(hostname)
         self._client.publish(payload, xmpp_node)
 
@@ -295,16 +319,17 @@ class OMFAPIFactory(object):
     """ 
     .. note::
 
-        It allows the different RM to use the same xmpp client if they use the same credentials. 
-        For the moment, it is focused on Xmpp.
+        It allows the different RM to use the same xmpp client if they use 
+        the same credentials.  For the moment, it is focused on XMPP.
 
     """
-    # use lock to avoid concurrent access to the Api list at the same times by 2 different threads
+    # use lock to avoid concurrent access to the Api list at the same times by 2 
+    # different threads
     lock = threading.Lock()
     _apis = dict()
 
     @classmethod 
-    def get_api(cls, slice, host, port, password):
+    def get_api(cls, slice, host, port, password, exp_id = None):
         """ Get an OMF Api
 
         :param slice: Xmpp Slice Name
@@ -318,7 +343,7 @@ class OMFAPIFactory(object):
 
         """
         if slice and host and port and password:
-            key = cls._make_key(slice, host, port, password)
+            key = cls._make_key(slice, host, port, password, exp_id)
             cls.lock.acquire()
             if key in cls._apis:
                 #print "Api Counter : " + str(cls._apis[key]['cnt'])
@@ -326,13 +351,13 @@ class OMFAPIFactory(object):
                 cls.lock.release()
                 return cls._apis[key]['api']
             else :
-                omf_api = cls.create_api(slice, host, port, password)
+                omf_api = cls.create_api(slice, host, port, password, exp_id)
                 cls.lock.release()
                 return omf_api
         return None
 
     @classmethod 
-    def create_api(cls, slice, host, port, password):
+    def create_api(cls, slice, host, port, password, exp_id):
         """ Create an OMF API if this one doesn't exist yet with this credentials
 
         :param slice: Xmpp Slice Name
@@ -345,15 +370,15 @@ class OMFAPIFactory(object):
         :type password: str
 
         """
-        omf_api = OMFAPI(slice, host, port, password)
-        key = cls._make_key(slice, host, port, password)
+        omf_api = OMFAPI(slice, host, port, password, exp_id = exp_id)
+        key = cls._make_key(slice, host, port, password, exp_id)
         cls._apis[key] = {}
         cls._apis[key]['api'] = omf_api
         cls._apis[key]['cnt'] = 1
         return omf_api
 
     @classmethod 
-    def release_api(cls, slice, host, port, password):
+    def release_api(cls, slice, host, port, password, exp_id = None):
         """ Release an OMF API with this credentials
 
         :param slice: Xmpp Slice Name
@@ -367,7 +392,7 @@ class OMFAPIFactory(object):
 
         """
         if slice and host and port and password:
-            key = cls._make_key(slice, host, port, password)
+            key = cls._make_key(slice, host, port, password, exp_id)
             if key in cls._apis:
                 cls._apis[key]['cnt'] -= 1
                 #print "Api Counter : " + str(cls._apis[key]['cnt'])