示例#1
0
        int _fetched     = 0; // 已经 Present 获取的条数

        private async void button_search_Click(object sender, EventArgs e)
        {
            string strError = "";

            EnableControls(false);

            try
            {
                this.ClearHtml();

                _resultCount = 0;
                _fetched     = 0;
                this.button_nextBatch.Text = ">>";


                // 如果 targetInfo 没有变化,就持续使用
                if (_targetInfo.HostName != this.textBox_serverAddr.Text ||
                    _targetInfo.Port != Convert.ToInt32(this.textBox_serverPort.Text) ||
                    string.Join(",", _targetInfo.DbNames) != string.Join(",", new string[] { this.textBox_database.Text }) ||
                    _targetInfo.AuthenticationMethod != GetAuthentcationMethod() ||
                    _targetInfo.UserName != this.textBox_userName.Text ||
                    _targetInfo.Password != this.textBox_password.Text)
                {
                    _targetInfo = new TargetInfo
                    {
                        HostName             = this.textBox_serverAddr.Text,
                        Port                 = Convert.ToInt32(this.textBox_serverPort.Text),
                        DbNames              = new string[] { this.textBox_database.Text },
                        AuthenticationMethod = GetAuthentcationMethod(),
                        GroupID              = this.textBox_groupID.Text,
                        UserName             = this.textBox_userName.Text,
                        Password             = this.textBox_password.Text,
                    };
                    _zclient.CloseConnection();
                }

                IsbnConvertInfo isbnconvertinfo = new IsbnConvertInfo
                {
                    IsbnSplitter = this._isbnSplitter,
                    ConvertStyle =
                        (_targetInfo.IsbnAddHyphen == true ? "addhyphen," : "")
                        + (_targetInfo.IsbnRemoveHyphen == true ? "removehyphen," : "")
                        + (_targetInfo.IsbnForce10 == true ? "force10," : "")
                        + (_targetInfo.IsbnForce13 == true ? "force13," : "")
                        + (_targetInfo.IsbnWild == true ? "wild," : "")
                };

                // 构造 XML 检索式
                string strQueryXml = BuildQueryXml();
                // 将 XML 检索式变化为简明格式检索式
                int nRet = ZClient.GetQueryString(
                    this._useList,
                    strQueryXml,
                    isbnconvertinfo,
                    out string strQueryString,
                    out strError);
                if (nRet == -1)
                {
                    goto ERROR1;
                }

                {
                    // return Value:
                    //      -1  出错
                    //      0   成功
                    //      1   调用前已经是初始化过的状态,本次没有进行初始化
                    InitialResult result = await _zclient.TryInitialize(_targetInfo);

                    if (result.Value == -1)
                    {
                        strError = "Initialize error: " + result.ErrorInfo;
                        goto ERROR1;
                    }

                    /*
                     * this.Invoke(
                     * (Action)(() => MessageBox.Show(this, result.ToString()))
                     * );
                     */
                }

                SearchResult search_result = await _zclient.Search(
                    strQueryString,
                    _targetInfo.DefaultQueryTermEncoding,
                    _targetInfo.DbNames,
                    _targetInfo.PreferredRecordSyntax,
                    "default");

                this.AppendHtml("<div class='debug green' >检索共命中记录 " + search_result.ResultCount + "</div>");

#if NO
                this.Invoke(
                    (Action)(() => MessageBox.Show(this, search_result.ToString()))
                    );
#endif
                _resultCount = search_result.ResultCount;

#if NO
                if (_resultCount - _fetched > 0)
                {
                    this.button_nextBatch.Enabled = true;
                }
                else
                {
                    this.button_nextBatch.Enabled = false;
                }
#endif

                await FetchRecords();

                return;
            }
            finally
            {
                EnableControls(true);
            }
ERROR1:
            MessageBox.Show(this, strError);
        }
示例#2
0
文件: Form1.cs 项目: keji56/chord
        int _fetched      = 0; // 已经 Present 获取的条数

        private async void button_search_Click(object sender, EventArgs e)
        {
            this.toolStripStatusLabel1.Text = "";

            string strError = "";

            this.ClearHtml();
            _resultCount = 0;
            _fetched     = 0;

            EnableControls(false);

            try
            {
                // 如果 _targetInfo 涉及到的信息字段对比环境没有变化,就持续使用
                if (_targetInfo.HostName != this.textBox_serverAddr.Text ||
                    _targetInfo.Port != Convert.ToInt32(this.textBox_serverPort.Text) ||
                    string.Join(",", _targetInfo.DbNames) != this.textBox_database.Text ||
                    _targetInfo.AuthenticationMethod != GetAuthentcationMethod() ||
                    _targetInfo.UserName != this.textBox_userName.Text ||
                    _targetInfo.Password != this.textBox_password.Text)
                {
                    _targetInfo = new TargetInfo
                    {
                        HostName             = this.textBox_serverAddr.Text,
                        Port                 = Convert.ToInt32(this.textBox_serverPort.Text),
                        DbNames              = StringUtil.SplitList(this.textBox_database.Text).ToArray(),
                        AuthenticationMethod = GetAuthentcationMethod(),
                        GroupID              = this.textBox_groupID.Text,
                        UserName             = this.textBox_userName.Text,
                        Password             = this.textBox_password.Text,
                    };
                    _zclient.CloseConnection();
                }

                IsbnConvertInfo isbnconvertinfo = new IsbnConvertInfo
                {
                    IsbnSplitter = this._isbnSplitter,
                    ConvertStyle =
                        (_targetInfo.IsbnAddHyphen == true ? "addhyphen," : "")
                        + (_targetInfo.IsbnRemoveHyphen == true ? "removehyphen," : "")
                        + (_targetInfo.IsbnForce10 == true ? "force10," : "")
                        + (_targetInfo.IsbnForce13 == true ? "force13," : "")
                        + (_targetInfo.IsbnWild == true ? "wild," : "")
                };

                string strQueryString = "";

                if (this.radioButton_query_easy.Checked)
                {
                    // 创建只包含一个检索词的简单 XML 检索式
                    // 注:这种 XML 检索式不是 Z39.50 函数库必需的。只是用它来方便构造 API 检索式的过程
                    string strQueryXml = BuildQueryXml();
                    // 将 XML 检索式变化为 API 检索式
                    Result result = ZClient.ConvertQueryString(
                        this._useList,
                        strQueryXml,
                        isbnconvertinfo,
                        out strQueryString);
                    if (result.Value == -1)
                    {
                        strError = result.ErrorInfo;
                        goto ERROR1;
                    }

                    this.textBox_queryString.Text = strQueryString; // 便于调试观察
                }
                else
                {
                    strQueryString = this.textBox_queryString.Text;
                }

REDO_SEARCH:
                {
                    // return Value:
                    //      -1  出错
                    //      0   成功
                    //      1   调用前已经是初始化过的状态,本次没有进行初始化
                    // InitialResult result = _zclient.TryInitialize(_targetInfo).GetAwaiter().GetResult();
                    // InitialResult result = _zclient.TryInitialize(_targetInfo).Result;
                    InitialResult result = await _zclient.TryInitialize(_targetInfo);

                    if (result.Value == -1)
                    {
                        strError = "Initialize error: " + result.ErrorInfo;
                        goto ERROR1;
                    }

                    /*
                     * this.Invoke(
                     * (Action)(() => MessageBox.Show(this, result.ToString()))
                     * );
                     */
                }

                // result.Value:
                //		-1	error
                //		0	fail
                //		1	succeed
                // result.ResultCount:
                //      命中结果集内记录条数 (当 result.Value 为 1 时)
                SearchResult search_result = await _zclient.Search(
                    strQueryString,
                    _targetInfo.DefaultQueryTermEncoding,
                    _targetInfo.DbNames,
                    _targetInfo.PreferredRecordSyntax,
                    "default");

                if (search_result.Value == -1 || search_result.Value == 0)
                {
                    this.AppendHtml("<div class='debug error' >检索出错 " + search_result.ErrorInfo + "</div>");
                    if (search_result.ErrorCode == "ConnectionAborted")
                    {
                        this.AppendHtml("<div class='debug error' >自动重试检索 ...</div>");
                        goto REDO_SEARCH;
                    }
                }
                else
                {
                    this.AppendHtml("<div class='debug green' >检索共命中记录 " + search_result.ResultCount + "</div>");
                }

#if NO
                this.Invoke(
                    (Action)(() => MessageBox.Show(this, search_result.ToString()))
                    );
#endif
                _resultCount = search_result.ResultCount;

#if NO
                if (_resultCount - _fetched > 0)
                {
                    this.button_nextBatch.Enabled = true;
                }
                else
                {
                    this.button_nextBatch.Enabled = false;
                }
#endif

                await FetchRecords(_targetInfo);

                return;
            }
            finally
            {
                EnableControls(true);
            }
ERROR1:
            this.AppendHtml("<div class='debug error' >" + HttpUtility.HtmlEncode(strError) + "</div>");
            MessageBox.Show(this, strError);
        }
