Exemplo n.º 1
0
        public static Device NewSearchResponse()
        {
            var idInt = Interlocked.Increment(ref idSeq);
            var id    = "0x0000000011748cfe".Replace("11748", idInt.ToString());

            return(Device.Parse(SEARCH_RESPONSE.Replace("0x0000000011748cfe", id)));
        }
Exemplo n.º 2
0
        // 获得记录
        // 本函数每次调用前,最好调用一次 TryInitialize()
        // 不确保一定可以获得nCount个
        // parameters:
        //		nStart	获取记录的开始位置(从0开始计数)
        public async Task <PresentResult> OncePresent(
            string strResultSetName,
            int nStart,
            int nCount,
            string strElementSetName,
            string strPreferredRecordSyntax)
        {
            if (nCount == 0)
            {
                return new PresentResult {
                           Value = 0, ErrorInfo = "nCount 参数为 0,本次没有真正请求服务器获取记录"
                }
            }
            ;

            BerTree         tree = new BerTree();
            PRESENT_REQUEST struPresent_request = new PRESENT_REQUEST();

            //byte[] baPackage = null;
            //int nRet;

            struPresent_request.m_strReferenceId            = this._currentRefID;
            struPresent_request.m_strResultSetName          = strResultSetName; // "default";
            struPresent_request.m_lResultSetStartPoint      = nStart + 1;
            struPresent_request.m_lNumberOfRecordsRequested = nCount;
            struPresent_request.m_strElementSetNames        = strElementSetName;
            struPresent_request.m_strPreferredRecordSyntax  = strPreferredRecordSyntax;

            int nRet = tree.PresentRequest(struPresent_request,
                                           out byte[] baPackage);

            if (nRet == -1)
            {
                return new PresentResult {
                           Value = -1, ErrorInfo = "CBERTree::PresentRequest() fail!"
                }
            }
            ;
            if (this._channel.Connected == false)
            {
                this.CloseConnection();

                return(new PresentResult {
                    Value = -1, ErrorInfo = "socket尚未连接或者已经被关闭"
                });
            }

#if DUMPTOFILE
            DeleteFile("presentrequest.bin");
            DumpPackage("presentrequest.bin",
                        (char *)baPackage.GetData(),
                        baPackage.GetSize());
            DeleteFile("presentrequest.txt");
            tree.m_RootNode.DumpToFile("presentrequest.txt");
#endif

            BerTree tree1 = new BerTree();

            {
                RecvResult result = await this._channel.SendAndRecv(
                    baPackage);

                if (result.Value == -1)
                {
                    return(new PresentResult(result));
                }

#if DUMPTOFILE
                DeleteFile("presendresponse.bin");
                DumpPackage("presentresponse.bin",
                            (char *)baPackage.GetData(),
                            baPackage.GetSize());
#endif


                tree1.m_RootNode.BuildPartTree(result.Package,
                                               0,
                                               result.Package.Length,
                                               out int nTotalLen);
            }

#if DUMPTOFILE
            DeleteFile("PresentResponse.txt");
            tree1.m_RootNode.DumpDebugInfoToFile("PresentResponse.txt");
#endif
            SEARCH_RESPONSE search_response = new SEARCH_RESPONSE();
            nRet = BerTree.GetInfo_PresentResponse(tree1.GetAPDuRoot(),
                                                   ref search_response,
                                                   out RecordCollection records,
                                                   true,
                                                   out string strError);
            if (nRet == -1)
            {
                return new PresentResult {
                           Value = -1, ErrorInfo = strError
                }
            }
            ;

            SetElementSetName(records, strElementSetName);

            if (search_response.m_diagRecords.Count != 0)
            {
                return new PresentResult {
                           Value = -1, ErrorInfo = "error diagRecords:\r\n\r\n---\r\n" + search_response.m_diagRecords.GetMessage()
                }
            }
            ;

            return(new PresentResult {
                Records = records
            });
        }
