Fixing wrong license
[nepi.git] / doc / templates / template_api.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
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;
8 #
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.
13 #
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/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 #         Julien Tribino <julien.tribino@inria.fr>
19
20
21 import hashlib
22 import threading
23
24
25 class NewAPIFactory(object):
26     """ 
27         Class for the new Api Factoy
28
29     """
30     # use lock to avoid concurrent access to the Api list at the same times by 2 
31     # different threads
32     lock = threading.Lock()
33     _apis = dict()
34
35     @classmethod 
36     def get_api(cls, cred1, cred2):
37         """ Get an instance of the API depending on the credentials
38
39         """
40         if cred1 and cred2:
41             key = cls._make_key(cred1,cred2)
42             cls.lock.acquire()
43             if key in cls._apis:
44                 cls._apis[key]['cnt'] += 1
45                 cls.lock.release()
46                 return cls._apis[key]['api']
47             else :
48                 new_api = cls.create_api(cred1, cred2)
49                 cls.lock.release()
50                 return new_api
51         return None
52
53     @classmethod 
54     def create_api(cls, cred1, cred2):
55         """ Create an instance of the API depending on the credentials
56
57         """
58         key = cls._make_key(cred1,cred2)
59         new_api = ClientAPI(cred1,cred2)
60         cls._apis[key] = {}
61         cls._apis[key]['api'] = new_api
62         cls._apis[key]['cnt'] = 1
63         return new_api
64
65     @classmethod 
66     def release_api(cls, cred1, cred2):
67         """ Release the API with this credentials
68
69         """
70         if cred1 and cred2:
71             key = cls._make_key(cred1,cred2)
72             if key in cls._apis:
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
77                     new_api.disconnect()
78
79
80     @classmethod 
81     def _make_key(cls, *args):
82         """ Hash the credentials in order to create a key
83
84         :param args: list of arguments used to create the hash (server, user, port, ...)
85         :type args: list
86
87         """
88         skey = "".join(map(str, args))
89         return hashlib.md5(skey).hexdigest()
90
91
92