OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
MapGetServerConnector.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 System;
29 using System.IO;
30 using System.Net;
31 using System.Reflection;
32 using System.Threading;
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 using OpenMetaverse;
42 
43 namespace OpenSim.Server.Handlers.MapImage
44 {
46  {
47 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 
49  private IMapImageService m_MapService;
50 
51  private string m_ConfigName = "MapImageService";
52 
53  public MapGetServiceConnector(IConfigSource config, IHttpServer server, string configName) :
54  base(config, server, configName)
55  {
56  IConfig serverConfig = config.Configs[m_ConfigName];
57  if (serverConfig == null)
58  throw new Exception(String.Format("No section {0} in config file", m_ConfigName));
59 
60  string gridService = serverConfig.GetString("LocalServiceModule",
61  String.Empty);
62 
63  if (gridService == String.Empty)
64  throw new Exception("No LocalServiceModule in config file");
65 
66  Object[] args = new Object[] { config };
67  m_MapService = ServerUtils.LoadPlugin<IMapImageService>(gridService, args);
68 
69  server.AddStreamHandler(new MapServerGetHandler(m_MapService));
70  }
71  }
72 
74  {
75  public static ManualResetEvent ev = new ManualResetEvent(true);
76 
77 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
78 
79  private IMapImageService m_MapService;
80 
82  base("GET", "/map")
83  {
84  m_MapService = service;
85  }
86 
87  protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
88  {
89  ev.WaitOne();
90  lock (ev)
91  {
92  ev.Reset();
93  }
94 
95  byte[] result = new byte[0];
96  string format = string.Empty;
97 
98 // UUID scopeID = new UUID("07f8d88e-cd5e-4239-a0ed-843f75d09992");
99  UUID scopeID = UUID.Zero;
100 
101  string[] bits = path.Trim('/').Split(new char[] {'/'});
102  if (bits.Length > 1)
103  {
104  scopeID = new UUID(bits[0]);
105  path = bits[1];
106  }
107  result = m_MapService.GetMapTile(path.Trim('/'), scopeID, out format);
108  if (result.Length > 0)
109  {
110  httpResponse.StatusCode = (int)HttpStatusCode.OK;
111  if (format.Equals(".png"))
112  httpResponse.ContentType = "image/png";
113  else if (format.Equals(".jpg") || format.Equals(".jpeg"))
114  httpResponse.ContentType = "image/jpeg";
115  }
116  else
117  {
118  httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
119  httpResponse.ContentType = "text/plain";
120  }
121 
122  lock (ev)
123  {
124  ev.Set();
125  }
126 
127  return result;
128  }
129 
130  }
131 }
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
Definition: IHttpServer.cs:36
override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
MapGetServiceConnector(IConfigSource config, IHttpServer server, string configName)