Browse Source

Persistent KV database files are now properly written on the current working directory (maybe they shouldn’t…); added a ‘compatibility mode’ with W-Hat, namely, if “compat=false” is passed, it send back cute messages; if not, it just sends back the UUID (or avatar name)

Gwyneth Llewelyn 6 years ago
parent
commit
4a4688b2e5
2 changed files with 58 additions and 48 deletions
  1. 15 4
      gosl.go
  2. 43 44
      query.lsl

+ 15 - 4
gosl.go

@@ -8,7 +8,7 @@ import (
 	"github.com/dgraph-io/badger"
 	"github.com/op/go-logging"
 	"gopkg.in/natefinch/lumberjack.v2"
-	"io/ioutil"
+//	"io/ioutil"
 	"net/http"
 	"net/http/fcgi"
 	"os"
@@ -84,8 +84,10 @@ func main() {
 
 	log.Info("gosl started and logging is set up. Proceeding to test KV database.")
 	
+	var err error
 	Opt = badger.DefaultOptions
-	Opt.Dir, _ = ioutil.TempDir("", "goslkv")
+	Opt.Dir, err = os.Getwd()
+	checkErr(err)
 	Opt.ValueDir = Opt.Dir
 	kv, err := badger.NewKV(&Opt)
 	checkErr(err) // should probably panic
@@ -165,6 +167,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
 	}
 	name := r.Form.Get("name") // can be empty
 	key := r.Form.Get("key") // can be empty
+	compat := r.Form.Get("compat") // compatibility mode with W-Hat
 	messageToSL := "" // this is what we send back to SL - defined here due to scope issues.
 	if name != "" {
 		if key != "" {
@@ -177,7 +180,11 @@ func handler(w http.ResponseWriter, r *http.Request) {
 		} else {
 			// we just received the name: look up its UUID key.
 			key = searchKV(name)
-			messageToSL += "UUID for '" + name + "' is: " + key
+			if compat == "false" {
+				messageToSL += "UUID for '" + name + "' is: " + key
+			} else { // empty also means true!
+				messageToSL += key		
+			}
 		}
 	} else if key != "" {
 		// in this scenario, we have the UUID key but no avatar name: do the equivalent of a llKey2Name
@@ -189,7 +196,11 @@ func handler(w http.ResponseWriter, r *http.Request) {
 		}
 		name = string(item.Value())
 		kv.Close()
-		messageToSL += "Avatar name for " + key + "' is '" + name + "'"
+		if compat == "false" {
+			messageToSL += "Avatar name for " + key + "' is '" + name + "'"
+		} else { // empty also means true!
+			messageToSL += name		
+		}
 	} else {
 		// neither UUID key nor avatar received, this is an error
 		logErrHTTP(w, http.StatusNotFound, "Empty avatar name and UUID key received, cannot proceed")

+ 43 - 44
query.lsl

@@ -6,50 +6,49 @@ key avatar;
 
 default
 {
-    state_entry()
-    {
-        llSetText("Touch to get an avatar UUID from Second Life®", <1,1,1>, 1);
-    }
-    
-    touch_start(integer howmany)
-    {
-        if (listener == 0)
-        {
-            avatar = llDetectedKey(0);
-            listener = llListen(CHAT_CHANNEL, "", avatar, "");
-            llSetText("In use by " + llDetectedName(0), <1,1,1>,1);
-            llWhisper(0, "Say an avatar name in chat prefixing it with /" + (string)CHAT_CHANNEL + "; touch to reset");
-            llSetTimerEvent(360.0);
-        }
-        else if (avatar == llDetectedKey(0))
-        {
-            llResetScript();
-        }
-        else
-            llWhisper(0, "In use");
-    }
-    
-    timer()
-    {
-        llWhisper(0, "No response from web services...");
-        llResetScript();
-    }
+	state_entry()
+	{
+		llSetText("Touch to get an avatar UUID from Second Life®", <1,1,1>, 1);
+	}
 
-    listen(integer channel, string name, key id, string message)
-    {
-        http_request_id = llHTTPRequest(queryRequestURL + "?name=" + llEscapeURL(message), [], "");
-        llSetTimerEvent(60.0);
-    }
+	touch_start(integer howmany)
+	{
+		if (listener == 0)
+		{
+			avatar = llDetectedKey(0);
+			listener = llListen(CHAT_CHANNEL, "", avatar, "");
+			llSetText("In use by " + llDetectedName(0), <1,1,1>,1);
+			llWhisper(0, "Say an avatar name in chat prefixing it with /" + (string)CHAT_CHANNEL + "; touch to reset");
+			llSetTimerEvent(60.0);
+		}
+		else if (avatar == llDetectedKey(0))
+		{
+			llResetScript();
+		}
+		else
+			llWhisper(0, "In use");
+	}
 
-    http_response(key request_id, integer status, list metadata, string body)
-    {
-        if (request_id == http_request_id)
-        {
-            if (status == 200)
-                llInstantMessage(avatar, body);
-            else
-                llInstantMessage(avatar, "Error " + (string)status + ": " + body);
-        }
-        llSetTimerEvent(0.0);
-    }
+	timer()
+	{
+		llResetScript();
+	}
+
+	listen(integer channel, string name, key id, string message)
+	{
+		http_request_id = llHTTPRequest(queryRequestURL + "?name=" + llEscapeURL(message) + "&compat=false", [], "");
+		llSetTimerEvent(60.0);
+	}
+
+	http_response(key request_id, integer status, list metadata, string body)
+	{
+		if (request_id == http_request_id)
+		{
+			if (status == 200)
+				llInstantMessage(avatar, body);
+			else
+				llInstantMessage(avatar, "Error " + (string)status + ": " + body);
+		}
+		llSetTimerEvent(0.0);
+	}
 }