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 class NewAPIFactory(object):
27 Class for the new Api Factoy
30 # use lock to avoid concurrent access to the Api list at the same times by 2
32 lock = threading.Lock()
36 def get_api(cls, cred1, cred2):
37 """ Get an instance of the API depending on the credentials
41 key = cls._make_key(cred1,cred2)
44 cls._apis[key]['cnt'] += 1
46 return cls._apis[key]['api']
48 new_api = cls.create_api(cred1, cred2)
54 def create_api(cls, cred1, cred2):
55 """ Create an instance of the API depending on the credentials
58 key = cls._make_key(cred1,cred2)
59 new_api = ClientAPI(cred1,cred2)
61 cls._apis[key]['api'] = new_api
62 cls._apis[key]['cnt'] = 1
66 def release_api(cls, cred1, cred2):
67 """ Release the API with this credentials
71 key = cls._make_key(cred1,cred2)
73 cls._apis[key]['cnt'] -= 1
74 if cls._apis[key]['cnt'] == 0:
75 new_api = cls._apis[key]['api']
76 # if necessary, we can disconnect
81 def _make_key(cls, *args):
82 """ Hash the credentials in order to create a key
84 :param args: list of arguments used to create the hash (server, user, port, ...)
88 skey = "".join(map(str, args))
89 return hashlib.md5(skey).hexdigest()