~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/rpc/drivers/Base.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# GNU Enterprise RPC interface - Base for all drivers
 
2
#
 
3
# Copyright 2001-2005 Free Software Foundation
 
4
#
 
5
# This file is part of GNU Enterprise.
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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.
 
21
#
 
22
# $Id: Base.py 7009 2005-02-11 17:04:16Z reinhard $
 
23
 
 
24
from types import *
 
25
 
 
26
import string
 
27
import sys
 
28
import thread
 
29
import traceback
 
30
import urlparse
 
31
 
 
32
from gnue.common.apps import i18n
 
33
 
 
34
# Indicate that this is not a valid plugin
 
35
__noplugin__ = True
 
36
 
 
37
# =============================================================================
 
38
# Client adapter
 
39
# =============================================================================
 
40
 
 
41
class Client:
 
42
  """
 
43
  Basic client adapter
 
44
  """
 
45
  _default_transport = None
 
46
  _default_host      = 'localhost'
 
47
  _default_port      = None
 
48
  _default_path      = '/'
 
49
 
 
50
  # ---------------------------------------------------------------------------
 
51
  # Initialize object
 
52
  # ---------------------------------------------------------------------------
 
53
 
 
54
  def __init__ (self, params):
 
55
 
 
56
    checktype (params, DictionaryType)
 
57
 
 
58
    self._url       = None
 
59
    self._transport = None
 
60
    self._host      = None
 
61
    self._port      = None
 
62
    self._path      = None
 
63
 
 
64
    if params.has_key ('url'):
 
65
 
 
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)
 
70
 
 
71
    else:
 
72
 
 
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'     ]
 
78
 
 
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
 
84
 
 
85
    # Make sure port is an integer
 
86
    self._port = int (self._port)
 
87
 
 
88
    # Now build the full URL
 
89
    self._url = '%s://%s:%d%s' % (self._transport, self._host, self._port,
 
90
                                  self._path)
 
91
 
 
92
    if params.has_key ('timeout'):
 
93
      self._timeout = params ['timeout']
 
94
    else:
 
95
      self._timeout = 1.0
 
96
 
 
97
    self.__open = True
 
98
 
 
99
  # ---------------------------------------------------------------------------
 
100
  # Set timeout
 
101
  # ---------------------------------------------------------------------------
 
102
 
 
103
  def setTimeout (self, timeout):
 
104
 
 
105
    self._timeout = timeout
 
106
 
 
107
  # ---------------------------------------------------------------------------
 
108
  # Request a (static) proxy object
 
109
  # ---------------------------------------------------------------------------
 
110
 
 
111
  def request (self, service):
 
112
 
 
113
    return ProxyObject (self, service, False)
 
114
 
 
115
  # ---------------------------------------------------------------------------
 
116
  # Close the connection
 
117
  # ---------------------------------------------------------------------------
 
118
 
 
119
  def close ():
 
120
    """
 
121
    Close the connection to the server
 
122
    """
 
123
    if self.__open:
 
124
      self._close ()
 
125
 
 
126
  # ---------------------------------------------------------------------------
 
127
  # Clean up
 
128
  # ---------------------------------------------------------------------------
 
129
 
 
130
  def __del__ (self):
 
131
 
 
132
    if self.__open:
 
133
      self._close ()
 
134
 
 
135
  # ---------------------------------------------------------------------------
 
136
  # Run a procedure on the server (abstract)
 
137
  # ---------------------------------------------------------------------------
 
138
 
 
139
  def _runMethod (self, method, *args, **params):
 
140
    """
 
141
    execute a remote method
 
142
    """
 
143
    pass
 
144
 
 
145
  # ---------------------------------------------------------------------------
 
146
  # Close the server (virtual)
 
147
  # ---------------------------------------------------------------------------
 
148
 
 
149
  def _close (self):
 
150
 
 
151
    pass
 
152
 
 
153
  # ---------------------------------------------------------------------------
 
154
  # Create a dynamic proxy object
 
155
  # ---------------------------------------------------------------------------
 
156
 
 
157
  def _createproxy (self, service):
 
158
 
 
159
    return ProxyObject (self, service, True)
 
160
 
 
161
# =============================================================================
 
162
# Proxy object for clients
 
163
# =============================================================================
 
