Thread: services: services/perl/lib/AppSwitch/Base Service.pm
Status: Pre-Alpha
Brought to you by:
jgsmith
From: <app...@li...> - 2001-07-25 21:00:31
|
jgsmith 01/07/25 14:00:30 Added: perl/lib/AppSwitch/Base Service.pm Log: Initial commit Revision Changes Path 1.1 services/perl/lib/AppSwitch/Base/Service.pm Index: Service.pm =================================================================== package AppSwitch::Base::Service; our $VERSION = 0.01; our %METHODS = ( version => q(do_version), ); =head1 NAME AppSwitch::Base::Service; =head1 SYNOPSIS my $handler = initialize My::Service $daemon; package My::Service; use base q(AppSwitch::Base::Service); our $RPC_VERSION = "0.1"; our %METHODS = ( echo => 'do_echo', ); =head1 DESCRIPTION This module provides the basic glue between the service daemon and the service handler, which is derived from this module. =head1 METHODS =over 4 =item dispatch($method, @args) It is this method that actually calls the working code for the service daemon. The called function will have the service handler object and the method name as the first two arguments followed by C<@args>. If the method is not defined in the package global C<%METHODS>, then a default method name of C<do_$method> is assumed. =item initialize($daemon) This method creates a handler object and registers it with the service daemon. This method also walks the C<@ISA> tree and collects all the pre-defined methods. Note that modifications to the C<%METHODS> globals will not affect the method table after initialization. =item set_method($method, $code) This method will associate either a method name or a code reference with a method in the service handler's dispatch table. The previous value, if any, is returned. =item remove_method($method) This method will remove a method from the service handler's dispatch table. The previous value, if any, is returned. =back =head1 GLOBALS =over 4 =item %METHODS This hash contains a mapping of method names to either code references or object method names. The C<initialize> method will walk the C<@ISA> tree and merge all C<%METHODS> hashes, with method mappings closer to the object class having precedence. The tree is walked depth-first in imitation of Perl's handling of the C<@ISA> array for class inheritance. =item $RPC_VERSION This is the version of the interface specification. This allows clients and switches to know what capabilities to expect. =back =head1 AUTHOR James Smith <jg...@ja...> =head1 SEE ALSO L<AppSwitch::Base::Client>. =head1 COPYRIGHT Copyright (C) 2001 James Smith Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the Project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. =cut sub initialize { my $class = shift; $class = ref $class || $class; my $self = bless { }, $class; # $class tells us where to start looking for methods... # assume we are using SOAP::Lite or XMLRPC::Lite my $daemon = shift; my @classes = ( $class ); my %methods; while(@classes) { my $c = shift; unshift @classes, @{"${c}::ISA"} if defined @{"${c}::ISA"}; if(defined %{"${c}::METHODS"}) { foreach my $k (keys %{"${c}::METHODS"}) { $methods{$k} = ${"${c}::METHODS"}{$k} unless defined $methods{$k}; } } } $self -> {dispatch} = \%methods; $daemon -> register($self); return $self; } sub dispatch { my($self) = shift; my($method) = shift; my($code) = $self -> {dispatch} -> {$mthod}; $code = "do_$method" unless defined $code; return &$code -> ($self, $method, @_)] if ref $code; return $self -> $code($method, @_); } sub set_method { my($self, $method, $coderef) = @_; my $o = $self -> {dispatch} -> {$method}; $self -> {dispatch} -> {$method} = $coderef; return $o; } sub remove_method { my($self, $method) = @_; my $o = $self -> {dispatch} -> {$method}; delete $self -> {dispatch} -> {$method}; return $o; } sub do_version { my($self, $method) = @_; my $class = ref $self || $self; my @classes = ($class); while(@classes) { my $c = shift; unshift @classes, @{"${c}::ISA"} if defined @{"${c}::ISA"}; if(defined ${"${c}::RPC_VERSION"}) { return "1." . ${"${c}::RPC_VERSION"}; } } return "1.0.0"; } 1; |
From: <app...@li...> - 2001-07-27 23:12:01
|
jgsmith 01/07/27 16:12:01 Modified: perl/lib/AppSwitch/Base Service.pm Log: Added Method code attribute to map subs to methods Revision Changes Path 1.2 +26 -8 services/perl/lib/AppSwitch/Base/Service.pm Index: Service.pm =================================================================== RCS file: /cvsroot/appswitch/services/perl/lib/AppSwitch/Base/Service.pm,v retrieving revision 1.1 retrieving revision 1.2 diff -b -u -r1.1 -r1.2 --- Service.pm 2001/07/25 21:00:30 1.1 +++ Service.pm 2001/07/27 23:12:01 1.2 @@ -2,9 +2,7 @@ our $VERSION = 0.01; -our %METHODS = ( - version => q(do_version), -); +use base q(Attribute::Handlers); =head1 NAME @@ -20,9 +18,7 @@ our $RPC_VERSION = "0.1"; - our %METHODS = ( - echo => 'do_echo', - ); + sub do_echo : Method(echo) { $_[2] -> {content} } =head1 DESCRIPTION @@ -74,6 +70,8 @@ class having precedence. The tree is walked depth-first in imitation of Perl's handling of the C<@ISA> array for class inheritance. +The C<Method(method(s))> subroutine attribute should be used to mark methods. + =item $RPC_VERSION This is the version of the interface specification. This allows clients @@ -143,6 +141,26 @@ return $self; } +sub Method : ATTR(CODE, INIT) { + my ($package, $symbol, $referent, $attr, $data) = @_; + + no strict 'refs'; + + # if it is an anonymous sub, then map method to the code ref + # if named lexical, use it as a method call on an object + + my $value; + if($symbol eq 'LEXICAL' || $symbol eq 'ANON' || !*{$symbol}{NAME}) { + $value = $referent; + } else { + $value = *{$symbol}{NAME}; + } + + $data = [ $data ] unless ref $data; + + @{"${package}::METHODS"}{@{$data}} = ($value) x @{$data}; +} + sub dispatch { my($self) = shift; @@ -152,7 +170,7 @@ $code = "do_$method" unless defined $code; - return &$code -> ($self, $method, @_)] if ref $code; + return &$code -> ($self, $method, @_) if ref $code; return $self -> $code($method, @_); } @@ -175,7 +193,7 @@ return $o; } -sub do_version { +sub do_version : Method(version) { my($self, $method) = @_; my $class = ref $self || $self; |
From: <app...@li...> - 2001-07-29 18:26:59
|
jgsmith 01/07/29 11:26:58 Modified: perl/lib/AppSwitch/Base Service.pm Log: Reflect updated Echo spec Revision Changes Path 1.4 +8 -8 services/perl/lib/AppSwitch/Base/Service.pm Index: Service.pm =================================================================== RCS file: /cvsroot/appswitch/services/perl/lib/AppSwitch/Base/Service.pm,v retrieving revision 1.3 retrieving revision 1.4 diff -b -u -r1.3 -r1.4 --- Service.pm 2001/07/29 05:12:31 1.3 +++ Service.pm 2001/07/29 18:26:58 1.4 @@ -19,7 +19,7 @@ our $RPC_VERSION = "0.1"; - sub do_echo : Method(echo) { $_[2] -> {content} } + sub do_echo : Method(echo) { $_[2] } =head1 DESCRIPTION @@ -71,7 +71,8 @@ class having precedence. The tree is walked depth-first in imitation of Perl's handling of the C<@ISA> array for class inheritance. -The C<Method(method(s))> subroutine attribute should be used to mark methods. +The C<Method(method)> or C<Methods(methods)> subroutine attribute +should be used to mark methods. =item $RPC_VERSION @@ -110,7 +111,7 @@ =cut sub initialize { - my $class = shift; + my($class, $daemon) = @_; $class = ref $class || $class; @@ -118,7 +119,6 @@ # $class tells us where to start looking for methods... # assume we are using SOAP::Lite or XMLRPC::Lite - my $daemon = shift; my @classes = ( $class ); @@ -128,12 +128,10 @@ my $c = shift; unshift @classes, @{"${c}::ISA"} if defined @{"${c}::ISA"}; if(defined %{"${c}::METHODS"}) { - foreach my $k (keys %{"${c}::METHODS"}) { - $methods{$k} = ${"${c}::METHODS"}{$k} - unless defined $methods{$k}; + my @k = grep { ! defined $methods{$_} } keys %{"${c}::METHODS"}; + @methods{@k} = @{"${c}::METHODS"}{@k} if @k; } } - } $self -> {dispatch} = \%methods; @@ -161,6 +159,8 @@ @{"${package}::METHODS"}{@{$data}} = ($value) x @{$data}; } + +sub Methods : ATTR(CODE, INIT) { &Method } sub dispatch { my($self) = shift; |