Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Simple Server Product Distribution Kit

Kage Seraph
I Dig Giant Mecha
Join date: 3 Nov 2004
Posts: 513
12-19-2006 21:00
Hi, folks,

A few people have asked me how I'm distributing the free Elemental Multiform Craft. Below are the scripts and the PHP files to do it. To run a setup like this, you will need a PHP-enabled website. Pretty much all non-free webhosting supports PHP.

The system works like this:
--an interested person clicks on an inworld copy of the doodad (rezzed from a HUD).
--The doodad contacts your website, which tells it the key of your item server.
--The doodad then contacts the server with the person's key
--The server sends a fresh HUD to the person, who can now rez doodads via the HUD.

--The HUD also checks for updates via the same mechanism.

1) Put this in your doodad:
CODE

//script by Kage Seraph
//use, sell, give away, but most importantly enjoy.
default
{
touch_start(integer total_number_touching)
{
if( llDetectedKey(0) == llGetOwner() )
{
//put code here if you want your doodad to respond to its owner's touch.
}
else
{ //it was not the owner
llInstantMessage(llDetectedKey(0), "Hi! I'm the doodad!");
key requestor = llDetectedKey(0);
llInstantMessage(requestor,"Contacting server for your free doodad");
llInstantMessage(requestor,"Have a nice day.");

//set the following web address to your website, including "update-relay.php" at the end.
string geturl = "http://www.YOUR-COOL-WEBSITE.net/update-relay.php";
llHTTPRequest(geturl, [HTTP_METHOD, "GET"], "");
}
}

http_response(key request_id, integer status, list metadata, string body)
{
key x = llList2Key(llParseString2List(body, ["|"], [""]),1);
llOwnerSay("Server contacted!");
llEmail((string)x+"@lsl.secondlife.com",requestor,(string)-1);
// the format is subject = requestor's key
// body = version. This server will interpret the -1 above as a special command to pass the HUD to the requestor.
}

}


2) Save the next bit as "update-relay.php" file on your computer, then upload it to YOUR-COOL-WEBSITE.net, to the address corresponding to what you set in the above touch-script. Remember, your website must support PHP.

CODE
 
<?php
$answer = "THE-KEY-OF-YOUR-INWORLD-SERVER-PRIM/OBJECT";
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>|' . strval($answer) . '|';
header('Content-type: text/xml');
echo($xml);
?>


Be sure to change "THE-KEY-OF-YOUR-INWORLD-SERVER-PRIM/OBJECT" to the proper value.



3) Put the following script in your inworld server object.
CODE

float newestVersion = 0.1; //Be sure to update the version here AND in the HUD script
//as new versions come along. =)
string senderName;
string objectName = "Doodad HUD"; //change this to your HUD object
integer numberServed = 0;

default
{
on_rez(integer start_param)
{
llOwnerSay((string)llGetKey());
}

state_entry()
{
llSetTimerEvent(5); //the time in seconds between polls for emails.
llSetText("",<1,1,1>,1);
llInstantMessage(llGetOwner(),"Did you remember to update the numberServed?");
llInstantMessage(llGetOwner(),(string)llGetKey()); // copy and paste the key spoken to chat to the update-relay.php file.
}

timer()
{
llGetNextEmail("","");
}

//this inworld server receives emails from HUDs trying to update and from doodads that have been clicked.

//here, we parse the email and figure out what kind of request we're getting.
email(string time, string address, string subject, string message, integer num_left)
{
key requestorKey = (key)subject; //who is doing the asking?
string messageWithoutHeaders = llDeleteSubString(message, 0, llSubStringIndex(message, "\n\n") + 1); //clean off the info prepended to the body of every SL email
if( (float)messageWithoutHeaders < 0 )//then someone's clicked a doodad and wants one.
{
++numberServed;
llInstantMessage(requestorKey,"Sending the latest doodad to you now.");
llGiveInventory(requestorKey, objectName);

}
else if((float)messageWithoutHeaders < newestVersion)
{
llInstantMessage(requestorKey,"Your doodad is obsolete. Sending the latest version to you now.");
llGiveInventory(requestorKey, objectName);
llInstantMessage(requestorKey,"Changes since last version: X Y Z");
}

else
{
llInstantMessage(requestorKey,"Your doodad HUD is up-to-date.");
}
}
}





4) Put the following into your doodad HUD. It checks for updates once a day when it rezzes (when you wear it)

CODE

string geturl = "http://www.YOUR-COOL-WEBSITE.net/update-relay.php";
key initiator_key;
string version = "0.1"; //update this and the version number on the server too as necessary.
string lastChecked = "2006-01-30";

default
{
on_rez(integer start_parameter)
{
llSleep(1);
if(lastChecked != llGetDate())
{
lastChecked = llGetDate();
//poll for updated version with the following email.
//if there is an updated version, the server will send it.
initiator_key = llGetOwner();
string outgoing_url = geturl + "?key=" + (string)initiator_key;
llHTTPRequest(outgoing_url, [HTTP_METHOD, "GET"], "");
}
else
{
//we've already checked for today
//so we won't bother the resi again

}
}

state_entry()
{
llSleep(1);
if(lastChecked != llGetDate())
{
lastChecked = llGetDate();
//poll for updated version with the following email.
//if there is an updated version, the server will send it.
initiator_key = llGetOwner();
string outgoing_url = geturl;
llHTTPRequest(outgoing_url, [HTTP_METHOD, "GET"], "");
}
else
{
//we've already checked for today
//so we won't bother the resi again
//also, this reduces load on the server for popular objects (!).
}
}

http_response(key request_id, integer status, list metadata, string body)
{
//llOwnerSay("http ok");
key x = llList2Key(llParseString2List(body, ["|"], [""]),1);
llEmail((string)x+"@lsl.secondlife.com",llGetOwner(),(string)version);
}
}



