opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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.Reflection;
31 using log4net;
32 using OpenMetaverse;
33 using OpenSim.Framework;
34 using OpenSim.Region.Framework.Scenes;
35 using OpenSim.Services.Interfaces;
36 using PermissionMask = OpenSim.Framework.PermissionMask;
37  
38 namespace OpenSim.Region.CoreModules.Agent.AssetTransaction
39 {
40 public class AssetXferUploader
41 {
42 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43  
44 /// <summary>
45 /// Upload state.
46 /// </summary>
47 /// <remarks>
48 /// New -> Uploading -> Complete
49 /// </remarks>
50 private enum UploadState
51 {
52 New,
53 Uploading,
54 Complete
55 }
56  
57 /// <summary>
58 /// Reference to the object that holds this uploader. Used to remove ourselves from it's list if we
59 /// are performing a delayed update.
60 /// </summary>
61 AgentAssetTransactions m_transactions;
62  
63 private UploadState m_uploadState = UploadState.New;
64  
65 private AssetBase m_asset;
66 private UUID InventFolder = UUID.Zero;
67 private sbyte invType = 0;
68  
69 private bool m_createItem;
70 private uint m_createItemCallback;
71  
72 private bool m_updateItem;
73 private InventoryItemBase m_updateItemData;
74  
75 private bool m_updateTaskItem;
76 private TaskInventoryItem m_updateTaskItemData;
77  
78 private string m_description = String.Empty;
79 private bool m_dumpAssetToFile;
80 private string m_name = String.Empty;
81 // private bool m_storeLocal;
82 private uint nextPerm = 0;
83 private IClientAPI ourClient;
84  
85 private UUID m_transactionID;
86  
87 private sbyte type = 0;
88 private byte wearableType = 0;
89 public ulong XferID;
90 private Scene m_Scene;
91  
92 /// <summary>
93 /// AssetXferUploader constructor
94 /// </summary>
95 /// <param name='transactions'>/param>
96 /// <param name='scene'></param>
97 /// <param name='transactionID'></param>
98 /// <param name='dumpAssetToFile'>
99 /// If true then when the asset is uploaded it is dumped to a file with the format
100 /// String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat",
101 /// now.Year, now.Month, now.Day, now.Hour, now.Minute,
102 /// now.Second, m_asset.Name, m_asset.Type);
103 /// for debugging purposes.
104 /// </param>
105 public AssetXferUploader(
106 AgentAssetTransactions transactions, Scene scene, UUID transactionID, bool dumpAssetToFile)
107 {
108 m_asset = new AssetBase();
109  
110 m_transactions = transactions;
111 m_transactionID = transactionID;
112 m_Scene = scene;
113 m_dumpAssetToFile = dumpAssetToFile;
114 }
115  
116 /// <summary>
117 /// Process transfer data received from the client.
118 /// </summary>
119 /// <param name="xferID"></param>
120 /// <param name="packetID"></param>
121 /// <param name="data"></param>
122 /// <returns>True if the transfer is complete, false otherwise or if the xferID was not valid</returns>
123 public bool HandleXferPacket(ulong xferID, uint packetID, byte[] data)
124 {
125 // m_log.DebugFormat(
126 // "[ASSET XFER UPLOADER]: Received packet {0} for xfer {1} (data length {2})",
127 // packetID, xferID, data.Length);
128  
129 if (XferID == xferID)
130 {
131 if (m_asset.Data.Length > 1)
132 {
133 byte[] destinationArray = new byte[m_asset.Data.Length + data.Length];
134 Array.Copy(m_asset.Data, 0, destinationArray, 0, m_asset.Data.Length);
135 Array.Copy(data, 0, destinationArray, m_asset.Data.Length, data.Length);
136 m_asset.Data = destinationArray;
137 }
138 else
139 {
140 byte[] buffer2 = new byte[data.Length - 4];
141 Array.Copy(data, 4, buffer2, 0, data.Length - 4);
142 m_asset.Data = buffer2;
143 }
144  
145 ourClient.SendConfirmXfer(xferID, packetID);
146  
147 if ((packetID & 0x80000000) != 0)
148 {
149 SendCompleteMessage();
150 return true;
151 }
152 }
153  
154 return false;
155 }
156  
157 /// <summary>
158 /// Start asset transfer from the client
159 /// </summary>
160 /// <param name="remoteClient"></param>
161 /// <param name="assetID"></param>
162 /// <param name="transaction"></param>
163 /// <param name="type"></param>
164 /// <param name="data">
165 /// Optional data. If present then the asset is created immediately with this data
166 /// rather than requesting an upload from the client. The data must be longer than 2 bytes.
167 /// </param>
168 /// <param name="storeLocal"></param>
169 /// <param name="tempFile"></param>
170 public void StartUpload(
171 IClientAPI remoteClient, UUID assetID, UUID transaction, sbyte type, byte[] data, bool storeLocal,
172 bool tempFile)
173 {
174 // m_log.DebugFormat(
175 // "[ASSET XFER UPLOADER]: Initialised xfer from {0}, asset {1}, transaction {2}, type {3}, storeLocal {4}, tempFile {5}, already received data length {6}",
176 // remoteClient.Name, assetID, transaction, type, storeLocal, tempFile, data.Length);
177  
178 lock (this)
179 {
180 if (m_uploadState != UploadState.New)
181 {
182 m_log.WarnFormat(
183 "[ASSET XFER UPLOADER]: Tried to start upload of asset {0}, transaction {1} for {2} but this is already in state {3}. Aborting.",
184 assetID, transaction, remoteClient.Name, m_uploadState);
185  
186 return;
187 }
188  
189 m_uploadState = UploadState.Uploading;
190 }
191  
192 ourClient = remoteClient;
193  
194 m_asset.FullID = assetID;
195 m_asset.Type = type;
196 m_asset.CreatorID = remoteClient.AgentId.ToString();
197 m_asset.Data = data;
198 m_asset.Local = storeLocal;
199 m_asset.Temporary = tempFile;
200  
201 // m_storeLocal = storeLocal;
202  
203 if (m_asset.Data.Length > 2)
204 {
205 SendCompleteMessage();
206 }
207 else
208 {
209 RequestStartXfer();
210 }
211 }
212  
213 protected void RequestStartXfer()
214 {
215 XferID = Util.GetNextXferID();
216  
217 // m_log.DebugFormat(
218 // "[ASSET XFER UPLOADER]: Requesting Xfer of asset {0}, type {1}, transfer id {2} from {3}",
219 // m_asset.FullID, m_asset.Type, XferID, ourClient.Name);
220  
221 ourClient.SendXferRequest(XferID, m_asset.Type, m_asset.FullID, 0, new byte[0]);
222 }
223  
224 protected void SendCompleteMessage()
225 {
226 // We must lock in order to avoid a race with a separate thread dealing with an inventory item or create
227 // message from other client UDP.
228 lock (this)
229 {
230 m_uploadState = UploadState.Complete;
231  
232 ourClient.SendAssetUploadCompleteMessage(m_asset.Type, true, m_asset.FullID);
233  
234 if (m_createItem)
235 {
236 CompleteCreateItem(m_createItemCallback);
237 }
238 else if (m_updateItem)
239 {
240 CompleteItemUpdate(m_updateItemData);
241 }
242 else if (m_updateTaskItem)
243 {
244 CompleteTaskItemUpdate(m_updateTaskItemData);
245 }
246 // else if (m_storeLocal)
247 // {
248 // m_Scene.AssetService.Store(m_asset);
249 // }
250 }
251  
252 m_log.DebugFormat(
253 "[ASSET XFER UPLOADER]: Uploaded asset {0} for transaction {1}",
254 m_asset.FullID, m_transactionID);
255  
256 if (m_dumpAssetToFile)
257 {
258 DateTime now = DateTime.Now;
259 string filename =
260 String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat",
261 now.Year, now.Month, now.Day, now.Hour, now.Minute,
262 now.Second, m_asset.Name, m_asset.Type);
263 SaveAssetToFile(filename, m_asset.Data);
264 }
265 }
266  
267 private void SaveAssetToFile(string filename, byte[] data)
268 {
269 string assetPath = "UserAssets";
270 if (!Directory.Exists(assetPath))
271 {
272 Directory.CreateDirectory(assetPath);
273 }
274 FileStream fs = File.Create(Path.Combine(assetPath, filename));
275 BinaryWriter bw = new BinaryWriter(fs);
276 bw.Write(data);
277 bw.Close();
278 fs.Close();
279 }
280  
281 public void RequestCreateInventoryItem(IClientAPI remoteClient,
282 UUID folderID, uint callbackID,
283 string description, string name, sbyte invType,
284 sbyte type, byte wearableType, uint nextOwnerMask)
285 {
286 InventFolder = folderID;
287 m_name = name;
288 m_description = description;
289 this.type = type;
290 this.invType = invType;
291 this.wearableType = wearableType;
292 nextPerm = nextOwnerMask;
293 m_asset.Name = name;
294 m_asset.Description = description;
295 m_asset.Type = type;
296  
297 // We must lock to avoid a race with a separate thread uploading the asset.
298 lock (this)
299 {
300 if (m_uploadState == UploadState.Complete)
301 {
302 CompleteCreateItem(callbackID);
303 }
304 else
305 {
306 m_createItem = true; //set flag so the inventory item is created when upload is complete
307 m_createItemCallback = callbackID;
308 }
309 }
310 }
311  
312 public void RequestUpdateInventoryItem(IClientAPI remoteClient, InventoryItemBase item)
313 {
314 // We must lock to avoid a race with a separate thread uploading the asset.
315 lock (this)
316 {
317 m_asset.Name = item.Name;
318 m_asset.Description = item.Description;
319 m_asset.Type = (sbyte)item.AssetType;
320  
321 // We must always store the item at this point even if the asset hasn't finished uploading, in order
322 // to avoid a race condition when the appearance module retrieves the item to set the asset id in
323 // the AvatarAppearance structure.
324 item.AssetID = m_asset.FullID;
325 if (item.AssetID != UUID.Zero)
326 m_Scene.InventoryService.UpdateItem(item);
327  
328 if (m_uploadState == UploadState.Complete)
329 {
330 CompleteItemUpdate(item);
331 }
332 else
333 {
334 // m_log.DebugFormat(
335 // "[ASSET XFER UPLOADER]: Holding update inventory item request {0} for {1} pending completion of asset xfer for transaction {2}",
336 // item.Name, remoteClient.Name, transactionID);
337  
338 m_updateItem = true;
339 m_updateItemData = item;
340 }
341 }
342 }
343  
344 public void RequestUpdateTaskInventoryItem(IClientAPI remoteClient, TaskInventoryItem taskItem)
345 {
346 // We must lock to avoid a race with a separate thread uploading the asset.
347 lock (this)
348 {
349 m_asset.Name = taskItem.Name;
350 m_asset.Description = taskItem.Description;
351 m_asset.Type = (sbyte)taskItem.Type;
352 taskItem.AssetID = m_asset.FullID;
353  
354 if (m_uploadState == UploadState.Complete)
355 {
356 CompleteTaskItemUpdate(taskItem);
357 }
358 else
359 {
360 m_updateTaskItem = true;
361 m_updateTaskItemData = taskItem;
362 }
363 }
364 }
365  
366 /// <summary>
367 /// Store the asset for the given item when it has been uploaded.
368 /// </summary>
369 /// <param name="item"></param>
370 private void CompleteItemUpdate(InventoryItemBase item)
371 {
372 // m_log.DebugFormat(
373 // "[ASSET XFER UPLOADER]: Storing asset {0} for earlier item update for {1} for {2}",
374 // m_asset.FullID, item.Name, ourClient.Name);
375  
376 m_Scene.AssetService.Store(m_asset);
377  
378 m_transactions.RemoveXferUploader(m_transactionID);
379 }
380  
381 /// <summary>
382 /// Store the asset for the given task item when it has been uploaded.
383 /// </summary>
384 /// <param name="taskItem"></param>
385 private void CompleteTaskItemUpdate(TaskInventoryItem taskItem)
386 {
387 // m_log.DebugFormat(
388 // "[ASSET XFER UPLOADER]: Storing asset {0} for earlier task item update for {1} for {2}",
389 // m_asset.FullID, taskItem.Name, ourClient.Name);
390  
391 m_Scene.AssetService.Store(m_asset);
392  
393 m_transactions.RemoveXferUploader(m_transactionID);
394 }
395  
396 private void CompleteCreateItem(uint callbackID)
397 {
398 m_Scene.AssetService.Store(m_asset);
399  
400 InventoryItemBase item = new InventoryItemBase();
401 item.Owner = ourClient.AgentId;
402 item.CreatorId = ourClient.AgentId.ToString();
403 item.ID = UUID.Random();
404 item.AssetID = m_asset.FullID;
405 item.Description = m_description;
406 item.Name = m_name;
407 item.AssetType = type;
408 item.InvType = invType;
409 item.Folder = InventFolder;
410 item.BasePermissions = (uint)(PermissionMask.All | PermissionMask.Export);
411 item.CurrentPermissions = item.BasePermissions;
412 item.GroupPermissions=0;
413 item.EveryOnePermissions=0;
414 item.NextPermissions = nextPerm;
415 item.Flags = (uint) wearableType;
416 item.CreationDate = Util.UnixTimeSinceEpoch();
417  
418 if (m_Scene.AddInventoryItem(item))
419 ourClient.SendInventoryItemCreateUpdate(item, callbackID);
420 else
421 ourClient.SendAlertMessage("Unable to create inventory item");
422  
423 m_transactions.RemoveXferUploader(m_transactionID);
424 }
425 }
426 }