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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 # Julien Tribino <julien.tribino@inria.fr>
26 class NewAPIFactory(object):
28 Class for the new Api Factoy
31 # use lock to avoid concurrent access to the Api list at the same times by 2
33 lock = threading.Lock()
37 def get_api(cls, cred1, cred2):
38 """ Get an instance of the API depending on the credentials
42 key = cls._make_key(cred1,cred2)
45 cls._apis[key]['cnt'] += 1
47 return cls._apis[key]['api']
49 new_api = cls.create_api(cred1, cred2)
55 def create_api(cls, cred1, cred2):
56 """ Create an instance of the API depending on the credentials
59 key = cls._make_key(cred1,cred2)
60 new_api = ClientAPI(cred1,cred2)
62 cls._apis[key]['api'] = new_api
63 cls._apis[key]['cnt'] = 1
67 def release_api(cls, cred1, cred2):
68 """ Release the API with this credentials
72 key = cls._make_key(cred1,cred2)
74 cls._apis[key]['cnt'] -= 1
75 if cls._apis[key]['cnt'] == 0:
76 new_api = cls._apis[key]['api']
77 # if necessary, we can disconnect
82 def _make_key(cls, *args):
83 """ Hash the credentials in order to create a key
85 :param args: list of arguments used to create the hash (server, user, port, ...)
89 skey = "".join(map(str, args))
90 return hashlib.md5(skey).hexdigest()