OSSL Script Library/JsonStore

From OpenSimulator

Revision as of 15:12, 17 April 2012 by Cmickeyb (Talk | contribs)

Jump to: navigation, search

Contents

Introduction

This functionality current only exists in OpenSimulator development code (0.7.4-dev).

Enabling modInvoke()

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
General
About This Wiki