Difference between revisions of "LlGetSimulatorHostname"

From Second Life Wiki
Jump to navigation Jump to search
m (Fixed Minor typo in code example)
Line 34: Line 34:
               "\n    REGION FPS : " + (string)llGetRegionFPS(),
               "\n    REGION FPS : " + (string)llGetRegionFPS(),
             <0,1,0>, 1.0);
             <0,1,0>, 1.0);
    }
}
</lsl>
<lsl>
// Alternative method by Silky Mesmeriser
// This example is more complex than the direct llGetSimulatorHostname() call however it avoids the
// imposed delay of the ll function and is useful sometimes if the undue delay is a problem for your
// script.  Of course HTTP transactions to third party services like this can fail at times thus this
// example will fall back to the LSL function with it's delay if it is unable to successfully get
// the hostname from the selected third party source.
string HOSTNAME_URI = "http://www.displaymyhostname.com/"; // URL to grab hostname from
string HOSTNAME_START = "<div class=\"hostname\">"; // What to look for before the hostname string
string HOSTNAME_END = "</div>"; // What marks the end of the hostname string
// Variable to store the simulator hostname set initially to Pending... to inform the end user
// if they request the information from the object prior to successful retrieval.
string g_sSimHost = "Pending...";
key g_kHostReqID; // Variable to store the HTTP request ID so we can handle the correct http_response
string smReadHostname(string sData)
{ // Parse the reply from the remote server and grab the hostname
    integer iIndex = llSubStringIndex(sData, HOSTNAME_START);
    sData = llGetSubString(sData, iIndex + llStringLength(HOSTNAME_START), llStringLength(sData));
    iIndex = llSubStringIndex(sData, HOSTNAME_END);
    sData = llGetSubString(sData, 0, iIndex -1);
    // Sanity check the hostname it should end with .lindenlab.com and not contain any characters like spaces or newlines
    // If this is untrue for some reason it's very likely that something on the page changed making it parse incorrectly
    // and thus the result can't be trusted.
    if (llSubStringIndex(sData, ".lindenlab.com") != (llStringLength(sData) - llStringLength(".lindenlab.com")) || llSubStringIndex(sData, " ") > -1 || llSubStringIndex(sData, "\n") > -1) sData == "ERROR";
    return sData;
}
string smGetSimulatorHostname()
{
    // If called before we have a hostname start the request process
    if (g_sSimHost == "Pending...") g_kHostReqID = llHTTPRequest(HOSTNAME_URI,[HTTP_METHOD, "GET"], "");
    // Return the hostname if available or indicate pending status
    return g_sSimHost;
}
default
{
    changed(integer iChange)
    { // If the region restarts it may be moved to another simulator this should be the only time we need to check again.
        if (iChange & CHANGED_REGION_START) smGetSimulatorHostname();
    }
    http_response(key kReqID, integer iStatus, list lMeta, string sBody)
    {
        if (kReqID == g_kHostReqID && iStatus == 200) {
            g_sSimHost = smReadHostname(sBody);
            if (g_sSimHost == "ERROR")
            { // The hostname doesn't seem to be parsing properly so lets inform the user and fall back to the LSL function
                llOwnerSay("API error determining simulator hostname, falling back to LSL method this will take a few seconds due to imposed limitations in LSL");
                g_sSimHost = llGetSimulatorHostname();
            }
        } else if (kReqID == g_kHostReqID) { // The HTTP status code indicates something went wrong we inform the user and fall back to the LSL function to be safe
            llOwnerSay("HTTP error determining simulator hostname: HTTP Code " + (string)iStatus + " falling back to LSL method this will take a few seconds due to imposed limitations in LSL");
            g_sSimHost = llGetSimulatorHostname();
        }
        llOwnerSay("Simulator Hostname: " + smGetSimulatorHostname());
    }
    state_entry()
    {
        // This begins the HTTP request for the sim hostname I think it makes
        // sense to do this right away at startup as there is no need to ask
        // every time we want to use it.
        smGetSimulatorHostname();
     }
     }
}
}

Revision as of 08:54, 24 June 2013

Summary

Function: string llGetSimulatorHostname( );

Returns a string that is the hostname of the machine the script is running on (same as string in viewer Help dialog)

Caveats

  • This function causes the script to sleep for 10.0 seconds.
All Issues ~ Search JIRA for related Bugs

Examples

<lsl> // The beginnings of a region-info script. string region; string sim;

default {

   state_entry()
   {
       llSetTimerEvent(1.0);
   }
   timer()
   {
       string here = llGetRegionName();
       if(region != here)
       {
           sim = llGetSimulatorHostname();
           region = here;
       }
       llSetText(
               "   REGION NAME : " + region + 
             "\n  SIM HOSTNAME : " + sim + 
             "\nTIME DIALATION : " + (string)llGetRegionTimeDilation() +
             "\n    REGION FPS : " + (string)llGetRegionFPS(),
           <0,1,0>, 1.0);
   }

} </lsl> <lsl> // Alternative method by Silky Mesmeriser // This example is more complex than the direct llGetSimulatorHostname() call however it avoids the // imposed delay of the ll function and is useful sometimes if the undue delay is a problem for your // script. Of course HTTP transactions to third party services like this can fail at times thus this // example will fall back to the LSL function with it's delay if it is unable to successfully get // the hostname from the selected third party source.

string HOSTNAME_URI = "http://www.displaymyhostname.com/"; // URL to grab hostname from

string HOSTNAME_START = "
"; // What to look for before the hostname string string HOSTNAME_END = "
"; // What marks the end of the hostname string

// Variable to store the simulator hostname set initially to Pending... to inform the end user // if they request the information from the object prior to successful retrieval. string g_sSimHost = "Pending..."; key g_kHostReqID; // Variable to store the HTTP request ID so we can handle the correct http_response

string smReadHostname(string sData) { // Parse the reply from the remote server and grab the hostname

   integer iIndex = llSubStringIndex(sData, HOSTNAME_START);
   sData = llGetSubString(sData, iIndex + llStringLength(HOSTNAME_START), llStringLength(sData));
   iIndex = llSubStringIndex(sData, HOSTNAME_END);
   sData = llGetSubString(sData, 0, iIndex -1);
   // Sanity check the hostname it should end with .lindenlab.com and not contain any characters like spaces or newlines
   // If this is untrue for some reason it's very likely that something on the page changed making it parse incorrectly
   // and thus the result can't be trusted.
if (llSubStringIndex(sData, ".lindenlab.com") != (llStringLength(sData) - llStringLength(".lindenlab.com"))

See Also

Functions

•  llGetRegionFPS Gets the region FPS
•  llGetRegionTimeDilation Gets the region time dilation

Articles

•  Simulator IP Addresses

Deep Notes

Search JIRA for related Issues

Signature

function string llGetSimulatorHostname();