object TextureFetchJob(object arg)
        {
            IrrManager.IrrWorkItem irrWorkItem = (IrrManager.IrrWorkItem)arg;
            TextureDownloadRequest req = (TextureDownloadRequest)irrWorkItem.arg;

            m_log.Debug("[TextureFetchJob]: handling req " + req.uuid);
            try
            {
                RestClient rest = new RestClient(VUtil.assetServerUri);
                rest.RequestMethod = "GET";
                rest.AddResourcePath("assets");
                rest.AddResourcePath(req.uuid.ToString());
                rest.AddHeader("Authorization", "OpenGrid " + VUtil.authToken.ToString());

                System.IO.Stream stream = rest.Request(timeout);
                m_log.Debug("[TextureFetchJob]: downloaded JP2 for " + req.uuid);

                XmlSerializer xs = new XmlSerializer(typeof(AssetBase));

                //System.IO.StreamReader x = new StreamReader(stream);
                //string test = x.ReadToEnd();

                //stream.Seek(0, SeekOrigin.Begin);
                AssetBase ab = (AssetBase)xs.Deserialize(stream);
                OpenViewer.Primitives.ExtendedAssetTexture texAsset = new OpenViewer.Primitives.ExtendedAssetTexture(req.uuid, ab.Data,ab.Type);
                imageReceivedCallback(texAsset);
                return null;
            }
            catch (Exception e)
            {
                m_log.Warn("[TextureFetchJob]: could not fetch texture " + req.uuid + ": " + e.Message);
                if (e is System.Net.WebException && ((System.Net.WebException)e).Status == System.Net.WebExceptionStatus.ProtocolError)
                {
                    // no retry
                    return null;
                }
                else
                {
                    return irrWorkItem;
                }
            }
        }
Example #2
0
        public string IrrFileCreateCache(string _filename, string _directory)
        {
            if (string.IsNullOrEmpty(_filename))
                return "Filename is null or empty.";

            if (System.IO.File.Exists(_directory + "/" + _filename))
                return _filename + " is Already exist";

            string err = string.Empty;
            string uuid = System.IO.Path.GetFileNameWithoutExtension(_filename);
            string filenameWithoutPath = System.IO.Path.GetFileName(_filename);
            int timeout = Reference.Viewer.Config.Source.Configs["Startup"].GetInt("asset_timeout", 60000);

            try
            {
                RestClient rest = new RestClient(VUtil.assetServerUri);
                rest.RequestMethod = "GET";
                rest.AddResourcePath("assets");
                rest.AddResourcePath(uuid);
                rest.AddHeader("Authorization", "OpenGrid " + VUtil.authToken.ToString());

                System.IO.Stream stream = rest.Request(timeout);

                XmlSerializer xs = new XmlSerializer(typeof(AssetBase));
                AssetBase ab = (AssetBase)xs.Deserialize(stream);

                bool tryToDecompress = true;
                if (tryToDecompress
                    && ab.Data.Length >= 2
                    && ab.Data[0] == 0x1f // gzip header == 0x1f8b
                    && ab.Data[1] == 0x8b)
                {
                    try
                    {
                        // try to uncompress
                        string compressedFilename = _directory + "/" + filenameWithoutPath + ".gz";
                        System.IO.File.WriteAllBytes(compressedFilename, ab.Data);
                        string dstFile = _directory + "/" + filenameWithoutPath;

                        Lib3Di.Compress.AddDecompressionRequest(new DecompressionRequest(compressedFilename, dstFile));
                        Lib3Di.Compress.DecompressWaitingRequests(); // Handle all waiting decompression requests here - one at a time (to prevent file collisions and simultaneous decompression in separate IrrMeshThreads). We could also make a separate thread to handle all decompression requests in sequence, but that thread would need to live forever and would only be active at the start of the program when assets are being downloaded, so it makes more sense to handle decompression requests here (invoked from an IrrMeshThread), where DecompressWaitingRequests() locks the queue and processes everything serially.
                    }
                    catch (Exception e)
                    {
                        Reference.Log.Warn("[IRR MANAGER]: " + e.Message);
                        Reference.Log.Debug("[IRR MANAGER]: " + e.StackTrace);
                        File.WriteAllBytes(_directory + "/" + filenameWithoutPath, ab.Data);
                    }
                    finally
                    {
                        File.Delete(_directory + "/" + filenameWithoutPath + ".gz");
                    }
                }
                else
                {
                    System.IO.File.WriteAllBytes(_directory + "/" + filenameWithoutPath, ab.Data);
                }
            }
            catch (Exception e)
            {
                err = e.Message;
            }

            return err;
        }