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
29 #Try to see if we are Python 3 or 4
32 #Some one already go here, so we are in 4 if None
33 if Pyro
.core
.ObjBase
== None:
35 print "Using Pyro", Pyro
.constants
.VERSION
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()
60 daemon
= Pyro
.core
.Daemon(host
=host
, port
=port
, prtcol
='PYROSSL')
62 daemon
= Pyro
.core
.Daemon(host
=host
, port
=port
)
63 if daemon
.port
!= port
:
64 print "Sorry, the port %d is not free" % port
69 def create_uri(address
, port
, obj_name
, use_ssl
):
71 return "PYROLOC://%s:%d/%s" % (address
, port
, obj_name
)
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
)
82 return Pyro
.core
.getProxyForURI(uri
)
85 except AttributeError:
86 print "Using Pyro", Pyro
.constants
.VERSION
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
93 Pyro
.core
.getProxyForURI
= Pyro
.core
.Proxy
94 #Hack for Pyro 4 : with it, there is
95 #no more way to send huge packet!
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
120 Pyro
.config
.SERVERTYPE
="select"
121 #And port already use now raise an exception
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
))
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
139 return Pyro
.core
.Proxy(uri
)