Annotation of /trunk/TextureRedirector/GridProxyLoader.cs
Parent Directory
|
Revision Log
Revision 22 - (view) (download)
| 1 : | jhurliman | 22 | /* |
| 2 : | * Copyright (c) 2006-2008, openmetaverse.org | ||
| 3 : | * All rights reserved. | ||
| 4 : | * | ||
| 5 : | * - Redistribution and use in source and binary forms, with or without | ||
| 6 : | * modification, are permitted provided that the following conditions are met: | ||
| 7 : | * | ||
| 8 : | * - Redistributions of source code must retain the above copyright notice, this | ||
| 9 : | * list of conditions and the following disclaimer. | ||
| 10 : | * - Neither the name of the openmetaverse.org nor the names | ||
| 11 : | * of its contributors may be used to endorse or promote products derived from | ||
| 12 : | * this software without specific prior written permission. | ||
| 13 : | * | ||
| 14 : | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 15 : | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 16 : | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 17 : | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 18 : | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 19 : | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 20 : | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 21 : | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 22 : | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 23 : | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 24 : | * POSSIBILITY OF SUCH DAMAGE. | ||
| 25 : | */ | ||
| 26 : | jhurliman | 21 | |
| 27 : | using System; | ||
| 28 : | using System.Collections.Generic; | ||
| 29 : | using System.Globalization; | ||
| 30 : | using System.IO; | ||
| 31 : | using System.Net; | ||
| 32 : | using System.Text.RegularExpressions; | ||
| 33 : | using System.Reflection; | ||
| 34 : | using Nwc.XmlRpc; | ||
| 35 : | using OpenMetaverse; | ||
| 36 : | using OpenMetaverse.Packets; | ||
| 37 : | using GridProxy; | ||
| 38 : | |||
| 39 : | |||
| 40 : | namespace GridProxy | ||
| 41 : | { | ||
| 42 : | public class ProxyFrame | ||
| 43 : | { | ||
| 44 : | public Proxy proxy; | ||
| 45 : | private Dictionary<string, CommandDelegate> commandDelegates = new Dictionary<string, CommandDelegate>(); | ||
| 46 : | private UUID agentID; | ||
| 47 : | private UUID sessionID; | ||
| 48 : | private UUID inventoryRoot; | ||
| 49 : | private bool logLogin = false; | ||
| 50 : | private string[] args; | ||
| 51 : | |||
| 52 : | |||
| 53 : | public delegate void CommandDelegate(string[] words); | ||
| 54 : | |||
| 55 : | public string[] Args | ||
| 56 : | { | ||
| 57 : | get { return args; } | ||
| 58 : | } | ||
| 59 : | |||
| 60 : | public UUID AgentID | ||
| 61 : | { | ||
| 62 : | get { return agentID; } | ||
| 63 : | } | ||
| 64 : | |||
| 65 : | public UUID SessionID | ||
| 66 : | { | ||
| 67 : | get { return sessionID; } | ||
| 68 : | } | ||
| 69 : | |||
| 70 : | public UUID InventoryRoot | ||
| 71 : | { | ||
| 72 : | get { return inventoryRoot; } | ||
| 73 : | } | ||
| 74 : | |||
| 75 : | public void AddCommand(string cmd, CommandDelegate deleg) | ||
| 76 : | { | ||
| 77 : | commandDelegates[cmd] = deleg; | ||
| 78 : | } | ||
| 79 : | |||
| 80 : | public ProxyFrame(string[] args) | ||
| 81 : | { | ||
| 82 : | //bool externalPlugin = false; | ||
| 83 : | this.args = args; | ||
| 84 : | |||
| 85 : | ProxyConfig proxyConfig = new ProxyConfig("GridProxy", "Austin Jennings / Andrew Ortman", args); | ||
| 86 : | proxy = new Proxy(proxyConfig); | ||
| 87 : | |||
| 88 : | // add delegates for login | ||
| 89 : | proxy.SetLoginRequestDelegate(new XmlRpcRequestDelegate(LoginRequest)); | ||
| 90 : | proxy.SetLoginResponseDelegate(new XmlRpcResponseDelegate(LoginResponse)); | ||
| 91 : | |||
| 92 : | // add a delegate for outgoing chat | ||
| 93 : | proxy.AddDelegate(PacketType.ChatFromViewer, Direction.Outgoing, new PacketDelegate(ChatFromViewerOut)); | ||
| 94 : | |||
| 95 : | // handle command line arguments | ||
| 96 : | foreach (string arg in args) | ||
| 97 : | if (arg == "--log-login") | ||
| 98 : | logLogin = true; | ||
| 99 : | else if (arg.Substring(0, 2) == "--") | ||
| 100 : | { | ||
| 101 : | int ipos = arg.IndexOf("="); | ||
| 102 : | if (ipos != -1) | ||
| 103 : | { | ||
| 104 : | string sw = arg.Substring(0, ipos); | ||
| 105 : | string val = arg.Substring(ipos + 1); | ||
| 106 : | Console.WriteLine("arg '" + sw + "' val '" + val + "'"); | ||
| 107 : | if (sw == "--load") | ||
| 108 : | { | ||
| 109 : | //externalPlugin = true; | ||
| 110 : | LoadPlugin(val); | ||
| 111 : | } | ||
| 112 : | } | ||
| 113 : | } | ||
| 114 : | |||
| 115 : | commandDelegates["/load"] = new CommandDelegate(CmdLoad); | ||
| 116 : | } | ||
| 117 : | |||
| 118 : | private void CmdLoad(string[] words) | ||
| 119 : | { | ||
| 120 : | if (words.Length != 2) | ||
| 121 : | SayToUser("Usage: /load <plugin name>"); | ||
| 122 : | else | ||
| 123 : | { | ||
| 124 : | try | ||
| 125 : | { | ||
| 126 : | LoadPlugin(words[1]); | ||
| 127 : | } | ||
| 128 : | catch (Exception e) | ||
| 129 : | { | ||
| 130 : | Console.WriteLine(e.ToString()); | ||
| 131 : | } | ||
| 132 : | } | ||
| 133 : | } | ||
| 134 : | |||
| 135 : | public void LoadPlugin(string name) | ||
| 136 : | { | ||
| 137 : | |||
| 138 : | Assembly assembly = Assembly.LoadFile(Path.GetFullPath(name)); | ||
| 139 : | foreach (Type t in assembly.GetTypes()) | ||
| 140 : | { | ||
| 141 : | try | ||
| 142 : | { | ||
| 143 : | if (t.IsSubclassOf(typeof(ProxyPlugin))) | ||
| 144 : | { | ||
| 145 : | ConstructorInfo info = t.GetConstructor(new Type[] { typeof(ProxyFrame) }); | ||
| 146 : | ProxyPlugin plugin = (ProxyPlugin)info.Invoke(new object[] { this }); | ||
| 147 : | plugin.Init(); | ||
| 148 : | } | ||
| 149 : | } | ||
| 150 : | catch (Exception e) | ||
| 151 : | { | ||
| 152 : | Console.WriteLine(e.ToString()); | ||
| 153 : | } | ||
| 154 : | } | ||
| 155 : | |||
| 156 : | } | ||
| 157 : | |||
| 158 : | // LoginRequest: dump a login request to the console | ||
| 159 : | private void LoginRequest(XmlRpcRequest request) | ||
| 160 : | { | ||
| 161 : | if (logLogin) | ||
| 162 : | { | ||
| 163 : | Console.WriteLine("==> Login Request"); | ||
| 164 : | Console.WriteLine(request); | ||
| 165 : | } | ||
| 166 : | } | ||
| 167 : | |||
| 168 : | // Loginresponse: dump a login response to the console | ||
| 169 : | private void LoginResponse(XmlRpcResponse response) | ||
| 170 : | { | ||
| 171 : | System.Collections.Hashtable values = (System.Collections.Hashtable)response.Value; | ||
| 172 : | if (values.Contains("agent_id")) | ||
| 173 : | agentID = new UUID((string)values["agent_id"]); | ||
| 174 : | if (values.Contains("session_id")) | ||
| 175 : | sessionID = new UUID((string)values["session_id"]); | ||
| 176 : | if (values.Contains("inventory-root")) | ||
| 177 : | { | ||
| 178 : | inventoryRoot = new UUID( | ||
| 179 : | (string)((System.Collections.Hashtable)(((System.Collections.ArrayList)values["inventory-root"])[0]))["folder_id"] | ||
| 180 : | ); | ||
| 181 : | //Console.WriteLine("inventory root: " + inventoryRoot); | ||
| 182 : | } | ||
| 183 : | |||
| 184 : | if (logLogin) | ||
| 185 : | { | ||
| 186 : | Console.WriteLine("<== Login Response"); | ||
| 187 : | Console.WriteLine(response); | ||
| 188 : | } | ||
| 189 : | } | ||
| 190 : | |||
| 191 : | // ChatFromViewerOut: outgoing ChatFromViewer delegate; check for Analyst commands | ||
| 192 : | private Packet ChatFromViewerOut(Packet packet, IPEndPoint sim) | ||
| 193 : | { | ||
| 194 : | // deconstruct the packet | ||
| 195 : | ChatFromViewerPacket cpacket = (ChatFromViewerPacket)packet; | ||
| 196 : | string message = System.Text.Encoding.UTF8.GetString(cpacket.ChatData.Message).Replace("\0", ""); | ||
| 197 : | |||
| 198 : | if (message.Length > 1 && message[0] == '/') | ||
| 199 : | { | ||
| 200 : | string[] words = message.Split(' '); | ||
| 201 : | if (commandDelegates.ContainsKey(words[0])) | ||
| 202 : | { | ||
| 203 : | // this is an Analyst command; act on it and drop the chat packet | ||
| 204 : | ((CommandDelegate)commandDelegates[words[0]])(words); | ||
| 205 : | return null; | ||
| 206 : | } | ||
| 207 : | } | ||
| 208 : | |||
| 209 : | return packet; | ||
| 210 : | } | ||
| 211 : | |||
| 212 : | // SayToUser: send a message to the user as in-world chat | ||
| 213 : | public void SayToUser(string message) | ||
| 214 : | { | ||
| 215 : | ChatFromSimulatorPacket packet = new ChatFromSimulatorPacket(); | ||
| 216 : | packet.ChatData.FromName = Utils.StringToBytes("GridProxy"); | ||
| 217 : | packet.ChatData.SourceID = UUID.Random(); | ||
| 218 : | packet.ChatData.OwnerID = agentID; | ||
| 219 : | packet.ChatData.SourceType = (byte)2; | ||
| 220 : | packet.ChatData.ChatType = (byte)1; | ||
| 221 : | packet.ChatData.Audible = (byte)1; | ||
| 222 : | packet.ChatData.Position = new Vector3(0, 0, 0); | ||
| 223 : | packet.ChatData.Message = Utils.StringToBytes(message); | ||
| 224 : | proxy.InjectPacket(packet, Direction.Incoming); | ||
| 225 : | } | ||
| 226 : | |||
| 227 : | } | ||
| 228 : | |||
| 229 : | public abstract class ProxyPlugin : MarshalByRefObject | ||
| 230 : | { | ||
| 231 : | // public abstract ProxyPlugin(ProxyFrame main); | ||
| 232 : | public abstract void Init(); | ||
| 233 : | } | ||
| 234 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

