KeyToName and NameToKey

From libopenmetaverse - libomv - Developer Wiki

Jump to: navigation, search

KeyToName and NameToKey Examples

Here is an example of a program that does name2key, and key2name.

 
using System;
using System.Collections.Generic;
using OpenMetaverse;
namespace Example {
    class Program {
        private static GridClient Client;
        private static List<UUID> RequestedUUIDs;
        private static Dictionary<string, UUID> NameQueries;
        private static void Main(string[] argv) {
            Client = new GridClient();
            RequestedUUIDs = new List<UUID>();
            NameQueries = new Dictionary<string, UUID>();
            if (Client.Network.Login("firstName","lastName","password","userAgent","userVersion")) {
                //requesting agent UUIDs and obtaining names
                Client.Avatars.UUIDNameReply += new EventHandler<UUIDNameReplyEventArgs>(Avatars_UUIDNameReply);
                RequestedUUIDs.Add(Client.Self.AgentID); //this is silly, but we're going to request our bot's name
                Client.Avatars.RequestAvatarNames(RequestedUUIDs); //sending the request
                //requesting agent names and obtaining UUIDs
                Client.Directory.DirPeopleReply += new EventHandler<DirPeopleReplyEventArgs>(Directory_DirPeopleReply);
                string name = string.Format("{0} {1}", Client.Self.FirstName, Client.Self.LastName);
                //the name doesn't have to be a complete name it could be partial.
                lock (NameQueries) NameQueries.Add(name, Client.Directory.StartPeopleSearch(name, 0));
            }
        }
 
        private static void Directory_DirPeopleReply(object sender, DirPeopleReplyEventArgs e) {
            lock (NameQueries) { //preventing possible threading issues!
                foreach (KeyValuePair<string, UUID> pair in NameQueries) {
                    if (pair.Value == e.QueryID) { //making sure that our query ID matches with the returned queryID
                        foreach (DirectoryManager.AgentSearchData data in e.MatchedPeople) {
                            string status = data.Online ? "online" : "offline";
                            string fullname = string.Format("{0} {1}",data.FirstName, data.LastName);
                            UUID agentID = data.AgentID;
                            Console.WriteLine(string.Format("{0} is {1} and has the UUID of {2}", fullname, status, agentID));
                        }
                    }
                }
            }
        }
 
        private static void Avatars_UUIDNameReply(object sender, UUIDNameReplyEventArgs e) {
            foreach (KeyValuePair<UUID, string> pair in e.Names) {
                lock (RequestedUUIDs) { //preventing possible threading issues!
                    if (RequestedUUIDs.Contains(pair.Key)) { //checking to see if this is the key that we requested
                        RequestedUUIDs.Remove(pair.Key); //remove it, unless you want to cache it
                        string name = pair.Value; //and we have gotten the name!
                        break;
                    }
                }
            }
        }
    }
}