示例#3
0
        // 针对一个特定 Z30.50 服务器发起检索
        async Task <NormalResult> SearchOne(
            ZClientChannel channel,
            UseCollection useList,
            IsbnSplitter isbnSplitter,
            string strQueryWord,
            int nMax,
            string strFromStyle,
            string strMatchStyle,
            delegate_searchCompleted searchCompleted,
            delegate_presentCompleted presentCompleted)
        {
            var ok = channel.Enter();

            try
            {
                if (ok == false)
                {
                    return(new NormalResult
                    {
                        Value = -1,
                        ErrorInfo = "通道已被占用",
                        ErrorCode = "channelInUse"
                    });
                }

                var             _targetInfo     = channel.TargetInfo;
                IsbnConvertInfo isbnconvertinfo = new IsbnConvertInfo
                {
                    IsbnSplitter = isbnSplitter,
                    ConvertStyle =
                        (_targetInfo.IsbnAddHyphen == true ? "addhyphen," : "")
                        + (_targetInfo.IsbnRemoveHyphen == true ? "removehyphen," : "")
                        + (_targetInfo.IsbnForce10 == true ? "force10," : "")
                        + (_targetInfo.IsbnForce13 == true ? "force13," : "")
                        + (_targetInfo.IsbnWild == true ? "wild," : "")
                        // TODO:
                        + (_targetInfo.IssnForce8 == true ? "issnforce8," : "")
                };

                string strQueryString = "";
                {
                    // 创建只包含一个检索词的简单 XML 检索式
                    // 注:这种 XML 检索式不是 Z39.50 函数库必需的。只是用它来方便构造 API 检索式的过程
                    string strQueryXml = BuildQueryXml(strQueryWord, strFromStyle);
                    // 将 XML 检索式变化为 API 检索式
                    var result = ZClient.ConvertQueryString(
                        useList,
                        strQueryXml,
                        isbnconvertinfo,
                        out strQueryString);
                    if (result.Value == -1)
                    {
                        searchCompleted?.Invoke(channel, new SearchResult(result));
                        var final_result = new NormalResult {
                            Value = result.Value, ErrorInfo = result.ErrorInfo
                        };
                        if (result.ErrorCode == "notFound")
                        {
                            final_result.ErrorCode = "useNotFound";
                        }
                        return(final_result);
                    }
                }

REDO_SEARCH:
                {
                    // return Value:
                    //      -1  出错
                    //      0   成功
                    //      1   调用前已经是初始化过的状态,本次没有进行初始化
                    // InitialResult result = _zclient.TryInitialize(_targetInfo).GetAwaiter().GetResult();
                    // InitialResult result = _zclient.TryInitialize(_targetInfo).Result;
                    InitialResult result = await channel.ZClient.TryInitialize(_targetInfo);

                    if (result.Value == -1)
                    {
                        searchCompleted?.Invoke(channel, new SearchResult(result));
                        // TODO: 是否继续向后检索其他 Z39.50 服务器?
                        return(new NormalResult {
                            Value = -1, ErrorInfo = "Initialize error: " + result.ErrorInfo
                        });
                    }
                }

                // result.Value:
                //		-1	error
                //		0	fail
                //		1	succeed
                // result.ResultCount:
                //      命中结果集内记录条数 (当 result.Value 为 1 时)
                SearchResult search_result = await channel.ZClient.Search(
                    strQueryString,
                    _targetInfo.DefaultQueryTermEncoding,
                    _targetInfo.DbNames,
                    _targetInfo.PreferredRecordSyntax,
                    "default");

                if (search_result.Value == -1 || search_result.Value == 0)
                {
                    if (search_result.ErrorCode == "ConnectionAborted")
                    {
                        // 自动重试检索
                        goto REDO_SEARCH;
                    }
                }

                searchCompleted?.Invoke(channel, search_result);
                channel._resultCount = search_result.ResultCount;

                if (search_result.Value == -1 ||
                    search_result.Value == 0 ||
                    search_result.ResultCount == 0)
                {
                    return(new NormalResult());  // continue
                }
                var present_result = await _fetchRecords(channel, PresentBatchSize /*10*/);

                presentCompleted?.Invoke(channel, present_result);

                if (present_result.Value != -1)
                {
                    channel._fetched += present_result.Records.Count;
                }

                return(new NormalResult());
            }
            finally
            {
                channel.Exit();
            }
        }