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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 # Julien Tribino <julien.tribino@inria.fr>
25 from nepi.resources.omf.omf5_api import OMF5API
26 from nepi.resources.omf.omf6_api import OMF6API
28 class OMFAPIFactory(object):
32 It allows the different RM to use the same xmpp client if they use
33 the same credentials. For the moment, it is focused on XMPP.
36 # use lock to avoid concurrent access to the Api list at the same times by 2
38 lock = threading.Lock()
42 def get_api(cls, version, server, user, port, password, exp_id = None):
45 :param version: OMF Version. Either 5 or 6
47 :param server: Xmpp Server Adress
49 :param user: Xmpp User
51 :param port: Xmpp Port (Default : 5222)
53 :param password: Xmpp Password
55 :param exp_id: Id of the experiment
59 if version and user and server and port and password:
60 key = cls._make_key(version, server, user, port, password, exp_id)
63 #print "Api Counter : " + str(cls._apis[key]['cnt'])
64 cls._apis[key]['cnt'] += 1
66 return cls._apis[key]['api']
68 omf_api = cls.create_api(version, server, user, port, password, exp_id)
74 def create_api(cls, version, server, user, port, password, exp_id):
75 """ Create an OMF API if this one doesn't exist yet with this credentials
77 :param version: OMF Version. Either 5 or 6
79 :param server: Xmpp Server Adress
81 :param user: Xmpp User
83 :param port: Xmpp Port (Default : 5222)
85 :param password: Xmpp Password
87 :param exp_id: Id of the experiment
91 key = cls._make_key(version, server, user, port, password, exp_id)
93 omf_api = OMF5API(server, user, port, password, exp_id = exp_id)
95 omf_api = OMF6API(server, user = user, port = port, password = password, exp_id = exp_id)
97 cls._apis[key]['api'] = omf_api
98 cls._apis[key]['cnt'] = 1
102 def release_api(cls, version, server, user, port, password, exp_id = None):
103 """ Release an OMF API with this credentials
105 :param version: OMF Version. Either 5 or 6
107 :param server: Xmpp Server Adress
109 :param user: Xmpp User
111 :param port: Xmpp Port (Default : 5222)
113 :param password: Xmpp Password
115 :param exp_id: Id of the experiment
119 if version and user and server and port and password:
120 key = cls._make_key(version, server, user, port, password, exp_id)
122 cls._apis[key]['cnt'] -= 1
123 #print "Api Counter : " + str(cls._apis[key]['cnt'])
124 if cls._apis[key]['cnt'] == 0:
125 omf_api = cls._apis[key]['api']
130 def _make_key(cls, *args):
131 """ Hash the credentials in order to create a key
133 :param args: list of arguments used to create the hash (server, user, port, ...)
137 skey = "".join(map(str, args))
138 return hashlib.md5(skey).hexdigest()