User:Void Singer/Red Tea

From Second Life Wiki
Jump to: navigation, search

Red Tea

What is it?

Red Tea is an open source file system back end custom built for Teacup in support of LSL's HTTP-in MOAP functionality. It builds on the work of Several other people including but not limited to, Kelly Linden, Torley Linden, Tali Rosca, Vegas Silverweb, and special mentions to Kate and/or Edelman Linden (whoever wrote the original align for LSL). The idea is to make it easy to serve web content from within SL, with a minimum of work or understanding, in the most standards compliant and flexible way possible.

How does it work?

When Red Tea starts, it looks for any notecards in the prim, reads them, converting and wrapping any notecards name ending with .tsp (Teacup Server Page) and storing them in memory, waiting for a request from the server. if the prim contents change, it waits 3 seconds then resets itself to start again. if the server requests a stored file, the file is sent with a 200 "success" code. if the file does not exist, a 404 "not found" code is sent.

Format Requirments?

Red Tea reads a modified html page from a notecard

  • For html content, the notecard name should end with ".tsp" (Teacup server page)
  • For html content, the notecard contents are limited to what's valid inside a normal html "body" tag.
  • no more than 255 bytes per line in the notecard (this is an LSL limitation)

why all the tea jokes?

So why all the tea jokes? Well I happen to like steam punk stuff, and it has a sort of pseudo-victorian-era feel to it, but in all truth it was sparked by a bit of geek humor. There was an old april fool's joke passed around as an official sounding memo about creating a protocol for controlling coffee pots with http (back before they actually started making net enabled appliances) and part of that proposal was that teapots should return an error response "418 I'm a teapot" if you tried to make it brew coffee... since these servers are small, and 418 is safe to use internally because it wouldn't normally be encountered, I figured hey, Teapot --> Teacup --> Victorian Imagery --> Steam Punk --> teacup that is it's own teapot --> SIP --> many different types of tea --> Success!

... And thus was born the SIP, Teapot server, and Red Tea file system

Return to top

Code

<lsl>/*( Red Tea v0.2 )*/

//-- Master Handling lists list gLstNom; list gLstTxt;

//-- These track memory while loading integer gIntCap; integer gIntBfr;

//-- these handle the notecard loading key gKeyQry; string gStrNcd; integer gIntNcd; string gStrTmp;

