Exemplo n.º 1
0
        private static void _StartPicDownload(Uri uri, string target)
        {
            do
            {
                bool downloaded = false;

                //System.Threading.Thread.Sleep (5000);
                downloaded = Download(uri, target);
                if (!downloaded)
                {
                    Console.WriteLine("Error fetching picture for {0} to {1}", uri, target);
                }

                // Cluster all updates together
                bool doInvoke = false;

                lock (requestQueue)
                {
                    if (downloaded)
                    {
                        queuedUpdates.Add(uri);

                        // If this is the first queued update, must notify
                        if (queuedUpdates.Count == 1)
                        {
                            doInvoke = true;
                        }
                    }
                    else
                    {
                        pendingRequests.Remove(uri);
                    }

                    // Try to get more jobs.
                    if (requestQueue.Count > 0)
                    {
                        uri = requestQueue.Pop();
                        if (uri == null)
                        {
                            Console.Error.WriteLine("Dropping request {0} because url is null", uri);
                            pendingRequests.Remove(uri);
                            uri = null;
                        }
                    }
                    else
                    {
                        //Util.Log ("Leaving because requestQueue.Count = {0} NOTE: {1}", requestQueue.Count, pendingRequests.Count);
                        uri = null;
                    }
                }
                if (doInvoke)
                {
                    nsDispatcher.BeginInvokeOnMainThread(NotifyImageListeners);
                }
            } while (uri != null);
        }
Exemplo n.º 2
0
        void Launch(string url, bool callbackOnMainThread, Action <Stream> callback)
        {
            Util.PushNetworkActive();
            Uri uri = new Uri(url);

            // Wake up 3G if it has been more than 3 minutes
            lock (minuteLock){
                var nowTicks = DateTime.UtcNow.Ticks;
                if (nowTicks - lastLaunchTick > TimeSpan.TicksPerMinute * 3)
                {
                    MonoTouch.ObjCRuntime.Runtime.StartWWAN(uri);
                }
                lastLaunchTick = nowTicks;
            }

            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.Headers [HttpRequestHeader.Authorization] = OAuthAuthorizer.AuthorizeRequest(OAuthConfig, OAuthToken, OAuthTokenSecret, "GET", uri, null);

            request.BeginGetResponse(ar => {
                try {
                    lock (queue)
                        pending--;
                    Util.PopNetworkActive();
                    Stream stream = null;

                    try {
                        var response = (HttpWebResponse)request.EndGetResponse(ar);
                        stream       = response.GetResponseStream();

                        // Since the stream will deliver in chunks, make a copy before passing to the main UI
                        if (callbackOnMainThread)
                        {
                            var ms = new MemoryStream();
                            CopyStream(stream, ms);
                            ms.Position = 0;
                            stream.Close();
                            stream = ms;
                        }
                    } catch (WebException we) {
                        var response = we.Response as HttpWebResponse;
                        if (response != null)
                        {
                            switch (response.StatusCode)
                            {
                            case HttpStatusCode.Unauthorized:
                                // This is the case of sharing two keys
                                break;
                            }
                            stream = null;
                        }
                        Console.WriteLine(we);
                    } catch (Exception e) {
                        Console.WriteLine(e);
                        stream = null;
                    }

                    if (callbackOnMainThread)
                    {
                        invoker.BeginInvokeOnMainThread(delegate { InvokeCallback(callback, stream); });
                    }
                    else
                    {
                        InvokeCallback(callback, stream);
                    }
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
                lock (queue){
                    if (queue.Count > 0)
                    {
                        var nextRequest = queue.Dequeue();
                        Launch(nextRequest.Url, nextRequest.CallbackOnMainThread, nextRequest.Callback);
                    }
                }
            }, null);
        }
Exemplo n.º 3
0
        static void StartImageDownload(long id, Uri url)
        {
            ThreadCount++;

            Util.PushNetworkActive();
            do
            {
                var buffer = new byte [4 * 1024];

                using (var file = new FileStream(SmallPicDir + id + ".jpg", FileMode.Create, FileAccess.Write, FileShare.Read)) {
                    var req = WebRequest.Create(url) as HttpWebRequest;

                    using (var resp = req.GetResponse()) {
                        using (var s = resp.GetResponseStream()) {
                            int n;
                            while ((n = s.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                file.Write(buffer, 0, n);
                            }
                            file.Flush();
                        }
                    }
                }

                // Cluster all updates together
                bool doInvoke = false;
                lock (queuedUpdates){
                    queuedUpdates.Add(id);

                    // If this is the first queued update, must notify
                    if (queuedUpdates.Count > 0)
                    {
                        doInvoke = true;
                    }
                }

                // Try to get more jobs.
                lock (requestQueue){
                    if (requestQueue.Count > 0)
                    {
                        id = requestQueue.Dequeue();
                    }
                    else
                    {
                        id = -1;
                    }
                }

                url = GetImageUrlFromId(id, null);
                if (url == null)
                {
                    id = -1;
                }

                if (doInvoke)
                {
                    nsDispatcher.BeginInvokeOnMainThread(NotifyImageListeners);
                }
            } while (id != -1);

            Util.PopNetworkActive();

            ThreadCount--;
        }
Exemplo n.º 4
0
        static void _StartPicDownload(long id, Uri url)
        {
            do
            {
                string picdir     = id < TempStartId ? PicDir : TmpDir;
                bool   downloaded = false;

                downloaded = Download(url, picdir + id + ".png");
                if (!downloaded)
                {
                    Console.WriteLine("Error fetching picture for {0} from {1}", id, url);
                }

                // Cluster all updates together
                bool doInvoke = false;

                lock (requestQueue){
                    if (downloaded)
                    {
                        queuedUpdates.Add(id);

                        // If this is the first queued update, must notify
                        if (queuedUpdates.Count == 1)
                        {
                            doInvoke = true;
                        }
                    }
                    else
                    {
                        pendingRequests.Remove(id);
#if DEBUGIMAGE
                        pendingTimes.Remove(id);
#endif
                    }
                    idToUrl.Remove(id);

                    // Try to get more jobs.
                    if (requestQueue.Count > 0)
                    {
                        id  = requestQueue.Pop();
                        url = GetPicUrlFromId(id, null);
                        if (url == null)
                        {
                            Util.Log("Dropping request {0} because url is null", id);
                            pendingRequests.Remove(id);
#if DEBUGIMAGE
                            pendingTimes.Remove(id);
#endif
                            id = -1;
                        }
                    }
                    else
                    {
                        //Util.Log ("Leaving because requestQueue.Count = {0} NOTE: {1}", requestQueue.Count, pendingRequests.Count);
                        id = -1;
                    }
                }
                if (doInvoke)
                {
                    nsDispatcher.BeginInvokeOnMainThread(NotifyImageListeners);
                }
            } while (id != -1);
        }