2
# This file is part of GNU Enterprise.
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.
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.
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.
19
# Copyright 2001-2005 Free Software Foundation
22
# sockets/CommDriver.py
25
# Class that implements a socket-based driver for GNUe Comm.
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
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.
41
# Note: If both socket and host,port are specified, then
42
# both a sockets file and a TCP port are opened.
46
# socket The filename of the file socket.
47
# (e.g., /tmp/gnue/MyApp.socket)
49
# host The hostname or IP address of the service
50
# port The port that the service is located on
56
# We provide both a client and a server driver...
58
CLIENT = 1 # ClientAdapter
59
SERVER = 1 # ServerAdapter
61
from gnue.common.rpc.drivers._helpers.AsyncSocketServer import AsyncSocketServer
62
from gnue.common.rpc.drivers._helpers import ObjectLibrarian
64
from gnue.common.rpc.drivers import GCommBase
65
from gnue.common.rpc import GComm
74
class ClientAdapter(GCommBase.Client):
76
def __init__(self, params):
78
file = params['socket']
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
93
self.__socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
94
self.__socket.connect(file)
97
tmsg = u_("Unable to initialize the requested socket located at %s") \
99
raise AdapterInitializationError, tmsg
106
host = params['host']
107
port = params['port']
108
self._location = "%s:%s" % (host, port)
110
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
111
self.__socket.connect((host, port))
114
tmsg = _("To use the sockets commdriver, you must specify either a "
115
"sockets\nfile or a host and port number. Please see "
117
raise GComm.AdapterConfigurationError, tmsg
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
127
return "<sockets.ClientAdapter [talking with %s] at %s>" % \
128
( self._location, id(self) )
131
def requestService(self, service, params={}):
132
proxy = _ProxyObject(self, None, servicer=self._server.__getattr__(service))
138
def runMethod(self, method, *args, **params):
141
def getAttribute(self, attribute):
144
def setAttribute(self, attribute):
151
class ServerAdapter(AsyncSocketServer):
153
def raiseException(self, exception, message, event=None):
160
class _ProxyObject(GCommBase.ProxyObject):
162
def __getattr__(self, attr):
163
self.__dict__[attr] = _ProxyObject(self._server, self, name=attr)
164
return self.__dict__[attr]
167
return "<sockets.ProxyObject at %s>" % (id(self))
169
def __call__(self, *args, **params):
170
self._server.runMethod(self, args, params)
172
# Server raised an exception...
173
# Translate the exception into a local python
174
# exception and raise it...
175
def _exceptionRaised(self, data):