예제 #1
0
        private UrlPair GetNextUrl()
        {
            UrlPair url = null;

            lock (lockThis)
            {
                if (this.currentUrlIndex < this.urls.Count)
                {
                    Logger.Info("get url at count: {0}/{1}", this.currentUrlIndex, this.urls.Count);

                    url = this.urls[this.currentUrlIndex++];
                }
            }

            return(url);
        }
예제 #2
0
        private void RakerThread(object param)
        {
            int threadId = (int)param;

            Logger.Log("thread{0}: started", threadId);

            try
            {
                using (HttpWebUtility http = new HttpWebUtility())
                {
                    UrlPair urlpair = null;

                    while ((urlpair = GetNextUrl()) != null)
                    {
                        string filepath      = filePathMaker.MakeFilePath(urlpair.Url, fileNameMakingMethod);
                        bool   reportSucceed = true;

                        urlpair.Filename = filepath;

                        Logger.Log("    thread{0}: saving url: {1} to file: {2} (referer: {3})", threadId, urlpair.Url, filepath, urlpair.RefererUrl);

                        if (!AllowOverwrite() && File.Exists(filepath))
                        {
                            // duplicated
                            Logger.Log("    thread{0}: file already exists! continue to next url.", threadId);

                            urlpair.Result = UrlPair.SaveResult.Duplicated;
                            numOfDuplicated++;
                            reportSucceed = false;
                        }
                        else
                        {
                            try
                            {
                                http.SaveContentToFile(urlpair.Url, filepath, urlpair.RefererUrl);

                                ExceptionTester.Instance.Throw("exception");
                            }
                            catch (ThreadAbortException ex)
                            {
                                Logger.Warn("	thread{0}: ThreadAbortException raised.", threadId);

                                throw;
                            }
                            catch (Exception ex)
                            {
                                Logger.Warn("	thread{0}: exception has occured during saving image. '{1}'. try again, using encoded refererurl",
                                            threadId, ex.Message);

                                // try again, using encoded refererurl
                                try
                                {
                                    // refererUrl에 한글이 포함되어있으면 exception이 발생하므로 인코딩 해줘야 함.
                                    http.SaveContentToFile(urlpair.Url, filepath, HttpUtility.UrlEncode(urlpair.RefererUrl));

                                    ExceptionTester.Instance.Throw("savecontenttofile");
                                }
                                catch (ThreadAbortException ex2)
                                {
                                    Logger.Warn("	thread{0}: ThreadAbortException raised.", threadId);

                                    throw;
                                }
                                catch (Exception ex2)
                                {
                                    Logger.Warn("	thread{0}: exception has occured during saving image. '{1}'", threadId, ex2.Message);

                                    reportSucceed = false;
                                }
                            }

                            if (reportSucceed)
                            {
                                Logger.Log("	thread{0}: {1} saved.", threadId, filepath);

                                urlpair.Result = UrlPair.SaveResult.Saved;

                                lock (lockThis)
                                {
                                    numOfSucceed++;
                                }
                            }
                            else
                            {
                                Logger.Warn("	thread{0}: {1} save failed.", threadId, filepath);

                                urlpair.Result = UrlPair.SaveResult.Failed;

                                lock (lockThis)
                                {
                                    numOfFailed++;
                                }
                            }
                        }

                        // notify
                        lock (lockThis)
                        {
                            if (Progress != null)
                            {
                                // this.currentUrlIndex를 사용하지 않는 이유는, 전부 다 돼서 current == url.length인데,
                                // 락에 걸려서 progressbar performstep은 아직 다 안됐을 경우 자연스럽게 보이기 위함.
                                Progress(urlpair.Url, reportSucceed, urls.Count);
                            }
                        }
                    }
                }
            }
            catch (ThreadAbortException ex)
            {
                Logger.Warn("thread{0}: ThreadAbortException raised. aborting thread.", threadId);
            }
            catch (Exception ex)
            {
                Logger.Warn("thread{0}: exception has occured in work thread. '{1}'", threadId, ex.Message);
            }
            finally
            {
                Logger.Log("thread{0}: terminated.", threadId);
                this.manualEvents[threadId].Set();
            }
        }