OSSL Script Library/JsonStore

From OpenSimulator

Jump to: navigation, search

Contents

Introduction

This page describes two scripts, a generator and a consumer, that demonstrate how to use the JsonStore JsonStoreRead and JsonStoreTake operations for creating a master/worker pair of scripts. Place the generator script in an object and the consumer script in another object. Touch the generator to start the process.

To see the value of the master/worker pattern, try to make multiple copies of the consumer object. Each one will read tasks created by the generator.

This functionality current only exists in OpenSimulator development code (0.7.4-dev). Please be aware that it is currently experimental and subject to change which may make older scripts fail to work or work slightly differently. You can find documentation of all the functions available and examples of their use can be found on the JsonStore Module page.

Enabling the JsonStore Module

The first thing is to enable the JsonStore module in OpenSim.ini. Add the following to enable the module:

[JsonStore]
Enabled = True

The Generator Script

The generator script writes values to a shared array where consumers can pull those values. The script uses the global store (public scratch storage space) to share the identity of the Json store that it creates.

string sStoreName = "JsonStoreTest";
key kGlobalStore = (key)NULL_KEY;
 
integer iCounter = 0;
key kStoreID;
 
default
{
    state_entry()
    {
        llOwnerSay("running...");
    }
 
    touch_start(integer i)
    {
        state running;
    }
}
 
state running
{
    state_entry()
    {
        // Create a JsonStore initialized with an empty array associated with the key 'Event'
        kStoreID = JsonCreateStore("{'Event' : []}");
 
        // Save the store that was just created in the global store, this makes it easier for
        // other scripts to find; an alternative is to say the store id in a private channel
        JsonSetValue(kGlobalStore,sStoreName,(string)kStoreID);
 
        // Start a timer that will generate counters periodically
        llSetTimerEvent(1.0);
    }
 
    timer()
    {
        // Generate a new counter and add it to the end of the Event array
        iCounter += 1;
        JsonSetValue(kStoreID,"Event[+]",(string)iCounter);
    }
 
    touch_start(integer i)
    {
        // When the test is done, remove the identity of the store and destroy it
        JsonRemoveValue(kGlobalStore,sStoreName);
        JsonDestroyStore(kStoreID);
 
        llResetScript();
    }
}

The Consumer Script

The consumer script waits for the generator to create the store and place its identity into the global store. At that point it begins to read values from the generator's store.

string sStoreName = "JsonStoreTest";
key kGlobalStore = (key)NULL_KEY;
 
key kStoreID;
key kReqID;
 
default
{
    state_entry()
    {
        // Set up a request for the identity of the generator's store
        // This posts a request, when the value is available a link message event will be fired
        kReqID = JsonReadValue(kGlobalStore,sStoreName);
    }
 
    link_message(integer sender, integer result, string msg, key id)
    {
        if (sender != -1)
            return;
 
        llOwnerSay("shared store id: " + msg);
        kStoreID = (key)msg;
 
        state running;
    }
}
 
state running
{
    state_entry()
    {
        // Set up a request that will take a value (remove it from the store) when it is available
        // A link_message will be fired when the value is available
        kReqID = JsonTakeValue(kStoreID,"Event[0]");
    }
 
    link_message(integer sender, integer result, string msg, key id)
    {
        if (sender != -1)
            return;
 
        llOwnerSay("read " + msg);
        kReqID = JsonTakeValue(kStoreID,"Event[0]");
    }
 
    touch_start(integer i)
    {
        llResetScript();
    }
}
Personal tools
About This Wiki