Advertisement
innula

Fragment for Remote Server

Apr 25th, 2021
1,921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. key kSelfCheckRequest;
  2. key kRequestURL;
  3. key kKVPUpdate;
  4. key kCustomer;
  5.  
  6. string strProduct;
  7. string strMyURL;
  8. string strKVPKeyForServer = "CurrentURL:";
  9. string strMyExperience= "My Experience Name";
  10. default {
  11.     state_entry() {
  12.  
  13.         if(llList2String(llGetExperienceDetails(NULL_KEY),0)!=strMyExperience){
  14.             llOwnerSay("Please ensure that "+strMyExperience+" is able to run on this parcel");
  15.         }
  16.         llReleaseURL(strMyURL);
  17.         kRequestURL = llRequestURL();      
  18.     }
  19.  
  20.     timer() {
  21.         kSelfCheckRequest  = llHTTPRequest(strMyURL,[HTTP_METHOD, "POST"],"testing");
  22.            
  23.     }
  24.  
  25.     changed(integer ) {
  26.         if(change & CHANGED_REGION_START){
  27.             llReleaseURL(strMyURL);
  28.             kRequestURL = llRequestURL();  
  29.         }
  30.     }
  31.  
  32.     dataserver(key queryid, string data) {
  33.         if(kKVPUpdate == queryid){
  34.             integer success = (integer)llGetSubString(data,0,0);
  35.             data = llDeleteSubString(data,0,1);
  36.             if(success){
  37.                 llOwnerSay("Successfully updated value of "+strKVPKeyForServer+" to "+data);
  38.             }
  39.             else{
  40.                 llOwnerSay("Could not read server url because "+llGetExperienceErrorMessage((integer)data));
  41.             }
  42.         }
  43.     }
  44.  
  45.     http_response(key request_id, integer status, list metadata, string body)
  46.     {
  47.         if (request_id== kSelfCheckRequest){
  48.             llOwnerSay("response to self-check test -- status is "+(string)status);
  49.             kSelfCheckRequest= NULL_KEY;
  50.  
  51.             if (status != 200){
  52.                 llReleaseURL(strMyURL);
  53.                 kRequestURL = llRequestURL();
  54.             }
  55.         }
  56.     }
  57.  
  58.  
  59.     http_request(key id, string method, string body){
  60.         if(kRequestURL == id){
  61.             if(URL_REQUEST_DENIED == method){
  62.                 llOwnerSay("The following error occurred while attempting to get a free URL for this device:\n \n" + body);
  63.             }
  64.             else if (URL_REQUEST_GRANTED == method){
  65.                 strMyURL = body;
  66.                 llOwnerSay("updating value held by "+strKVPKeyForServer+ " to "+strMyURL);
  67.                 kKVPUpdate = llUpdateKeyValue(strKVPKeyForServer, strMyURL, FALSE, "");
  68.                 llSetTimerEvent(300.0);
  69.             }
  70.         }
  71.         else {
  72.             llOwnerSay("body is "+body);
  73.             if("testing"==body){
  74.                 llHTTPResponse(id, 200,"OK");
  75.             }
  76.  
  77.             // string who = (key)llList2String(lIncoming, -1); //for testing
  78.             else {
  79.  
  80.                 list lIncoming = llParseString2List(body, ["~"], []);
  81.                 kCustomer = (key) llList2String(lIncoming, 0);
  82.                 llOwnerSay("kCustomer is "+(string)kCustomer);
  83.                 llOwnerSay("strProduct is "+strProduct);
  84.  
  85.                 if((key)kCustomer){
  86.                     llOwnerSay("kCustomer is a key");
  87.                     llHTTPResponse(id, 200, "OK");
  88.                     //and send the product to the customer
  89.                 }
  90.                 else{
  91.                     llOwnerSay("kCustomer is not a key");
  92.                     //dunno what to do here
  93.                 }
  94.  
  95.             }//end strPing == strProduct
  96.  
  97.         }  //end else
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement