# AMF::Perl - Flash Remoting in Perl and Python

To use AMF::Perl, download it and install in the usual way (perl Makefile.PL; make; make install). To build/export .swf files with "Flash Remoting" you need to install Flash Remoting MX Components (free download). This will install the files "NetServices.as" and "NetDebug.as" that are used in the ActionScript. ## Sample code that uses Flash Remoting (See also examples that are distributed with the module). Read this if you want to send and receive strings in non-English encodings. Client code: //Obligatory includes #include "NetServices.as" #include "NetDebug.as" //Get a connection object NetServices.setDefaultGatewayURL("http://host/cpu.pl"); connection = NetServices.createGatewayConnection(); //Get a pointer to a service remoteService = connection.getService("Foo", this); //Call a remote method on that service remoteService.bar(); //or... send arguments to the server: remoteService.bar(arg1, arg2); //This callback function will be invoked function bar_result(value) { //do something with the value } ### Server code, option A - service registration. Use in simple applications.
Perl | Python
--- | ---
use AMF::Perl; package Foo; sub new { my ($proto)=@_; my $self={}; bless $self, $proto; return $self; } sub bar { my ($self, $arg1, $arg2) = @_; my $value; #Compute a return value #... return $value; } #Create the gateway object my $gateway = AMF::Perl->new; #Register a service that provides methods. #You can register more than one service. #This can happen during server startup (if running under mod_perl). $gateway->registerService("Foo",new Foo()); #Let the gateway figure out who will be called. $gateway->service(); | File cpu.py: import AMF import cpuUsage import sys gateway = AMF.AMFPython.AMFPython() gateway.registerService("CpuUsage",cpuUsage.cpuUsage()) gateway.service() File cpuUsage.py: import os import sys import string class cpuUsage: def getCpuUsage(self, arg): child = os.popen("uptime") output = child.read() tokens = output.split() #Remove commas. map(lambda x: x.replace(',', ''), tokens) array = [] hash = {'Name' : 'L 1', 'Value' : tokens[9]} array.append(hash) hash = {'Name' : 'L 5', 'Value' : tokens[10]} array.append(hash) hash = {'Name' : 'L 15', 'Value' : tokens[11]} array.append(hash) return array
### Server code, option B - limited service discovery. Use in complex applications. ### Part 1. The gateway script.
Perl | Python
--- | ---
use AMF::Perl; #Create the gateway object my $gateway = AMF::Perl->new; #Set a directory that will contain Perl package. #Each package will correspond to one service - #there can be as many as you want! #You can set only one class path, though. $gateway->setBaseClassPath("./basicservices/"); #Let the gateway figure out who will be called. $gateway->service(); | #!/usr/bin/python from AMF import AMF import sys import os gateway = AMF.AMF() gateway.setRelativeBaseClassPath("/basicservices") gateway.service()
### Part 2. Sample class in the registered directory.
Perl | Python
--- | ---
package DataEcho; sub new { my ($proto)=@_; my $self={}; bless $self, $proto; return $self; } sub echoNormal { my ($self, $data) = @_; return $data; } sub echoDate { my ($self, $data) = @_; return $data; } sub echoXML { my ($self, $data) = @_; return $data; } sub methodTable { return { "echoNormal" => { "description" => "Echoes the passed argument back to Flash (no need to set the return t ype)", "access" => "remote", # available values are private, public, remote }, "echoDate" => { "description" => "Echoes a Flash Date Object (the returnType needs setting)", "access" => "remote", # available values are private, public, remote "returns" => "date" }, "echoXML" => { "description" => "Echoes a Flash XML Object (the returnType needs setting)", "access" => "private", # available values are private, public, remote "returns" => "xml" } }; } 1; | import sys class DataEcho: def echoNormal(self, arg): return arg def echoDate(self, arg): return arg def echoXML(self, arg): return arg def methodTable(self): table = {} table["echoNormal"]= { "description" : "Echoes the passed argument back to Flash (no need to set the return type)", "access" : "remote", # available values are private, public, remote } table["echoDate"] = { "description" : "Echoes a Flash Date Object (the returnType needs setting)", "access" : "remote", # available values are private, public, remote "returns" : "date" } table["echoXML"] = { "description" : "Echoes a Flash XML Object (the returnType needs setting)", "access" : "private", # available values are private, public, remote "returns" : "xml" } return table