Esempio n. 1
0
 public void f_stopAndFreeResource()
 {
     if (_state != JOB_STATE.STOPED)
     {
         lock (jobInfo)
             jobInfo.f_stopJob();
     }
     list.Clear();
     msg.Clear();
 }
Esempio n. 2
0
        public void f_runLoop(object state, bool timedOut)
        {
            JobInfo ti = (JobInfo)state;

            if (!timedOut)
            {
                Tracer.WriteLine("J{0} executes on thread {1}: SIGNAL -> STOP", Id, Thread.CurrentThread.GetHashCode().ToString());
                ti.f_stopJob();
                return;
            }

            Tracer.WriteLine("J{0} executes on thread {1}: Do something ...", Id, Thread.CurrentThread.GetHashCode().ToString());
        }
Esempio n. 3
0
        public void f_runLoop(object state, bool timedOut)
        {
            JobInfo ti = (JobInfo)state;

            if (!timedOut)
            {
                ti.f_stopJob();
                return;
            }


            if (this.queue.Count > 0)
            {
                string s = this.queue.Dequeue(string.Empty);
                if (s.Length > 0)
                {
                    test_run_v1(s);
                    //test_run_v2(s);
                }
            }
        }
Esempio n. 4
0
        public void f_runLoop(object state, bool timedOut)
        {
            JobInfo ti = (JobInfo)state;

            if (!timedOut)
            {
                System.Tracer.WriteLine("J{0} executes on thread {1}: SIGNAL -> STOP", Id, Thread.CurrentThread.GetHashCode().ToString());
                ti.f_stopJob();
                return;
            }


            if (this.queue.Count > 0)
            {
                string s = this.queue.Dequeue(string.Empty);
                if (s.Length > 0)
                {
                    test_run();

                    System.Tracer.WriteLine("J{0} executes on thread {1}: Speech = {2}", Id, Thread.CurrentThread.GetHashCode().ToString(), s);
                }
            }
        }
Esempio n. 5
0
        public void f_runLoop(object state, bool timedOut)
        {
            if (!_inited)
            {
                _inited = true;
                f_Init();
                return;
            }

            JobInfo ti = (JobInfo)state;

            if (!timedOut)
            {
                // Tracer.WriteLine("J{0} executes on thread {1}: SIGNAL -> STOP ...", Id, Thread.CurrentThread.GetHashCode().ToString());
                ti.f_stopJob();
                return;
            }

            // Tracer.WriteLine("J{0} executes on thread {1}:DO SOMETHING ...", Id, Thread.CurrentThread.GetHashCode().ToString());
            // Do something ...

            if (msg.Count > 0)
            {
                Message m = msg.Dequeue(null);
                if (m != null)
                {
                    switch (m.getAction())
                    {
                    case MESSAGE_ACTION.ITEM_SEARCH:
                        #region
                        if (true)
                        {
                            oLink[] a = new oLink[] { };
                            if (m.Input != null)
                            {
                                string key = m.Input as string;
                                a = list.Where(x => x.Link.Contains(key) || x.Title.Contains(key) || x.Tags.Contains(key), false, int.MaxValue);
                                m.Output.Counter = a.Length;
                            }
                            else
                            {
                                a = list.Take(10).ToArray();
                                m.Output.Counter = list.Count;
                            }

                            m.Type    = MESSAGE_TYPE.RESPONSE;
                            m.JobName = this._groupName;

                            m.Output.Ok         = true;
                            m.Output.PageSize   = 10;
                            m.Output.PageNumber = 1;
                            m.Output.Total      = list.Count;
                            m.Output.SetData(a);

                            this.StoreJob.f_responseMessageFromJob(m);
                        }
                        #endregion
                        break;

                    case MESSAGE_ACTION.URL_REQUEST_CACHE:
                        #region
                        if (m.Input != null)
                        {
                            string url = m.Input as string;
                            if (urlData.ContainsKey(url))
                            {
                                string htm = urlData[url];

                                m.Type    = MESSAGE_TYPE.RESPONSE;
                                m.JobName = this._groupName;

                                m.Output.Ok = true;
                                m.Output.SetData(htm);

                                this.StoreJob.f_responseMessageFromJob(m);
                            }
                            else
                            {
                                UrlService.GetAsync(url, m, UrlService.Func_GetHTML_UTF8_FORMAT_BROWSER, (result) =>
                                {
                                    if (result.Ok)
                                    {
                                        string htm = result.Html;
                                        if (!urlData.ContainsKey(url))
                                        {
                                            urlData.Add(url, htm);
                                        }

                                        m.Type    = MESSAGE_TYPE.RESPONSE;
                                        m.JobName = this._groupName;

                                        m.Output.Ok = true;
                                        m.Output.SetData(htm);

                                        this.StoreJob.f_responseMessageFromJob(m);
                                    }
                                });
                            }
                        }
                        #endregion
                        break;
                    }
                }
            }
        }