So there you go. It isn't secure communication, and it doesn't handle money. Like the earlier, non-http server, an unscrupulous person could potentially use this to harm your privacy. But it is a nice, reasonably simple way to keep people who like your doodad updated.

zOMG.
_____________________
Kitty Barnett
Registered User
Join date: 10 May 2006
Posts: 5,586
12-19-2006 22:42
From: Kage Seraph
2) Save the next bit as "update-relay.php" file on your computer, then upload it to YOUR-COOL-WEBSITE.net, to the address corresponding to what you set in the above touch-script. Remember, your website must support PHP.
Why not simply use a static file instead?

Or have the website proxy the delivery request over to the server prim, that would get rid of the email delay in the first object and remove the need to poll for email in the prim :).

(Actually thinking about what it does.. why not use "everyone can copy?".. or put a copy inside of itself and it can a copy on touch without any extra hassle)
Kage Seraph
I Dig Giant Mecha
Join date: 3 Nov 2004
Posts: 513
12-20-2006 11:12
Hi, Kitty!

From: someone
Have the website proxy the delivery request over to the server prim.


How is this done? My brain is fried and it's only midweek. o.o; I am only aware that HTTP communication is by prim-request only, not push.

If it can be done without email, I'd love to tinker with how-- as it is I'm pounding the email server with request pings every 5 seconds, given the high demand currently.

From: someone
why not use "everyone can copy?".. or put a copy inside of itself and it can a copy on touch without any extra hassle


Good question, and the answer is more based on my situation than on technical concerns. When I set out to design the distribution and update system, I went with a centralized system such that I knew that all new copies served would be the most recently released ones, not older versions. Since the Elemental is still alpha-stage, I know I'm going to be pushing updates often, and a centralized system seemed an easy way to go. If you have a better solution, I'm totally open to it. =)

Also, a non-distributed distribution system, if you will, makes tracking the trends in serving rates much easier. I stripped out the bookkeeping code for this forum post, but it's in there inworld. This is mostly just for my own curiosity. It's encouraging, for example, for me to know that I've served just more than 3000 since last Saturday. It helps me to make time for development knowing that people like it enough to request one.

So central-vs-distributed comes down to preference, I think. I'd love to hear more about removing the email steps by replacing with web comm sos I can ease up on the email server. Thanks!
_____________________
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
12-20-2006 11:36
Maybe I'm missing something, but... One of the scripts sends the user's key to the website, the other one doesn't. The PHP script extracts the key, bu doesn't seem to do anything with it.

?
Kage Seraph
I Dig Giant Mecha
Join date: 3 Nov 2004
Posts: 513
12-20-2006 11:55
From: Ziggy Puff
Maybe I'm missing something, but... One of the scripts sends the user's key to the website, the other one doesn't. The PHP script extracts the key, bu doesn't seem to do anything with it.

?


Good catch-- that's an artifact of copying and pasting the web communication script from an example here in the forums. The php file isn't looking for any arguments to be passed.

edit: updated the php and the script to strip out the vestigial code.
_____________________
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
12-20-2006 12:34
OK, thanks. It looked like the LSL script handled all the business of the av's key, but I wanted to make sure I didn't miss anything. I'm starting to write scripts that talk to a PHP/MySQL backend, so this is helpful :)
Kage Seraph
I Dig Giant Mecha
Join date: 3 Nov 2004
Posts: 513
12-20-2006 19:45
Ziggy, message me inworld if you get a break n the action? I'd like to talk to you more about this kinda stuff. Thanks!
_____________________
Sebastian Pedro
Registered User
Join date: 27 Dec 2006
Posts: 5
01-03-2007 18:53
thanks kage this is awesome... and btw, i too would love hear about getting code like this working with a mysql backend.
Yumi Murakami
DoIt!AttachTheEarOfACat!
Join date: 27 Sep 2005
Posts: 6,860
01-03-2007 20:54
From: Kage Seraph

How is this done? My brain is fried and it's only midweek. o.o; I am only aware that HTTP communication is by prim-request only, not push.

If it can be done without email, I'd love to tinker with how-- as it is I'm pounding the email server with request pings every 5 seconds, given the high demand currently.


The client calls a PHP page via HttpRequest which sends an XML-RPC call to the server.

I've been playing with this for a large project and it is very noticably faster than e-mail.
Kage Seraph
I Dig Giant Mecha
Join date: 3 Nov 2004
Posts: 513
01-04-2007 08:23
I will definitely have to get into the RPC side of things. Just gotta finish the next version of the Elemental and work on some consulting in the meantime. Thanks, Yumi! Sebastian, if you have any trouble setting it up, let me know. -Kage
_____________________