Fix : fix hot module under windows test. (at leat I hope...)
[shinken.git] / shinken / pyro_wrapper.py
blob21261444b484c46f0786a36cebfc2c8152a077b1
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
24 #This class is a wrapper for managing Pyro 3 and 4 version
26 import select, errno
28 import Pyro.core
31 class InvalidWorkDir(Exception): pass
32 class PortNotFree(Exception): pass
36 #Try to see if we are Python 3 or 4
37 try:
38 Pyro.core.ObjBase
39 #Some one already go here, so we are in 4 if None
40 if Pyro.core.ObjBase is None:
41 raise AttributeError
42 print "Using Pyro", Pyro.constants.VERSION
44 Pyro.errors.CommunicationError = Pyro.errors.ProtocolError
46 class Pyro3Daemon(Pyro.core.Daemon):
47 pyro_version = 3
48 protocol = 'PYROLOC'
50 def __init__(self, host, port, use_ssl=False):
51 try:
52 Pyro.core.initServer()
53 except OSError, e: # must be problem with workdir :
54 raise InvalidWorkDir(e)
55 if use_ssl:
56 prtcol = 'PYROSSL'
57 else:
58 prtcol = 'PYRO'
59 try:
60 Pyro.core.Daemon.__init__(self, host=host, port=port, prtcol=prtcol, norange=True)
61 except OSError, e:
62 # must be problem with workdir :
63 raise InvalidWorkDir(e)
64 except Pyro.errors.DaemonError, e:
65 msg = "Sorry, the port %d is not free: %s" % (port, e)
66 raise PortNotFree(msg)
68 def register(self, obj, name):
69 return self.connect(obj, name)
71 def unregister(self, obj):
72 self.disconnect(obj)
74 def get_sockets(self):
75 return self.getServerSockets()
77 def handleRequests(self, s):
78 Pyro.core.Daemon.handleRequests(self)
80 def create_uri(address, port, obj_name, use_ssl):
81 if not use_ssl:
82 return "PYROLOC://%s:%d/%s" % (address, port, obj_name)
83 else:
84 return "PYROLOCSSL://%s:%d/%s" % (address, port, obj_name)
86 # Timeout way is also changed between 3 and 4
87 # it's a method in 3, a property in 4
88 def set_timeout(con, timeout):
89 con._setTimeout(timeout)
91 def getProxy(uri):
92 return Pyro.core.getProxyForURI(uri)
95 PyroClass = Pyro3Daemon
98 except AttributeError:
100 print "Using Pyro", Pyro.constants.VERSION
102 # Ok, in Pyro 4, interface do not need to
103 # inherit from ObjBase, just object is good
104 Pyro.core.ObjBase = object
105 Pyro.errors.URIError = Pyro.errors.ProtocolError
106 Pyro.core.getProxyForURI = Pyro.core.Proxy
108 # Hack for Pyro 4 : with it, there is
109 # no more way to send huge packet!
110 import socket
111 if hasattr(socket, 'MSG_WAITALL'):
112 del socket.MSG_WAITALL
114 class Pyro4Daemon(Pyro.core.Daemon):
115 pyro_version = 4
116 protocol = 'PYRO'
118 def __init__(self, host, port, use_ssl=False):
119 #Pyro 4 i by default thread, should do select
120 #(I hate threads!)
121 Pyro.config.SERVERTYPE = "select"
122 #And port already use now raise an exception
123 try:
124 Pyro.core.Daemon.__init__(self, host=host, port=port)
125 except socket.error, exp:
126 msg = "Sorry, the port %d is not free : %s" % (port, str(exp))
127 raise PortNotFree(msg)
128 except Exception, e:
129 # must be problem with pyro workdir :
130 raise InvalidWorkDir(e)
132 ## same than this super class so no need:
133 # def register(self, obj, name):
134 # def unregister(self, obj, name):
137 def get_sockets(self):
138 return self.sockets()
140 def handleRequests(self, s):
141 Pyro.core.Daemon.handleRequests(self, [s])
144 def create_uri(address, port, obj_name, use_ssl=False):
145 return "PYRO:%s@%s:%d" % (obj_name, address, port)
148 def set_timeout(con, timeout):
149 con._pyroTimeout = timeout
152 def getProxy(uri):
153 return Pyro.core.Proxy(uri)
156 PyroClass = Pyro4Daemon
160 class ShinkenPyroDaemon(PyroClass):
162 def get_socks_activity(self, timeout):
163 try:
164 ins, _, _ = select.select(self.get_sockets(), [], [], timeout)
165 except select.error, e:
166 errnum, _ = e
167 if errnum == errno.EINTR:
168 return []
169 raise
170 return ins