diff options
Diffstat (limited to 'OpenSim/Services/Connectors/Inventory/InventoryServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/Inventory/InventoryServiceConnector.cs | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/Inventory/InventoryServiceConnector.cs b/OpenSim/Services/Connectors/Inventory/InventoryServiceConnector.cs new file mode 100644 index 0000000..34478ae --- /dev/null +++ b/OpenSim/Services/Connectors/Inventory/InventoryServiceConnector.cs | |||
@@ -0,0 +1,343 @@ | |||
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 log4net; | ||
29 | using System; | ||
30 | using System.Collections.Generic; | ||
31 | using System.IO; | ||
32 | using System.Reflection; | ||
33 | using Nini.Config; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Framework.Servers.HttpServer; | ||
36 | using OpenSim.Services.Interfaces; | ||
37 | using OpenMetaverse; | ||
38 | |||
39 | namespace OpenSim.Services.Connectors | ||
40 | { | ||
41 | public class InventoryServicesConnector : ISessionAuthInventoryService | ||
42 | { | ||
43 | private static readonly ILog m_log = | ||
44 | LogManager.GetLogger( | ||
45 | MethodBase.GetCurrentMethod().DeclaringType); | ||
46 | |||
47 | private string m_ServerURI = String.Empty; | ||
48 | |||
49 | private Dictionary<UUID, InventoryReceiptCallback> m_RequestingInventory = new Dictionary<UUID, InventoryReceiptCallback>(); | ||
50 | |||
51 | public InventoryServicesConnector() | ||
52 | { | ||
53 | } | ||
54 | |||
55 | public InventoryServicesConnector(string serverURI) | ||
56 | { | ||
57 | m_ServerURI = serverURI.TrimEnd('/'); | ||
58 | } | ||
59 | |||
60 | public InventoryServicesConnector(IConfigSource source) | ||
61 | { | ||
62 | Initialise(source); | ||
63 | } | ||
64 | |||
65 | public virtual void Initialise(IConfigSource source) | ||
66 | { | ||
67 | IConfig inventoryConfig = source.Configs["InventoryService"]; | ||
68 | if (inventoryConfig == null) | ||
69 | { | ||
70 | m_log.Error("[INVENTORY CONNECTOR]: InventoryService missing from OpanSim.ini"); | ||
71 | throw new Exception("Inventory connector init error"); | ||
72 | } | ||
73 | |||
74 | string serviceURI = inventoryConfig.GetString("InventoryServerURI", | ||
75 | String.Empty); | ||
76 | |||
77 | if (serviceURI == String.Empty) | ||
78 | { | ||
79 | m_log.Error("[INVENTORY CONNECTOR]: No Server URI named in section InventoryService"); | ||
80 | throw new Exception("Inventory connector init error"); | ||
81 | } | ||
82 | m_ServerURI = serviceURI.TrimEnd('/'); | ||
83 | } | ||
84 | |||
85 | #region ISessionAuthInventoryService | ||
86 | |||
87 | public string Host | ||
88 | { | ||
89 | get { return m_ServerURI; } | ||
90 | } | ||
91 | |||
92 | /// <summary> | ||
93 | /// Caller must catch eventual Exceptions. | ||
94 | /// </summary> | ||
95 | /// <param name="userID"></param> | ||
96 | /// <param name="sessionID"></param> | ||
97 | /// <param name="callback"></param> | ||
98 | public void GetUserInventory(string userIDStr, UUID sessionID, InventoryReceiptCallback callback) | ||
99 | { | ||
100 | UUID userID = UUID.Zero; | ||
101 | if (UUID.TryParse(userIDStr, out userID)) | ||
102 | { | ||
103 | lock (m_RequestingInventory) | ||
104 | { | ||
105 | if (!m_RequestingInventory.ContainsKey(userID)) | ||
106 | m_RequestingInventory.Add(userID, callback); | ||
107 | else | ||
108 | { | ||
109 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: GetUserInventory - ignoring repeated request for user {0}", userID); | ||
110 | return; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | m_log.InfoFormat( | ||
115 | "[INVENTORY CONNECTOR]: Requesting inventory from {0}/GetInventory/ for user {1}", | ||
116 | m_ServerURI, userID); | ||
117 | |||
118 | RestSessionObjectPosterResponse<Guid, InventoryCollection> requester | ||
119 | = new RestSessionObjectPosterResponse<Guid, InventoryCollection>(); | ||
120 | requester.ResponseCallback = InventoryResponse; | ||
121 | |||
122 | requester.BeginPostObject(m_ServerURI + "/GetInventory/", userID.Guid, sessionID.ToString(), userID.ToString()); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | public bool AddFolder(string userID, InventoryFolderBase folder, UUID sessionID) | ||
127 | { | ||
128 | try | ||
129 | { | ||
130 | return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject( | ||
131 | "POST", m_ServerURI + "/NewFolder/", folder, sessionID.ToString(), folder.Owner.ToString()); | ||
132 | } | ||
133 | catch (Exception e) | ||
134 | { | ||
135 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory folder operation failed, {0} {1}", | ||
136 | e.Source, e.Message); | ||
137 | } | ||
138 | |||
139 | return false; | ||
140 | } | ||
141 | |||
142 | public bool UpdateFolder(string userID, InventoryFolderBase folder, UUID sessionID) | ||
143 | { | ||
144 | try | ||
145 | { | ||
146 | return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject( | ||
147 | "POST", m_ServerURI + "/UpdateFolder/", folder, sessionID.ToString(), folder.Owner.ToString()); | ||
148 | } | ||
149 | catch (Exception e) | ||
150 | { | ||
151 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update inventory folder operation failed, {0} {1}", | ||
152 | e.Source, e.Message); | ||
153 | } | ||
154 | |||
155 | return false; | ||
156 | } | ||
157 | |||
158 | public bool MoveFolder(string userID, InventoryFolderBase folder, UUID sessionID) | ||
159 | { | ||
160 | try | ||
161 | { | ||
162 | return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject( | ||
163 | "POST", m_ServerURI + "/MoveFolder/", folder, sessionID.ToString(), folder.Owner.ToString()); | ||
164 | } | ||
165 | catch (Exception e) | ||
166 | { | ||
167 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}", | ||
168 | e.Source, e.Message); | ||
169 | } | ||
170 | |||
171 | return false; | ||
172 | } | ||
173 | |||
174 | public bool PurgeFolder(string userID, InventoryFolderBase folder, UUID sessionID) | ||
175 | { | ||
176 | try | ||
177 | { | ||
178 | return SynchronousRestSessionObjectPoster<InventoryFolderBase, bool>.BeginPostObject( | ||
179 | "POST", m_ServerURI + "/PurgeFolder/", folder, sessionID.ToString(), folder.Owner.ToString()); | ||
180 | } | ||
181 | catch (Exception e) | ||
182 | { | ||
183 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Move inventory folder operation failed, {0} {1}", | ||
184 | e.Source, e.Message); | ||
185 | } | ||
186 | |||
187 | return false; | ||
188 | } | ||
189 | |||
190 | public bool AddItem(string userID, InventoryItemBase item, UUID sessionID) | ||
191 | { | ||
192 | try | ||
193 | { | ||
194 | return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject( | ||
195 | "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString()); | ||
196 | } | ||
197 | catch (Exception e) | ||
198 | { | ||
199 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Add new inventory item operation failed, {0} {1}", | ||
200 | e.Source, e.Message); | ||
201 | } | ||
202 | |||
203 | return false; | ||
204 | } | ||
205 | |||
206 | public bool UpdateItem(string userID, InventoryItemBase item, UUID sessionID) | ||
207 | { | ||
208 | try | ||
209 | { | ||
210 | return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject( | ||
211 | "POST", m_ServerURI + "/NewItem/", item, sessionID.ToString(), item.Owner.ToString()); | ||
212 | } | ||
213 | catch (Exception e) | ||
214 | { | ||
215 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Update new inventory item operation failed, {0} {1}", | ||
216 | e.Source, e.Message); | ||
217 | } | ||
218 | |||
219 | return false; | ||
220 | } | ||
221 | |||
222 | public bool DeleteItem(string userID, InventoryItemBase item, UUID sessionID) | ||
223 | { | ||
224 | try | ||
225 | { | ||
226 | return SynchronousRestSessionObjectPoster<InventoryItemBase, bool>.BeginPostObject( | ||
227 | "POST", m_ServerURI + "/DeleteItem/", item, sessionID.ToString(), item.Owner.ToString()); | ||
228 | } | ||
229 | catch (Exception e) | ||
230 | { | ||
231 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Delete inventory item operation failed, {0} {1}", | ||
232 | e.Source, e.Message); | ||
233 | } | ||
234 | |||
235 | return false; | ||
236 | } | ||
237 | |||
238 | public InventoryItemBase QueryItem(string userID, InventoryItemBase item, UUID sessionID) | ||
239 | { | ||
240 | try | ||
241 | { | ||
242 | return SynchronousRestSessionObjectPoster<InventoryItemBase, InventoryItemBase>.BeginPostObject( | ||
243 | "POST", m_ServerURI + "/QueryItem/", item, sessionID.ToString(), item.Owner.ToString()); | ||
244 | } | ||
245 | catch (Exception e) | ||
246 | { | ||
247 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}", | ||
248 | e.Source, e.Message); | ||
249 | } | ||
250 | |||
251 | return null; | ||
252 | } | ||
253 | |||
254 | public InventoryFolderBase QueryFolder(string userID, InventoryFolderBase item, UUID sessionID) | ||
255 | { | ||
256 | try | ||
257 | { | ||
258 | return SynchronousRestSessionObjectPoster<InventoryFolderBase, InventoryFolderBase>.BeginPostObject( | ||
259 | "POST", m_ServerURI + "/QueryFolder/", item, sessionID.ToString(), item.Owner.ToString()); | ||
260 | } | ||
261 | catch (Exception e) | ||
262 | { | ||
263 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Query inventory item operation failed, {0} {1}", | ||
264 | e.Source, e.Message); | ||
265 | } | ||
266 | |||
267 | return null; | ||
268 | } | ||
269 | |||
270 | #endregion | ||
271 | |||
272 | /// <summary> | ||
273 | /// Callback used by the inventory server GetInventory request | ||
274 | /// </summary> | ||
275 | /// <param name="userID"></param> | ||
276 | private void InventoryResponse(InventoryCollection response) | ||
277 | { | ||
278 | UUID userID = response.UserID; | ||
279 | InventoryReceiptCallback callback = null; | ||
280 | lock (m_RequestingInventory) | ||
281 | { | ||
282 | if (m_RequestingInventory.ContainsKey(userID)) | ||
283 | { | ||
284 | callback = m_RequestingInventory[userID]; | ||
285 | m_RequestingInventory.Remove(userID); | ||
286 | } | ||
287 | else | ||
288 | { | ||
289 | m_log.WarnFormat( | ||
290 | "[INVENTORY CONNECTOR]: " + | ||
291 | "Received inventory response for {0} for which we do not have a record of requesting!", | ||
292 | userID); | ||
293 | return; | ||
294 | } | ||
295 | } | ||
296 | |||
297 | m_log.InfoFormat("[INVENTORY CONNECTOR]: " + | ||
298 | "Received inventory response for user {0} containing {1} folders and {2} items", | ||
299 | userID, response.Folders.Count, response.Items.Count); | ||
300 | |||
301 | InventoryFolderImpl rootFolder = null; | ||
302 | |||
303 | ICollection<InventoryFolderImpl> folders = new List<InventoryFolderImpl>(); | ||
304 | ICollection<InventoryItemBase> items = new List<InventoryItemBase>(); | ||
305 | |||
306 | foreach (InventoryFolderBase folder in response.Folders) | ||
307 | { | ||
308 | if (folder.ParentID == UUID.Zero) | ||
309 | { | ||
310 | rootFolder = new InventoryFolderImpl(folder); | ||
311 | folders.Add(rootFolder); | ||
312 | |||
313 | break; | ||
314 | } | ||
315 | } | ||
316 | |||
317 | if (rootFolder != null) | ||
318 | { | ||
319 | foreach (InventoryFolderBase folder in response.Folders) | ||
320 | { | ||
321 | if (folder.ID != rootFolder.ID) | ||
322 | { | ||
323 | folders.Add(new InventoryFolderImpl(folder)); | ||
324 | } | ||
325 | } | ||
326 | |||
327 | foreach (InventoryItemBase item in response.Items) | ||
328 | { | ||
329 | items.Add(item); | ||
330 | } | ||
331 | } | ||
332 | else | ||
333 | { | ||
334 | m_log.ErrorFormat("[INVENTORY CONNECTOR]: Did not get back an inventory containing a root folder for user {0}", userID); | ||
335 | } | ||
336 | |||
337 | callback(folders, items); | ||
338 | |||
339 | } | ||
340 | |||
341 | |||
342 | } | ||
343 | } | ||