123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- using log4net;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Reflection;
- using Nini.Config;
- using OpenSim.Framework;
- using OpenSim.Framework.Console;
- using OpenSim.Framework.Communications;
- using OpenSim.Framework.ServiceAuth;
- using OpenSim.Server.Base;
- using OpenSim.Services.Interfaces;
- using OpenMetaverse;
- using OpenMetaverse.StructuredData;
- namespace OpenSim.Services.Connectors
- {
- public class MapImageServicesConnector : BaseServiceConnector, IMapImageService
- {
- private static readonly ILog m_log =
- LogManager.GetLogger(
- MethodBase.GetCurrentMethod().DeclaringType);
- private string m_ServerURI = String.Empty;
- public MapImageServicesConnector()
- {
- }
- public MapImageServicesConnector(string serverURI)
- {
- m_ServerURI = serverURI.TrimEnd('/');
- }
- public MapImageServicesConnector(IConfigSource source)
- {
- Initialise(source);
- }
- public virtual void Initialise(IConfigSource source)
- {
- IConfig config = source.Configs["MapImageService"];
- if (config == null)
- {
- m_log.Error("[MAP IMAGE CONNECTOR]: MapImageService missing");
- throw new Exception("MapImage connector init error");
- }
- string serviceURI = config.GetString("MapImageServerURI",
- String.Empty);
- if (serviceURI == String.Empty)
- {
- m_log.Error("[MAP IMAGE CONNECTOR]: No Server URI named in section MapImageService");
- throw new Exception("MapImage connector init error");
- }
- m_ServerURI = serviceURI;
- m_ServerURI = serviceURI.TrimEnd('/');
- base.Initialise(source, "MapImageService");
- }
- public bool RemoveMapTile(int x, int y, out string reason)
- {
- reason = string.Empty;
- int tickstart = Util.EnvironmentTickCount();
- Dictionary<string, object> sendData = new Dictionary<string, object>();
- sendData["X"] = x.ToString();
- sendData["Y"] = y.ToString();
- string reqString = ServerUtils.BuildQueryString(sendData);
- string uri = m_ServerURI + "/removemap";
- try
- {
- string reply = SynchronousRestFormsRequester.MakeRequest("POST",
- uri,
- reqString,
- m_Auth);
- if (reply != string.Empty)
- {
- Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
- if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
- {
- return true;
- }
- else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
- {
- m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Delete failed: {0}", replyData["Message"].ToString());
- reason = replyData["Message"].ToString();
- return false;
- }
- else if (!replyData.ContainsKey("Result"))
- {
- m_log.DebugFormat("[MAP IMAGE CONNECTOR]: reply data does not contain result field");
- }
- else
- {
- m_log.DebugFormat("[MAP IMAGE CONNECTOR]: unexpected result {0}", replyData["Result"].ToString());
- reason = "Unexpected result " + replyData["Result"].ToString();
- }
- }
- else
- {
- m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Map post received null reply");
- }
- }
- catch (Exception e)
- {
- m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Exception when contacting map server at {0}: {1}", uri, e.Message);
- }
- finally
- {
-
- int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
- m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile deleted in {0}ms", tickdiff);
- }
- return false;
- }
- public bool AddMapTile(int x, int y, byte[] jpgData, out string reason)
- {
- reason = string.Empty;
- int tickstart = Util.EnvironmentTickCount();
- Dictionary<string, object> sendData = new Dictionary<string, object>();
- sendData["X"] = x.ToString();
- sendData["Y"] = y.ToString();
- sendData["TYPE"] = "image/jpeg";
- sendData["DATA"] = Convert.ToBase64String(jpgData);
- string reqString = ServerUtils.BuildQueryString(sendData);
- string uri = m_ServerURI + "/map";
- try
- {
- string reply = SynchronousRestFormsRequester.MakeRequest("POST",
- uri,
- reqString,
- m_Auth);
- if (reply != string.Empty)
- {
- Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
- if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
- {
- return true;
- }
- else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
- {
- reason = string.Format("Map post to {0} failed: {1}", uri, replyData["Message"].ToString());
- m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
- return false;
- }
- else if (!replyData.ContainsKey("Result"))
- {
- reason = string.Format("Reply data from {0} does not contain result field", uri);
- m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
- }
- else
- {
- reason = string.Format("Unexpected result {0} from {1}" + replyData["Result"].ToString(), uri);
- m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
- }
- }
- else
- {
- reason = string.Format("Map post received null reply from {0}", uri);
- m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
- }
- }
- catch (Exception e)
- {
- reason = string.Format("Exception when posting to map server at {0}: {1}", uri, e.Message);
- m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
- }
- finally
- {
-
- int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
- m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile uploaded in {0}ms", tickdiff);
- }
- return false;
- }
- public byte[] GetMapTile(string fileName, out string format)
- {
- format = string.Empty;
- new Exception("GetMapTile method not Implemented");
- return null;
- }
- }
- }
|