Intra-Region Update Server

From Second Life Wiki
Revision as of 17:01, 11 June 2007 by Emma Nowhere (talk | contribs) (New page: = Intra-Region Update Server = This set of scripts allows you to centrally update objects such as Freeview screens or teleport pads within a region that are configured by notecards or cont...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Intra-Region Update Server

This set of scripts allows you to centrally update objects such as Freeview screens or teleport pads within a region that are configured by notecards or contain modifiable objects or media assets.

Place the UpdateServer script inside a prim along with the objects (notecards, etc.) that you want to broadcast as updates to objects containing the UpdateSubscriber script.

To broadcast an update from the server, click on the server prim, and then click on the "Broadcast" button in the dialog. The "Info" button displays the server's object key and listening channel. The "Purge" button removes all contents from the prim so you can drag new updates objects onto it.

Place the UpdateSubscriber script inside a prim that you want to receive object updates from an UpdateServer. You should have your UpdateServer already set up before adding this script to a prim. At startup the script will broadcast a request for all UpdateServers in the region and output the results to you.

You need to register the subscriber with a specific UpdateServer to enable updates to be received. To do this, type:

/128 UpdateSubscriberRegister <subscriberkey> <serverkey>

where <subscriberkey> is the key of the prin containing the script and <serverkey> is the key shown next to the UpdateServer name that is displayed when this script displays the available UpdateServers in the region. The UpdateSubscriber script will output exact text that you can copy and past into your chat line to do this.

Note to Sellers!

Please give some attribution if you use these scripts. Thanks!

UpdateServer

//////////////////////////////////////////////////////////////////////////////////////
//
//    UpdateServer
//    Version 1.0a Release
//    Copyright (C) 2007, Emma Nowhere
//    emma.nowhere@yahoo.com
//
//    Place inside a prim along with the objects (notecards, etc.) that you
//    want to broadcast as updates to objects containing the UpdateSubscriber
//    script.  
//
//    To broadcast an update, click on the prim, and then click on the "Broadcast"
//    button in the dialog.
//
//    The "Info" button displays the server's object key and listening channel.
//
//    The "Purge" button removes all contents from the prim so you can drag new
//    updates objects onto it.
//    
//////////////////////////////////////////////////////////////////////////////////////

integer listenChannel = 128;
integer listenHandle = 0;
integer dialogChannel = 52;
integer dialogHandle = 0;
string scriptName;
key serverKey;

list mainMenu = ["Info", "Broadcast", "Purge"];

deleteAllItems() {

    integer total = llGetInventoryNumber(INVENTORY_ALL);
    integer i;
    for (i = 0; i < total; i++) {
        string name = llGetInventoryName(INVENTORY_ALL, i);
        if (name != scriptName) llRemoveInventory(name);
    }
}

integer containsItem(string itemName) {

    integer total = llGetInventoryNumber(INVENTORY_ALL);
    integer i;
    for (i = 0; i < total; i++) {
        string name = llGetInventoryName(INVENTORY_ALL, i);
        if ((name != scriptName) && (itemName == name)) return TRUE;
    }
    return FALSE;

}

broadcastAvailableUpdates() {

    llSay(0, "Broadcasting available updates...");   
     
    integer total = llGetInventoryNumber(INVENTORY_ALL);
    integer i;
    for (i = 0; i < total; i++) {
        string name = llGetInventoryName(INVENTORY_ALL, i);
        if (name != scriptName) {
            llRegionSay(listenChannel, "UpdateAvailable " + llEscapeURL(name));        
            llSay(0, "UpdateAvailable item: " + name); 
        }
    }

    llSay(0, "Broadcasting complete"); 
       
}

showServerInfo() {

    llOwnerSay("Update Server key: " + (string)serverKey);

    llOwnerSay("Update Server listening on channel #" + (string)listenChannel);

    llRegionSay(listenChannel, "UpdateSubscribersQuery " + (string)serverKey);   
     
}    

default {

    state_entry() {
        scriptName = llGetScriptName();
        serverKey = llGetKey();
        
        llSetText("Update Server\n" + llGetObjectName() + "\n" + (string)serverKey, <1,0,0>, 1.0);
        
        listenHandle = llListen(listenChannel, "", NULL_KEY, "");
        
        showServerInfo();
    }
    
    touch_start(integer num_detected) {
        llListenRemove(dialogHandle);
        dialogChannel = llFloor(llFrand(-99899.0) - 100);
        dialogHandle = llListen(dialogChannel, "", NULL_KEY, "");

        integer group = llDetectedGroup(0);
        key agent = llDetectedKey(0);
        key objectowner = llGetOwner();
        if ((objectowner == agent) || group)  {
            llDialog(agent, "Update Server v1.0a", mainMenu, dialogChannel);
        }
    }

    listen(integer channel, string name, key id, string message) 
    {

        if (channel == dialogChannel) {

            llListenRemove(dialogHandle);

            if (message == "Broadcast") broadcastAvailableUpdates();

            if (message == "Purge") deleteAllItems();

            if (message == "Info") showServerInfo();

            return;
        }

        list parsed = llParseString2List(message, [" "], []);
        integer l = llGetListLength(parsed);
        
        if ((l == 4) && (llList2String(parsed, 0) == "UpdateRequest")) {

            string itemName = llUnescapeURL(llList2String(parsed, 1));
            key destination = (key)llList2String(parsed, 2);
            key requestServer = (key)llList2String(parsed, 3);

            if (requestServer != serverKey) return;

            if (containsItem(itemName)) {
                llSay(0, "Giving item " + itemName + " to object " + llKey2Name(destination) + " (" + (string)destination + ")");        
                llGiveInventory(destination, itemName);
            }
        }
        else if (llList2String(parsed, 0) == "UpdateServersQuery") {

            llRegionSay(listenChannel, "UpdateServerAvailable " + (string)serverKey);

        }
        else if ((l == 3) && (llList2String(parsed, 0) == "UpdateSubscriberRegistered")) {

            key subscriberUpdateServer = (key)llList2String(parsed, 1);
            key subscriberKey = (key)llList2String(parsed, 2);

            if (subscriberUpdateServer != serverKey) return;

            if (subscriberUpdateServer == serverKey) {

                string msg = "UpdateSubscriber " + llKey2Name(subscriberKey) + 
                " (" + (string)subscriberKey + ") registered to UpdateServer " + 
                llKey2Name(subscriberUpdateServer) + " (" + (string)subscriberUpdateServer + ")";

                llSay(0, msg);        
            }

        }
    }

}

UpdateSubscriber

//////////////////////////////////////////////////////////////////////////////////////
//
//    UpdateSubscriber
//    Version 1.0a Release
//    Copyright (C) 2007, Emma Nowhere
//    emma.nowhere@yahoo.com
//    
//    Place inside a prim that you want to receive object updates from an
//    UpdateServer.  You should have your UpdateServer already set up
//    before adding this script to a prim.
//
//    At startup the script will broadcast a request for all UpdateServers 
//    in the region and output the results to you.
//
//    You need to register with a specific UpdateServer to enable updates
//    to be received.
//
//    To lock onto a specific UpdateServer, type:
//
//    /128 UpdateSubscriberRegister <subscriberkey> <serverkey>
//
//    where <subscriberkey> is the key of the prin containing the script
//    and <serverkey> is the key shown next to the UpdateServer name that is
//    displayed when this script displays the available UpdateServers in
//    the region.
//
//////////////////////////////////////////////////////////////////////////////////////

integer listenChannel = 128;
integer listenHandle = 0;
string scriptName;
key subscriberKey;
key serverKey;
integer registered = FALSE;

integer containsItem(string itemName) {

    integer total = llGetInventoryNumber(INVENTORY_ALL);
    integer i;
    for (i = 0; i < total; i++) {
        string name = llGetInventoryName(INVENTORY_ALL, i);
        if ((name != scriptName) && (itemName == name)) return TRUE;
    }
    return FALSE;
}

default {

    state_entry() {

        scriptName = llGetScriptName();
        subscriberKey = llGetKey();

        llOwnerSay("UpdateSubscriber installed in object " + llGetObjectName() + " (" + (string)subscriberKey + ")");
        listenHandle = llListen(listenChannel, "", NULL_KEY, "");

        llOwnerSay("UpdateSubscriber listening on channel #" + (string)listenChannel);
        llRegionSay(listenChannel, "UpdateServersQuery");
    }  

    listen(integer channel, string name, key id, string message) {

        list parsed = llParseString2List(message, [" "], []);
        integer l = llGetListLength(parsed);

        if (!registered) {

                if ((l == 3) && (llList2String(parsed, 0) == "UpdateSubscriberRegister")) {

                    if ((key)llList2String(parsed, 1) != subscriberKey) return;

                    serverKey = (key)llList2String(parsed, 2);

                    llListenRemove(listenHandle);

                    listenHandle = llListen(listenChannel, "", serverKey, "");

                    llSay(0, "Registered to Update Server " + llKey2Name(serverKey) + " (" + (string)serverKey + ")"); 

                    registered = TRUE;
                }
                else if ((l == 2) && (llList2String(parsed, 0) == "UpdateServerAvailable")) {

                    key serverKey = (key)llList2String(parsed, 1);

                    string msg = "Update Server available:\n" + llKey2Name(serverKey) + " (" + (string)serverKey + ")\n\n" +
                    "Copy and paste the following line to register to this server:\n\n" +
                    "/128 UpdateSubscriberRegister " + (string)subscriberKey + " " + (string)serverKey + "\n\n";
                    
                    llSay(0, msg);        
                }


                return;
        }


        if ((l == 2) && (llList2String(parsed, 0) == "UpdateAvailable")) {

            string itemName = llUnescapeURL(llList2String(parsed, 1));

            if (containsItem(itemName)) {

                llSay(0, "Deleting old version of item " + itemName);
                llRemoveInventory(itemName);

                llSay(0, "Requesting new version of item " + itemName);
                llRegionSay(listenChannel, "UpdateRequest " + llEscapeURL(itemName) + " " + (string)subscriberKey + " " + (string)serverKey);
            }
        }
        else if ((l == 2) && (llList2String(parsed, 0) == "UpdateSubscribersQuery")) {

            key serverKey = (key)llList2String(parsed, 1);

            llRegionSay(listenChannel, "UpdateSubscriberRegistered " + (string)serverKey + " " + (string)subscriberKey);
        }
    
    }

}