1
# GNU Enterprise RPC interface - Base for all drivers
3
# Copyright 2001-2005 Free Software Foundation
5
# This file is part of GNU Enterprise.
7
# GNU Enterprise is free software; you can redistribute it
8
# and/or modify it under the terms of the GNU General Public
9
# License as published by the Free Software Foundation; either
10
# version 2, or (at your option) any later version.
12
# GNU Enterprise is distributed in the hope that it will be
13
# useful, but WITHOUT ANY WARRANTY; without even the implied
14
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
# PURPOSE. See the GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public
18
# License along with program; see the file COPYING. If not,
19
# write to the Free Software Foundation, Inc., 59 Temple Place
20
# - Suite 330, Boston, MA 02111-1307, USA.
22
# $Id: Base.py 7009 2005-02-11 17:04:16Z reinhard $
32
from gnue.common.apps import i18n
34
# Indicate that this is not a valid plugin
37
# =============================================================================
39
# =============================================================================
45
_default_transport = None
46
_default_host = 'localhost'
50
# ---------------------------------------------------------------------------
52
# ---------------------------------------------------------------------------
54
def __init__ (self, params):
56
checktype (params, DictionaryType)
59
self._transport = None
64
if params.has_key ('url'):
66
# Connection defined as URL
67
(self._transport, netloc, self._path, params, query, fragment) \
68
= urlparse.urlparse (params ['url'])
69
(self._host, self._port) = split (netloc, ':', 1)
73
# Connection defined as transport/host/port/path info
74
if params.has_key ('transport'): self._transport = params ['transport']
75
if params.has_key ('host' ): self._host = params ['host' ]
76
if params.has_key ('port' ): self._port = params ['port' ]
77
if params.has_key ('path' ): self._path = params ['path' ]
79
# If some info isn't given, fall back to default values
80
if not self._transport: self._transport = self._default_transport
81
if not self._host: self._host = self._default_host
82
if not self._port: self._port = self._default_port
83
if not self._path: self._path = self._default_path
85
# Make sure port is an integer
86
self._port = int (self._port)
88
# Now build the full URL
89
self._url = '%s://%s:%d%s' % (self._transport, self._host, self._port,
92
if params.has_key ('timeout'):
93
self._timeout = params ['timeout']
99
# ---------------------------------------------------------------------------
101
# ---------------------------------------------------------------------------
103
def setTimeout (self, timeout):
105
self._timeout = timeout
107
# ---------------------------------------------------------------------------
108
# Request a (static) proxy object
109
# ---------------------------------------------------------------------------
111
def request (self, service):
113
return ProxyObject (self, service, False)
115
# ---------------------------------------------------------------------------
116
# Close the connection
117
# ---------------------------------------------------------------------------
121
Close the connection to the server
126
# ---------------------------------------------------------------------------
128
# ---------------------------------------------------------------------------
135
# ---------------------------------------------------------------------------
136
# Run a procedure on the server (abstract)
137
# ---------------------------------------------------------------------------
139
def _runMethod (self, method, *args, **params):
141
execute a remote method
145
# ---------------------------------------------------------------------------
146
# Close the server (virtual)
147
# ---------------------------------------------------------------------------
153
# ---------------------------------------------------------------------------
154
# Create a dynamic proxy object
155
# ---------------------------------------------------------------------------
157
def _createproxy (self, service):
159
return ProxyObject (self, service, True)
161
# =============================================================================
162
# Proxy object for clients
163
# =============================================================================
167
# ---------------------------------------------------------------------------
168
# Initialize proxy object
169
# ---------------------------------------------------------------------------
171
def __init__ (self, adapter, service, dynamic):
173
self.__adapter = adapter
174
self.__service = service
175
self.__dynamic = dynamic
177
# ---------------------------------------------------------------------------
178
# Get a (proxy) method
179
# ---------------------------------------------------------------------------
181
def __getattr__ (self, attr):
184
raise AttributeError, attr
186
method = ProxyMethod (self.__adapter, self.__service + '.' + attr)
187
self.__dict__ [attr] = method
190
# ---------------------------------------------------------------------------
191
# Set an object attribute (must start with '_')
192
# ---------------------------------------------------------------------------
194
def __setattr__ (self, attr, value):
196
# FIXME: what do we need this for?
199
self.__dict__[attr] = value
201
raise AttributeError, attr
203
# ---------------------------------------------------------------------------
205
# ---------------------------------------------------------------------------
210
self.__adapter._runMethod (self.__service + '._close')
212
# =============================================================================
213
# Proxy method for clients
214
# =============================================================================
218
# ---------------------------------------------------------------------------
219
# Initialize proxy method
220
# ---------------------------------------------------------------------------
222
def __init__ (self, adapter, methodname):
224
self._adapter = adapter
225
self._methodname = methodname
227
# ---------------------------------------------------------------------------
229
# ---------------------------------------------------------------------------
231
def __call__ (self, *args, **params):
233
gDebug (3, "%s (%s)" \
235
string.join ([repr (x) for x in args] + \
236
["%s = %s" % (x [0], repr (x [1])) \
237
for x in params.items ()], ', ')))
238
return self._adapter._runMethod (self._methodname, *args, **params)
240
# =============================================================================
242
# =============================================================================
248
_default_transport = None
251
# ---------------------------------------------------------------------------
252
# Initialize server object
253
# ---------------------------------------------------------------------------
255
def __init__ (self, rpcdef, bindings, params):
256
self._bindings = bindings
257
self._rpcdef = rpcdef
259
checktype (params, DictionaryType)
261
self._transport = None
264
if params.has_key ('transport'): self._transport = params ['transport']
265
if params.has_key ('port' ): self._port = params ['port' ]
267
# If some info isn't given, fall back to default values
268
if not self._transport: self._transport = self._default_transport
269
if not self._port: self._port = self._default_port
271
# Make sure port is an integer
272
self._port = int (self._port)
274
# ---------------------------------------------------------------------------
276
# ---------------------------------------------------------------------------
281
# ---------------------------------------------------------------------------
282
# Start server as new thread
283
# ---------------------------------------------------------------------------
285
def serveAsNewThread (self):
286
thread.start_new_thread (self.serve, ())
288
# ---------------------------------------------------------------------------
289
# Return an exception
290
# ---------------------------------------------------------------------------
292
def raiseException (self, exception, message, event=None):
293
raise exception, message