Add : the full demoCA certification autority so people can sign their own keys.
[shinken.git] / shinken / pyro_wrapper.py
blobcac7b73d20f719dda20a7a6c519e32665c1edd8b
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 #This class is a wrapper for managing Pyro 3 and 4 version
25 import sys
26 import Pyro.core
29 #Try to see if we are Python 3 or 4
30 try:
31 Pyro.core.ObjBase
32 #Some one already go here, so we are in 4 if None
33 if Pyro.core.ObjBase == None:
34 raise AttributeError
35 print "Using Pyro", Pyro.constants.VERSION
36 pyro_version = 3
37 protocol = 'PYROLOC'
38 Pyro.errors.CommunicationError = Pyro.errors.ProtocolError
41 def register(daemon, obj, name):
42 return daemon.connect(obj, name)
45 def unregister(daemon, obj):
46 daemon.disconnect(obj)
49 def get_sockets(daemon):
50 return daemon.getServerSockets()
53 def handleRequests(daemon, s):
54 daemon.handleRequests()
57 def init_daemon(host, port, use_ssl=False):
58 Pyro.core.initServer()
59 if use_ssl:
60 daemon = Pyro.core.Daemon(host=host, port=port, prtcol='PYROSSL')
61 else:
62 daemon = Pyro.core.Daemon(host=host, port=port)
63 if daemon.port != port:
64 print "Sorry, the port %d is not free" % port
65 sys.exit(1)
66 return daemon
69 def create_uri(address, port, obj_name, use_ssl):
70 if not use_ssl:
71 return "PYROLOC://%s:%d/%s" % (address, port, obj_name)
72 else:
73 return "PYROLOCSSL://%s:%d/%s" % (address, port, obj_name)
75 #Timeout way is also changed between 3 and 4
76 #it's a method in 3, a property in 4
77 def set_timeout(con, timeout):
78 con._setTimeout(timeout)
81 def getProxy(uri):
82 return Pyro.core.getProxyForURI(uri)
85 except AttributeError:
86 print "Using Pyro", Pyro.constants.VERSION
87 pyro_version = 4
88 #Ok, in Pyro 4, interface do not need to
89 #inherit from ObjBase, just object is good
90 Pyro.core.ObjBase = object
91 Pyro.errors.URIError = Pyro.errors.ProtocolError
92 protocol = 'PYRO'
93 Pyro.core.getProxyForURI = Pyro.core.Proxy
94 #Hack for Pyro 4 : with it, there is
95 #no more way to send huge packet!
96 import socket
97 if hasattr(socket, 'MSG_WAITALL'):
98 del socket.MSG_WAITALL
101 def register(daemon, obj, name):
102 return daemon.register(obj, name)
105 def unregister(daemon, obj, name):
106 daemon.unregister(obj)
109 def get_sockets(daemon):
110 return daemon.sockets()
113 def handleRequests(daemon, s):
114 daemon.handleRequests([s])
117 def init_daemon(host, port, use_ssl=False):
118 #Pyro 4 i by default thread, should do select
119 #(I hate threads!)
120 Pyro.config.SERVERTYPE="select"
121 #And port already use now raise an exception
122 try:
123 daemon = Pyro.core.Daemon(host=host, port=port)
124 except socket.error, exp:
125 print "Sorry, the port %d is not free : %s" % (port, str(exp))
126 sys.exit(1)
127 return daemon
130 def create_uri(address, port, obj_name, use_ssl=False):
131 return "PYRO:%s@%s:%d" % (obj_name, address, port)
134 def set_timeout(con, timeout):
135 con._pyroTimeout = timeout
138 def getProxy(uri):
139 return Pyro.core.Proxy(uri)