X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fnepi%2Fresources%2Fomf%2Fomf_api.py;h=edd139710deab810fde683e33023030728828785;hb=a88506e43bebe8f21cf735551ee151722ee4925e;hp=df4fe03498d4112d7ce91fd625d9c73cdc6175d2;hpb=dc8f5de864464f4a2ccbaa73ac881f50d2c64899;p=nepi.git diff --git a/src/nepi/resources/omf/omf_api.py b/src/nepi/resources/omf/omf_api.py index df4fe034..edd13971 100644 --- a/src/nepi/resources/omf/omf_api.py +++ b/src/nepi/resources/omf/omf_api.py @@ -1,23 +1,23 @@ -""" - NEPI, a framework to manage network experiments - Copyright (C) 2013 INRIA +# +# 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 . +# +# Author: Alina Quereilhac +# Julien Tribino - 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 . - -""" - -import datetime import ssl import sys import time @@ -34,42 +34,42 @@ class OMFAPI(Logger): .. class:: Class Args : :param slice: Xmpp Slice - :type slice: Str + :type slice: str :param host: Xmpp Server - :type host: Str + :type host: str :param port: Xmpp Port - :type port: Str + :type port: str :param password: Xmpp password - :type password: Str + :type password: str :param xmpp_root: Root of the Xmpp Topic Architecture - :type xmpp_root: Str + :type xmpp_root: str .. 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 - :type slice: Str + :type slice: str :param host: Xmpp Server - :type host: Str + :type host: str :param port: Xmpp Port - :type port: Str + :type port: str :param password: Xmpp password - :type password: Str + :type password: str :param xmpp_root: Root of the Xmpp Topic Architecture - :type xmpp_root: Str + :type xmpp_root: str """ super(OMFAPI, self).__init__("OMFAPI") - - date = datetime.datetime.now().strftime("%Y-%m-%dt%H.%M.%S") - 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 @@ -79,6 +79,7 @@ class OMFAPI(Logger): # OMF xmpp client self._client = None + # message handler self._message = None @@ -131,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) @@ -158,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 @@ -199,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 @@ -225,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 @@ -250,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) @@ -293,17 +319,18 @@ 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): - """ Get an Api + def get_api(cls, slice, host, port, password, exp_id = None): + """ Get an OMF Api :param slice: Xmpp Slice Name :type slice: str @@ -316,21 +343,22 @@ 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']) cls._apis[key]['cnt'] += 1 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): - """ Create an API if this one doesn't exist yet with this credentials + 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 :type slice: str @@ -342,16 +370,16 @@ 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): - """ Release an API with this credentials + def release_api(cls, slice, host, port, password, exp_id = None): + """ Release an OMF API with this credentials :param slice: Xmpp Slice Name :type slice: str @@ -364,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'])