Exemple #1
0
        public ActionResult SearchIp(string ipAddr)
        {
            try
            {
                SearchRecord binary_recorder;
                SearchRecord fibonacci_recorder;

                var ipRecords = _cacheManager.Get(IPRECORDS_ALL_KEY, () =>
                                                  IpRecordHelper.LoadIpRecordResult(_ipResultFilePath).ToList()
                                                  ).ToArray();

                IpRecordHelper.SearchIpRecord(ipAddr, ipRecords, out binary_recorder, out fibonacci_recorder);

                if (binary_recorder.Index == -1)
                {
                    return(Json(new
                    {
                        success = false,
                        message = string.Format("没有找到该ip: {0}", ipAddr)
                    }));
                }

                SearchResultModel model = new SearchResultModel();
                model.Binary_recorder    = binary_recorder;
                model.Fibonacci_recorder = fibonacci_recorder;
                // model.Summary = string.Format("第{0}个找到, ip 信息为\n{1}", binary_recorder.Index + 1, binary_recorder.IpRecord);
                model.Summary = string.Format("第{0}个找到, ip 信息为\n{1}", binary_recorder.IpRecord.Id, binary_recorder.IpRecord);

                return(Json(new
                {
                    success = true,
                    message = this.RenderPartialViewToString("SearchResult", model)
                }));
            }
            catch (FormatException)
            {
                return(Json(new
                {
                    success = false,
                    message = "请输入正确ip地址"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }
        /// <summary>
        /// Search
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                SearchRecord binary_recorder;
                SearchRecord fibonacci_recorder;

                IpRecordHelper.SearchIpRecord(tbxSearch.Text.Trim(), _ipRecords, out binary_recorder, out fibonacci_recorder);

                if (binary_recorder.Index == -1)
                {
                    MessageBox.Show(string.Format(Messages.NotFound, tbxSearch.Text.Trim()));
                    return;
                }

                var content = (string.Format("第{0}个找到, ip 信息为", binary_recorder.Index + 1)
                               + Environment.NewLine
                               + string.Format("{0}-{1} {2}", binary_recorder.IpRecord.StartIp, binary_recorder.IpRecord.EndIp, binary_recorder.IpRecord.Address)
                               + Environment.NewLine
                               + Environment.NewLine
                               + string.Format("{0}: 查找次数: {1}, 用时(ms): {2}", "二分法查找", binary_recorder.Count, binary_recorder.ElapsedMilliseconds)
                               + Environment.NewLine
                               + string.Format("{0} 查找次数: {1}, 用时(ms): {2}", "斐波那契查找", fibonacci_recorder.Count, fibonacci_recorder.ElapsedMilliseconds)
                               );

                lblContent.Text = content;
            }
            catch (FormatException)
            {
                MessageBox.Show(Messages.WrongIpFormat);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }