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

« back to all changes in this revision

Viewing changes to src/rpc/drivers/sockets/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
# sockets/CommDriver.py
 
23
#
 
24
# DESCRIPTION:
 
25
# Class that implements a socket-based driver for GNUe Comm.
 
26
#
 
27
# NOTES:
 
28
# Unlike the other drivers, this driver implements it's own, proprietary 
 
29
# (but not unfree :) interface.  The client portion of this driver is only
 
30
# intended to connect to instances of the Server contained in this file
 
31
# (i.e., the client isn't designed to connect to, e.g., a PostgreSQL socket
 
32
# interface).   
 
33
#
 
34
# Server parameters:
 
35
#
 
36
#    socket      The filename of the file socket to create. 
 
37
#                (e.g., /tmp/gnue/MyApp.socket) 
 
38
#    host        The hostname or IP address of the service
 
39
#    port        The port that the service is located on.
 
40
#
 
41
#                Note: If both socket and host,port are specified, then
 
42
#                      both a sockets file and a TCP port are opened.
 
43
#
 
44
# Client parameters:
 
45
#
 
46
#    socket      The filename of the file socket.
 
47
#                (e.g., /tmp/gnue/MyApp.socket) 
 
48
#    ..or..
 
49
#    host        The hostname or IP address of the service
 
50
#    port        The port that the service is located on
 
51
#
 
52
 
 
53
 
 
54
 
 
55
#
 
56
# We provide both a client and a server driver...
 
57
#
 
58
CLIENT = 1      # ClientAdapter
 
59
SERVER = 1      # ServerAdapter
 
60
 
 
61
from gnue.common.rpc.drivers._helpers.AsyncSocketServer import AsyncSocketServer
 
62
from gnue.common.rpc.drivers._helpers import ObjectLibrarian
 
63
 
 
64
from gnue.common.rpc.drivers import GCommBase
 
65
from gnue.common.rpc import GComm
 
66
 
 
67
import string
 
68
import socket
 
69
 
 
70
 
 
71
#
 
72
# ClientAdapter
 
73
#
 
74
class ClientAdapter(GCommBase.Client):
 
75
 
 
76
  def __init__(self, params):
 
77
    try:
 
78
      file = params['socket']
 
79
      if not len(file):
 
80
 
 
81
        raise KeyError
 
82
 
 
83
      if not hasattr(socket, 'AF_UNIX'):
 
84
        tmsg = _("Your configuration references a file-based sockets "
 
85
                 "connection.\nHowever, your operating system does not "
 
86
                 "support file-based sockets.")
 
87
        raise GComm.AdapterConfigurationError,  tmsg
 
88
 
 
89
      self._location = file
 
90
 
 
91
      try:
 
92
 
 
93
        self.__socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) 
 
94
        self.__socket.connect(file) 
 
95
 
 
96
      except: 
 
97
        tmsg = u_("Unable to initialize the requested socket located at %s") \
 
98
               % file
 
99
        raise AdapterInitializationError, tmsg
 
100
 
 
101
 
 
102
    except KeyError:
 
103
 
 
104
      try: 
 
105
 
 
106
        host = params['host']
 
107
        port = params['port']
 
108
        self._location = "%s:%s" % (host, port)
 
109
 
 
110
        self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
 
111
        self.__socket.connect((host, port)) 
 
112
 
 
113
      except KeyError: 
 
114
        tmsg = _("To use the sockets commdriver, you must specify either a "
 
115
                 "sockets\nfile or a host and port number. Please see "
 
116
                 "documentation.")
 
117
        raise GComm.AdapterConfigurationError, tmsg
 
118
 
 
119
      except: 
 
120
        tmsg = u_("Unable to initialize the requested socket located "
 
121
                  "at %(host)s:%(port)s") \
 
122
               % {'host': host, 'port': port}
 
123
        raise AdapterInitializationError, tmsg
 
124
 
 
125
 
 
126
  def __repr__(self): 
 
127
    return "<sockets.ClientAdapter [talking with %s] at %s>" % \
 
128
        ( self._location, id(self) )
 
129
 
 
130
 
 
131
  def requestService(self, service, params={}):
 
132
    proxy = _ProxyObject(self, None, servicer=self._server.__getattr__(service))
 
133
 
 
134
 
 
135
  def close():
 
136
    pass
 
137
 
 
138
  def runMethod(self, method, *args, **params):
 
139
    pass
 
140
 
 
141
  def getAttribute(self, attribute):
 
142
    pass
 
143
 
 
144
  def setAttribute(self, attribute):
 
145
    pass
 
146
 
 
147
 
 
148
#
 
149
# ServerAdapter
 
150
#
 
151
class ServerAdapter(AsyncSocketServer):
 
152
 
 
153
  def raiseException(self, exception, message, event=None):
 
154
    pass
 
155
 
 
156
 
 
157
#
 
158
# ProxyObject
 
159
#
 
160
class _ProxyObject(GCommBase.ProxyObject):
 
161
 
 
162
  def __getattr__(self, attr):
 
163
    self.__dict__[attr] = _ProxyObject(self._server, self, name=attr)
 
164
    return self.__dict__[attr]
 
165
 
 
166
  def __repr__(self): 
 
167
    return "<sockets.ProxyObject at %s>" % (id(self))
 
168
 
 
169
  def __call__(self, *args, **params):
 
170
    self._server.runMethod(self, args, params)
 
171
 
 
172
  # Server raised an exception...
 
173
  # Translate the exception into a local python
 
174
  # exception and raise it...
 
175
  def _exceptionRaised(self, data):
 
176
    pass
 
177