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 from nepi.resources.omf.omf5_api import OMF5API
27 from nepi.resources.omf.omf6_api import OMF6API
29 class OMFAPIFactory(object):
33 It allows the different RM to use the same xmpp client if they use
34 the same credentials. For the moment, it is focused on XMPP.
37 # use lock to avoid concurrent access to the Api list at the same times by 2
39 lock = threading.Lock()
43 def get_api(cls, version, server, user, port, password, exp_id = None):
46 :param version: OMF Version. Either 5 or 6
48 :param server: Xmpp Server Adress
50 :param user: Xmpp User
52 :param port: Xmpp Port (Default : 5222)
54 :param password: Xmpp Password
56 :param exp_id: Id of the experiment
60 if version and user and server and port and password:
61 key = cls._make_key(version, server, user, port, password, exp_id)
64 #print "Api Counter : " + str(cls._apis[key]['cnt'])
65 cls._apis[key]['cnt'] += 1
67 return cls._apis[key]['api']
69 omf_api = cls.create_api(version, server, user, port, password, exp_id)
75 def create_api(cls, version, server, user, port, password, exp_id):
76 """ Create an OMF API if this one doesn't exist yet with this credentials
78 :param version: OMF Version. Either 5 or 6
80 :param server: Xmpp Server Adress
82 :param user: Xmpp User
84 :param port: Xmpp Port (Default : 5222)
86 :param password: Xmpp Password
88 :param exp_id: Id of the experiment
92 key = cls._make_key(version, server, user, port, password, exp_id)
94 omf_api = OMF5API(server, user, port, password, exp_id = exp_id)
96 omf_api = OMF6API(server, user = user, port = port, password = password, exp_id = exp_id)
98 cls._apis[key]['api'] = omf_api
99 cls._apis[key]['cnt'] = 1
103 def release_api(cls, version, server, user, port, password, exp_id = None):
104 """ Release an OMF API with this credentials
106 :param version: OMF Version. Either 5 or 6
108 :param server: Xmpp Server Adress
110 :param user: Xmpp User
112 :param port: Xmpp Port (Default : 5222)
114 :param password: Xmpp Password
116 :param exp_id: Id of the experiment
120 if version and user and server and port and password:
121 key = cls._make_key(version, server, user, port, password, exp_id)
123 cls._apis[key]['cnt'] -= 1
124 #print "Api Counter : " + str(cls._apis[key]['cnt'])
125 if cls._apis[key]['cnt'] == 0:
126 omf_api = cls._apis[key]['api']
131 def _make_key(cls, *args):
132 """ Hash the credentials in order to create a key
134 :param args: list of arguments used to create the hash (server, user, port, ...)
138 skey = "".join(map(str, args))
139 return hashlib.md5(skey).hexdigest()