diff options
Merge branch 'master' into diva-textures
Diffstat (limited to 'OpenSim/Services/GridService/GridService.cs')
-rw-r--r-- | OpenSim/Services/GridService/GridService.cs | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/OpenSim/Services/GridService/GridService.cs b/OpenSim/Services/GridService/GridService.cs new file mode 100644 index 0000000..991acf2 --- /dev/null +++ b/OpenSim/Services/GridService/GridService.cs | |||
@@ -0,0 +1,199 @@ | |||
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.Generic; | ||
30 | using System.Net; | ||
31 | using System.Reflection; | ||
32 | using Nini.Config; | ||
33 | using log4net; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Framework.Console; | ||
36 | using OpenSim.Data; | ||
37 | using OpenSim.Services.Interfaces; | ||
38 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
39 | using OpenMetaverse; | ||
40 | |||
41 | namespace OpenSim.Services.GridService | ||
42 | { | ||
43 | public class GridService : GridServiceBase, IGridService | ||
44 | { | ||
45 | private static readonly ILog m_log = | ||
46 | LogManager.GetLogger( | ||
47 | MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | public GridService(IConfigSource config) | ||
50 | : base(config) | ||
51 | { | ||
52 | m_log.DebugFormat("[GRID SERVICE]: Starting..."); | ||
53 | } | ||
54 | |||
55 | #region IGridService | ||
56 | |||
57 | public bool RegisterRegion(UUID scopeID, GridRegion regionInfos) | ||
58 | { | ||
59 | // This needs better sanity testing. What if regionInfo is registering in | ||
60 | // overlapping coords? | ||
61 | RegionData region = m_Database.Get(regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID); | ||
62 | if ((region != null) && (region.RegionID != regionInfos.RegionID)) | ||
63 | { | ||
64 | m_log.WarnFormat("[GRID SERVICE]: Region {0} tried to register in coordinates {1}, {2} which are already in use in scope {3}.", | ||
65 | regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID); | ||
66 | return false; | ||
67 | } | ||
68 | if ((region != null) && (region.RegionID == regionInfos.RegionID) && | ||
69 | ((region.posX != regionInfos.RegionLocX) || (region.posY != regionInfos.RegionLocY))) | ||
70 | { | ||
71 | // Region reregistering in other coordinates. Delete the old entry | ||
72 | m_Database.Delete(regionInfos.RegionID); | ||
73 | } | ||
74 | |||
75 | // Everything is ok, let's register | ||
76 | RegionData rdata = RegionInfo2RegionData(regionInfos); | ||
77 | rdata.ScopeID = scopeID; | ||
78 | m_Database.Store(rdata); | ||
79 | return true; | ||
80 | } | ||
81 | |||
82 | public bool DeregisterRegion(UUID regionID) | ||
83 | { | ||
84 | return m_Database.Delete(regionID); | ||
85 | } | ||
86 | |||
87 | public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID) | ||
88 | { | ||
89 | List<GridRegion> rinfos = new List<GridRegion>(); | ||
90 | RegionData region = m_Database.Get(regionID, scopeID); | ||
91 | if (region != null) | ||
92 | { | ||
93 | // Not really? Maybe? | ||
94 | List<RegionData> rdatas = m_Database.Get(region.posX - (int)Constants.RegionSize, region.posY - (int)Constants.RegionSize, | ||
95 | region.posX + (int)Constants.RegionSize, region.posY + (int)Constants.RegionSize, scopeID); | ||
96 | |||
97 | foreach (RegionData rdata in rdatas) | ||
98 | if (rdata.RegionID != regionID) | ||
99 | rinfos.Add(RegionData2RegionInfo(rdata)); | ||
100 | |||
101 | } | ||
102 | return rinfos; | ||
103 | } | ||
104 | |||
105 | public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID) | ||
106 | { | ||
107 | RegionData rdata = m_Database.Get(regionID, scopeID); | ||
108 | if (rdata != null) | ||
109 | return RegionData2RegionInfo(rdata); | ||
110 | |||
111 | return null; | ||
112 | } | ||
113 | |||
114 | public GridRegion GetRegionByPosition(UUID scopeID, int x, int y) | ||
115 | { | ||
116 | int snapX = (int)(x / Constants.RegionSize) * (int)Constants.RegionSize; | ||
117 | int snapY = (int)(y / Constants.RegionSize) * (int)Constants.RegionSize; | ||
118 | RegionData rdata = m_Database.Get(snapX, snapY, scopeID); | ||
119 | if (rdata != null) | ||
120 | return RegionData2RegionInfo(rdata); | ||
121 | |||
122 | return null; | ||
123 | } | ||
124 | |||
125 | public GridRegion GetRegionByName(UUID scopeID, string regionName) | ||
126 | { | ||
127 | List<RegionData> rdatas = m_Database.Get(regionName + "%", scopeID); | ||
128 | if ((rdatas != null) && (rdatas.Count > 0)) | ||
129 | return RegionData2RegionInfo(rdatas[0]); // get the first | ||
130 | |||
131 | return null; | ||
132 | } | ||
133 | |||
134 | public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber) | ||
135 | { | ||
136 | List<RegionData> rdatas = m_Database.Get("%" + name + "%", scopeID); | ||
137 | |||
138 | int count = 0; | ||
139 | List<GridRegion> rinfos = new List<GridRegion>(); | ||
140 | |||
141 | if (rdatas != null) | ||
142 | { | ||
143 | foreach (RegionData rdata in rdatas) | ||
144 | { | ||
145 | if (count++ < maxNumber) | ||
146 | rinfos.Add(RegionData2RegionInfo(rdata)); | ||
147 | } | ||
148 | } | ||
149 | |||
150 | return rinfos; | ||
151 | } | ||
152 | |||
153 | public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax) | ||
154 | { | ||
155 | int xminSnap = (int)(xmin / Constants.RegionSize) * (int)Constants.RegionSize; | ||
156 | int xmaxSnap = (int)(xmax / Constants.RegionSize) * (int)Constants.RegionSize; | ||
157 | int yminSnap = (int)(ymin / Constants.RegionSize) * (int)Constants.RegionSize; | ||
158 | int ymaxSnap = (int)(ymax / Constants.RegionSize) * (int)Constants.RegionSize; | ||
159 | |||
160 | List<RegionData> rdatas = m_Database.Get(xminSnap, yminSnap, xmaxSnap, ymaxSnap, scopeID); | ||
161 | List<GridRegion> rinfos = new List<GridRegion>(); | ||
162 | foreach (RegionData rdata in rdatas) | ||
163 | rinfos.Add(RegionData2RegionInfo(rdata)); | ||
164 | |||
165 | return rinfos; | ||
166 | } | ||
167 | |||
168 | #endregion | ||
169 | |||
170 | #region Data structure conversions | ||
171 | |||
172 | protected RegionData RegionInfo2RegionData(GridRegion rinfo) | ||
173 | { | ||
174 | RegionData rdata = new RegionData(); | ||
175 | rdata.posX = (int)rinfo.RegionLocX; | ||
176 | rdata.posY = (int)rinfo.RegionLocY; | ||
177 | rdata.RegionID = rinfo.RegionID; | ||
178 | rdata.RegionName = rinfo.RegionName; | ||
179 | rdata.Data = rinfo.ToKeyValuePairs(); | ||
180 | rdata.Data["regionHandle"] = Utils.UIntsToLong((uint)rdata.posX, (uint)rdata.posY); | ||
181 | return rdata; | ||
182 | } | ||
183 | |||
184 | protected GridRegion RegionData2RegionInfo(RegionData rdata) | ||
185 | { | ||
186 | GridRegion rinfo = new GridRegion(rdata.Data); | ||
187 | rinfo.RegionLocX = rdata.posX; | ||
188 | rinfo.RegionLocY = rdata.posY; | ||
189 | rinfo.RegionID = rdata.RegionID; | ||
190 | rinfo.RegionName = rdata.RegionName; | ||
191 | rinfo.ScopeID = rdata.ScopeID; | ||
192 | |||
193 | return rinfo; | ||
194 | } | ||
195 | |||
196 | #endregion | ||
197 | |||
198 | } | ||
199 | } | ||