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
30 #Try to see if we are Python 3 or 4
33 #Some one already go here, so we are in 4 if None
34 if Pyro
.core
.ObjBase
== None:
36 print "Using Pyro", Pyro
.constants
.VERSION
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
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
)
77 return Pyro
.core
.getProxyForURI(uri
)
80 except AttributeError:
81 print "Using Pyro", Pyro
.constants
.VERSION
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
88 Pyro
.core
.getProxyForURI
= Pyro
.core
.Proxy
89 #Hack for Pyro 4 : with it, there is
90 #no more way to send huge packet!
92 if hasattr(socket
, 'MSG_WAITALL'):
93 del socket
.MSG_WAITALL
96 def register(daemon
, obj
, name
):
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
115 Pyro
.config
.SERVERTYPE
="select"
116 #And port already use now raise an exception
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
))
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
134 return Pyro
.core
.Proxy(uri
)