diff options
Added OpenSim.Grid.GridServer.Modules, for the GridServer modules/components.
Diffstat (limited to 'OpenSim/Grid/GridServer.Modules/GridRestModule.cs')
-rw-r--r-- | OpenSim/Grid/GridServer.Modules/GridRestModule.cs | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/OpenSim/Grid/GridServer.Modules/GridRestModule.cs b/OpenSim/Grid/GridServer.Modules/GridRestModule.cs new file mode 100644 index 0000000..cd6d4d5 --- /dev/null +++ b/OpenSim/Grid/GridServer.Modules/GridRestModule.cs | |||
@@ -0,0 +1,282 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
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 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Collections.Generic; | ||
31 | using System.IO; | ||
32 | using System.Reflection; | ||
33 | using System.Xml; | ||
34 | using log4net; | ||
35 | using OpenMetaverse; | ||
36 | using OpenSim.Data; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Framework.Communications; | ||
39 | using OpenSim.Framework.Servers; | ||
40 | using OpenSim.Grid.Framework; | ||
41 | |||
42 | namespace OpenSim.Grid.GridServer.Modules | ||
43 | { | ||
44 | public class GridRestModule | ||
45 | { | ||
46 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
47 | |||
48 | private GridDBService m_gridDBService; | ||
49 | private IUGAIMCore m_gridCore; | ||
50 | |||
51 | protected GridConfig m_config; | ||
52 | |||
53 | /// <value> | ||
54 | /// Used to notify old regions as to which OpenSim version to upgrade to | ||
55 | /// </value> | ||
56 | private string m_opensimVersion; | ||
57 | |||
58 | protected BaseHttpServer m_httpServer; | ||
59 | |||
60 | /// <summary> | ||
61 | /// Constructor | ||
62 | /// </summary> | ||
63 | /// <param name="opensimVersion"> | ||
64 | /// Used to notify old regions as to which OpenSim version to upgrade to | ||
65 | /// </param> | ||
66 | public GridRestModule() | ||
67 | { | ||
68 | } | ||
69 | |||
70 | public void Initialise(string opensimVersion, GridDBService gridDBService, IUGAIMCore gridCore, GridConfig config) | ||
71 | { | ||
72 | m_opensimVersion = opensimVersion; | ||
73 | m_gridDBService = gridDBService; | ||
74 | m_gridCore = gridCore; | ||
75 | m_config = config; | ||
76 | RegisterHandlers(); | ||
77 | } | ||
78 | |||
79 | public void PostInitialise() | ||
80 | { | ||
81 | |||
82 | } | ||
83 | |||
84 | public void RegisterHandlers() | ||
85 | { | ||
86 | //have these in separate method as some servers restart the http server and reregister all the handlers. | ||
87 | m_httpServer = m_gridCore.GetHttpServer(); | ||
88 | |||
89 | m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/sims/", RestGetSimMethod)); | ||
90 | m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/sims/", RestSetSimMethod)); | ||
91 | |||
92 | m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/regions/", RestGetRegionMethod)); | ||
93 | m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/regions/", RestSetRegionMethod)); | ||
94 | } | ||
95 | |||
96 | /// <summary> | ||
97 | /// Performs a REST Get Operation | ||
98 | /// </summary> | ||
99 | /// <param name="request"></param> | ||
100 | /// <param name="path"></param> | ||
101 | /// <param name="param"></param> | ||
102 | /// <param name="httpRequest">HTTP request header object</param> | ||
103 | /// <param name="httpResponse">HTTP response header object</param> | ||
104 | /// <returns></returns> | ||
105 | public string RestGetRegionMethod(string request, string path, string param, | ||
106 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
107 | { | ||
108 | return RestGetSimMethod(String.Empty, "/sims/", param, httpRequest, httpResponse); | ||
109 | } | ||
110 | |||
111 | /// <summary> | ||
112 | /// Performs a REST Set Operation | ||
113 | /// </summary> | ||
114 | /// <param name="request"></param> | ||
115 | /// <param name="path"></param> | ||
116 | /// <param name="param"></param> | ||
117 | /// <param name="httpRequest">HTTP request header object</param> | ||
118 | /// <param name="httpResponse">HTTP response header object</param> | ||
119 | /// <returns></returns> | ||
120 | public string RestSetRegionMethod(string request, string path, string param, | ||
121 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
122 | { | ||
123 | return RestSetSimMethod(String.Empty, "/sims/", param, httpRequest, httpResponse); | ||
124 | } | ||
125 | |||
126 | /// <summary> | ||
127 | /// Returns information about a sim via a REST Request | ||
128 | /// </summary> | ||
129 | /// <param name="request"></param> | ||
130 | /// <param name="path"></param> | ||
131 | /// <param name="param">A string representing the sim's UUID</param> | ||
132 | /// <param name="httpRequest">HTTP request header object</param> | ||
133 | /// <param name="httpResponse">HTTP response header object</param> | ||
134 | /// <returns>Information about the sim in XML</returns> | ||
135 | public string RestGetSimMethod(string request, string path, string param, | ||
136 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
137 | { | ||
138 | string respstring = String.Empty; | ||
139 | |||
140 | RegionProfileData TheSim; | ||
141 | |||
142 | UUID UUID; | ||
143 | if (UUID.TryParse(param, out UUID)) | ||
144 | { | ||
145 | TheSim = m_gridDBService.GetRegion(UUID); | ||
146 | |||
147 | if (!(TheSim == null)) | ||
148 | { | ||
149 | respstring = "<Root>"; | ||
150 | respstring += "<authkey>" + TheSim.regionSendKey + "</authkey>"; | ||
151 | respstring += "<sim>"; | ||
152 | respstring += "<uuid>" + TheSim.UUID.ToString() + "</uuid>"; | ||
153 | respstring += "<regionname>" + TheSim.regionName + "</regionname>"; | ||
154 | respstring += "<sim_ip>" + TheSim.serverIP + "</sim_ip>"; | ||
155 | respstring += "<sim_port>" + TheSim.serverPort.ToString() + "</sim_port>"; | ||
156 | respstring += "<region_locx>" + TheSim.regionLocX.ToString() + "</region_locx>"; | ||
157 | respstring += "<region_locy>" + TheSim.regionLocY.ToString() + "</region_locy>"; | ||
158 | respstring += "<estate_id>1</estate_id>"; | ||
159 | respstring += "</sim>"; | ||
160 | respstring += "</Root>"; | ||
161 | } | ||
162 | } | ||
163 | else | ||
164 | { | ||
165 | respstring = "<Root>"; | ||
166 | respstring += "<error>Param must be a UUID</error>"; | ||
167 | respstring += "</Root>"; | ||
168 | } | ||
169 | |||
170 | return respstring; | ||
171 | } | ||
172 | |||
173 | /// <summary> | ||
174 | /// Creates or updates a sim via a REST Method Request | ||
175 | /// BROKEN with SQL Update | ||
176 | /// </summary> | ||
177 | /// <param name="request"></param> | ||
178 | /// <param name="path"></param> | ||
179 | /// <param name="param"></param> | ||
180 | /// <param name="httpRequest">HTTP request header object</param> | ||
181 | /// <param name="httpResponse">HTTP response header object</param> | ||
182 | /// <returns>"OK" or an error</returns> | ||
183 | public string RestSetSimMethod(string request, string path, string param, | ||
184 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
185 | { | ||
186 | m_log.Info("Processing region update via REST method"); | ||
187 | RegionProfileData theSim; | ||
188 | theSim = m_gridDBService.GetRegion(new UUID(param)); | ||
189 | if (theSim == null) | ||
190 | { | ||
191 | theSim = new RegionProfileData(); | ||
192 | UUID UUID = new UUID(param); | ||
193 | theSim.UUID = UUID; | ||
194 | theSim.regionRecvKey = m_config.SimRecvKey; | ||
195 | } | ||
196 | |||
197 | XmlDocument doc = new XmlDocument(); | ||
198 | doc.LoadXml(request); | ||
199 | XmlNode rootnode = doc.FirstChild; | ||
200 | XmlNode authkeynode = rootnode.ChildNodes[0]; | ||
201 | if (authkeynode.Name != "authkey") | ||
202 | { | ||
203 | return "ERROR! bad XML - expected authkey tag"; | ||
204 | } | ||
205 | |||
206 | XmlNode simnode = rootnode.ChildNodes[1]; | ||
207 | if (simnode.Name != "sim") | ||
208 | { | ||
209 | return "ERROR! bad XML - expected sim tag"; | ||
210 | } | ||
211 | |||
212 | //theSim.regionSendKey = Cfg; | ||
213 | theSim.regionRecvKey = m_config.SimRecvKey; | ||
214 | theSim.regionSendKey = m_config.SimSendKey; | ||
215 | theSim.regionSecret = m_config.SimRecvKey; | ||
216 | theSim.regionDataURI = String.Empty; | ||
217 | theSim.regionAssetURI = m_config.DefaultAssetServer; | ||
218 | theSim.regionAssetRecvKey = m_config.AssetRecvKey; | ||
219 | theSim.regionAssetSendKey = m_config.AssetSendKey; | ||
220 | theSim.regionUserURI = m_config.DefaultUserServer; | ||
221 | theSim.regionUserSendKey = m_config.UserSendKey; | ||
222 | theSim.regionUserRecvKey = m_config.UserRecvKey; | ||
223 | |||
224 | for (int i = 0; i < simnode.ChildNodes.Count; i++) | ||
225 | { | ||
226 | switch (simnode.ChildNodes[i].Name) | ||
227 | { | ||
228 | case "regionname": | ||
229 | theSim.regionName = simnode.ChildNodes[i].InnerText; | ||
230 | break; | ||
231 | |||
232 | case "sim_ip": | ||
233 | theSim.serverIP = simnode.ChildNodes[i].InnerText; | ||
234 | break; | ||
235 | |||
236 | case "sim_port": | ||
237 | theSim.serverPort = Convert.ToUInt32(simnode.ChildNodes[i].InnerText); | ||
238 | break; | ||
239 | |||
240 | case "region_locx": | ||
241 | theSim.regionLocX = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText); | ||
242 | theSim.regionHandle = Utils.UIntsToLong((theSim.regionLocX * Constants.RegionSize), (theSim.regionLocY * Constants.RegionSize)); | ||
243 | break; | ||
244 | |||
245 | case "region_locy": | ||
246 | theSim.regionLocY = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText); | ||
247 | theSim.regionHandle = Utils.UIntsToLong((theSim.regionLocX * Constants.RegionSize), (theSim.regionLocY * Constants.RegionSize)); | ||
248 | break; | ||
249 | } | ||
250 | } | ||
251 | |||
252 | theSim.serverURI = "http://" + theSim.serverIP + ":" + theSim.serverPort + "/"; | ||
253 | bool requirePublic = false; | ||
254 | bool requireValid = true; | ||
255 | |||
256 | if (requirePublic && | ||
257 | (theSim.serverIP.StartsWith("172.16") || theSim.serverIP.StartsWith("192.168") || | ||
258 | theSim.serverIP.StartsWith("10.") || theSim.serverIP.StartsWith("0.") || | ||
259 | theSim.serverIP.StartsWith("255."))) | ||
260 | { | ||
261 | return "ERROR! Servers must register with public addresses."; | ||
262 | } | ||
263 | |||
264 | if (requireValid && (theSim.serverIP.StartsWith("0.") || theSim.serverIP.StartsWith("255."))) | ||
265 | { | ||
266 | return "ERROR! 0.*.*.* / 255.*.*.* Addresses are invalid, please check your server config and try again"; | ||
267 | } | ||
268 | |||
269 | try | ||
270 | { | ||
271 | m_log.Info("[DATA]: " + | ||
272 | "Updating / adding via " + m_gridDBService.GetNumberOfPlugins() + " storage provider(s) registered."); | ||
273 | |||
274 | return m_gridDBService.CheckReservations(theSim, authkeynode); | ||
275 | } | ||
276 | catch (Exception e) | ||
277 | { | ||
278 | return "ERROR! Could not save to database! (" + e.ToString() + ")"; | ||
279 | } | ||
280 | } | ||
281 | } | ||
282 | } | ||