RPC::PlServer - Perl extension for writing PlRPC servers
# Create a subclass of RPC::PlServer use RPC::PlServer;
package MyServer; $MyServer::VERSION = '0.01'; @MyServer::ISA = qw(RPC::PlServer);
# Overwrite the Run() method to handle a single connection sub Run { my $self = shift; my $socket = $self->{'socket'}; }
# Create an instance of the MyServer class package main; my $server = MyServer->new({'localport' => '1234'}, \@ARGV);
# Bind the server to its port to make it actually running $server->Bind();
RPC::PlServer is the package used on the server side, and you guess what RPC::PlClient is for. Both share the package RPC::PlServer::Comm for communication purposes. See PlRPC::Client(3) and RPC::PlServer::Comm for these parts.
PlRPC works by defining a set of methods that may be executed by the client. For example, the server might offer a method ``multiply'' to the client. Now the clients method call
@result = $client->multiply($a, $b);
will be immediately mapped to a method call
@result = $server->multiply($a, $b);
on the server. The arguments and results will be transferred to or from the server automagically. (This magic has a name in Perl: It's the Storable module, my thanks to Raphael Manfredi for this excellent package.) Simple, eh? :-)
The RPC::PlServer and RPC::PlClient are abstract servers and clients: You have to derive your own classes from it.
eval { # Do something here. Don't care for errors. ... }; if ($@) { # An error occurred. ... }
my $server = RPC::PlServer(\%options, \@args);
(Class method) This constructor is immediately inherited from the Net::Daemon package. See Net::Daemon(3) for details.
$ok = $self->AcceptApplication($app); $ok = $self->AcceptVersion($version); $ok = $self->AcceptUser($user, $password);
The RPC::PlServer package has a very detailed access control scheme: First of all it inherits Net::Daemon's host based access control. It adds version control and user authorization. To achieve that, the method Accept from Net::Daemon is split into three methods, AcceptApplication, AcceptVersion and AcceptUser, each of them returning TRUE or FALSE. The client receives the arguments as the attributes application, version, user and password. A client is accepted only if all of the above methods are returning TRUE.
The default implementations are as follows: The AcceptApplication method returns TRUE, if $self is a subclass of $app. The AcceptVersion method returns TRUE, if the requested version is less or equal to ${$class}::VERSION, $self being an instance of $class. Whether a user is permitted to connect depends on the client configuration. See ``CONFIGURATION FILE'' below for examples.
$self->{'methods'} = { 'CalcServer' => { 'NewHandle' => 1, 'CallMethod' => 1 }, 'Calculator' => { 'new' => 1, 'multiply' => 1, 'add' => 1, 'divide' => 1, 'subtract' => 1 } };
then the client may use the CalcServer's NewHandle method to create objects, but only via the permitted constructor Calculator->new. Once a Calculator object is created, the server may invoke the methods multiply, add, divide and subtract.
# Load external modules; this is not required unless you use # the chroot() option. #require DBD::mysql; #require DBD::CSV;
# Create keys my $myhost_key = Crypt::IDEA->new('83fbd23390ade239'); my $bob_key = Crypt::IDEA->new('be39893df23f98a2');
{ # 'chroot' => '/var/dbiproxy', 'facility' => 'daemon', 'pidfile' => '/var/dbiproxy/dbiproxy.pid', 'user' => 'nobody', 'group' => 'nobody', 'localport' => '1003', 'mode' => 'fork',
# Access control 'clients' => [ # Accept the local LAN (192.168.1.*) { 'mask' => '^192\.168\.1\.\d+$', 'accept' => 1, 'users' => [ 'bob', 'jim' ], 'cipher' => $myhost_key }, # Accept myhost.company.com { 'mask' => '^myhost\.company\.com$', 'accept' => 1, 'users' => [ { 'name' => 'bob', 'cipher' => $bob_key } ] }, # Deny everything else { 'mask' => '.*', 'accept' => 0 } ] }
Things you should note: The user list of 192.168.1.* contains scalar values, but the user list of myhost.company.com contains hash refs: This is required, because the user configuration is more specific for user based encryption.
#!/usr/bin/perl -wT # Note the -T switch! This is always recommended for Perl servers.
use strict; # Always a good choice.
require RPC::PlServer; require MD5;
package MD5_Server; # Clients need to request application # "MD5_Server"
$MD5_Server::VERSION = '1.0'; # Clients will be refused, if they # request version 1.1 @MD5_Server::ISA = qw(RPC::PlServer);
eval { # Server options below can be overwritten in the config file or # on the command line. my $server = MD5_Server->new({ 'pidfile' => '/var/run/md5serv.pid', 'configfile' => '/etc/md5serv.conf', 'facility' => 'daemon', # Default 'user' => 'nobody', 'group' => 'nobody', 'localport' => 2000, 'logfile' => 0, # Use syslog 'mode' => 'fork', # Recommended for Unix 'methods' => { 'MD5_Server' => { 'ClientObject' => 1, 'CallMethod' => 1, 'NewHandle' => 1 }, 'MD5' => { 'new' => 1, 'add' => 1, 'hexdigest' => 1 }, } }); $server->Bind(); };
I highly recommend the following design principles:
Copyright (C) 1998, Jochen Wiedmann Am Eisteich 9 72555 Metzingen Germany
Phone: +49 7123 14887 Email: joe@ispsoft.de
All rights reserved.
You may distribute this package under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
See DBI::ProxyServer(3) for an example application.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |