*Implement in_check_period/in_notification_period for livestatus to make multisite...
[shinken.git] / shinken / pyro_wrapper.py
blob8bb7d40ecbce1439ea7537d95499b10eeb9009d9
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
30 #Try to see if we are Python 3 or 4
31 try:
32 Pyro.core.ObjBase
33 #Some one already go here, so we are in 4 if None
34 if Pyro.core.ObjBase == None:
35 raise AttributeError
36 print "Using Pyro", Pyro.constants.VERSION
37 pyro_version = 3
38 protocol = 'PYROLOC'
39 Pyro.errors.CommunicationError = Pyro.errors.ProtocolError
42 def register(daemon, obj, name):
43 return daemon.connect(obj, name)
46 def unregister(daemon, obj):
47 daemon.disconnect(obj)
50 def get_sockets(daemon):
51 return daemon.getServerSockets()
54 def handleRequests(daemon, s):
55 daemon.handleRequests()
58 def init_daemon(host, port):
59 Pyro.core.initServer()
60 daemon = Pyro.core.Daemon(host=host, port=port)
61 if daemon.port != port:
62 print "Sorry, the port %d is not free" % port
63 sys.exit(1)
64 return daemon
67 def create_uri(address, port, obj_name):
68 return "PYROLOC://%s:%d/%s" % (address, port, obj_name)
70 #Timeout way is also changed between 3 and 4
71 #it's a method in 3, a property in 4
72 def set_timeout(con, timeout):
73 con._setTimeout(timeout)
76 def getProxy(uri):
77 return Pyro.core.getProxyForURI(uri)
80 except AttributeError:
81 print "Using Pyro", Pyro.constants.VERSION
82 pyro_version = 4
83 #Ok, in Pyro 4, interface do not need to
84 #inherit from ObjBase, just object is good
85 Pyro.core.ObjBase = object
86 Pyro.errors.URIError = Pyro.errors.ProtocolError
87 protocol = 'PYRO'
88 Pyro.core.getProxyForURI = Pyro.core.Proxy
89 #Hack for Pyro 4 : with it, there is
90 #no more way to send huge packet!
91 import socket
92 if hasattr(socket, 'MSG_WAITALL'):
93 del socket.MSG_WAITALL
96 def register(daemon, obj, name):
97 daemon.register(obj)
100 def unregister(daemon, obj, name):
101 daemon.unregister(obj)
104 def get_sockets(daemon):
105 return daemon.sockets()
108 def handleRequests(daemon, s):
109 daemon.handleRequests([s])
112 def init_daemon(host, port):
113 #Pyro 4 i by default thread, should do select
114 #(I hate threads!)
115 Pyro.config.SERVERTYPE="select"
116 #And port already use now raise an exception
117 try:
118 daemon = Pyro.core.Daemon(host=host, port=port)
119 except socket.error, exp:
120 print "Sorry, the port %d is not free : %s" % (port, str(exp))
121 sys.exit(1)
122 return daemon
125 def create_uri(address, port, obj_name):
126 return "PYRO:%s@%s:%d" % (obj_name, address, port)
129 def set_timeout(con, timeout):
130 con._pyroTimeout = timeout
133 def getProxy(uri):
134 return Pyro.core.Proxy(uri)