public override InitialResult Initial(IList <string> parameters) { var result = new InitialResult() { IsOk = false, Msg = "initial error" }; if (Sheet.Shape.Equals(new Shape())) { result.Msg = "Please create a spreadsheet first!"; return(result); } if (parameters.Count != 3) { result.Msg = "The number of parameters is not correct!"; return(result); } var parametersInt = new List <int>(); foreach (string parameter in parameters) { if (int.TryParse(parameter, out int value)) { parametersInt.Add(value); } else { result.Msg = $"The parameter:'{parameter}' is not correct!"; return(result); } } if (parametersInt[0] > Sheet.Shape.Width) { result.Msg = $"The parameter:'{parametersInt[0]}' should be less then {Sheet.Shape.Width}!"; return(result); } if (parametersInt[1] > Sheet.Shape.Height) { result.Msg = $"The parameter:'{parametersInt[1]}' should be less then {Sheet.Shape.Height}!"; return(result); } var key1 = new Coordinate() { X = parametersInt[1] - 1, Y = parametersInt[0] - 1 }; var value1 = parametersInt[2]; Sheet.Cells[key1] = value1; result.IsOk = true; result.Msg = "ok"; return(result); }
// Z39.50 初始化 // 注:经过字符集协商,targetinfo 里面的某些成员会发生变化。因此需要把它保存下来继续使用 // return Value: // -1 出错 // 0 成功 // 1 调用前已经是初始化过的状态,本次没有进行初始化 public async Task <InitialResult> TryInitialize(TargetInfo targetinfo) { { // 处理通讯缓冲区中可能残留的 Close Response // return value: // -1 error // 0 不是Close // 1 是Close,已经迫使ZChannel处于尚未初始化状态 InitialResult result = await CheckServerCloseRequest(); } if (this._channel.Connected == false || this._channel.Initialized == false || this._channel.HostName != targetinfo.HostName || this._channel.Port != targetinfo.Port) { if (this._channel.Connected == false) { Result result = await this._channel.Connect(targetinfo.HostName, targetinfo.Port); if (result.Value == -1) { return new InitialResult { Value = -1, ErrorInfo = result.ErrorInfo } } ; } // this.Stop.SetMessage("正在执行Z39.50初始化 ..."); { // return Value: // -1 出错 // 0 成功 InitialResult result = await Initial( targetinfo, targetinfo.IgnoreReferenceID, this._currentRefID); if (result.Value == -1) { return(result); } return(result); } } return(new InitialResult { Value = 1 }); }
public override InitialResult Initial(IList <string> parameters) { var result = new InitialResult() { IsOk = false, Msg = "initial error" }; if (parameters.Count != 2) { result.Msg = "The number of parameters is not correct!"; return(result); } int p0 = 0; int p1 = 0; if (!int.TryParse(parameters[0], out p0)) { result.Msg = "The first parameter should be integer!"; return(result); } if (!int.TryParse(parameters[1], out p1)) { result.Msg = "The second parameter should be integer!"; return(result); } if (p0 < 1 || p1 < 1) { result.Msg = "The two parameters should be greater than 0 !"; return(result); } Sheet.Shape = new Shape() { Width = p0, Height = p1 }; Sheet.Cells = new Dictionary <Coordinate, int>(); result.IsOk = true; result.Msg = "ok"; return(result); }
// 执行一个通道的测试 async Task HandleTesting(ZClient client, TargetInfo targetInfo, ListViewItem item) { string strError = ""; Random rnd = new Random(); int i = 0; while (cancel.Token.IsCancellationRequested == false) { // return Value: // -1 出错 // 0 成功 // 1 调用前已经是初始化过的状态,本次没有进行初始化 InitialResult result = await client.TryInitialize(targetInfo, false); if (result.Value == -1) { strError = "Initialize error: " + result.ErrorInfo; this.Invoke( (Action)(() => ListViewUtil.ChangeItemText(item, 2, strError) )); return; } this.Invoke( (Action)(() => ListViewUtil.ChangeItemText(item, 1, ((i++) + 1).ToString()) )); await Task.Delay(rnd.Next(1, 500)); } }
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); }
public async Task <InitialResult> HugeRequestAttack(TargetInfo targetinfo, int length) { bool bTry = true; { // 处理通讯缓冲区中可能残留的 Close Response // return value: // -1 error // 0 不是Close // 1 是Close,已经迫使ZChannel处于尚未初始化状态 InitialResult result = await CheckServerCloseRequest().ConfigureAwait(false); } if (bTry == false || this._channel.Connected == false || this._channel.Initialized == false || this._channel.HostName != targetinfo.HostName || this._channel.Port != targetinfo.Port) { if (this._channel.Connected == false) { Result result = await this._channel.Connect(targetinfo.HostName, targetinfo.Port).ConfigureAwait(false); if (result.Value == -1) { return new InitialResult { Value = -1, ErrorInfo = result.ErrorInfo } } ; } // this.Stop.SetMessage("正在执行Z39.50初始化 ..."); // 2018/7/4 if (bTry == false) { this._channel.Initialized = false; } { // return Value: // -1 出错 // 0 成功 Result result = await ZChannel.SendInfinitPackage( this._channel._client, length).ConfigureAwait(false); if (result.Value == -1) { return(new InitialResult(result)); } return(new InitialResult(result)); } } return(new InitialResult { Value = 1 }); } }
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); }
// 执行一个通道的测试 async Task HandleTesting(ZClient client, TargetInfo targetInfo, ListViewItem item) { string strError = ""; Random rnd = new Random(); int i = 0; while (cancel.Token.IsCancellationRequested == false) { { // return Value: // -1 出错 // 0 成功 // 1 调用前已经是初始化过的状态,本次没有进行初始化 InitialResult result = await client.TryInitialize(targetInfo, false); if (result.Value == -1) { strError = "Initialize error: " + result.ErrorInfo; this.Invoke( (Action)(() => ListViewUtil.ChangeItemText(item, 2, strError) )); return; } } this.Invoke( (Action)(() => ListViewUtil.ChangeItemText(item, 1, ((i++) + 1).ToString()) )); // 检索 { string strQuery = "\"中国\"/1=4"; SearchResult result = await client.Search(strQuery, targetInfo.DefaultQueryTermEncoding, targetInfo.DbNames, targetInfo.PreferredRecordSyntax, "default"); if (result.Value == -1) { strError = "Search error: " + result.ErrorInfo; this.Invoke( (Action)(() => ListViewUtil.ChangeItemText(item, 2, strError) )); return; } } // 获取 // 切断 if ((i % 10) == 0) { client.CloseConnection(); } await Task.Delay(rnd.Next(1, 500)); } }
// 针对一个特定 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(); } }