Advertisement
lewismoten

dynamic-host.lsl

Jun 25th, 2011
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // --[ Dynamic Host ]-------------------
  2. // This script allows you to register a URL for your object.
  3. // The registration process will attempt to verify that your object
  4. // may be contacted before it responds.
  5.  
  6. string host = "http://services.lewismoten.com/sl/dynamic-host/";
  7.  
  8. // Leave these as they are
  9. key urlRequestId = NULL_KEY;
  10. key registerRequestId = NULL_KEY;
  11. string url = "";
  12. integer isRegistered = FALSE;
  13.  
  14. processRequest(key id, string method, string body)
  15. {
  16.     // The world is talking to me. I better do something about it.
  17.     llHTTPResponse(id, 200, "Hello World! I am an object!");
  18. }
  19.  
  20. initialize()
  21. {
  22.     string myUrl = host + (string)llGetKey();
  23.     llOwnerSay("My external URL is " + myUrl);
  24.    
  25.     changeUrl();
  26. }
  27.  
  28. changeUrl()
  29. {
  30.     llSetTimerEvent(0);
  31.     isRegistered = FALSE;
  32.    
  33.     if(url != "")
  34.     {
  35.         llReleaseURL(url);
  36.         url = "";
  37.     }
  38.  
  39.     if(llGetFreeURLs() == 0)
  40.     {
  41.         tryAgainLater();
  42.         return;
  43.     }
  44.  
  45.     urlRequestId = llRequestURL();
  46. }
  47.  
  48. register()
  49. {
  50.     isRegistered = FALSE;
  51.     string registrationUrl = host + "register";
  52.     list headers = [HTTP_METHOD, "POST"];
  53.     headers += [HTTP_MIMETYPE, "application/x-www-form-urlencoded"];
  54.     string body = "url=" + llEscapeURL(url);
  55.     registerRequestId = llHTTPRequest(registrationUrl, headers, body);
  56. }
  57.  
  58. processUrlAssignment(string method, string body)
  59. {
  60.     if(method == URL_REQUEST_GRANTED)
  61.     {
  62.         url = body;
  63.         register();
  64.     }
  65.     else
  66.     {
  67.         tryAgainLater();
  68.     }
  69. }
  70.  
  71. processPingRequest(key id)
  72. {
  73.     llHTTPResponse(id, 200, "pong");
  74. }
  75.  
  76. registered()
  77. {
  78.     isRegistered = TRUE;
  79. }
  80.  
  81. tryAgainLater()
  82. {
  83.     llSetTimerEvent(300);
  84. }
  85.  
  86. default
  87. {
  88.     state_entry()
  89.     {
  90.         initialize();
  91.     }
  92.    
  93.     on_rez(integer start_parameter)
  94.     {
  95.         initialize();
  96.     }
  97.    
  98.     changed(integer change)
  99.     {
  100.         if(change & CHANGED_REGION_START)
  101.         {
  102.             changeUrl();
  103.         }
  104.         else if(change & CHANGED_REGION)
  105.         {
  106.             changeUrl();
  107.         }
  108.         else if(change & CHANGED_OWNER)
  109.         {
  110.             changeUrl();
  111.         }
  112.     }
  113.    
  114.     http_response(key id, integer status, list metadata, string body)
  115.     {
  116.         if(id == registerRequestId)
  117.         {
  118.             if(status == 200 && body == "registered")
  119.             {
  120.                 registered();
  121.             }
  122.             else
  123.             {
  124.                 tryAgainLater();
  125.             }
  126.         }
  127.     }
  128.    
  129.     http_request(key id, string method, string body)
  130.     {
  131.         if(urlRequestId == id)
  132.         {
  133.             processUrlAssignment(method, body);
  134.             return;
  135.         }
  136.        
  137.         if(llGetHTTPHeader(id, "x-path-info") == "/ping")
  138.         {
  139.             processPingRequest(id);
  140.             return;
  141.         }
  142.        
  143.         processRequest(id, method, body);
  144.     }
  145.    
  146.     timer()
  147.     {
  148.         if(url == "")
  149.         {
  150.             changeUrl();
  151.         }
  152.         else
  153.         {
  154.             register();
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement