OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
MapSearchModule.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 using System;
28 using System.Collections.Generic;
29 using System.Reflection;
30 using log4net;
31 using Nini.Config;
32 using OpenMetaverse;
33 using Mono.Addins;
34 using OpenSim.Framework;
35 using OpenSim.Region.Framework.Interfaces;
36 using OpenSim.Region.Framework.Scenes;
37 using OpenSim.Services.Interfaces;
39 
40 namespace OpenSim.Region.CoreModules.World.WorldMap
41 {
42  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "MapSearchModule")]
44  {
45  private static readonly ILog m_log =
46  LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47 
48  Scene m_scene = null; // only need one for communication with GridService
49  List<Scene> m_scenes = new List<Scene>();
50  List<UUID> m_Clients;
51 
52  IWorldMapModule m_WorldMap;
53  IWorldMapModule WorldMap
54  {
55  get
56  {
57  if (m_WorldMap == null)
58  m_WorldMap = m_scene.RequestModuleInterface<IWorldMapModule>();
59  return m_WorldMap;
60  }
61 
62  }
63 
64  #region ISharedRegionModule Members
65  public void Initialise(IConfigSource source)
66  {
67  }
68 
69  public void AddRegion(Scene scene)
70  {
71  if (m_scene == null)
72  {
73  m_scene = scene;
74  }
75 
76  m_scenes.Add(scene);
77  scene.EventManager.OnNewClient += OnNewClient;
78  m_Clients = new List<UUID>();
79 
80  }
81 
82  public void RemoveRegion(Scene scene)
83  {
84  m_scenes.Remove(scene);
85  if (m_scene == scene && m_scenes.Count > 0)
86  m_scene = m_scenes[0];
87 
88  scene.EventManager.OnNewClient -= OnNewClient;
89  }
90 
91  public void PostInitialise()
92  {
93  }
94 
95  public void Close()
96  {
97  m_scene = null;
98  m_scenes.Clear();
99  }
100 
101  public string Name
102  {
103  get { return "MapSearchModule"; }
104  }
105 
106  public Type ReplaceableInterface
107  {
108  get { return null; }
109  }
110 
111  public void RegionLoaded(Scene scene)
112  {
113  }
114  #endregion
115 
116  private void OnNewClient(IClientAPI client)
117  {
118  client.OnMapNameRequest += OnMapNameRequestHandler;
119  }
120 
121  private void OnMapNameRequestHandler(IClientAPI remoteClient, string mapName, uint flags)
122  {
123  lock (m_Clients)
124  {
125  if (m_Clients.Contains(remoteClient.AgentId))
126  return;
127 
128  m_Clients.Add(remoteClient.AgentId);
129  }
130 
131  try
132  {
133  OnMapNameRequest(remoteClient, mapName, flags);
134  }
135  finally
136  {
137  lock (m_Clients)
138  m_Clients.Remove(remoteClient.AgentId);
139  }
140  }
141 
142  private void OnMapNameRequest(IClientAPI remoteClient, string mapName, uint flags)
143  {
144  Util.FireAndForget(x =>
145  {
146  List<MapBlockData> blocks = new List<MapBlockData>();
147  if (mapName.Length < 3 || (mapName.EndsWith("#") && mapName.Length < 4))
148  {
149  // final block, closing the search result
150  AddFinalBlock(blocks,mapName);
151 
152  // flags are agent flags sent from the viewer.
153  // they have different values depending on different viewers, apparently
154  remoteClient.SendMapBlock(blocks, flags);
155  remoteClient.SendAlertMessage("Use a search string with at least 3 characters");
156  return;
157  }
158 
159  //m_log.DebugFormat("MAP NAME=({0})", mapName);
160 
161  // Hack to get around the fact that ll V3 now drops the port from the
162  // map name. See https://jira.secondlife.com/browse/VWR-28570
163  //
164  // Caller, use this magic form instead:
165  // secondlife://http|!!mygrid.com|8002|Region+Name/128/128
166  // or url encode if possible.
167  // the hacks we do with this viewer...
168  //
169  bool needOriginalName = false;
170  string mapNameOrig = mapName;
171  if (mapName.Contains("|"))
172  {
173  mapName = mapName.Replace('|', ':');
174  needOriginalName = true;
175  }
176  if (mapName.Contains("+"))
177  {
178  mapName = mapName.Replace('+', ' ');
179  needOriginalName = true;
180  }
181  if (mapName.Contains("!"))
182  {
183  mapName = mapName.Replace('!', '/');
184  needOriginalName = true;
185  }
186  if (mapName.Contains("."))
187  needOriginalName = true;
188 
189  // try to fetch from GridServer
190  List<GridRegion> regionInfos = m_scene.GridService.GetRegionsByName(m_scene.RegionInfo.ScopeID, mapName, 20);
191  // if (regionInfos.Count == 0)
192  // remoteClient.SendAlertMessage("Hyperlink could not be established.");
193 
194  //m_log.DebugFormat("[MAPSEARCHMODULE]: search {0} returned {1} regions", mapName, regionInfos.Count);
195 
196  MapBlockData data;
197  if (regionInfos.Count > 0)
198  {
199  foreach (GridRegion info in regionInfos)
200  {
201  data = new MapBlockData();
202  data.Agents = 0;
203  data.Access = info.Access;
204  MapBlockData block = new MapBlockData();
205  WorldMap.MapBlockFromGridRegion(block, info, flags);
206 
207  if (flags == 2 && regionInfos.Count == 1 && needOriginalName)
208  block.Name = mapNameOrig;
209  blocks.Add(block);
210  }
211  }
212 
213  // final block, closing the search result
214  AddFinalBlock(blocks,mapNameOrig);
215 
216  // flags are agent flags sent from the viewer.
217  // they have different values depending on different viewers, apparently
218  remoteClient.SendMapBlock(blocks, flags);
219 
220  // send extra user messages for V3
221  // because the UI is very confusing
222  // while we don't fix the hard-coded urls
223  if (flags == 2)
224  {
225  if (regionInfos.Count == 0)
226  remoteClient.SendAgentAlertMessage("No regions found with that name.", true);
227 // else if (regionInfos.Count == 1)
228 // remoteClient.SendAgentAlertMessage("Region found!", false);
229  }
230  });
231  }
232 
233  private void AddFinalBlock(List<MapBlockData> blocks,string name)
234  {
235  // final block, closing the search result
236  MapBlockData data = new MapBlockData();
237  data.Agents = 0;
238  data.Access = (byte)SimAccess.NonExistent;
239  data.MapImageId = UUID.Zero;
240  data.Name = name;
241  data.RegionFlags = 0;
242  data.WaterHeight = 0; // not used
243  data.X = 0;
244  data.Y = 0;
245  blocks.Add(data);
246  }
247 // private Scene GetClientScene(IClientAPI client)
248 // {
249 // foreach (Scene s in m_scenes)
250 // {
251 // if (client.Scene.RegionInfo.RegionHandle == s.RegionInfo.RegionHandle)
252 // return s;
253 // }
254 // return m_scene;
255 // }
256  }
257 }
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
OpenSim.Services.Interfaces.GridRegion GridRegion
Interactive OpenSim region server
Definition: OpenSim.cs:55
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...