View of /trunk/TextureRedirector/Plugins/TextureRedirector.cs
Parent Directory
|
Revision Log
Revision 25 -
(download)
(annotate)
Sat Nov 15 03:56:39 2008 UTC (4 years, 6 months ago) by jhurliman
File size: 14521 byte(s)
Sat Nov 15 03:56:39 2008 UTC (4 years, 6 months ago) by jhurliman
File size: 14521 byte(s)
* Added MemcacheStorage, sits on top of another storage provider and uses memcached to cache metadata and asset data * Implemented Robert's suggestion to replace AssetURL with a list of method to URI mappings that contains data=>uri * Upgraded to latest ExtensionLoader with simplified extension starting * Simplified SimpleStorage by removing SimpleMetadata and using a dictionary of UUID to filenames * Moved Metadata into its own file, added serialization and deserialization methods * Added LICENSES.txt file for third party libraries (not complete yet)
/*
* Copyright (c) 2008 Intel Corporation
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* -- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* -- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* -- Neither the name of the Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using OpenMetaverse;
using OpenMetaverse.Imaging;
using OpenMetaverse.Packets;
using OpenMetaverse.StructuredData;
using AssetClient;
using GridProxy;
using System.IO;
using System.Security.Cryptography;
using System.Net;
namespace GridProxy.Plugins
{
public class ImageDownload
{
public const int FIRST_IMAGE_PACKET_SIZE = 600;
public const int IMAGE_PACKET_SIZE = 1000;
public AssetTexture Texture;
public int DiscardLevel;
public float Priority;
public int CurrentPacket;
public int StopPacket;
public ImageDownload(AssetTexture texture, int discardLevel, float priority, int packet)
{
Texture = texture;
Update(discardLevel, priority, packet);
}
/// <summary>
/// Updates an image transfer with new information and recalculates
/// offsets
/// </summary>
/// <param name="discardLevel">New requested discard level</param>
/// <param name="priority">New requested priority</param>
/// <param name="packet">New requested packet offset</param>
public void Update(int discardLevel, float priority, int packet)
{
Priority = priority;
DiscardLevel = Utils.Clamp(discardLevel, 0, Texture.LayerInfo.Length - 1);
StopPacket = GetPacketForBytePosition(Texture.LayerInfo[(Texture.LayerInfo.Length - 1) - DiscardLevel].End);
CurrentPacket = Utils.Clamp(packet, 1, TexturePacketCount());
}
/// <summary>
/// Returns the total number of packets needed to transfer this texture,
/// including the first packet of size FIRST_IMAGE_PACKET_SIZE
/// </summary>
/// <returns>Total number of packets needed to transfer this texture</returns>
public int TexturePacketCount()
{
return ((Texture.AssetData.Length - FIRST_IMAGE_PACKET_SIZE + IMAGE_PACKET_SIZE - 1) / IMAGE_PACKET_SIZE) + 1;
}
/// <summary>
/// Returns the current byte offset for this transfer, calculated from
/// the CurrentPacket
/// </summary>
/// <returns>Current byte offset for this transfer</returns>
public int CurrentBytePosition()
{
return FIRST_IMAGE_PACKET_SIZE + (CurrentPacket - 1) * IMAGE_PACKET_SIZE;
}
/// <summary>
/// Returns the size, in bytes, of the last packet. This will be somewhere
/// between 1 and IMAGE_PACKET_SIZE bytes
/// </summary>
/// <returns>Size of the last packet in the transfer</returns>
public int LastPacketSize()
{
return Texture.AssetData.Length - (FIRST_IMAGE_PACKET_SIZE + ((TexturePacketCount() - 2) * IMAGE_PACKET_SIZE));
}
/// <summary>
/// Find the packet number that contains a given byte position
/// </summary>
/// <param name="bytePosition">Byte position</param>
/// <returns>Packet number that contains the given byte position</returns>
int GetPacketForBytePosition(int bytePosition)
{
return ((bytePosition - FIRST_IMAGE_PACKET_SIZE + IMAGE_PACKET_SIZE - 1) / IMAGE_PACKET_SIZE);
}
}
public class TextureRedirector
{
ProxyFrame proxyFrame;
Dictionary<UUID, ImageDownload> CurrentDownloads = new Dictionary<UUID, ImageDownload>();
BlockingQueue<ImageDownload> CurrentDownloadQueue = new BlockingQueue<ImageDownload>();
public TextureRedirector(ProxyFrame proxyFrame)
{
this.proxyFrame = proxyFrame;
proxyFrame.proxy.AddDelegate(PacketType.RequestImage, Direction.Outgoing, RequestImageHandler);
}
public void Init()
{
}
public Packet RequestImageHandler(Packet packet, IPEndPoint endPoint)
{
RequestImagePacket request = (RequestImagePacket)packet;
for (int i = 0; i < request.RequestImage.Length; i++)
{
RequestImagePacket.RequestImageBlock block = request.RequestImage[i];
ImageDownload download;
bool downloadFound = CurrentDownloads.TryGetValue(block.Image, out download);
if (downloadFound)
{
lock (download)
{
if (block.DiscardLevel == -1 && block.DownloadPriority == 0.0f)
{
Logger.DebugLog(String.Format("Image download {0} is aborting", block.Image));
}
else
{
if (block.DiscardLevel < download.DiscardLevel)
Logger.DebugLog(String.Format("Image download {0} is changing from DiscardLevel {1} to {2}",
block.Image, download.DiscardLevel, block.DiscardLevel));
if (block.DownloadPriority != download.Priority)
Logger.DebugLog(String.Format("Image download {0} is changing from Priority {1} to {2}",
block.Image, download.Priority, block.DownloadPriority));
if (block.Packet != download.CurrentPacket)
Logger.DebugLog(String.Format("Image download {0} is changing from Packet {1} to {2}",
block.Image, download.CurrentPacket, block.Packet));
}
// Update download
download.Update(block.DiscardLevel, block.DownloadPriority, (int)block.Packet);
}
}
else if (block.DiscardLevel == -1 && block.DownloadPriority == 0.0f)
{
// Aborting a download we are not tracking, ignore
//Logger.DebugLog(String.Format("Aborting an image download for untracked image " + block.Image.ToString()));
}
else
{
bool bake = ((ImageType)block.Type == ImageType.Baked);
// New download, check if we have this image
byte[] mdata;
byte[] assetData;
UUID myID = block.Image;
if (AssetClient.AssetClient.TryGetMetadata(myID, out mdata))
{
OSD metadata = OSDParser.DeserializeLLSDXml(mdata);
OSDMap map = (OSDMap)metadata;
string assetURL = (map["methods"] as OSDMap)["data"].AsString();
if (AssetClient.AssetClient.TryGetAsset(myID, assetURL, out assetData))
{
AssetTexture asset = new AssetTexture(myID, assetData);
if (asset.DecodeLayerBoundaries())
{
download = new ImageDownload((AssetTexture)asset, block.DiscardLevel, block.DownloadPriority,
(int)block.Packet);
Logger.DebugLog(String.Format(
"Starting new download for {0}, DiscardLevel: {1}, Priority: {2}, Start: {3}, End: {4}, Total: {5}",
block.Image, block.DiscardLevel, block.DownloadPriority, download.CurrentPacket, download.StopPacket,
download.TexturePacketCount()));
// Send initial data
ImageDataPacket data = new ImageDataPacket();
data.ImageID.Codec = (byte)ImageCodec.J2C;
data.ImageID.ID = download.Texture.AssetID;
data.ImageID.Packets = (ushort)download.TexturePacketCount();
data.ImageID.Size = (uint)download.Texture.AssetData.Length;
// The first bytes of the image are always sent in the ImageData packet
data.ImageData = new ImageDataPacket.ImageDataBlock();
int imageDataSize = (download.Texture.AssetData.Length >= ImageDownload.FIRST_IMAGE_PACKET_SIZE) ?
ImageDownload.FIRST_IMAGE_PACKET_SIZE : download.Texture.AssetData.Length;
data.ImageData.Data = new byte[imageDataSize];
Buffer.BlockCopy(download.Texture.AssetData, 0, data.ImageData.Data, 0, imageDataSize);
//SEND PACKET HERE
proxyFrame.proxy.InjectPacket(data, Direction.Incoming);
// Check if ImagePacket packets need to be sent to complete this transfer
if (download.CurrentPacket <= download.StopPacket)
{
// Insert this download into the dictionary
lock (CurrentDownloads)
CurrentDownloads[block.Image] = download;
// Send all of the remaining packets
ThreadPool.QueueUserWorkItem(
delegate(object obj)
{
while (download.CurrentPacket <= download.StopPacket)
{
if (download.Priority == 0.0f && download.DiscardLevel == -1)
break;
lock (download)
{
int imagePacketSize = (download.CurrentPacket == download.TexturePacketCount() - 1) ?
download.LastPacketSize() : ImageDownload.IMAGE_PACKET_SIZE;
ImagePacketPacket transfer = new ImagePacketPacket();
transfer.ImageID.ID = block.Image;
transfer.ImageID.Packet = (ushort)download.CurrentPacket;
transfer.ImageData.Data = new byte[imagePacketSize];
Buffer.BlockCopy(download.Texture.AssetData, download.CurrentBytePosition(),
transfer.ImageData.Data, 0, imagePacketSize);
proxyFrame.proxy.InjectPacket(transfer, Direction.Incoming);
//add code to log packet
++download.CurrentPacket;
}
}
Logger.DebugLog("Completed image transfer for " + block.Image.ToString());
// Transfer is complete, remove the reference
lock (CurrentDownloads)
CurrentDownloads.Remove(block.Image);
}
);
}
}
else
{
Logger.Log("Texture decoding failed for " + myID.ToString(), Helpers.LogLevel.Error);
}
}
}
else
{
Logger.Log("Metadata request failed for " + block.Image.ToString(), Helpers.LogLevel.Warning);
ImageNotInDatabasePacket notfound = new ImageNotInDatabasePacket();
notfound.ImageID.ID = block.Image;
proxyFrame.proxy.InjectPacket(notfound, Direction.Incoming);
}
}
}
// Suppress this packet so it does not get sent to the original destination
return null;
}
}
}
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

