aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Services/Connectors/MapImage
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Services/Connectors/MapImage')
-rw-r--r--OpenSim/Services/Connectors/MapImage/MapImageServiceConnector.cs158
1 files changed, 158 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/MapImage/MapImageServiceConnector.cs b/OpenSim/Services/Connectors/MapImage/MapImageServiceConnector.cs
new file mode 100644
index 0000000..ff0e9d9
--- /dev/null
+++ b/OpenSim/Services/Connectors/MapImage/MapImageServiceConnector.cs
@@ -0,0 +1,158 @@
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
28using log4net;
29using System;
30using System.Collections.Generic;
31using System.IO;
32using System.Net;
33using System.Reflection;
34
35using Nini.Config;
36using OpenSim.Framework;
37using OpenSim.Framework.Console;
38using OpenSim.Framework.Communications;
39using OpenSim.Services.Interfaces;
40using OpenMetaverse;
41using OpenMetaverse.StructuredData;
42
43namespace OpenSim.Services.Connectors
44{
45 public class MapImageServicesConnector : IMapImageService
46 {
47 private static readonly ILog m_log =
48 LogManager.GetLogger(
49 MethodBase.GetCurrentMethod().DeclaringType);
50
51 private string m_ServerURI = String.Empty;
52 private IImprovedAssetCache m_Cache = null;
53
54 public MapImageServicesConnector()
55 {
56 }
57
58 public MapImageServicesConnector(string serverURI)
59 {
60 m_ServerURI = serverURI.TrimEnd('/');
61 }
62
63 public MapImageServicesConnector(IConfigSource source)
64 {
65 Initialise(source);
66 }
67
68 public virtual void Initialise(IConfigSource source)
69 {
70 IConfig config = source.Configs["MapImageService"];
71 if (config == null)
72 {
73 m_log.Error("[MAP IMAGE CONNECTOR]: MapImageService missing");
74 throw new Exception("MapImage connector init error");
75 }
76
77 string serviceURI = config.GetString("MapImageServerURI",
78 String.Empty);
79
80 if (serviceURI == String.Empty)
81 {
82 m_log.Error("[MAP IMAGE CONNECTOR]: No Server URI named in section MapImageService");
83 throw new Exception("MapImage connector init error");
84 }
85 m_ServerURI = serviceURI;
86 }
87
88 public bool AddMapTile(int x, int y, byte[] pngData, out string reason)
89 {
90 List<MultipartForm.Element> postParameters = new List<MultipartForm.Element>()
91 {
92 new MultipartForm.Parameter("X", x.ToString()),
93 new MultipartForm.Parameter("Y", y.ToString()),
94 new MultipartForm.File("Tile", "tile.png", "image/png", pngData)
95 };
96
97 reason = string.Empty;
98 int tickstart = Util.EnvironmentTickCount();
99
100 // Make the remote storage request
101 try
102 {
103 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_ServerURI);
104 request.Timeout = 20000;
105 request.ReadWriteTimeout = 5000;
106
107 using (HttpWebResponse response = MultipartForm.Post(request, postParameters))
108 {
109 using (Stream responseStream = response.GetResponseStream())
110 {
111 string responseStr = responseStream.GetStreamString();
112 OSD responseOSD = OSDParser.Deserialize(responseStr);
113 if (responseOSD.Type == OSDType.Map)
114 {
115 OSDMap responseMap = (OSDMap)responseOSD;
116 if (responseMap["Success"].AsBoolean())
117 return true;
118
119 reason = "Upload failed: " + responseMap["Message"].AsString();
120 }
121 else
122 {
123 reason = "Response format was invalid:\n" + responseStr;
124 }
125 }
126 }
127 }
128 catch (WebException we)
129 {
130 reason = we.Message;
131 if (we.Status == WebExceptionStatus.ProtocolError)
132 {
133 HttpWebResponse webResponse = (HttpWebResponse)we.Response;
134 reason = String.Format("[{0}] {1}", webResponse.StatusCode, webResponse.StatusDescription);
135 }
136 }
137 catch (Exception ex)
138 {
139 reason = ex.Message;
140 }
141 finally
142 {
143 // This just dumps a warning for any operation that takes more than 100 ms
144 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
145 m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile uploaded in {0}ms", tickdiff);
146 }
147
148 return false;
149 }
150
151 public byte[] GetMapTile(string fileName, out string format)
152 {
153 format = string.Empty;
154 new Exception("GetMapTile method not Implemented");
155 return null;
156 }
157 }
158}