Difference between revisions of "LlGetSimulatorHostname"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
m (chop some expensive delay workarounds we don't need any more)
 
Line 38: Line 38:
</source>
</source>
<source lang="lsl2">
<source lang="lsl2">
// Alternative method by Silky Mesmeriser
// llGetEnv now offers an equivalent function without the 10 second delay.
// This example is more complex than the direct llGetSimulatorHostname() call however it avoids the
SaySimulatorHostname()
// 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();
    }
}
</source>
 
<source lang="lsl2">
// Alternative method : keep it simple, Silky . The hostname is in the URLs when we request an URL  for http-in
// Try to request an URL ; if it fails , use llGetSimulatorHostname . If it success release the URL to not saturate the available resources
string url;
key urlRequestId;
smGetSimulatorHostname()
{  
{  
     if ( llGetFreeURLs() > 0 )
     llOwnerSay("Simulator Hostname: " + llGetEnv("simulator_hostname") );
    {
        urlRequestId = llRequestURL();
    }
    else
    {
        string hostname = llGetSimulatorHostname();
        llOwnerSay("Simulator Hostname: " + hostname );
    }
}
}
   
   
Line 131: Line 48:
     on_rez(integer start_param)
     on_rez(integer start_param)
     {
     {
         smGetSimulatorHostname();
         SaySimulatorHostname();
     }
     }
   
   
     changed(integer iChange)
     changed(integer iChange)
     {
     {
         if (iChange & CHANGED_REGION_START) smGetSimulatorHostname();
         if (iChange & (CHANGED_REGION_START|CHANGED_REGION))
            SaySimulatorHostname();
     }
     }
 
     state_entry()
     state_entry()
     {
     {
         smGetSimulatorHostname();
         SaySimulatorHostname();
    }
    http_request(key id, string method, string body)
    {
        if (method == URL_REQUEST_DENIED)
        {
            string hostname = llGetSimulatorHostname();
            llOwnerSay("Simulator Hostname: " + hostname );
        }
        else if (method == URL_REQUEST_GRANTED)
        {
            list l = llParseString2List(body, [":"] ,[]);
            string hostname= llGetSubString(llList2String(l,1),2,-1);
            llReleaseURL(body);
           
            llOwnerSay("Simulator Hostname: " + hostname );
        }
     }
     }
}
}
Line 167: Line 66:
|also_functions={{LSL DefineRow||[[llGetRegionFPS]]|Gets the region FPS}}
|also_functions={{LSL DefineRow||[[llGetRegionFPS]]|Gets the region FPS}}
{{LSL DefineRow||[[llGetRegionTimeDilation]]|Gets the region time dilation}}
{{LSL DefineRow||[[llGetRegionTimeDilation]]|Gets the region time dilation}}
{{LSL DefineRow||[[llGetEnv]]("simulator_hostname")|Equivalent function without the script delay}}
|also_tests
|also_tests
|also_events
|also_events

Latest revision as of 13:53, 17 May 2015

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

// 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);
    }
}
// llGetEnv now offers an equivalent function without the 10 second delay.
SaySimulatorHostname()
{ 
    llOwnerSay("Simulator Hostname: " + llGetEnv("simulator_hostname") );
}
 
default
{
    on_rez(integer start_param)
    {
        SaySimulatorHostname();
    }
 
    changed(integer iChange)
    {
        if (iChange & (CHANGED_REGION_START|CHANGED_REGION))
            SaySimulatorHostname();
    }
 
    state_entry()
    {
        SaySimulatorHostname();
    }
}

See Also

Functions

•  llGetRegionFPS Gets the region FPS
•  llGetRegionTimeDilation Gets the region time dilation
•  llGetEnv("simulator_hostname") Equivalent function without the script delay

Articles

•  Simulator IP Addresses

Deep Notes

Search JIRA for related Issues

Signature

function string llGetSimulatorHostname();