[vwrap] VWRAP example client code

"Hurliman, John" <john.hurliman@intel.com> Thu, 08 April 2010 17:14 UTC

Return-Path: <john.hurliman@intel.com>
X-Original-To: vwrap@core3.amsl.com
Delivered-To: vwrap@core3.amsl.com
Received: from localhost (localhost [127.0.0.1]) by core3.amsl.com (Postfix) with ESMTP id C791F3A69A6 for <vwrap@core3.amsl.com>; Thu, 8 Apr 2010 10:14:15 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -5.699
X-Spam-Level:
X-Spam-Status: No, score=-5.699 tagged_above=-999 required=5 tests=[AWL=-2.900, BAYES_50=0.001, J_CHICKENPOX_33=0.6, J_CHICKENPOX_51=0.6, RCVD_IN_DNSWL_MED=-4]
Received: from mail.ietf.org ([64.170.98.32]) by localhost (core3.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ndY99UT092zc for <vwrap@core3.amsl.com>; Thu, 8 Apr 2010 10:14:11 -0700 (PDT)
Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by core3.amsl.com (Postfix) with ESMTP id 473513A6839 for <vwrap@ietf.org>; Thu, 8 Apr 2010 10:14:06 -0700 (PDT)
Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga102.jf.intel.com with ESMTP; 08 Apr 2010 10:13:41 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="4.52,171,1270450800"; d="scan'208";a="507493664"
Received: from rrsmsx602.amr.corp.intel.com ([10.31.0.33]) by orsmga002.jf.intel.com with ESMTP; 08 Apr 2010 10:13:56 -0700
Received: from rrsmsx601.amr.corp.intel.com (10.31.0.151) by rrsmsx602.amr.corp.intel.com (10.31.0.33) with Microsoft SMTP Server (TLS) id 8.2.176.0; Thu, 8 Apr 2010 11:13:45 -0600
Received: from rrsmsx506.amr.corp.intel.com ([10.31.0.39]) by rrsmsx601.amr.corp.intel.com ([10.31.0.151]) with mapi; Thu, 8 Apr 2010 11:13:36 -0600
From: "Hurliman, John" <john.hurliman@intel.com>
To: "vwrap@ietf.org" <vwrap@ietf.org>
Date: Thu, 8 Apr 2010 11:13:31 -0600
Thread-Topic: VWRAP example client code
Thread-Index: AcrXPtA1kDN3gliuQGGEdwi4PbPaqA==
Message-ID: <62BFE5680C037E4DA0B0A08946C0933DCB7AF0E8@rrsmsx506.amr.corp.intel.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
acceptlanguage: en-US
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Subject: [vwrap] VWRAP example client code
X-BeenThere: vwrap@ietf.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: Virtual World Region Agent Protocol - IETF working group <vwrap.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/listinfo/vwrap>, <mailto:vwrap-request@ietf.org?subject=unsubscribe>
List-Archive: <http://www.ietf.org/mail-archive/web/vwrap>
List-Post: <mailto:vwrap@ietf.org>
List-Help: <mailto:vwrap-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/vwrap>, <mailto:vwrap-request@ietf.org?subject=subscribe>
X-List-Received-Date: Thu, 08 Apr 2010 17:14:16 -0000

I was trying out Unity3D last night to see if I could build a simple VWRAP client. I don't have a full client working, but I did hack together a first pass at the VWRAP login implementation. I'm posting the code here to get the ball rolling on the VWRAP teleport discussion and shine some light on a few cloudy issues.

Notes:

* This code was designed assuming the launch document contains a pre-authenticated login URL. Pre-authenticated login URLs are actually kind of silly; if you are going that route why not just embed the agent seed capability right in the launch doc and skip draft-hamrick-vwrap-authentication entirely. This sticks closer to the VWRAP drafts as published though.

* Speaking of steps you can skip, the agent/session capability only seems relevant to platforms using LLUDP. This code is trying to get a websocket_event_queue capability which has the session token embedded in the websocket URL, so there's no need for a session_id or secure_session_id. I included the code to fetch agent/session just to stick with what is currently published in OGP Teleport Draft 5, and in case I missed some obvious use case where a separate session_id token is needed.

* Unity3D makes heavy use of coroutines for things like fetching web resources, and since coroutines are far from optimal in C# (supported at the compiler level instead of the runtime, no way to return values or use "out" parameters with coroutines) it uses closures to return values from PostLLSD() and GetLLSD(). Apologies if the logic is difficult to follow there.

* If you look closely you'll notice I just invented a seed capability format. It is sending { "cap1": {},  "cap2": {} } and receiving { "cap1": {"uri": "..."}, "cap2": {"uri": "..."} }. I think this captures the spirit of the "map of maps" concept that was discussed at IETF77, although nothing has been discussed for the specific format yet.

John



using UnityEngine;
using System;
using System.Collections;
using System.Text;
using OpenMetaverse;
using OpenMetaverse.StructuredData;

public class VWRAP : MonoBehaviour
{
    public static bool TryParseLaunchDoc(string document, out Uri loginUri, out Uri regionUri)
    {
        OSDMap launchDoc = OSDParser.Deserialize(document) as OSDMap;

        if (launchDoc != null)
        {
            loginUri = launchDoc["loginuri"].AsUri();
            regionUri = launchDoc["region"].AsUri();
        }
        else
        {
            loginUri = null;
            regionUri = null;
        }

        return loginUri != null && regionUri != null;
    }

    public IEnumerator PreAuthenticatedLogin(Uri loginUri, Uri regionUri)
    {
        OSDMap request;
        OSDMap response = null;

        // 1. POST to the pre-authenticated loginUri and receive an agent_seed_capability
        request = new OSDMap();
        yield return StartCoroutine(PostLLSD(loginUri.ToString(), request, delegate(OSDMap ret) { response = ret; }));
        if (response == null || !response.ContainsKey("agent_seed_capability"))
        {
            DebugConsole.Log("Login failed POSTing to loginuri " + loginUri + ", response=\"" + response + "\"");
            yield break;
        }
        string agentSeedCapUrl = response["agent_seed_capability"].AsString();

        // 2. POST to the agent_seed_capability and get a list of capabilities
        request = new OSDMap();
        request["agent/info"] = new OSDMap();
        request["agent/session"] = new OSDMap();
        yield return StartCoroutine(PostLLSD(agentSeedCapUrl, request, delegate(OSDMap ret) { response = ret; }));
        if (response == null || !(response["agent/info"] is OSDMap) || !(response["agent/session"] is OSDMap))
        {
            DebugConsole.Log("Login failed POSTing to agent_seed_capability " + agentSeedCapUrl + ", response=\"" + response + "\"");
            yield break;
        }
        string agentInfoUrl = ((OSDMap)response["agent/info"])["uri"].AsString();
        string agentSessionUrl = ((OSDMap)response["agent/session"])["uri"].AsString();

        // 3. GET agent/info to get our agent_id, name, and home/last locations
        yield return StartCoroutine(GetLLSD(agentInfoUrl, delegate(OSDMap ret) { response = ret; }));
        if (response == null || !response.ContainsKey("agent_id"))
        {
            DebugConsole.Log("Login failed POSTing to agent/info " + agentSeedCapUrl + ", response=\"" + response + "\"");
            yield break;
        }
        UUID agentID = response["agent_id"].AsUUID();
        string name = response["name"].AsString();

        // 4. GET agent/session to get our session_id and secure_session_id
        yield return StartCoroutine(GetLLSD(agentSessionUrl, delegate(OSDMap ret) { response = ret; }));
        if (response == null || !response.ContainsKey("session_id"))
        {
            DebugConsole.Log("Login failed POSTing to agent/session " + agentSeedCapUrl + ", response=\"" + response + "\"");
            yield break;
        }
        UUID sessionID = response["session_id"].AsUUID();

        // 5. POST to the regionUri (public region seed capability) to get the rez_avatar/place capability
        request = new OSDMap();
        request["rez_avatar/place"] = new OSDMap();
        yield return StartCoroutine(PostLLSD(regionUri.ToString(), request, delegate(OSDMap ret) { response = ret; }));
        if (response == null || !(response["rez_avatar/place"] is OSDMap))
        {
            DebugConsole.Log("Login failed POSTing to public region seed capabability " + regionUri + ", response=\"" + response + "\"");
            yield break;
        }
        string rezAvatarPlaceUrl = ((OSDMap)response["rez_avatar/place"])["uri"].AsString();

        // 6. POST to rez_avatar/place to get the region_seed_capability
        request = new OSDMap();
        request["public_region_seed_capability"] = OSD.FromUri(regionUri);
        // TODO: request["position"] = OSD.FromVector3(...);
        yield return StartCoroutine(PostLLSD(rezAvatarPlaceUrl, request, delegate(OSDMap ret) { response = ret; }));
        if (response == null || !response["connect"].AsBoolean() || !response.ContainsKey("region_seed_capability"))
        {
            DebugConsole.Log("Login failed POSTing to rez_avatar/place " + rezAvatarPlaceUrl + ", response=\"" + response + "\"");
            yield break;
        }
        string regionSeedCapUrl = response["region_seed_capability"].AsString();

        // 7. POST to region_seed_capability to get the region capabilities
        request = new OSDMap();
        request["websocket_event_queue"] = new OSDMap();
        yield return StartCoroutine(PostLLSD(regionSeedCapUrl, request, delegate(OSDMap ret) { response = ret; }));
        if (response == null || !(response["websocket_event_queue"] is OSDMap))
        {
            DebugConsole.Log("Login failed POSTing to region_seed_capability " + regionSeedCapUrl + ", response=\"" + response + "\"");
            yield break;
        }
        string websocketEventQueue = ((OSDMap)response["websocket_event_queue"])["uri"].AsString();

        // FIXME: Return the data we collected
    }

    public static IEnumerator PostLLSD(string url, OSDMap requestMap, Action<OSDMap> callback)
    {
        byte[] requestData = Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(requestMap));

        Hashtable headers = new Hashtable();
        headers["Content-Type"] = "application/json+llsd";

        WWW request = new WWW(url, requestData, headers);
        
        yield return request;

        if (!String.IsNullOrEmpty(request.error))
            DebugConsole.Log("Error posting data to " + url + ": " + request.error);

        if (request.bytes != null && request.bytes.Length > 0)
        {
            OSDMap responseMap = OSDParser.Deserialize(request.bytes) as OSDMap;
            callback(responseMap);
        }
        else
        {
            callback(null);
        }
    }

    public static IEnumerator GetLLSD(string url, Action<OSDMap> callback)
    {
        WWW request = new WWW(url);

        yield return request;

        if (!String.IsNullOrEmpty(request.error))
            DebugConsole.Log("Error posting data to " + url + ": " + request.error);

        if (request.bytes != null && request.bytes.Length > 0)
        {
            OSDMap responseMap = OSDParser.Deserialize(request.bytes) as OSDMap;
            callback(responseMap);
        }
        else
        {
            callback(null);
        }
    }
}