default{

   state_entry(){
        //-- grab all possible notecard names
       if (gIntCap = llGetInventoryNumber( INVENTORY_NOTECARD )){
           llWhisper( 0, "Attempting to load " + (string)gIntCap + " notecards" );
           do{
               gLstNom += [llGetInventoryName( INVENTORY_NOTECARD, gIntNcd )];
           }while (++gIntNcd < gIntCap);
            //-- work on the firs one.
           gKeyQry = llGetNotecardLine( gStrNcd = llList2String( gLstNom, 0 ), gIntNcd = 0 );
           gIntCap = llGetFreeMemory();
       }else{
           llWhisper( 0, "No content to load" );
       }
   }
   
   changed( integer vBitChg ){
       if (CHANGED_INVENTORY & vBitChg){
            //-- when inventory changes, wait a few seconds before trying to load notecards
           llSetTimerEvent( 3.0 );
       }
   }
   
   timer(){
       llResetScript();
   }
   
   dataserver( key vKeyQID, string vStrDta ){
       if (gKeyQry == vKeyQID){ //-- is this our dataserver event?
           if (EOF == vStrDta){ //-- did we finish the notecard?
               if (~llSubStringIndex( gStrNcd, ".tsp" )){ //-- is this a html page?
                   gStrTmp = "vTea='" + gStrTmp + "';"; //-- wrap it
               }
               gLstTxt += [(gStrTmp = "") + gStrTmp]; //-- save it
               llWhisper( 0, gStrNcd + " successfully loaded");
               if ((integer)(vStrDta = (string)(gIntCap - llGetFreeMemory())) > gIntBfr){
                   gIntBfr = (integer)vStrDta; //-- adjust buffer if needed
               } //-- test if we have more cards to read and room to spare
               if ((gIntNcd = -~llListFindList( gLstNom, [gStrNcd] )) < (gLstNom != []) && (gIntCap = llGetFreeMemory()) > gIntBfr){
                   gKeyQry = llGetNotecardLine( gStrNcd = llList2String( gLstNom, gIntNcd ), gIntNcd = 0 ); //-- get next card
               }else{ //-- out of cards or space
                   gStrTmp = gKeyQry = ""; //-- clear vars
                    //-- save totals and set conditional failure message
                   if ((gIntCap = (gLstNom != [])) > (gIntNcd = (gLstTxt != []))){
                       gStrNcd = "\nFailed to read " + gStrNcd; 
                   }else{
                       gStrNcd = gStrTmp;
                   }//-- report load
                   llWhisper( 0, "Loaded " + (string)gIntNcd + " of " + (string)gIntCap + " pages" + (gStrNcd = "") +
                                 gStrNcd + "\n~" + (string)(llGetFreeMemory() - gIntBfr) + " bytes free"  );
               }
           }else{ //-- notecard read
               if (~llSubStringIndex( gStrNcd, ".tsp" )){ //-- is this a html page?
                    //-- tweak for javascript wrapper compatibility
                   vStrDta = llDumpList2String( llParseStringKeepNulls( vStrDta, ["\\"], [] ), "\\\\" );
                   vStrDta = llDumpList2String( llParseStringKeepNulls( vStrDta, ["'"], [] ), "\\'" );
               }
               gStrTmp += vStrDta + "\\n"; //-- accumulate to variable
               if ((integer)(vStrDta = (string)(gIntCap - llGetFreeMemory())) > gIntBfr){
                   gIntBfr = (integer)vStrDta; //-- adjust buffer if needed
               }
               if ((gIntCap >> 1) > gIntBfr){ //-- test free space against buffer
                   gKeyQry = llGetNotecardLine( gStrNcd, ++gIntNcd ); //-- get next line
               }else{ //-- our saftey cap was exceeded, stop and report what we have
                   gLstNom = llDeleteSubList( gLstNom, gIntNcd, -1 );
                   gIntCap = (gLstNom != []);
                   gIntNcd = (gLstTxt != []);
                   gStrTmp = gKeyQry = "";
                   llWhisper( 0, "Loaded " + (string)gIntNcd + " of " + (string)gIntCap + " page(s)\nFailed while reading " +
                                 gStrNcd + "\n~" + (string)(llGetFreeMemory() - gIntBfr) + " bytes free"  );
               }
           }
       }
   }
   
   link_message( integer vIntSrc, integer vIntDta, string vStrDta, key vKeyDta ){
       if (418 == vIntDta && !!llGetLinkNumber() == vIntSrc){ //-- server request?
           if (~vIntDta = llListFindList( gLstNom, [vStrDta] )){ //-- do we have that?
               llMessageLinked( vIntSrc, 200, llList2String( gLstTxt, vIntDta ), vKeyDta );
           }else{ //-- we don't have it
               llMessageLinked( vIntSrc, 404, "", vKeyDta );
           }
       }
   }

} /*//-- License Text --//*/ /*// Free to copy, use, modify, distribute, or sell, with attribution. //*/ /*// (C)2011 (CC-BY) [ http://creativecommons.org/licenses/by/3.0 ] //*/ /*// Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ] //*/ /*// All usages must contain a plain text copy of the previous 2 lines. //*/ /*//-- --//*/</lsl>

Return to top

Notes

Compatible Servers and Extensions

Teacup - Server front end

Known Bugs

  • Extra line break inserted at the end of every file
  • If the server request a file that has a notecard, before that notecard is finished loading, a 200 "Success" response will be generated and an empty file will be served.
  • Loading large files before small ones may lock them out if the buffer limit is reached. Try to read small files first. (they are read in alphabetic order)

Planned Upgrades

  • None really, the intent is to be an example. other "flavors" can use differnt methods.

NOTE TO WIKI EDITORS

I have kept this page as a sub page of my user page to denote that it should NOT be edited except to correct errors, add confirmed bugs, or add related resources.
The intent is to ensure executive control is in one person's hands to retain a cohesive vision of it's development.
If you have an improvement suggestion, or request, please use the discussion page.

Return to top

Questions or Comments?

Feel free to leave me a note on the discussion page.