示例#1
0
        /// <summary>
        ///   Creates a new asset
        /// </summary>
        /// Returns a random ID if none is passed into it
        /// <param name = "asset"></param>
        /// <returns></returns>
        public UUID Store(AssetBase asset)
        {
            if (String.IsNullOrEmpty(m_serverUrl))
            {
                MainConsole.Instance.Error("[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
                throw new InvalidOperationException();
            }

            bool   storedInCache = false;
            string errorMessage  = null;

            // AssetID handling
            if (asset.ID == UUID.Zero)
            {
                asset.ID = UUID.Random();
            }

            // Cache handling
            if (m_cache != null)
            {
                m_cache.Cache(asset);
                storedInCache = true;
            }

            // Local asset handling
            if ((asset.Flags & AssetFlags.Local) == AssetFlags.Local)
            {
                if (!storedInCache)
                {
                    MainConsole.Instance.Error("Cannot store local " + asset.TypeString + " asset without an asset cache");
                    asset.ID = UUID.Zero;
                }

                return(asset.ID);
            }

            // Distinguish public and private assets
            bool isPublic = true;

            switch ((AssetType)asset.Type)
            {
            case AssetType.CallingCard:
            case AssetType.Gesture:
            case AssetType.LSLBytecode:
            case AssetType.LSLText:
                isPublic = false;
                break;
            }

            // Make sure ContentType is set
            if (String.IsNullOrEmpty(asset.TypeString))
            {
                asset.TypeString = SLUtil.SLAssetTypeToContentType(asset.Type);
            }

            // Build the remote storage request
            List <MultipartForm.Element> postParameters = new List <MultipartForm.Element>
            {
                new MultipartForm.Parameter("AssetID",
                                            asset.ID.ToString()),
                new MultipartForm.Parameter("CreatorID",
                                            asset.CreatorID.ToString()),
                new MultipartForm.Parameter("Temporary",
                                            ((asset.Flags &
                                              AssetFlags.Temperary) ==
                                             AssetFlags.Temperary)
                                                                                                 ? "1"
                                                                                                 : "0"),
                new MultipartForm.Parameter("Public",
                                            isPublic ? "1" : "0"),
                new MultipartForm.File("Asset", asset.Name,
                                       asset.TypeString, asset.Data)
            };

            // Make the remote storage request
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_serverUrl);

                HttpWebResponse response = MultipartForm.Post(request, postParameters);
                using (Stream responseStream = response.GetResponseStream())
                {
                    string responseStr = null;

                    try
                    {
                        responseStr = responseStream.GetStreamString();
                        OSD responseOSD = OSDParser.Deserialize(responseStr);
                        if (responseOSD.Type == OSDType.Map)
                        {
                            OSDMap responseMap = (OSDMap)responseOSD;
                            if (responseMap["Success"].AsBoolean())
                            {
                                return(asset.ID);
                            }
                            else
                            {
                                errorMessage = "Upload failed: " + responseMap["Message"].AsString();
                            }
                        }
                        else
                        {
                            errorMessage = "Response format was invalid:\n" + responseStr;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!String.IsNullOrEmpty(responseStr))
                        {
                            errorMessage = "Failed to parse the response:\n" + responseStr;
                        }
                        else
                        {
                            errorMessage = "Failed to retrieve the response: " + ex.Message;
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                errorMessage = ex.Message;
            }

            MainConsole.Instance.WarnFormat("[SIMIAN ASSET CONNECTOR]: Failed to store asset \"{0}\" ({1}, {2}): {3}",
                                            asset.Name, asset.ID, asset.TypeString, errorMessage);
            return(UUID.Zero);
        }
        ///<summary>
        ///
        ///</summary>
        private void UploadMapTile(IScene scene)
        {
            m_log.DebugFormat("[SIMIAN MAPTILE]: upload maptile for {0}", scene.RegionInfo.RegionName);

            // Create a PNG map tile and upload it to the AddMapTile API
            byte[]             pngData       = Utils.EmptyBytes;
            IMapImageGenerator tileGenerator = scene.RequestModuleInterface <IMapImageGenerator>();

            if (tileGenerator == null)
            {
                m_log.Warn("[SIMIAN MAPTILE]: Cannot upload PNG map tile without an ImageGenerator");
                return;
            }

            using (Image mapTile = tileGenerator.CreateMapTile())
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    mapTile.Save(stream, ImageFormat.Png);
                    pngData = stream.ToArray();
                }
            }

            List <MultipartForm.Element> postParameters = new List <MultipartForm.Element>()
            {
                new MultipartForm.Parameter("X", scene.RegionInfo.RegionLocX.ToString()),
                new MultipartForm.Parameter("Y", scene.RegionInfo.RegionLocY.ToString()),
                new MultipartForm.File("Tile", "tile.png", "image/png", pngData)
            };

            string errorMessage = null;
            int    tickstart    = Util.EnvironmentTickCount();

            // Make the remote storage request
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_serverUrl);
                request.Timeout          = 20000;
                request.ReadWriteTimeout = 5000;

                using (HttpWebResponse response = MultipartForm.Post(request, postParameters))
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        string responseStr = responseStream.GetStreamString();
                        OSD    responseOSD = OSDParser.Deserialize(responseStr);
                        if (responseOSD.Type == OSDType.Map)
                        {
                            OSDMap responseMap = (OSDMap)responseOSD;
                            if (responseMap["Success"].AsBoolean())
                            {
                                return;
                            }

                            errorMessage = "Upload failed: " + responseMap["Message"].AsString();
                        }
                        else
                        {
                            errorMessage = "Response format was invalid:\n" + responseStr;
                        }
                    }
                }
            }
            catch (WebException we)
            {
                errorMessage = we.Message;
                if (we.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse webResponse = (HttpWebResponse)we.Response;
                    errorMessage = String.Format("[{0}] {1}",
                                                 webResponse.StatusCode, webResponse.StatusDescription);
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            finally
            {
                // This just dumps a warning for any operation that takes more than 100 ms
                int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
                m_log.DebugFormat("[SIMIAN MAPTILE]: map tile uploaded in {0}ms", tickdiff);
            }

            m_log.WarnFormat("[SIMIAN MAPTILE]: Failed to store {0} byte tile for {1}: {2}",
                             pngData.Length, scene.RegionInfo.RegionName, errorMessage);
        }
        private void DoCreateAsset(object o)
        {
            try
            {
                object[] array = (object[])o;

                string assetID     = (string)array[0];
                bool   isPublic    = (bool)array[1];
                string assetName   = (string)array[2];
                string contentType = (string)array[3];
                byte[] assetData   = (byte[])array[4];

                string errorMessage = null;

                // Build the remote storage request
                List <MultipartForm.Element> postParameters = new List <MultipartForm.Element>()
                {
                    new MultipartForm.Parameter("AssetID", assetID),
                    new MultipartForm.Parameter("Public", isPublic ? "1" : "0"),
                    new MultipartForm.File("Asset", assetName, contentType, assetData)
                };

                // Make the remote storage request
                try
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_assetUrl);

                    HttpWebResponse response = MultipartForm.Post(request, postParameters);
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        string responseStr = null;

                        try
                        {
                            responseStr = responseStream.GetStreamString();
                            OSD responseOSD = OSDParser.Deserialize(responseStr);
                            if (responseOSD.Type == OSDType.Map)
                            {
                                OSDMap responseMap = (OSDMap)responseOSD;
                                if (responseMap["Success"].AsBoolean())
                                {
                                    return;
                                }
                                else
                                {
                                    errorMessage = "Upload failed: " + responseMap["Message"].AsString();
                                }
                            }
                            else
                            {
                                errorMessage = "Response format was invalid:\n" + responseStr;
                            }
                        }
                        catch (Exception ex)
                        {
                            if (!String.IsNullOrEmpty(responseStr))
                            {
                                errorMessage = "Failed to parse the response:\n" + responseStr;
                            }
                            else
                            {
                                errorMessage = "Failed to retrieve the response: " + ex.Message;
                            }
                        }
                    }
                }
                catch (WebException ex)
                {
                    errorMessage = ex.Message;
                }

                Console.WriteLine("Failed to store asset \"{0}\" ({1}, {2}): {3}", assetName, assetID, contentType, errorMessage);
            }
            finally
            {
                m_semaphore.Release();
            }
        }
