// saves and closes XML document
        private void SaveAndCloseDocument(MSXML2.IXMLDOMElement responseElem, MSXML2.IXMLDOMDocument document)
        {
            document.async = false;
            // create Processing Instruction for XML documdnt
            MSXML2.IXMLDOMProcessingInstruction procsInstruct = document.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
            // create primary element
            MSXML2.IXMLDOMElement elem = document.createElement("p2p_lng");

            // add response object passed to the primary element as child node
            elem.appendChild(responseElem);
            // add Processing Instruction object and primary object to document
            // object passed as child nodes and save the document
            document.appendChild(procsInstruct);
            document.appendChild(elem);
            document.save(ResponsPath);
        }
        // writes request XML according to the parameter passed
        public void WriteRequest(string type, string searchValue, string mask)
        {
            // create a document object
            MSXML2.IXMLDOMDocument document = CreateDocument();

            // create request element
            MSXML2.IXMLDOMElement requestElem = document.createElement("request");

            // set attributes of request element
            requestElem.setAttribute("type", type);

            // if one of these kinds of request is to be made
            // specify the filename and pertaining info too.
            if (type.CompareTo("SHOWFILES") != 0)
            {
                string ReqType = "";
                if (type.CompareTo("CHAT") == 0)
                {
                    ReqType = "message";
                }
                else
                {
                    ReqType = "scope";
                }

                MSXML2.IXMLDOMElement file_infoElem = document.createElement(ReqType.ToString());

                if (type.CompareTo("CHAT") == 0)
                {
                    file_infoElem.setAttribute("sendername", searchValue.Substring(0, searchValue.IndexOf("(")));
                    file_infoElem.setAttribute("senderIP", searchValue.Substring(searchValue.IndexOf("(") + 1).Substring(0, searchValue.Substring(searchValue.IndexOf("(") + 1).Length - 1));
                    file_infoElem.setAttribute("chatmsg", mask);
                }
                else
                {
                    file_infoElem.setAttribute("type", searchValue);
                    file_infoElem.setAttribute("mask", mask);
                }
                requestElem.appendChild(file_infoElem);
            }

            // close and save the documdent
            SaveAndCloseDocument(requestElem, document);
        }
        // writes error XML responses
        public void WriteErrorResponse(string error)
        {
            // create a document object
            MSXML2.IXMLDOMDocument document = CreateDocument();
            // create response and error info elements
            MSXML2.IXMLDOMElement responseElem  = document.createElement("response");
            MSXML2.IXMLDOMElement errorInfoElem = document.createElement("errorinfo");

            // set attribute of response element
            responseElem.setAttribute("type", "ERROR");
            // set attribute of errorinfo element
            errorInfoElem.setAttribute("errorcode", "1");
            errorInfoElem.setAttribute("severity", "Error");
            errorInfoElem.setAttribute("description", error);
            // add errorinfo element to response object as a child
            responseElem.appendChild(errorInfoElem);
            // save the document
            SaveAndCloseDocument(responseElem, document);
        }
        private void WriteShowfileResponse(string reqType)
        {
            // create a document object
            MSXML2.IXMLDOMDocument document = CreateDocument();
            // create response and error info elements
            MSXML2.IXMLDOMElement responseElem = document.createElement("response");
            // set attribute of response element
            responseElem.setAttribute("type", reqType);

            // open share.ini for reading
            StreamReader readfile = new StreamReader(SharedResourceInfoPath);
            string       readData;

            // read entire file
            while ((readData = readfile.ReadLine()) != null)
            {
                try
                {
                    // for each entry in share .ini create a fileinfo element
                    // and fill it with required information
                    MSXML2.IXMLDOMElement file_infoElem = document.createElement("fileinfo");
                    int index = readData.IndexOf("=", 0);
                    file_infoElem.setAttribute("filename", readData.Substring(0, index));
                    file_infoElem.setAttribute("mask", readData.Substring(index + 1, 1));

                    int secindex = -1;
                    if (-1 != (secindex = readData.IndexOf("=", index + 1)))
                    {
                        file_infoElem.setAttribute("filesize", readData.Substring(secindex + 1));
                    }

                    // add this element to response element as child
                    responseElem.appendChild(file_infoElem);
                }
                catch (Exception e)      { MessageBox.Show("Problem faced while responding : " + e.Message); }
            }
            // close and save the documdent
            SaveAndCloseDocument(responseElem, document);
        }
Пример #5
0
 /// <summary>
 /// Initialize some important variables
 /// </summary>
 protected void InitVariables()
 {
     iTags    = 0;
     iCounter = 0;
     document = new MSXML2.DOMDocument();
 }
        // responds for search requests
        private void WriteSearchResponse(MSXML2.IXMLDOMNode node)
        {
            try
            {
                MSXML2.IXMLDOMNode         scope      = node.firstChild;
                MSXML2.IXMLDOMNamedNodeMap nodemap    = scope.attributes;
                MSXML2.IXMLDOMNode         childNode  = nodemap.nextNode();
                MSXML2.IXMLDOMNode         childNode2 = nodemap.nextNode();
                string scopeVal = childNode.nodeValue.ToString();
                string maskVal  = childNode2.nodeValue.ToString();

                // make sure that search request has criteria specified in it
                if (0 != scope.nodeName.CompareTo("scope"))
                {
                    return;
                }

                // validated that directory's existing
                if (!Directory.Exists(scopeVal.Substring(0, scopeVal.LastIndexOf("\\") + 1)))
                {
                    throw new Exception("Directory does not exists any more");
                }

                MSXML2.IXMLDOMDocument document     = CreateDocument();
                MSXML2.IXMLDOMElement  responseElem = document.createElement("response");
                responseElem.setAttribute("type", "SHOWFILES");

                int i = 0;
                // get files in the specified directory satisfying the
                // given criteria
                string[] files = Directory.GetFiles(scopeVal.Substring(0, scopeVal.LastIndexOf("\\") + 1), scopeVal.Substring(scopeVal.LastIndexOf("\\") + 1));
                files.Initialize();

                while (i < files.Length)
                {
                    // make fileinfo elements and fill then up with
                    // required
                    MSXML2.IXMLDOMElement file_infoElem = document.createElement("fileinfo");
                    file_infoElem.setAttribute("filename", files[i]);
                    file_infoElem.setAttribute("mask", maskVal);
                    file_infoElem.setAttribute("filesize", Convert.ToString(new FileInfo(files[i]).Length));
                    ++i;
                    // add them to response element as children;
                    responseElem.appendChild(file_infoElem);
                }

                // get files in the specified directory satisfying the
                // given criteria
                string[] dirs = Directory.GetDirectories(scopeVal.Substring(0, scopeVal.LastIndexOf("\\") + 1), scopeVal.Substring(scopeVal.LastIndexOf("\\") + 1));
                dirs.Initialize();

                i = 0;
                while (i < dirs.Length)
                {
                    // make fileinfo elements and fill then up with
                    // required
                    MSXML2.IXMLDOMElement file_infoElem = document.createElement("fileinfo");
                    file_infoElem.setAttribute("filename", dirs[i] + "\\");
                    file_infoElem.setAttribute("mask", maskVal);
                    ++i;

                    // add them to response element as children;
                    responseElem.appendChild(file_infoElem);
                }
                // close and save the document
                SaveAndCloseDocument(responseElem, document);
            }
            catch (Exception e) { WriteErrorResponse(e.Message); }
        }
 static extern int Get(MSXML2.IXMLDOMDocument statusUpdateXML, String IPAddress);