164
 
 
165
class ProxyObject:
 
166
 
 
167
  # ---------------------------------------------------------------------------
 
168
  # Initialize proxy object
 
169
  # ---------------------------------------------------------------------------
 
170
 
 
171
  def __init__ (self, adapter, service, dynamic):
 
172
 
 
173
    self.__adapter = adapter
 
174
    self.__service = service
 
175
    self.__dynamic = dynamic
 
176
 
 
177
  # ---------------------------------------------------------------------------
 
178
  # Get a (proxy) method
 
179
  # ---------------------------------------------------------------------------
 
180
 
 
181
  def __getattr__ (self, attr):
 
182
 
 
183
    if attr[0] == '_':
 
184
      raise AttributeError, attr
 
185
 
 
186
    method = ProxyMethod (self.__adapter, self.__service + '.' + attr)
 
187
    self.__dict__ [attr] = method
 
188
    return method
 
189
 
 
190
  # ---------------------------------------------------------------------------
 
191
  # Set an object attribute (must start with '_')
 
192
  # ---------------------------------------------------------------------------
 
193
 
 
194
  def __setattr__ (self, attr, value):
 
195
 
 
196
    # FIXME: what do we need this for?
 
197
 
 
198
    if attr[0] == '_':
 
199
      self.__dict__[attr] = value
 
200
    else:
 
201
      raise AttributeError, attr
 
202
 
 
203
  # ---------------------------------------------------------------------------
 
204
  # Clean up
 
205
  # ---------------------------------------------------------------------------
 
206
 
 
207
  def __del__ (self):
 
208
 
 
209
    if self.__dynamic:
 
210
      self.__adapter._runMethod (self.__service + '._close')
 
211
 
 
212
# =============================================================================
 
213
# Proxy method for clients
 
214
# =============================================================================
 
215
 
 
216
class ProxyMethod:
 
217
 
 
218
  # ---------------------------------------------------------------------------
 
219
  # Initialize proxy method
 
220
  # ---------------------------------------------------------------------------
 
221
 
 
222
  def __init__ (self, adapter, methodname):
 
223
 
 
224
    self._adapter    = adapter
 
225
    self._methodname = methodname
 
226
 
 
227
  # ---------------------------------------------------------------------------
 
228
  # Run the method
 
229
  # ---------------------------------------------------------------------------
 
230
 
 
231
  def __call__ (self, *args, **params):
 
232
 
 
233
    gDebug (3, "%s (%s)" \
 
234
            % (self._methodname,
 
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)
 
239
 
 
240
# =============================================================================
 
241
# Server adapter
 
242
# =============================================================================
 
243
 
 
244
class Server:
 
245
  """
 
246
  Basic server adapter
 
247
  """
 
248
  _default_transport = None
 
249
  _default_port      = None
 
250
 
 
251
  # ---------------------------------------------------------------------------
 
252
  # Initialize server object
 
253
  # ---------------------------------------------------------------------------
 
254
 
 
255
  def __init__ (self, rpcdef, bindings, params):
 
256
    self._bindings = bindings
 
257
    self._rpcdef = rpcdef
 
258
 
 
259
    checktype (params, DictionaryType)
 
260
 
 
261
    self._transport = None
 
262
    self._port      = None
 
263
 
 
264
    if params.has_key ('transport'): self._transport = params ['transport']
 
265
    if params.has_key ('port'     ): self._port      = params ['port'     ]
 
266
 
 
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
 
270
 
 
271
    # Make sure port is an integer
 
272
    self._port = int (self._port)
 
273
 
 
274
  # ---------------------------------------------------------------------------
 
275
  # Start server
 
276
  # ---------------------------------------------------------------------------
 
277
 
 
278
  def serve (self):
 
279
    pass
 
280
 
 
281
  # ---------------------------------------------------------------------------
 
282
  # Start server as new thread
 
283
  # ---------------------------------------------------------------------------
 
284
 
 
285
  def serveAsNewThread (self):
 
286
    thread.start_new_thread (self.serve, ())
 
287
 
 
288
  # ---------------------------------------------------------------------------
 
289
  # Return an exception
 
290
  # ---------------------------------------------------------------------------
 
291
 
 
292
  def raiseException (self, exception, message, event=None):
 
293
    raise exception, message