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

« back to all changes in this revision

Viewing changes to src/rpc/drivers/proxy/CommDriver.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
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2001-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# proxy/CommDriver.py
 
23
#
 
24
# DESCRIPTION:
 
25
# Class that provides a "proxy" GComm driver (i.e, the client and server
 
26
# are actually run in the same program instance -- requests are "proxied"
 
27
# from the client to the server via this driver; no network activity occurs)
 
28
#
 
29
# NOTES:
 
30
#
 
31
# Client Parameters:
 
32
#
 
33
#   _proxy    The actual ServerAdapter instance. This has to be set by
 
34
#             the application, not by a config file.
 
35
#
 
36
 
 
37
#
 
38
# We provide both a client and a server driver...
 
39
#
 
40
CLIENT = 1      # ClientAdapter
 
41
SERVER = 1      # ServerAdapter
 
42
 
 
43
 
 
44
from gnue.common.rpc.drivers import GCommBase
 
45
from gnue.common.rpc import GComm
 
46
 
 
47
import string, sys
 
48
 
 
49
##############################################################################
 
50
#
 
51
# ClientAdapter
 
52
#
 
53
class ClientAdapter(GCommBase.Client):
 
54
 
 
55
  def __init__(self, params):
 
56
    #print "Proxy Client Adapter created"
 
57
    try:
 
58
      self._serverproxy = params['_proxy']
 
59
    except KeyError:
 
60
      tmsg = _('Proxy client adapter should be called with the server object')
 
61
      raise GComm.AdapterInitializationError, tmsg
 
62
 
 
63
  def request(self, service, params={}):
 
64
    proxy = _ProxyObject(self, service, \
 
65
        servicer=self._serverproxy._bindings[service],
 
66
        proxyinstance=self._serverproxy._bindings[service]())
 
67
    return proxy
 
68
 
 
69
 
 
70
 
 
71
 
 
72
##############################################################################
 
73
#
 
74
# ServerAdapter
 
75
#
 
76
class ServerAdapter(GCommBase.Server):
 
77
 
 
78
  def __init__(self, rpcdef, bindings, params):
 
79
    #print "Proxy Server Adapter created"
 
80
    GCommBase.Server.__init__(self, rpcdef, bindings, params)
 
81
 
 
82
  def raiseException(self, exception, message, event=None):
 
83
    raise exception, message
 
84
 
 
85
 
 
86
 
 
87
 
 
88
##############################################################################
 
89
#
 
90
# ProxyObject
 
91
#
 
92
class _ProxyObject(GCommBase.ProxyObject):
 
93
  def __init__(self, adapter, attr, parent=None, **params):
 
94
    GCommBase.ProxyObject.__init__(self, adapter, attr, parent=None, **params)
 
95
 
 
96
 
 
97
  # Server raised an exception...
 
98
  # Translate the exception into a local python
 
99
  # exception and raise it...
 
100
  def _exceptionRaised(self, data):
 
101
    if data.isGNUeGenerated():
 
102
      name, text = string.split(data.message,': ',1)
 
103
      # TODO: _lookup???
 
104
      exception = _lookup[name]
 
105
      raise exception, text
 
106
    else:
 
107
      # TODO:
 
108
      raise StandardError, data.message
 
109
 
 
110
 
 
111
  def __call__(self, *args, **params):
 
112
    #print "Calling %s" % self._adapter
 
113
    return self._adapter.runMethod(self, string.join(self._attrPath,'.'),
 
114
                                  *args, **params)
 
115
 
 
116