README moves to markdown
[nepi.git] / nepi / resources / omf / omf_api_factory.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 time
22 import hashlib
23 import threading
24
25 from nepi.resources.omf.omf5_api import OMF5API
26 from nepi.resources.omf.omf6_api import OMF6API
27
28 class OMFAPIFactory(object):
29     """ 
30     .. note::
31
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.
34
35     """
36     # use lock to avoid concurrent access to the Api list at the same times by 2 
37     # different threads
38     lock = threading.Lock()
39     _apis = dict()
40
41     @classmethod 
42     def get_api(cls, version, server, user, port, password, exp_id = None):
43         """ Get an OMF Api
44
45         :param version: OMF Version. Either 5 or 6
46         :type version: str
47         :param server: Xmpp Server Adress
48         :type server: str
49         :param user: Xmpp User
50         :type user: str
51         :param port: Xmpp Port (Default : 5222)
52         :type port: str
53         :param password: Xmpp Password
54         :type password: str
55         :param exp_id: Id of the experiment
56         :type exp_id: str
57
58         """
59         if version and user and server and port and password:
60             key = cls._make_key(version, server, user, port, password, exp_id)
61             cls.lock.acquire()
62             if key in cls._apis:
63                 #print "Api Counter : " + str(cls._apis[key]['cnt'])
64                 cls._apis[key]['cnt'] += 1
65                 cls.lock.release()
66                 return cls._apis[key]['api']
67             else :
68                 omf_api = cls.create_api(version, server, user, port, password, exp_id)
69                 cls.lock.release()
70                 return omf_api
71         return None
72
73     @classmethod 
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
76
77         :param version: OMF Version. Either 5 or 6
78         :type version: str
79         :param server: Xmpp Server Adress
80         :type server: str
81         :param user: Xmpp User
82         :type user: str
83         :param port: Xmpp Port (Default : 5222)
84         :type port: str
85         :param password: Xmpp Password
86         :type password: str
87         :param exp_id: Id of the experiment
88         :type exp_id: str
89
90         """
91         key = cls._make_key(version, server, user, port, password, exp_id)
92         if version == "5":
93             omf_api = OMF5API(server, user, port, password, exp_id = exp_id)
94         else :
95             omf_api = OMF6API(server, user = user, port = port, password = password, exp_id = exp_id)
96         cls._apis[key] = {}
97         cls._apis[key]['api'] = omf_api
98         cls._apis[key]['cnt'] = 1
99         return omf_api
100
101     @classmethod 
102     def release_api(cls, version, server, user, port, password, exp_id = None):
103         """ Release an OMF API with this credentials
104
105         :param version: OMF Version. Either 5 or 6
106         :type version: str
107         :param server: Xmpp Server Adress
108         :type server: str
109         :param user: Xmpp User
110         :type user: str
111         :param port: Xmpp Port (Default : 5222)
112         :type port: str
113         :param password: Xmpp Password
114         :type password: str
115         :param exp_id: Id of the experiment
116         :type exp_id: str
117
118         """
119         if version and user and server and port and password:
120             key = cls._make_key(version, server, user, port, password, exp_id)
121             if key in cls._apis:
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']
126                     omf_api.disconnect()
127                     del cls._apis[key]
128
129     @classmethod 
130     def _make_key(cls, *args):
131         """ Hash the credentials in order to create a key
132
133         :param args: list of arguments used to create the hash (server, user, port, ...)
134         :type args: list
135
136         """
137         skey = "".join(map(str, args))
138         return hashlib.md5(skey).hexdigest()
139
140
141