Annotation of /trunk/GridServer/OpenSimGridComm.cs
Parent Directory
|
Revision Log
Revision 65 - (view) (download)
| 1 : | jhurliman | 65 | /* |
| 2 : | * Copyright (c) 2008 Intel Corporation | ||
| 3 : | * All rights reserved. | ||
| 4 : | * Redistribution and use in source and binary forms, with or without | ||
| 5 : | * modification, are permitted provided that the following conditions | ||
| 6 : | * are met: | ||
| 7 : | * | ||
| 8 : | * -- Redistributions of source code must retain the above copyright | ||
| 9 : | * notice, this list of conditions and the following disclaimer. | ||
| 10 : | * -- Redistributions in binary form must reproduce the above copyright | ||
| 11 : | * notice, this list of conditions and the following disclaimer in the | ||
| 12 : | * documentation and/or other materials provided with the distribution. | ||
| 13 : | * -- Neither the name of the Intel Corporation nor the names of its | ||
| 14 : | * contributors may be used to endorse or promote products derived from | ||
| 15 : | * this software without specific prior written permission. | ||
| 16 : | * | ||
| 17 : | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 18 : | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 19 : | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
| 20 : | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS | ||
| 21 : | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| 22 : | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| 23 : | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| 24 : | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| 25 : | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| 26 : | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 27 : | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 28 : | */ | ||
| 29 : | |||
| 30 : | using System; | ||
| 31 : | using System.Collections; | ||
| 32 : | using System.Collections.Generic; | ||
| 33 : | using System.IO; | ||
| 34 : | using System.Net; | ||
| 35 : | using System.Text; | ||
| 36 : | using System.Xml; | ||
| 37 : | using Nwc.XmlRpc; | ||
| 38 : | using OpenMetaverse; | ||
| 39 : | |||
| 40 : | namespace GridServer | ||
| 41 : | { | ||
| 42 : | public class RegionProfileData | ||
| 43 : | { | ||
| 44 : | public string RegionName; | ||
| 45 : | public UUID ID; | ||
| 46 : | public uint LocationX; | ||
| 47 : | public uint LocationY; | ||
| 48 : | public ulong Handle; | ||
| 49 : | public string ServerIP; | ||
| 50 : | public uint ServerPort; | ||
| 51 : | public uint HttpPort; | ||
| 52 : | public uint RemotingPort; | ||
| 53 : | public string ServerURI; | ||
| 54 : | public string HttpServerURI; | ||
| 55 : | } | ||
| 56 : | |||
| 57 : | public static class OpenSimGridComm | ||
| 58 : | { | ||
| 59 : | /// <summary> | ||
| 60 : | /// Request sim data based on arbitrary key/value | ||
| 61 : | /// </summary> | ||
| 62 : | private static RegionProfileData RequestSimData(Uri gridServer, string sendkey, string keyField, string keyValue) | ||
| 63 : | { | ||
| 64 : | RegionProfileData simData = null; | ||
| 65 : | |||
| 66 : | Hashtable requestData = new Hashtable(); | ||
| 67 : | requestData[keyField] = keyValue; | ||
| 68 : | requestData["authkey"] = sendkey; | ||
| 69 : | ArrayList sendParams = new ArrayList(); | ||
| 70 : | sendParams.Add(requestData); | ||
| 71 : | |||
| 72 : | try | ||
| 73 : | { | ||
| 74 : | XmlRpcRequest gridRequest = new XmlRpcRequest("simulator_data_request", sendParams); | ||
| 75 : | XmlRpcResponse gridResponse = gridRequest.Send(gridServer.ToString(), 3000); | ||
| 76 : | Hashtable responseData = gridResponse.Value as Hashtable; | ||
| 77 : | |||
| 78 : | if (!responseData.ContainsKey("error")) | ||
| 79 : | { | ||
| 80 : | simData = new RegionProfileData(); | ||
| 81 : | simData.LocationX = Convert.ToUInt32((string)responseData["region_locx"]); | ||
| 82 : | simData.LocationY = Convert.ToUInt32((string)responseData["region_locy"]); | ||
| 83 : | simData.Handle = Utils.UIntsToLong(simData.LocationX * 256, simData.LocationY * 256); | ||
| 84 : | simData.ServerIP = (string)responseData["sim_ip"]; | ||
| 85 : | simData.ServerPort = Convert.ToUInt32((string)responseData["sim_port"]); | ||
| 86 : | simData.HttpPort = Convert.ToUInt32((string)responseData["http_port"]); | ||
| 87 : | simData.RemotingPort = Convert.ToUInt32((string)responseData["remoting_port"]); | ||
| 88 : | simData.ServerURI = (string)responseData["server_uri"]; | ||
| 89 : | simData.HttpServerURI = String.Format("http://{0}:{1}/", simData.ServerIP, simData.HttpPort); | ||
| 90 : | simData.ID = new UUID((string)responseData["region_UUID"]); | ||
| 91 : | simData.RegionName = (string)responseData["region_name"]; | ||
| 92 : | } | ||
| 93 : | } | ||
| 94 : | catch (Exception ex) | ||
| 95 : | { | ||
| 96 : | Logger.Log.ErrorFormat("simulator_data_request to {0} with key {1} and value {2} failed: {3}", | ||
| 97 : | gridServer, keyField, keyValue, ex.Message); | ||
| 98 : | return null; | ||
| 99 : | } | ||
| 100 : | |||
| 101 : | return simData; | ||
| 102 : | } | ||
| 103 : | |||
| 104 : | /// <summary> | ||
| 105 : | /// Request sim profile information from a grid server by region UUID | ||
| 106 : | /// </summary> | ||
| 107 : | public static RegionProfileData RequestSimProfileData(UUID region_uuid, Uri gridserver_url, | ||
| 108 : | string gridserver_sendkey, string gridserver_recvkey) | ||
| 109 : | { | ||
| 110 : | return RequestSimData(gridserver_url, gridserver_sendkey, "region_UUID", region_uuid.Guid.ToString()); | ||
| 111 : | } | ||
| 112 : | |||
| 113 : | /// <summary> | ||
| 114 : | /// Request sim profile information from a grid server by region handle | ||
| 115 : | /// </summary> | ||
| 116 : | public static RegionProfileData RequestSimProfileData(ulong region_handle, Uri gridserver_url, | ||
| 117 : | string gridserver_sendkey, string gridserver_recvkey) | ||
| 118 : | { | ||
| 119 : | return RequestSimData(gridserver_url, gridserver_sendkey, "region_handle", region_handle.ToString()); | ||
| 120 : | } | ||
| 121 : | |||
| 122 : | /// <summary> | ||
| 123 : | /// Request sim profile information from a grid server by region location | ||
| 124 : | /// </summary> | ||
| 125 : | public static RegionProfileData RequestSimProfileData(uint regionX, uint regionY, Uri gridserver_url, | ||
| 126 : | string gridserver_sendkey, string gridserver_recvkey) | ||
| 127 : | { | ||
| 128 : | ulong handle = Utils.UIntsToLong(regionX * 256, regionY * 256); | ||
| 129 : | return RequestSimData(gridserver_url, gridserver_sendkey, "region_handle", handle.ToString()); | ||
| 130 : | } | ||
| 131 : | |||
| 132 : | /// <summary> | ||
| 133 : | /// Request sim profile information from a grid server by region name | ||
| 134 : | /// </summary> | ||
| 135 : | public static RegionProfileData RequestSimProfileData(string regionName, Uri gridserver_url, | ||
| 136 : | string gridserver_sendkey, string gridserver_recvkey) | ||
| 137 : | { | ||
| 138 : | return RequestSimData(gridserver_url, gridserver_sendkey, "region_name_search", regionName); | ||
| 139 : | } | ||
| 140 : | |||
| 141 : | /// <summary> | ||
| 142 : | /// Notify a simulator of a new avatar logging in | ||
| 143 : | /// </summary> | ||
| 144 : | public static bool NotifySimulatorOfLogin(RegionProfileData regionInfo, UUID sessionID, UUID secureSessionID, | ||
| 145 : | string firstName, string lastName, UUID agentID, int circuitCode, Vector3 position, string capsPath) | ||
| 146 : | { | ||
| 147 : | Logger.Log.InfoFormat("Notifying {0} @ {1},{2} ({3} to prepare for client connection", | ||
| 148 : | regionInfo.RegionName, regionInfo.LocationX, regionInfo.LocationY, regionInfo.HttpServerURI); | ||
| 149 : | |||
| 150 : | // Prepare notification | ||
| 151 : | Hashtable loginParams = new Hashtable(); | ||
| 152 : | loginParams["session_id"] = sessionID.ToString(); | ||
| 153 : | loginParams["secure_session_id"] = secureSessionID.ToString(); | ||
| 154 : | loginParams["firstname"] = firstName; | ||
| 155 : | loginParams["lastname"] = lastName; | ||
| 156 : | loginParams["agent_id"] = agentID.ToString(); | ||
| 157 : | loginParams["circuit_code"] = circuitCode; | ||
| 158 : | loginParams["startpos_x"] = position.X.ToString(); | ||
| 159 : | loginParams["startpos_y"] = position.Y.ToString(); | ||
| 160 : | loginParams["startpos_z"] = position.Z.ToString(); | ||
| 161 : | loginParams["regionhandle"] = regionInfo.Handle.ToString(); | ||
| 162 : | loginParams["caps_path"] = capsPath; | ||
| 163 : | |||
| 164 : | ArrayList SendParams = new ArrayList(); | ||
| 165 : | SendParams.Add(loginParams); | ||
| 166 : | |||
| 167 : | // Send | ||
| 168 : | XmlRpcRequest gridRequest = new XmlRpcRequest("expect_user", SendParams); | ||
| 169 : | XmlRpcResponse gridResponse = gridRequest.Send(regionInfo.HttpServerURI, 6000); | ||
| 170 : | |||
| 171 : | if (!gridResponse.IsFault) | ||
| 172 : | { | ||
| 173 : | if (gridResponse.Value != null) | ||
| 174 : | { | ||
| 175 : | Hashtable response = (Hashtable)gridResponse.Value; | ||
| 176 : | if (response.ContainsKey("success") && (string)response["success"] == "TRUE") | ||
| 177 : | return true; | ||
| 178 : | } | ||
| 179 : | } | ||
| 180 : | |||
| 181 : | return false; | ||
| 182 : | } | ||
| 183 : | |||
| 184 : | public static bool CreateInventory(Uri inventoryServer, UUID avatarID) | ||
| 185 : | { | ||
| 186 : | Uri method = new Uri(inventoryServer, "CreateInventory/"); | ||
| 187 : | |||
| 188 : | try | ||
| 189 : | { | ||
| 190 : | HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(method); | ||
| 191 : | request.Method = "POST"; | ||
| 192 : | SerializeUUID(request.GetRequestStream(), avatarID); | ||
| 193 : | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
| 194 : | return DeserializeBoolean(new XmlTextReader(response.GetResponseStream())); | ||
| 195 : | } | ||
| 196 : | catch (WebException ex) | ||
| 197 : | { | ||
| 198 : | Logger.Log.ErrorFormat("Call to {0} failed: {1}", method, ex.Message); | ||
| 199 : | return false; | ||
| 200 : | } | ||
| 201 : | } | ||
| 202 : | |||
| 203 : | public static List<InventoryItem> GetActiveGestures(Uri inventoryServer, UUID avatarID) | ||
| 204 : | { | ||
| 205 : | Uri method = new Uri(inventoryServer, "ActiveGestures/"); | ||
| 206 : | // FIXME: | ||
| 207 : | return new List<InventoryItem>(); | ||
| 208 : | } | ||
| 209 : | |||
| 210 : | public static List<InventoryFolder> GetInventoryFolders(Uri inventoryServer, UUID avatarID) | ||
| 211 : | { | ||
| 212 : | Uri method = new Uri(inventoryServer, "RootFolders/"); | ||
| 213 : | // FIXME: | ||
| 214 : | return new List<InventoryFolder>(); | ||
| 215 : | } | ||
| 216 : | |||
| 217 : | /*public static UUID ReadUUID(XmlReader reader, string name) | ||
| 218 : | { | ||
| 219 : | UUID value; | ||
| 220 : | |||
| 221 : | reader.ReadStartElement(name); | ||
| 222 : | UUID.TryParse(reader.ReadElementContentAsString("Guid", String.Empty), out value); | ||
| 223 : | reader.ReadEndElement(); | ||
| 224 : | |||
| 225 : | return value; | ||
| 226 : | }*/ | ||
| 227 : | |||
| 228 : | public static bool DeserializeBoolean(XmlReader reader) | ||
| 229 : | { | ||
| 230 : | bool value; | ||
| 231 : | |||
| 232 : | reader.MoveToContent(); | ||
| 233 : | value = reader.ReadElementContentAsBoolean("boolean", String.Empty); | ||
| 234 : | |||
| 235 : | return value; | ||
| 236 : | } | ||
| 237 : | |||
| 238 : | public static void SerializeUUID(Stream stream, UUID value) | ||
| 239 : | { | ||
| 240 : | using (XmlWriter writer = XmlWriter.Create(stream)) | ||
| 241 : | { | ||
| 242 : | writer.WriteStartDocument(); | ||
| 243 : | writer.WriteStartElement("guid"); | ||
| 244 : | writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); | ||
| 245 : | writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema"); | ||
| 246 : | writer.WriteString(value.ToString()); | ||
| 247 : | writer.WriteEndElement(); | ||
| 248 : | writer.WriteEndDocument(); | ||
| 249 : | writer.Flush(); | ||
| 250 : | } | ||
| 251 : | |||
| 252 : | stream.Flush(); | ||
| 253 : | stream.Close(); | ||
| 254 : | } | ||
| 255 : | |||
| 256 : | public static void SerializeBool(Stream stream, bool value) | ||
| 257 : | { | ||
| 258 : | using (XmlWriter writer = XmlWriter.Create(stream)) | ||
| 259 : | { | ||
| 260 : | writer.WriteStartDocument(); | ||
| 261 : | writer.WriteStartElement("boolean"); | ||
| 262 : | writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); | ||
| 263 : | writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema"); | ||
| 264 : | writer.WriteString(value.ToString().ToLower()); | ||
| 265 : | writer.WriteEndElement(); | ||
| 266 : | writer.WriteEndDocument(); | ||
| 267 : | writer.Flush(); | ||
| 268 : | } | ||
| 269 : | |||
| 270 : | stream.Flush(); | ||
| 271 : | stream.Close(); | ||
| 272 : | } | ||
| 273 : | |||
| 274 : | public static IPAddress GetIPFromURL(string url) | ||
| 275 : | { | ||
| 276 : | string[] urlSplit = url.Split(new char[] { '/', ':' }); | ||
| 277 : | if (urlSplit.Length >= 4) | ||
| 278 : | { | ||
| 279 : | return GetIPFromHostname(urlSplit[3]); | ||
| 280 : | } | ||
| 281 : | else | ||
| 282 : | { | ||
| 283 : | Logger.Log.Error("Failed to parse hostname from URL " + url); | ||
| 284 : | return null; | ||
| 285 : | } | ||
| 286 : | } | ||
| 287 : | |||
| 288 : | public static IPAddress GetIPFromHostname(string dnsAddress) | ||
| 289 : | { | ||
| 290 : | // Is it already a valid IP? No need to look it up. | ||
| 291 : | IPAddress ipa; | ||
| 292 : | if (IPAddress.TryParse(dnsAddress, out ipa)) | ||
| 293 : | return ipa; | ||
| 294 : | |||
| 295 : | IPAddress[] hosts = null; | ||
| 296 : | |||
| 297 : | // Not an IP, lookup required | ||
| 298 : | try { hosts = Dns.GetHostEntry(dnsAddress).AddressList; } | ||
| 299 : | catch (Exception ex) | ||
| 300 : | { | ||
| 301 : | Logger.Log.ErrorFormat("Failed to resolve hostname {0}: {1}", dnsAddress, ex.Message); | ||
| 302 : | return null; | ||
| 303 : | } | ||
| 304 : | |||
| 305 : | foreach (IPAddress host in hosts) | ||
| 306 : | { | ||
| 307 : | if (host.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) | ||
| 308 : | return host; | ||
| 309 : | } | ||
| 310 : | |||
| 311 : | if (hosts.Length > 0) | ||
| 312 : | return hosts[0]; | ||
| 313 : | else | ||
| 314 : | return null; | ||
| 315 : | } | ||
| 316 : | } | ||
| 317 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