示例#4
0
        private void UploadMapTile(IScene scene)
        {
            string errorMessage = null;

            // Create a PNG map tile and upload it to the AddMapTile API
            byte[]             pngData       = Utils.EmptyBytes;
            IMapImageGenerator tileGenerator = scene.RequestModuleInterface <IMapImageGenerator>();

            if (tileGenerator == null)
            {
                m_log.Warn("[SIMIAN GRID CONNECTOR]: Cannot upload PNG map tile without an IMapImageGenerator");
                return;
            }

            using (Image mapTile = tileGenerator.CreateMapTile("defaultstripe.png"))
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    mapTile.Save(stream, ImageFormat.Png);
                    pngData = stream.ToArray();
                }
            }

            List <MultipartForm.Element> postParameters = new List <MultipartForm.Element>()
            {
                new MultipartForm.Parameter("X", scene.RegionInfo.RegionLocX.ToString()),
                new MultipartForm.Parameter("Y", scene.RegionInfo.RegionLocY.ToString()),
                new MultipartForm.File("Tile", "tile.png", "image/png", pngData)
            };

            // Make the remote storage request
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_serverUrl);

                HttpWebResponse response = MultipartForm.Post(request, postParameters);
                using (Stream responseStream = response.GetResponseStream())
                {
                    string responseStr = null;

                    try
                    {
                        responseStr = responseStream.GetStreamString();
                        OSD responseOSD = OSDParser.Deserialize(responseStr);
                        if (responseOSD.Type == OSDType.Map)
                        {
                            OSDMap responseMap = (OSDMap)responseOSD;
                            if (responseMap["Success"].AsBoolean())
                            {
                                m_log.Info("[SIMIAN GRID CONNECTOR]: Uploaded " + pngData.Length + " byte PNG map tile to AddMapTile");
                            }
                            else
                            {
                                errorMessage = "Upload failed: " + responseMap["Message"].AsString();
                            }
                        }
                        else
                        {
                            errorMessage = "Response format was invalid:\n" + responseStr;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!String.IsNullOrEmpty(responseStr))
                        {
                            errorMessage = "Failed to parse the response:\n" + responseStr;
                        }
                        else
                        {
                            errorMessage = "Failed to retrieve the response: " + ex.Message;
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                errorMessage = ex.Message;
            }

            if (!String.IsNullOrEmpty(errorMessage))
            {
                m_log.WarnFormat("[SIMIAN GRID CONNECTOR]: Failed to store {0} byte PNG map tile for {1}: {2}",
                                 pngData.Length, scene.RegionInfo.RegionName, errorMessage.Replace('\n', ' '));
            }
        }
示例#5
0
        /// <summary>
        /// Creates a new asset
        /// </summary>
        /// Returns a random ID if none is passed into it
        /// <param name="asset"></param>
        /// <returns></returns>
        public string Store(AssetBase asset)
        {
            if (String.IsNullOrEmpty(m_serverUrl))
            {
                m_log.Error("[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
                throw new InvalidOperationException();
            }

            bool   storedInCache = false;
            string errorMessage  = null;

            // AssetID handling
            if (String.IsNullOrEmpty(asset.ID) || asset.ID == ZeroID)
            {
                asset.FullID = UUID.Random();
                asset.ID     = asset.FullID.ToString();
            }

            // Cache handling
            if (m_cache != null)
            {
                m_cache.Cache(asset);
                storedInCache = true;
            }

            // Local asset handling
            if (asset.Local)
            {
                if (!storedInCache)
                {
                    m_log.Error("Cannot store local " + asset.Metadata.ContentType + " asset without an asset cache");
                    asset.ID     = null;
                    asset.FullID = UUID.Zero;
                }

                return(asset.ID);
            }

            // Distinguish public and private assets
            bool isPublic = true;

            switch ((AssetType)asset.Type)
            {
            case AssetType.CallingCard:
            case AssetType.Gesture:
            case AssetType.LSLBytecode:
            case AssetType.LSLText:
                isPublic = false;
                break;
            }

            // Make sure ContentType is set
            if (String.IsNullOrEmpty(asset.Metadata.ContentType))
            {
                asset.Metadata.ContentType = SLUtil.SLAssetTypeToContentType(asset.Type);
            }

            // Build the remote storage request
            List <MultipartForm.Element> postParameters = new List <MultipartForm.Element>()
            {
                new MultipartForm.Parameter("AssetID", asset.FullID.ToString()),
                new MultipartForm.Parameter("CreatorID", asset.Metadata.CreatorID),
                new MultipartForm.Parameter("Temporary", asset.Temporary ? "1" : "0"),
                new MultipartForm.Parameter("Public", isPublic ? "1" : "0"),
                new MultipartForm.File("Asset", asset.Name, asset.Metadata.ContentType, asset.Data)
            };

            // Make the remote storage request
            try
            {
                // Simian does not require the asset ID to be in the URL because it's in the post data.
                // By appending it to the URL also, we allow caching proxies (squid) to invalidate asset URLs
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_serverUrl + asset.FullID.ToString());

                HttpWebResponse response = MultipartForm.Post(request, postParameters);
                using (Stream responseStream = response.GetResponseStream())
                {
                    string responseStr = null;

                    try
                    {
                        responseStr = responseStream.GetStreamString();
                        OSD responseOSD = OSDParser.Deserialize(responseStr);
                        if (responseOSD.Type == OSDType.Map)
                        {
                            OSDMap responseMap = (OSDMap)responseOSD;
                            if (responseMap["Success"].AsBoolean())
                            {
                                return(asset.ID);
                            }
                            else
                            {
                                errorMessage = "Upload failed: " + responseMap["Message"].AsString();
                            }
                        }
                        else
                        {
                            errorMessage = "Response format was invalid:\n" + responseStr;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!String.IsNullOrEmpty(responseStr))
                        {
                            errorMessage = "Failed to parse the response:\n" + responseStr;
                        }
                        else
                        {
                            errorMessage = "Failed to retrieve the response: " + ex.Message;
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                errorMessage = ex.Message;
            }

            m_log.WarnFormat("[SIMIAN ASSET CONNECTOR]: Failed to store asset \"{0}\" ({1}, {2}): {3}",
                             asset.Name, asset.ID, asset.Metadata.ContentType, errorMessage);
            return(null);
        }
示例#6
0
        public bool StoreAsset(Asset asset)
        {
            Debug.Assert(asset.Data != null, "Cannot store an asset without data");
            Debug.Assert(!String.IsNullOrEmpty(asset.ContentType), "Cannot store an asset without a ContentType");

            bool storedInCache = false;

            if (asset.ID == UUID.Zero)
            {
                asset.ID = UUID.Random();
            }

            // Run this asset through the incoming asset filter
            if (!m_simian.FilterAsset(asset))
            {
                m_log.InfoFormat("Asset {0} ({1}, {2} bytes) was rejected", asset.ID, asset.ContentType, asset.Data.Length);
                return(false);
            }

            #region Caching

            if (m_dataStore != null)
            {
                byte[] metadata = CreateMetadata(asset.CreatorID, asset.ContentType, asset.Local, asset.Temporary, asset.Data, asset.ExtraHeaders);
                m_dataStore.AddOrUpdateAsset(asset.ID, METADATA_MIME_TYPE, metadata, true);
                m_dataStore.AddOrUpdateAsset(asset.ID, asset.ContentType, asset.Data, true);

                storedInCache = true;
            }

            #endregion Caching

            // If this is a local asset we don't need to store it remotely
            if (asset.Local)
            {
                if (!storedInCache)
                {
                    m_log.Error("Cannot store asset " + asset.ID + " (" + asset.ContentType + ") without an IDataStore");
                }
                return(storedInCache);
            }

            #region Remote Storage

            // Distinguish public and private assets
            bool isPublic = true;
            switch (asset.ContentType)
            {
            case "application/vnd.ll.callingcard":
            case "application/vnd.ll.gesture":
            case "application/vnd.ll.lslbyte":
            case "application/vnd.ll.lsltext":
                isPublic = false;
                break;
            }

            // Build the remote storage request
            List <MultipartForm.Element> postParameters = new List <MultipartForm.Element>()
            {
                new MultipartForm.Parameter("AssetID", asset.ID.ToString()),
                new MultipartForm.Parameter("CreatorID", asset.CreatorID.ToString()),
                new MultipartForm.Parameter("Temporary", asset.Temporary ? "1" : "0"),
                new MultipartForm.Parameter("Public", isPublic ? "1" : "0"),
                new MultipartForm.File("Asset", asset.ID.ToString(), asset.ContentType, asset.Data)
            };

            // Make the remote storage request
            string errorMessage = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_serverUrl);

                HttpWebResponse response = MultipartForm.Post(request, postParameters);
                using (Stream responseStream = response.GetResponseStream())
                {
                    string responseStr = null;

                    try
                    {
                        responseStr = responseStream.GetStreamString();
                        OSD responseOSD = OSDParser.Deserialize(responseStr);
                        if (responseOSD.Type == OSDType.Map)
                        {
                            OSDMap responseMap = (OSDMap)responseOSD;
                            if (responseMap["Success"].AsBoolean())
                            {
                                return(true);
                            }
                            else
                            {
                                errorMessage = "Upload failed: " + responseMap["Message"].AsString();
                            }
                        }
                        else
                        {
                            errorMessage = "Response format was invalid:\n" + responseStr;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!String.IsNullOrEmpty(responseStr))
                        {
                            errorMessage = "Failed to parse the response:\n" + responseStr;
                        }
                        else
                        {
                            errorMessage = "Failed to retrieve the response: " + ex.Message;
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                errorMessage = ex.Message;
            }

            #endregion Remote Storage

            m_log.WarnFormat("Failed to remotely store asset {0} ({1}): {2}", asset.ID, asset.ContentType, errorMessage);
            return(false);
        }
示例#7
0
        public bool AddOrUpdateMapTile(SceneInfo sceneInfo, Image mapTile)
        {
            string errorMessage = null;

            byte[] pngData;

            using (MemoryStream stream = new MemoryStream())
            {
                mapTile.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                pngData = stream.ToArray();
            }

            // Get the scene X/Y position in increments of 256 meters
            uint x = (uint)sceneInfo.MinPosition.X / 256u;
            uint y = (uint)sceneInfo.MinPosition.Y / 256u;

            // Build the form POST data
            List <MultipartForm.Element> postParameters = new List <MultipartForm.Element>()
            {
                new MultipartForm.Parameter("X", x.ToString()),
                new MultipartForm.Parameter("Y", y.ToString()),
                new MultipartForm.File("Tile", "tile.png", "image/png", pngData)
            };

            // Make the remote storage request
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_serverUrl);

                HttpWebResponse response = MultipartForm.Post(request, postParameters);
                using (Stream responseStream = response.GetResponseStream())
                {
                    string responseStr = null;

                    try
                    {
                        responseStr = responseStream.GetStreamString();
                        OSD responseOSD = OSDParser.Deserialize(responseStr);
                        if (responseOSD.Type == OSDType.Map)
                        {
                            OSDMap responseMap = (OSDMap)responseOSD;
                            if (responseMap["Success"].AsBoolean())
                            {
                                m_log.Debug("Uploaded " + pngData.Length + " byte PNG map tile to AddMapTile");
                            }
                            else
                            {
                                errorMessage = "Upload failed: " + responseMap["Message"].AsString();
                            }
                        }
                        else
                        {
                            errorMessage = "Response format was invalid:\n" + responseStr;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!String.IsNullOrEmpty(responseStr))
                        {
                            errorMessage = "Failed to parse the response:\n" + responseStr;
                        }
                        else
                        {
                            errorMessage = "Failed to retrieve the response: " + ex.Message;
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                errorMessage = ex.Message;
            }

            if (String.IsNullOrEmpty(errorMessage))
            {
                return(true);
            }
            else
            {
                m_log.WarnFormat("Failed to store {0} byte PNG map tile for {1}: {2}", pngData.Length, sceneInfo.Name,
                                 errorMessage.Replace('\n', ' '));
                return(false);
            }
        }