Example of making webapp “distributed” with PHP and STRPC
September 23rd, 2008Distributing and scaling larger storage needs… buzz buzz buzz words. The thing in short goes like this:
I was making some shop for music and sound files. When you buy for example 3 sounds you get download links that hold a temporary key that expires after certain time or certain number of downloads. The link calls a PHP script that checks key, time, logs it, etc.. and “streams” the file with proper HTTP headers so it downloads as if url was to a normal binary static file.
There are mp3, ogg and wav (uncompressed) files for each music/sfx and they take up HUGE space on HD, so the client wanted to have the ability to have these files on different servers, basically wherever. So I called in my favorite RPC library STRPC (SoTinyRPC) and made the logic already working on a single server work in an DISTRIBUTED fashion. So here is a little simplified before / after code.
before: the class that handles these restricted download ability (DownloadLimiter) is lazily imported and instantiated with _inst(). Then a function is called on that sets up each download and returns a secret keys. (We then store it into database so that we can display download links to the buyer.)
$dl = _inst('DownloadLimiter');
foreach($items as $item)
{
$item['dl_key'] = $dl->createDownload($orderId, $item['path_to_file']);
}
after: code includes and instantiates phppulp class STRPC. Then we parse the url to the download file to get the domain (and path) and make a RPC call to that specific domain.
$rpc = _instPulp('STRPC');
foreach($items as $item)
{
$url = parse_url($item['url_to_file']);
$item['dl_key'] = $rpc->call(URL::getDomain($url),
'dl', 'createDownload',
array('order' => $orderId, 'file' => $url['path']));
}
STRPC server: So now we move DownloadLimiter class from here to all these remote servers and make it’s function createDownload available to STRPC calls.
require_once 'bin/globals.php';
$dl = _inst('DownloadLimiter');
echo _instPulp('STRPC')->serve(array(
array('dl', 'createDownload',
array(
array('order', 'string'),
array('file', 'string'),
),
'string'
),
));
This is the file that makes the STRPC server. The DownloadLimiter class stays exactly the same as it was when that function was called locally.
—
STRPC was so far used in 7 real life projects and as far as I know only by me. I used/made clients in JavaScript, AS3 (Flex), Haxe (Flash), PHP and Java. STRPC servers were made in PHP And Factor up to now. It’s born out of practice implementation and spec are about to be upgraded to some cool features but I haven’t yet found time to do it.
//EDIT: I changed the client examples a little because they had some app specific logic with 3 kinds of file per music/sfx that only diluted from the point here.

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=e31dde5d-e05c-413c-ae59-eda51fb6c87e)