diff options
Diffstat (limited to 'OpenSim/Server/Handlers/Map/MapAddServerConnector.cs')
-rw-r--r-- | OpenSim/Server/Handlers/Map/MapAddServerConnector.cs | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Map/MapAddServerConnector.cs b/OpenSim/Server/Handlers/Map/MapAddServerConnector.cs new file mode 100644 index 0000000..99f98b6 --- /dev/null +++ b/OpenSim/Server/Handlers/Map/MapAddServerConnector.cs | |||
@@ -0,0 +1,183 @@ | |||
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 OpenSimulator 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.Generic; | ||
30 | using System.IO; | ||
31 | using System.Reflection; | ||
32 | using System.Xml; | ||
33 | |||
34 | using Nini.Config; | ||
35 | using log4net; | ||
36 | |||
37 | using OpenSim.Server.Base; | ||
38 | using OpenSim.Services.Interfaces; | ||
39 | using OpenSim.Framework.Servers.HttpServer; | ||
40 | using OpenSim.Server.Handlers.Base; | ||
41 | |||
42 | namespace OpenSim.Server.Handlers.MapImage | ||
43 | { | ||
44 | public class MapAddServiceConnector : ServiceConnector | ||
45 | { | ||
46 | private IMapImageService m_MapService; | ||
47 | private string m_ConfigName = "MapImageService"; | ||
48 | |||
49 | public MapAddServiceConnector(IConfigSource config, IHttpServer server, string configName) : | ||
50 | base(config, server, configName) | ||
51 | { | ||
52 | IConfig serverConfig = config.Configs[m_ConfigName]; | ||
53 | if (serverConfig == null) | ||
54 | throw new Exception(String.Format("No section {0} in config file", m_ConfigName)); | ||
55 | |||
56 | string gridService = serverConfig.GetString("LocalServiceModule", | ||
57 | String.Empty); | ||
58 | |||
59 | if (gridService == String.Empty) | ||
60 | throw new Exception("No LocalServiceModule in config file"); | ||
61 | |||
62 | Object[] args = new Object[] { config }; | ||
63 | m_MapService = ServerUtils.LoadPlugin<IMapImageService>(gridService, args); | ||
64 | |||
65 | server.AddStreamHandler(new MapServerPostHandler(m_MapService)); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | class MapServerPostHandler : BaseStreamHandler | ||
70 | { | ||
71 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
72 | private IMapImageService m_MapService; | ||
73 | |||
74 | public MapServerPostHandler(IMapImageService service) : | ||
75 | base("POST", "/map") | ||
76 | { | ||
77 | m_MapService = service; | ||
78 | } | ||
79 | |||
80 | public override byte[] Handle(string path, Stream requestData, OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
81 | { | ||
82 | m_log.DebugFormat("[MAP SERVICE IMAGE HANDLER]: Received {0}", path); | ||
83 | StreamReader sr = new StreamReader(requestData); | ||
84 | string body = sr.ReadToEnd(); | ||
85 | sr.Close(); | ||
86 | body = body.Trim(); | ||
87 | |||
88 | try | ||
89 | { | ||
90 | Dictionary<string, object> request = ServerUtils.ParseQueryString(body); | ||
91 | |||
92 | if (!request.ContainsKey("X") || !request.ContainsKey("Y") || !request.ContainsKey("DATA")) | ||
93 | { | ||
94 | httpResponse.StatusCode = (int)OSHttpStatusCode.ClientErrorBadRequest; | ||
95 | return FailureResult("Bad request."); | ||
96 | } | ||
97 | int x = 0, y = 0; | ||
98 | Int32.TryParse(request["X"].ToString(), out x); | ||
99 | Int32.TryParse(request["Y"].ToString(), out y); | ||
100 | string type = "image/jpeg"; | ||
101 | if (request.ContainsKey("TYPE")) | ||
102 | type = request["TYPE"].ToString(); | ||
103 | byte[] data = Convert.FromBase64String(request["DATA"].ToString()); | ||
104 | |||
105 | string reason = string.Empty; | ||
106 | bool result = m_MapService.AddMapTile(x, y, data, out reason); | ||
107 | |||
108 | if (result) | ||
109 | return SuccessResult(); | ||
110 | else | ||
111 | return FailureResult(reason); | ||
112 | |||
113 | } | ||
114 | catch (Exception e) | ||
115 | { | ||
116 | m_log.ErrorFormat("[MAP SERVICE IMAGE HANDLER]: Exception {0} {1}", e.Message, e.StackTrace); | ||
117 | } | ||
118 | |||
119 | return FailureResult("Unexpected server error"); | ||
120 | |||
121 | } | ||
122 | |||
123 | private byte[] SuccessResult() | ||
124 | { | ||
125 | XmlDocument doc = new XmlDocument(); | ||
126 | |||
127 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
128 | "", ""); | ||
129 | |||
130 | doc.AppendChild(xmlnode); | ||
131 | |||
132 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
133 | ""); | ||
134 | |||
135 | doc.AppendChild(rootElement); | ||
136 | |||
137 | XmlElement result = doc.CreateElement("", "Result", ""); | ||
138 | result.AppendChild(doc.CreateTextNode("Success")); | ||
139 | |||
140 | rootElement.AppendChild(result); | ||
141 | |||
142 | return DocToBytes(doc); | ||
143 | } | ||
144 | |||
145 | private byte[] FailureResult(string msg) | ||
146 | { | ||
147 | XmlDocument doc = new XmlDocument(); | ||
148 | |||
149 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
150 | "", ""); | ||
151 | |||
152 | doc.AppendChild(xmlnode); | ||
153 | |||
154 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
155 | ""); | ||
156 | |||
157 | doc.AppendChild(rootElement); | ||
158 | |||
159 | XmlElement result = doc.CreateElement("", "Result", ""); | ||
160 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
161 | |||
162 | rootElement.AppendChild(result); | ||
163 | |||
164 | XmlElement message = doc.CreateElement("", "Message", ""); | ||
165 | message.AppendChild(doc.CreateTextNode(msg)); | ||
166 | |||
167 | rootElement.AppendChild(message); | ||
168 | |||
169 | return DocToBytes(doc); | ||
170 | } | ||
171 | |||
172 | private byte[] DocToBytes(XmlDocument doc) | ||
173 | { | ||
174 | MemoryStream ms = new MemoryStream(); | ||
175 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
176 | xw.Formatting = Formatting.Indented; | ||
177 | doc.WriteTo(xw); | ||
178 | xw.Flush(); | ||
179 | |||
180 | return ms.ToArray(); | ||
181 | } | ||
182 | } | ||
183 | } | ||