Esempio n. 6
0
        public void f_runLoop(object state, bool timedOut)
        {
            if (!_inited)
            {
                _inited = true;
                f_Init();
                _state = JOB_STATE.INIT;
                if (jobInfo != null)
                {
                    lock (jobInfo)
                        jobInfo = (JobInfo)state;
                }
                else
                {
                    jobInfo = (JobInfo)state;
                }
                return;
            }

            if (!timedOut)
            {
                //Tracer.WriteLine("J{0} executes on thread {1}: SIGNAL -> STOP ...", Id, Thread.CurrentThread.GetHashCode().ToString());
                JobInfo ti = (JobInfo)state;
                ti.f_stopJob();
                _state = JOB_STATE.STOPED;
                return;
            }

            if (_state != JOB_STATE.RUNNING)
            {
                _state = JOB_STATE.RUNNING;
            }

            //Tracer.WriteLine("J{0} executes on thread {1}:DO SOMETHING ...", Id, Thread.CurrentThread.GetHashCode().ToString());
            // Do something ...

            if (msg.Count > 0)
            {
                Message m = msg.Dequeue(null);
                if (m != null)
                {
                    //[1] SEND REQUEST TO JOB FOR EXECUTE
                    if (m.Type == MESSAGE_TYPE.REQUEST)
                    {
                        IJob[] jobs = this.StoreJob.f_job_getByID(m.GetReceiverId());
                        if (jobs.Length > 0)
                        {
                            for (int i = 0; i < jobs.Length; i++)
                            {
                                jobs[i].f_receiveMessage(m);
                            }
                        }
                    }
                    else
                    {
                        //[2] RESPONSE TO SENDER
                        switch (m.getSenderType())
                        {
                        case SENDER_TYPE.IS_FORM:
                            IFORM fom = this.StoreJob.f_form_Get(m.GetSenderId());
                            if (fom != null)
                            {
                                fom.f_receiveMessage(m.GetMessageId());
                            }
                            // write to LOG ...
                            break;

                        case SENDER_TYPE.HIDE_SENDER:
                            // do not send response to sender
                            // write to LOG ...
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        public void f_runLoop(object state, bool timedOut)
        {
            JobInfo ti = (JobInfo)state;

            if (!timedOut)
            {
                ti.f_stopJob();
                return;
            }

            lock (_lock)
                if (isDownloading)
                {
                    return;
                }

            string _url = StoreJob.f_url_getUrlPending();

            if (_url.Length == 0)
            {
                return;
            }

            //Interlocked.CompareExchange(ref Id, Id, 0);
            Tracer.WriteLine("J{0} -> {1}", Id, _url);

            HttpWebRequest w = (HttpWebRequest)WebRequest.Create(new Uri(_url));

            w.BeginGetResponse(asyncResult =>
            {
                string url     = ((HttpWebRequest)asyncResult.AsyncState).RequestUri.ToString();
                string data    = string.Empty;
                bool isSuccess = true;
                try
                {
                    HttpWebResponse rs = (HttpWebResponse)w.EndGetResponse(asyncResult);
                    if (rs.StatusCode == HttpStatusCode.OK)
                    {
                        using (StreamReader sr = new StreamReader(rs.GetResponseStream(), System.Text.Encoding.UTF8))
                            data = sr.ReadToEnd();
                        rs.Close();
                    }

                    if (!string.IsNullOrEmpty(data))
                    {
                        data = HttpUtility.HtmlDecode(data);
                        data = format_HTML(data);

                        string[] urls = get_UrlHtml(url, data);
                        if (urls.Length > 0)
                        {
                            StoreJob.f_url_AddRange(urls);
                        }
                    }
                    else
                    {
                        isSuccess = false;
                        data      = "REQUEST_FAIL";
                    }
                }
                catch (Exception ex)
                {
                    data      = ex.Message;
                    isSuccess = false;
                }

                Tracer.WriteLine("J{0} <- {1}", Id, _url);

                StoreJob.f_url_countResult(_url, data, isSuccess);

                int urlCounter = StoreJob.f_url_countPending();
                if (urlCounter == 0)
                {
                    bool end = StoreJob.f_url_stateJobIsComplete(Id);
                    if (end)
                    {
                        StoreJob.f_url_Complete();
                    }
                    return;
                }

                lock (_lock)
                    isDownloading = false;
            }, w);
        }