Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --[ Dynamic Host ]-------------------
- // This script allows you to register a URL for your object.
- // The registration process will attempt to verify that your object
- // may be contacted before it responds.
- string host = "http://services.lewismoten.com/sl/dynamic-host/";
- // Leave these as they are
- key urlRequestId = NULL_KEY;
- key registerRequestId = NULL_KEY;
- string url = "";
- integer isRegistered = FALSE;
- processRequest(key id, string method, string body)
- {
- // The world is talking to me. I better do something about it.
- llHTTPResponse(id, 200, "Hello World! I am an object!");
- }
- initialize()
- {
- string myUrl = host + (string)llGetKey();
- llOwnerSay("My external URL is " + myUrl);
- changeUrl();
- }
- changeUrl()
- {
- llSetTimerEvent(0);
- isRegistered = FALSE;
- if(url != "")
- {
- llReleaseURL(url);
- url = "";
- }
- if(llGetFreeURLs() == 0)
- {
- tryAgainLater();
- return;
- }
- urlRequestId = llRequestURL();
- }
- register()
- {
- isRegistered = FALSE;
- string registrationUrl = host + "register";
- list headers = [HTTP_METHOD, "POST"];
- headers += [HTTP_MIMETYPE, "application/x-www-form-urlencoded"];
- string body = "url=" + llEscapeURL(url);
- registerRequestId = llHTTPRequest(registrationUrl, headers, body);
- }
- processUrlAssignment(string method, string body)
- {
- if(method == URL_REQUEST_GRANTED)
- {
- url = body;
- register();
- }
- else
- {
- tryAgainLater();
- }
- }
- processPingRequest(key id)
- {
- llHTTPResponse(id, 200, "pong");
- }
- registered()
- {
- isRegistered = TRUE;
- }
- tryAgainLater()
- {
- llSetTimerEvent(300);
- }
- default
- {
- state_entry()
- {
- initialize();
- }
- on_rez(integer start_parameter)
- {
- initialize();
- }
- changed(integer change)
- {
- if(change & CHANGED_REGION_START)
- {
- changeUrl();
- }
- else if(change & CHANGED_REGION)
- {
- changeUrl();
- }
- else if(change & CHANGED_OWNER)
- {
- changeUrl();
- }
- }
- http_response(key id, integer status, list metadata, string body)
- {
- if(id == registerRequestId)
- {
- if(status == 200 && body == "registered")
- {
- registered();
- }
- else
- {
- tryAgainLater();
- }
- }
- }
- http_request(key id, string method, string body)
- {
- if(urlRequestId == id)
- {
- processUrlAssignment(method, body);
- return;
- }
- if(llGetHTTPHeader(id, "x-path-info") == "/ping")
- {
- processPingRequest(id);
- return;
- }
- processRequest(id, method, body);
- }
- timer()
- {
- if(url == "")
- {
- changeUrl();
- }
- else
- {
- register();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement