OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
MapImageServicesConnector.cs
Go to the documentation of this file.
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 log4net;
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Net;
33 using System.Reflection;
34 
35 using Nini.Config;
36 using OpenSim.Framework;
37 using OpenSim.Framework.Console;
38 
39 using OpenSim.Framework.ServiceAuth;
40 using OpenSim.Server.Base;
41 using OpenSim.Services.Interfaces;
42 using OpenMetaverse;
43 using OpenMetaverse.StructuredData;
44 
45 namespace OpenSim.Services.Connectors
46 {
48  {
49  private static readonly ILog m_log =
50  LogManager.GetLogger(
51  MethodBase.GetCurrentMethod().DeclaringType);
52 
53  private string m_ServerURI = String.Empty;
54 
56  {
57  }
58 
59  public MapImageServicesConnector(string serverURI)
60  {
61  m_ServerURI = serverURI.TrimEnd('/');
62  }
63 
64  public MapImageServicesConnector(IConfigSource source)
65  {
66  Initialise(source);
67  }
68 
69  public virtual void Initialise(IConfigSource source)
70  {
71  IConfig config = source.Configs["MapImageService"];
72  if (config == null)
73  {
74  m_log.Error("[MAP IMAGE CONNECTOR]: MapImageService missing");
75  throw new Exception("MapImage connector init error");
76  }
77 
78  string serviceURI = config.GetString("MapImageServerURI",
79  String.Empty);
80 
81  if (serviceURI == String.Empty)
82  {
83  m_log.Error("[MAP IMAGE CONNECTOR]: No Server URI named in section MapImageService");
84  throw new Exception("MapImage connector init error");
85  }
86  m_ServerURI = serviceURI;
87  m_ServerURI = serviceURI.TrimEnd('/');
88  base.Initialise(source, "MapImageService");
89  }
90 
91  public bool RemoveMapTile(int x, int y, out string reason)
92  {
93  reason = string.Empty;
94  int tickstart = Util.EnvironmentTickCount();
95  Dictionary<string, object> sendData = new Dictionary<string, object>();
96  sendData["X"] = x.ToString();
97  sendData["Y"] = y.ToString();
98 
99  string reqString = ServerUtils.BuildQueryString(sendData);
100  string uri = m_ServerURI + "/removemap";
101 
102  try
103  {
104  string reply = SynchronousRestFormsRequester.MakeRequest("POST",
105  uri,
106  reqString,
107  m_Auth);
108  if (reply != string.Empty)
109  {
110  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
111 
112  if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
113  {
114  return true;
115  }
116  else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
117  {
118  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Delete failed: {0}", replyData["Message"].ToString());
119  reason = replyData["Message"].ToString();
120  return false;
121  }
122  else if (!replyData.ContainsKey("Result"))
123  {
124  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: reply data does not contain result field");
125  }
126  else
127  {
128  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: unexpected result {0}", replyData["Result"].ToString());
129  reason = "Unexpected result " + replyData["Result"].ToString();
130  }
131 
132  }
133  else
134  {
135  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Map post received null reply");
136  }
137  }
138  catch (Exception e)
139  {
140  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Exception when contacting map server at {0}: {1}", uri, e.Message);
141  }
142  finally
143  {
144  // This just dumps a warning for any operation that takes more than 100 ms
145  int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
146  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile deleted in {0}ms", tickdiff);
147  }
148 
149  return false;
150  }
151 
152  public bool RemoveMapTile(int x, int y, UUID scopeID, out string reason)
153  {
154  reason = string.Empty;
155  int tickstart = Util.EnvironmentTickCount();
156  Dictionary<string, object> sendData = new Dictionary<string, object>();
157  sendData["X"] = x.ToString();
158  sendData["Y"] = y.ToString();
159  sendData["SCOPE"] = scopeID.ToString();
160 
161  string reqString = ServerUtils.BuildQueryString(sendData);
162  string uri = m_ServerURI + "/removemap";
163 
164  try
165  {
166  string reply = SynchronousRestFormsRequester.MakeRequest("POST",
167  uri,
168  reqString);
169  if (reply != string.Empty)
170  {
171  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
172 
173  if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
174  {
175  return true;
176  }
177  else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
178  {
179  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Delete failed: {0}", replyData["Message"].ToString());
180  reason = replyData["Message"].ToString();
181  return false;
182  }
183  else if (!replyData.ContainsKey("Result"))
184  {
185  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: reply data does not contain result field");
186  }
187  else
188  {
189  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: unexpected result {0}", replyData["Result"].ToString());
190  reason = "Unexpected result " + replyData["Result"].ToString();
191  }
192 
193  }
194  else
195  {
196  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Map post received null reply");
197  }
198  }
199  catch (Exception e)
200  {
201  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Exception when contacting map server at {0}: {1}", uri, e.Message);
202  }
203  finally
204  {
205  // This just dumps a warning for any operation that takes more than 100 ms
206  int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
207  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile deleted in {0}ms", tickdiff);
208  }
209 
210  return false;
211  }
212 
213  public bool AddMapTile(int x, int y, byte[] jpgData, UUID scopeID, out string reason)
214  {
215  reason = string.Empty;
216  int tickstart = Util.EnvironmentTickCount();
217  Dictionary<string, object> sendData = new Dictionary<string, object>();
218  sendData["X"] = x.ToString();
219  sendData["Y"] = y.ToString();
220  sendData["SCOPE"] = scopeID.ToString();
221  sendData["TYPE"] = "image/jpeg";
222  sendData["DATA"] = Convert.ToBase64String(jpgData);
223 
224  string reqString = ServerUtils.BuildQueryString(sendData);
225  string uri = m_ServerURI + "/map";
226 
227  try
228  {
229  string reply = SynchronousRestFormsRequester.MakeRequest("POST",
230  uri,
231  reqString,
232  m_Auth);
233  if (reply != string.Empty)
234  {
235  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
236 
237  if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
238  {
239  return true;
240  }
241  else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
242  {
243  reason = string.Format("Map post to {0} failed: {1}", uri, replyData["Message"].ToString());
244  m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
245 
246  return false;
247  }
248  else if (!replyData.ContainsKey("Result"))
249  {
250  reason = string.Format("Reply data from {0} does not contain result field", uri);
251  m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
252  }
253  else
254  {
255  reason = string.Format("Unexpected result {0} from {1}" + replyData["Result"].ToString(), uri);
256  m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
257  }
258  }
259  else
260  {
261  reason = string.Format("Map post received null reply from {0}", uri);
262  m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
263  }
264  }
265  catch (Exception e)
266  {
267  reason = string.Format("Exception when posting to map server at {0}: {1}", uri, e.Message);
268  m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
269  }
270  finally
271  {
272  // This just dumps a warning for any operation that takes more than 100 ms
273  int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
274  m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile uploaded in {0}ms", tickdiff);
275  }
276 
277  return false;
278 
279  }
280 
281  public byte[] GetMapTile(string fileName, UUID scopeID, out string format)
282  {
283  format = string.Empty;
284  new Exception("GetMapTile method not Implemented");
285  return null;
286  }
287  }
288 }
bool AddMapTile(int x, int y, byte[] jpgData, UUID scopeID, out string reason)
byte[] GetMapTile(string fileName, UUID scopeID, out string format)
bool RemoveMapTile(int x, int y, UUID scopeID, out string reason)