Exemplo n.º 3
0
        // 检索
        // 本函数每次调用前,最好调用一次 TryInitialize()
        // parameters:
        //      strQuery    Search() 专用的检索式。注意,不是一个检索词那么简单
        //      dbnames     一般是从 targetInfo.DbNames 里面获得。或者从中选择一个数据库名用在这里
        //      strPreferredRecordSyntax 一般是从 targetInfo.PreferredRecordSyntax 获得即可
        // result.Value:
        //		-1	error
        //		0	fail
        //		1	succeed
        // result.ResultCount:
        //      命中结果集内记录条数 (当 result.Value 为 1 时)
        public async Task <SearchResult> Search(
            string strQuery,
            Encoding queryTermEncoding,
            string[] dbnames,
            string strPreferredRecordSyntax,
            string strResultSetName)
        {
            BerTree        tree = new BerTree();
            SEARCH_REQUEST struSearch_request = new SEARCH_REQUEST
            {
                m_dbnames = dbnames
            };

            Debug.Assert(struSearch_request.m_dbnames.Length != 0, "");

            struSearch_request.m_strReferenceId              = this._currentRefID;
            struSearch_request.m_lSmallSetUpperBound         = 0;
            struSearch_request.m_lLargeSetLowerBound         = 1;
            struSearch_request.m_lMediumSetPresentNumber     = 0;
            struSearch_request.m_nReplaceIndicator           = 1;
            struSearch_request.m_strResultSetName            = strResultSetName; // "default";
            struSearch_request.m_strSmallSetElementSetNames  = "";
            struSearch_request.m_strMediumSetElementSetNames = "";
            struSearch_request.m_strPreferredRecordSyntax    = strPreferredRecordSyntax; //  ZTargetControl.GetLeftValue(this.TargetInfo.PreferredRecordSyntax);   // BerTree.MARC_SYNTAX;
            struSearch_request.m_strQuery          = strQuery;
            struSearch_request.m_nQuery_type       = 1;
            struSearch_request.m_queryTermEncoding = queryTermEncoding;


            // m_search_response.m_lErrorCode = 0;
            byte[] baPackage = null;

            try
            {
                // 这里可能抛出异常
                tree.SearchRequest(struSearch_request,
                                   out baPackage);
            }
            catch (Exception ex)
            {
                return(new SearchResult {
                    Value = -1, ErrorInfo = "CBERTree::SearchRequest() Exception: " + ex.Message
                });
            }

            if (this._channel.Connected == false)
            {
                this.CloseConnection();
                return(new SearchResult {
                    Value = -1, ErrorInfo = "socket尚未连接或者已经被关闭"
                });
            }

#if DUMPTOFILE
            string strBinFile = this.MainForm.DataDir + "\\searchrequest.bin";
            File.Delete(strBinFile);
            DumpPackage(strBinFile,
                        baPackage);
            string strLogFile = this.MainForm.DataDir + "\\searchrequest.txt";
            File.Delete(strLogFile);
            tree.m_RootNode.DumpToFile(strLogFile);
#endif


#if NO
            nRet = CheckConnect(
                out strError);
            if (nRet == -1)
            {
                return(-1);
            }
#endif

            BerTree tree1 = new BerTree();

            {
                RecvResult result = await this._channel.SendAndRecv(
                    baPackage);

                if (result.Value == -1)
                {
                    return(new SearchResult(result));
                }

#if NO
#if DEBUG
                if (nRet == 0)
                {
                    Debug.Assert(strError == "", "");
                }
#endif
#endif

#if DUPMTOFILE
                DeleteFile("searchresponse.bin");
                DumpPackage("searchresponse.bin",
                            (char *)baOutPackage.GetData(),
                            baOutPackage.GetSize());
#endif

                tree1.m_RootNode.BuildPartTree(result.Package,
                                               0,
                                               result.Package.Length,
                                               out int nTotalLen);
            }

            SEARCH_RESPONSE search_response = new SEARCH_RESPONSE();
            int             nRet            = BerTree.GetInfo_SearchResponse(tree1.GetAPDuRoot(),
                                                                             ref search_response,
                                                                             true,
                                                                             out string strError);
            if (nRet == -1)
            {
                return new SearchResult {
                           Value = -1, ErrorInfo = strError
                }
            }
            ;

#if DUMPTOFILE
            DeleteFile("SearchResponse.txt");
            tree1.m_RootNode.DumpDebugInfoToFile("SearchResponse.txt");
#endif
            {
                SearchResult result = new SearchResult();

                result.ResultCount = (int)search_response.m_lResultCount;

                if (search_response.m_nSearchStatus != 0)   // 不一定是1
                {
                    result.Value = 1;
                    return(result);
                }

                result.ErrorInfo = "Search Fail: diagRecords:\r\n" + search_response.m_diagRecords.GetMessage();
                result.Value     = 0; // search fail
                return(result);
            }
        }
Exemplo n.º 4
0
        // 解释包
        void menuItem_explainContent_Click(object sender,
                                           EventArgs e)
        {
            TreeNode node = this.treeView_ber.SelectedNode;

            if (node == null)
            {
                MessageBox.Show(this, "尚未选定节点");
                return;
            }

            BerNode bernode = (BerNode)node.Tag;

            if (TreeNodeLevel(node) != 1)
            {
                MessageBox.Show(this, "必须是Ber包根节点");
                return;
            }

            BerNode root         = bernode;
            int     nRet         = 0;
            string  strError     = "";
            string  strDebugInfo = "OK";

            if (root.m_uTag == BerTree.z3950_initRequest)
            {
                // 观察Initial请求包
                nRet = BerTree.GetInfo_InitRequest(
                    root,
                    out strDebugInfo,
                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_searchRequest)
            {
                nRet = BerTree.GetInfo_SearchRequest(
                    root,
                    out strDebugInfo,
                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_presentRequest)
            {
                nRet = BerTree.GetInfo_PresentRequest(
                    root,
                    out strDebugInfo,
                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_initResponse)
            {
                nRet = BerTree.GetInfo_InitResponse(root,
                                                    out strDebugInfo,
                                                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_searchResponse)
            {
                nRet = BerTree.GetInfo_SearchResponse(root,
                                                      out strDebugInfo,
                                                      out strError);
            }
            else if (root.m_uTag == BerTree.z3950_presentResponse)
            {
                RecordCollection records         = null;
                SEARCH_RESPONSE  search_response = new SEARCH_RESPONSE();
                nRet = BerTree.GetInfo_PresentResponse(root,
                                                       ref search_response,
                                                       out records,
                                                       true,
                                                       out strError);
            }

            if (nRet == -1)
            {
                MessageBox.Show(this, strError);
            }
            else
            {
                MessageBox.Show(this, strDebugInfo);
            }
        }
Exemplo n.º 5
0
        // 解释包
        void menuItem_explainContent_Click(object sender,
EventArgs e)
        {
            TreeNode node = this.treeView_ber.SelectedNode;

            if (node == null)
            {
                MessageBox.Show(this, "尚未选定节点");
                return;
            }

            BerNode bernode = (BerNode)node.Tag;

            if (TreeNodeLevel(node) != 1)
            {
                MessageBox.Show(this, "必须是Ber包根节点");
                return;
            }

            BerNode root = bernode;
            int nRet = 0;
            string strError = "";
            string strDebugInfo = "OK";

            if (root.m_uTag == BerTree.z3950_initRequest)
            {
        // 观察Initial请求包
                nRet = BerTree.GetInfo_InitRequest(
                    root,
                    out strDebugInfo,
                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_searchRequest)
            {
                nRet = BerTree.GetInfo_SearchRequest(
                    root,
                    out strDebugInfo,
                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_presentRequest)
            {
                nRet = BerTree.GetInfo_PresentRequest(
                    root,
                    out strDebugInfo,
                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_initResponse)
            {
                nRet = BerTree.GetInfo_InitResponse(root,
                    out strDebugInfo,
                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_searchResponse)
            {
                nRet = BerTree.GetInfo_SearchResponse(root,
                    out strDebugInfo,
                    out strError);
            }
            else if (root.m_uTag == BerTree.z3950_presentResponse)
            {
                RecordCollection records = null;
                SEARCH_RESPONSE search_response = new SEARCH_RESPONSE();
                nRet = BerTree.GetInfo_PresentResponse(root,
                                       ref search_response,
                                       out records,
                                       true,
                                       out strError);
            }

            if (nRet == -1)
                MessageBox.Show(this, strError);
            else
                MessageBox.Show(this, strDebugInfo);
        }
Exemplo n.º 6
0
        // 获得记录
        // 不确保一定可以获得nCount个
        // parameters:
        //		nStart	开始记录(从0计算)
        int DoOncePresent(
            ZConnection connection,
            string strResultSetName,
            int nStart,
            int nCount,
            string strElementSetName,
            string strPreferredRecordSyntax,
            out RecordCollection records,
            out string strError)
        {
            records = null;
            strError = "";

            if (nCount == 0)
            {
                strError = "nCount为0";
                return 0;
            }


            BerTree tree = new BerTree();
            PRESENT_REQUEST struPresent_request = new PRESENT_REQUEST();
            byte[] baPackage = null;
            int nRet;
            int nRecvLen;

            // -->
            BerTree tree1 = new BerTree();
            int nTotlen = 0;

            struPresent_request.m_strReferenceId = this.CurrentRefID;
            struPresent_request.m_strResultSetName = strResultSetName; // "default";
            struPresent_request.m_lResultSetStartPoint = nStart + 1;
            struPresent_request.m_lNumberOfRecordsRequested = nCount;
            struPresent_request.m_strElementSetNames = strElementSetName;
            struPresent_request.m_strPreferredRecordSyntax = strPreferredRecordSyntax;

            nRet = tree.PresentRequest(struPresent_request,
                                     out baPackage);
            if (nRet == -1)
            {
                strError = "CBERTree::PresentRequest() fail!";
                return -1;
            }


#if DUMPTOFILE
	DeleteFile("presentrequest.bin");
	DumpPackage("presentrequest.bin",
		(char *)baPackage.GetData(),
		baPackage.GetSize());
	DeleteFile ("presentrequest.txt");
	tree.m_RootNode.DumpToFile("presentrequest.txt");
#endif

            nRet = CheckConnect(
                connection,
                out strError);
            if (nRet == -1)
                return -1;

            /*
            nRet = this.ZChannel.SendTcpPackage(
                baPackage,
                baPackage.Length,
                out strError);
            if (nRet == -1 || nRet == 1)
            {
                // CloseZAssociation();
                return -1;
            }

            //////////////


            baPackage = null;
            nRet = this.ZChannel.RecvTcpPackage(
                        out baPackage,
                        out nRecvLen,
                        out strError);
            if (nRet == -1)
            {
                // CloseZAssociation();
                goto ERROR1;
            }
             * */

            byte [] baOutPackage = null;
            nRet = connection.ZChannel.SendAndRecv(
                baPackage,
                out baOutPackage,
                out nRecvLen,
                out strError);
            if (nRet == -1)
                return -1;

#if DUMPTOFILE	
	DeleteFile("presendresponse.bin");
	DumpPackage("presentresponse.bin",
				(char *)baPackage.GetData(),
				baPackage.GetSize());
#endif


            tree1.m_RootNode.BuildPartTree(baOutPackage,
                0,
                baOutPackage.Length,
                out nTotlen);

#if DUMPTOFILE
	DeleteFile("PresentResponse.txt"); 
	tree1.m_RootNode.DumpDebugInfoToFile("PresentResponse.txt");
#endif

            SEARCH_RESPONSE search_response = new SEARCH_RESPONSE();
            nRet = BerTree.GetInfo_PresentResponse(tree1.GetAPDuRoot(),
                                   ref search_response,
                                   out records,
                                   true,
                                   out strError);
            if (nRet == -1)
                goto ERROR1;

            /*
            nRet = FitDebugInfo_PresentResponse(&tree1,
                                      strError);
            if (nRet == -1) {
                goto ERROR1;
            }

            DeleteFile("PresentResponse.txt"); 
            tree1.m_RootNode.DumpDebugInfoToFile("PresentResponse.txt");
            */


            if (search_response.m_diagRecords.Count != 0)
            {
                /*
                string strDiagText;
                string strAddInfo;

                nRet = GetDiagTextByNumber("bib1diag.txt",
                                m_search_response.m_nDiagCondition,
                                strDiagText,
                                strAddInfo,
                                strError);
                if (nRet == -1) {
                    if (this->m_bAllowMessageBox)
                        AfxMessageBox(strError);
                    return -1;
                }
                if (strDiagText.GetLength())
                    strError = strDiagText;
                else
                    strError.Format("diag condition[%d] diag set id[%s]",
                    m_search_response.m_nDiagCondition,
                    m_search_response.m_strDiagSetID);
                 * */
                strError = "error diagRecords:\r\n\r\n---\r\n" + search_response.m_diagRecords.GetMessage();
                return -1;
            }

            return 0;
        ERROR1:
            return -1;
        }
Exemplo n.º 7
0
        // return:
        //		-1	error
        //		0	fail
        //		1	succeed
        int DoSearch(
            ZConnection connection,
            string strQuery,
            Encoding queryTermEncoding,
            string[] dbnames,
            string strResultSetName,
            out int nResultCount,
            out string strError)
        {
            strError = "";

            BerTree tree = new BerTree();
            SEARCH_REQUEST struSearch_request = new SEARCH_REQUEST();
            byte[] baPackage = null;
            int nRet;
            int nRecvLen;
            //int nMax;
            //int i;



            // -->
            BerTree tree1 = new BerTree();
            int nTotlen = 0;


            nResultCount = 0;

            struSearch_request.m_dbnames = dbnames;

            Debug.Assert(struSearch_request.m_dbnames.Length != 0, "");

            struSearch_request.m_strReferenceId = this.CurrentRefID;
            struSearch_request.m_lSmallSetUpperBound = 0;
            struSearch_request.m_lLargeSetLowerBound = 1;
            struSearch_request.m_lMediumSetPresentNumber = 0;
            struSearch_request.m_nReplaceIndicator = 1;
            struSearch_request.m_strResultSetName = strResultSetName;   // "default";
            struSearch_request.m_strSmallSetElementSetNames = "";
            struSearch_request.m_strMediumSetElementSetNames = "";
            struSearch_request.m_strPreferredRecordSyntax = ZTargetControl.GetLeftValue(this.comboBox_recordSyntax.Text);    //  this.CurrentTargetInfo.PreferredRecordSyntax;   // BerTree.MARC_SYNTAX;
            struSearch_request.m_strQuery = strQuery;
            struSearch_request.m_nQuery_type = 1;
            struSearch_request.m_queryTermEncoding = queryTermEncoding;
           

            // m_search_response.m_lErrorCode = 0;

            nRet = tree.SearchRequest(struSearch_request,
                out baPackage);

            if (nRet == -1)
            {
                strError = "CBERTree::SearchRequest() fail!";
                return -1;
            }
#if NOTCPIP
	if (m_hSocket == INVALID_SOCKET) {
		strError = "socket已经关闭!";
		return -1;
	}
#endif


            #if DUMPTOFILE
            string strBinFile = this.MainForm.DataDir + "\\searchrequest.bin";
            File.Delete(strBinFile);
            DumpPackage(strBinFile,
                baPackage);
            string strLogFile = this.MainForm.DataDir + "\\searchrequest.txt";
            File.Delete(strLogFile);
            tree.m_RootNode.DumpToFile(strLogFile);
            #endif



            nRet = CheckConnect(
                connection,
                out strError);
            if (nRet == -1)
                return -1;

            /*
            nRet = this.ZChannel.SendTcpPackage(
                baPackage,
                baPackage.Length,
                out strError);
            if (nRet == -1 || nRet == 1)
            {
                // CloseZAssociation();
                return -1;
            }
            //AfxMessageBox("发送成功");


            baPackage = null;
            nRet = this.ZChannel.RecvTcpPackage(
                        out baPackage,
                        out nRecvLen,
                        out strError);
            if (nRet == -1)
            {
                // CloseZAssociation();
                return -1;
            }
             * */

            byte [] baOutPackage = null;
            nRet = connection.ZChannel.SendAndRecv(
                baPackage,
                out baOutPackage,
                out nRecvLen,
                out strError);
            if (nRet == -1)
                return -1;

#if DEBUG
            if (nRet == 0)
            {
                Debug.Assert(strError == "", "");
            }
#endif

#if DUPMTOFILE
	DeleteFile("searchresponse.bin");
	DumpPackage("searchresponse.bin",
				(char *)baOutPackage.GetData(),
				baOutPackage.GetSize());
#endif

            tree1.m_RootNode.BuildPartTree(baOutPackage,
                0,
                baOutPackage.Length,
                out nTotlen);

            SEARCH_RESPONSE search_response = new SEARCH_RESPONSE();
            nRet = BerTree.GetInfo_SearchResponse(tree1.GetAPDuRoot(),
                                   ref search_response,
                                   true,
                                   out strError);
            if (nRet == -1)
                return -1;

#if DUMPTOFILE
	DeleteFile("SearchResponse.txt"); 
	tree1.m_RootNode.DumpDebugInfoToFile("SearchResponse.txt");
#endif
            /*
	nRet = FitDebugInfo_SearchResponse(&tree1,
							  strError);
	if (nRet == -1) {
		AfxMessageBox(strError);
		return;
	}
	*/
            nResultCount = (int)search_response.m_lResultCount;

            if (search_response.m_nSearchStatus != 0)	// 不一定是1
                return 1;

            strError = "Search Fail: diagRecords:\r\n" + search_response.m_diagRecords.GetMessage();
            return 0;	// search fail
        }