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