OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
NullRegionData.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.Collections;
30 using System.Collections.Generic;
31 using OpenMetaverse;
32 using OpenSim.Framework;
33 using OpenSim.Data;
34 using System.Reflection;
35 using log4net;
37 
38 namespace OpenSim.Data.Null
39 {
40  public class NullRegionData : IRegionData
41  {
42  private static NullRegionData Instance = null;
43 
47  private bool m_useStaticInstance = true;
48 
49 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
50 
51  Dictionary<UUID, RegionData> m_regionData = new Dictionary<UUID, RegionData>();
52 
53  public NullRegionData(string connectionString, string realm)
54  {
55 // m_log.DebugFormat(
56 // "[NULL REGION DATA]: Constructor got connectionString {0}, realm {1}", connectionString, realm);
57 
58  // The !static connection string is a hack so that regression tests can use this module without a high degree of fragility
59  // in having to deal with the static reference in the once-loaded NullRegionData class.
60  //
61  // In standalone operation, we have to use only one instance of this class since the login service and
62  // simulator have no other way of using a common data store.
63  if (connectionString == "!static")
64  m_useStaticInstance = false;
65  else if (Instance == null)
66  Instance = this;
67  }
68 
69  private delegate bool Matcher(string value);
70 
71  public List<RegionData> Get(string regionName, UUID scopeID)
72  {
73  if (m_useStaticInstance && Instance != this)
74  return Instance.Get(regionName, scopeID);
75 
76 // m_log.DebugFormat("[NULL REGION DATA]: Getting region {0}, scope {1}", regionName, scopeID);
77 
78  string cleanName = regionName.ToLower();
79 
80  // Handle SQL wildcards
81  const string wildcard = "%";
82  bool wildcardPrefix = false;
83  bool wildcardSuffix = false;
84  if (cleanName.Equals(wildcard))
85  {
86  wildcardPrefix = wildcardSuffix = true;
87  cleanName = string.Empty;
88  }
89  else
90  {
91  if (cleanName.StartsWith(wildcard))
92  {
93  wildcardPrefix = true;
94  cleanName = cleanName.Substring(1);
95  }
96  if (regionName.EndsWith(wildcard))
97  {
98  wildcardSuffix = true;
99  cleanName = cleanName.Remove(cleanName.Length - 1);
100  }
101  }
102 
103  Matcher queryMatch;
104  if (wildcardPrefix && wildcardSuffix)
105  queryMatch = delegate(string s) { return s.Contains(cleanName); };
106  else if (wildcardSuffix)
107  queryMatch = delegate(string s) { return s.StartsWith(cleanName); };
108  else if (wildcardPrefix)
109  queryMatch = delegate(string s) { return s.EndsWith(cleanName); };
110  else
111  queryMatch = delegate(string s) { return s.Equals(cleanName); };
112 
113  // Find region data
114  List<RegionData> ret = new List<RegionData>();
115 
116  lock (m_regionData)
117  {
118  foreach (RegionData r in m_regionData.Values)
119  {
120  // m_log.DebugFormat("[NULL REGION DATA]: comparing {0} to {1}", cleanName, r.RegionName.ToLower());
121  if (queryMatch(r.RegionName.ToLower()))
122  ret.Add(r);
123  }
124  }
125 
126  if (ret.Count > 0)
127  return ret;
128 
129  return null;
130  }
131 
132  public RegionData Get(int posX, int posY, UUID scopeID)
133  {
134  if (m_useStaticInstance && Instance != this)
135  return Instance.Get(posX, posY, scopeID);
136 
137  RegionData ret = null;
138 
139  lock (m_regionData)
140  {
141  foreach (RegionData r in m_regionData.Values)
142  {
143  if (posX >= r.posX && posX < r.posX + r.sizeX
144  && posY >= r.posY && posY < r.posY + r.sizeY)
145  {
146  ret = r;
147  break;
148  }
149  }
150  }
151 
152  return ret;
153  }
154 
155  public RegionData Get(UUID regionID, UUID scopeID)
156  {
157  if (m_useStaticInstance && Instance != this)
158  return Instance.Get(regionID, scopeID);
159 
160  lock (m_regionData)
161  {
162  if (m_regionData.ContainsKey(regionID))
163  return m_regionData[regionID];
164  }
165 
166  return null;
167  }
168 
169  public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID)
170  {
171  if (m_useStaticInstance && Instance != this)
172  return Instance.Get(startX, startY, endX, endY, scopeID);
173 
174  List<RegionData> ret = new List<RegionData>();
175 
176  lock (m_regionData)
177  {
178  foreach (RegionData r in m_regionData.Values)
179  {
180  if (r.posX + r.sizeX > startX && r.posX <= endX
181  && r.posY + r.sizeX > startY && r.posY <= endY)
182  ret.Add(r);
183  }
184  }
185 
186  return ret;
187  }
188 
189  public bool Store(RegionData data)
190  {
191  if (m_useStaticInstance && Instance != this)
192  return Instance.Store(data);
193 
194 // m_log.DebugFormat(
195 // "[NULL REGION DATA]: Storing region {0} {1}, scope {2}", data.RegionName, data.RegionID, data.ScopeID);
196 
197  lock (m_regionData)
198  {
199  m_regionData[data.RegionID] = data;
200  }
201 
202  return true;
203  }
204 
205  public bool SetDataItem(UUID regionID, string item, string value)
206  {
207  if (m_useStaticInstance && Instance != this)
208  return Instance.SetDataItem(regionID, item, value);
209 
210  lock (m_regionData)
211  {
212  if (!m_regionData.ContainsKey(regionID))
213  return false;
214 
215  m_regionData[regionID].Data[item] = value;
216  }
217 
218  return true;
219  }
220 
221  public bool Delete(UUID regionID)
222  {
223  if (m_useStaticInstance && Instance != this)
224  return Instance.Delete(regionID);
225 
226 // m_log.DebugFormat("[NULL REGION DATA]: Deleting region {0}", regionID);
227 
228  lock (m_regionData)
229  {
230  if (!m_regionData.ContainsKey(regionID))
231  return false;
232 
233  m_regionData.Remove(regionID);
234  }
235 
236  return true;
237  }
238 
239  public List<RegionData> GetDefaultRegions(UUID scopeID)
240  {
241  return Get((int)RegionFlags.DefaultRegion, scopeID);
242  }
243 
244  public List<RegionData> GetDefaultHypergridRegions(UUID scopeID)
245  {
246  return Get((int)RegionFlags.DefaultHGRegion, scopeID);
247  }
248 
249  public List<RegionData> GetFallbackRegions(UUID scopeID, int x, int y)
250  {
251  List<RegionData> regions = Get((int)RegionFlags.FallbackRegion, scopeID);
252  RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y);
253  regions.Sort(distanceComparer);
254  return regions;
255  }
256 
257  public List<RegionData> GetHyperlinks(UUID scopeID)
258  {
259  return Get((int)RegionFlags.Hyperlink, scopeID);
260  }
261 
262  private List<RegionData> Get(int regionFlags, UUID scopeID)
263  {
264  if (Instance != this)
265  return Instance.Get(regionFlags, scopeID);
266 
267  List<RegionData> ret = new List<RegionData>();
268 
269  lock (m_regionData)
270  {
271  foreach (RegionData r in m_regionData.Values)
272  {
273  if ((Convert.ToInt32(r.Data["flags"]) & regionFlags) != 0)
274  ret.Add(r);
275  }
276  }
277 
278  return ret;
279  }
280  }
281 }
List< RegionData > GetDefaultHypergridRegions(UUID scopeID)
int posX
The position in meters of this region.
Definition: IRegionData.cs:44
An interface for connecting to the authentication datastore
Definition: IRegionData.cs:70
OpenSim.Framework.RegionFlags RegionFlags
bool SetDataItem(UUID regionID, string item, string value)
int posY
The position in meters of this region.
Definition: IRegionData.cs:49
Dictionary< string, object > Data
Definition: IRegionData.cs:64
List< RegionData > Get(int startX, int startY, int endX, int endY, UUID scopeID)
RegionFlags
Region flags used internally by OpenSimulator to store installation specific information about region...
Definition: RegionFlags.cs:40
NullRegionData(string connectionString, string realm)
List< RegionData > GetFallbackRegions(UUID scopeID, int x, int y)
RegionData Get(UUID regionID, UUID scopeID)
List< RegionData > GetHyperlinks(UUID scopeID)
RegionData Get(int posX, int posY, UUID scopeID)
List< RegionData > Get(string regionName, UUID scopeID)
List< RegionData > GetDefaultRegions(UUID scopeID)