Пример #1
0
        /// <summary>
        /// This method is the server run loop.
        /// </summary>
        protected void ServerRunLoop()
        {
            TcpListener currentTcpListener = TcpServer;

            try
            {
                // The server will keep running until its thread is aborted.
                while (true)
                {
                    if (!File.Exists(Settings.Default.FrameworkPath + Path.DirectorySeparatorChar + "sif.jar"))
                    {
                        // Sif has not been installed correctly.
                        MessageBox.Show(Resources.tl_Path_missing
                                        +
                                        Settings.Default.FrameworkPath + Path.DirectorySeparatorChar +
                                        "sif.jar\n\n" + Resources.tl_Path_install, Resources.tl_MessageBox_Error);
                    }

                    // Launch a new instance of the Spreadsheet Inspection Framework
                    var startInfo = new ProcessStartInfo("cmd",
                                                         "/q /c java -jar \"" + Settings.Default.FrameworkPath + Path.DirectorySeparatorChar +
                                                         "sif.jar\" "
                                                         + Settings.Default.SifOptions + " " + Instance.Port);
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    Process.Start(startInfo);

                    // Wait for the client to connect.
                    var clientSocket = currentTcpListener.AcceptSocket();

                    #region Functionality

                    while (IsSocketConnected(clientSocket))
                    {
                        // Get the next inspection job from the inspection queue
                        var currentJob = InspectionQueue.Take();

                        try
                        {
                            // At first, send the policy as generated Xml
                            var writer = new StringWriter();
                            currentJob.PolicyXML.Save(writer);
                            clientSocket.SendString(writer.GetStringBuilder().ToString());
                            // Read the report from the socket connection

                            var report = clientSocket.ReadString();

                            // Let the inspection job know about the report
                            currentJob.Finalize(report);
                        }
                        catch (OutOfMemoryException)
                        {
                            // Try to release as many resources as possible#
                            ScanHelper.ScanUnsuccessful(Resources.tl_MemoryRestrictions + "\n" + Resources.tl_StartNewScan);
                            _hadMemoryRestriction = true;
                            currentJob.DeleteWorkbookFile();
                            foreach (InspectionJob job in InspectionQueue)
                            {
                                job.DeleteWorkbookFile();
                            }
                            InspectionQueue.Dispose();
                            InspectionQueue = new BlockingCollection <InspectionJob>();

                            // restart the server loop
                            throw new Exception();
                        }
                        catch (Exception e)
                        {
                            if (!_hadMemoryRestriction)
                            {
                                ScanHelper.ScanUnsuccessful();
                            }
                            Start();
                        }
                    }

                    #endregion
                }
            }
            catch (ExternalException) // Java is not on the path
            {
                if (Globals.Ribbons.Ribbon.scanButton != null && Globals.Ribbons.Ribbon != null &&
                    Globals.Ribbons != null)
                {
                    ScanHelper.ScanUnsuccessful(Resources.tl_No_Java_Enviroment);
                }
                else
                {
                    MessageBox.Show(
                        Resources.tl_No_Java_Enviroment,
                        Resources.tl_MessageBox_Error);
                }
            }
            catch (Exception e)
            {
                try
                {
                    ScanHelper.ScanUnsuccessful();
                }
                //Catch if Ribbon was never instantiated
                catch (NullReferenceException ex)
                {
                    // Quietly swallow exception
                }
                // start will fork into a new thread
                Start();
                // we will die in a short while, nothing to be done
            }
            finally
            {
                currentTcpListener.Stop();
            }
        }
Пример #2
0
        /// <summary>
        /// This method is the server run loop.
        /// </summary>
        protected void ServerRunLoop()
        {
            TcpListener currentTcpListener = TcpServer;

            try
            {
                // The server will keep running until its thread is aborted.
                while (true)
                {
                    if (!File.Exists(Settings.Default.FrameworkPath + Path.DirectorySeparatorChar + "sif.jar"))
                    {
                        // Sif has not been installed correctly.
                        MessageBox.Show(
                            "The Spreadsheet Inspection Framework was not found at this location:\n" +
                            Settings.Default.FrameworkPath + Path.DirectorySeparatorChar +
                            "sif.jar\n\nPlease install the Spreadsheet Inspection Framework and restart Excel.", "Error");
                    }

                    // Launch a new instance of the Spreadsheet Inspection Framework
                    var startInfo = new ProcessStartInfo("java",
                                                         "-jar \"" + Settings.Default.FrameworkPath + Path.DirectorySeparatorChar + "sif.jar\" "
                                                         + Settings.Default.SifOptions + " " + InspectionEngine.Instance.Port);
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    var process = Process.Start(startInfo);

                    // Wait for the client to connect.
                    var clientSocket = currentTcpListener.AcceptSocket();

                    #region Functionality

                    while (IsSocketConnected(clientSocket))
                    {
                        // Get the next inspection job from the inspection queue
                        var currentJob = this.InspectionQueue.Take();

                        try
                        {
                            // At first, send the policy as generated Xml
                            var writer = new StringWriter();
                            currentJob.PolicyXML.Save(writer);
                            clientSocket.SendString(writer.GetStringBuilder().ToString());

                            // Read the report from the socket connection
                            var report = clientSocket.ReadString();

                            // Let the inspection job know about the report
                            currentJob.Finalize(report);
                        }
                        catch (OutOfMemoryException)
                        {
                            // Try to release as many resources as possible
                            MessageBox.Show(
                                "The inspection queue has been cleared due to memory restrictions.\nPlease restart any needed scans.");
                            currentJob.DeleteWorkbookFile();
                            foreach (InspectionJob job in InspectionQueue)
                            {
                                job.DeleteWorkbookFile();
                            }
                            InspectionQueue.Dispose();
                            InspectionQueue = new BlockingCollection <InspectionJob>();
                            // restart the server loop
                            throw new Exception();
                        }
                        catch (Exception e)
                        {
                            // Put the job in the queue again
                            MessageBox.Show("The test of the current document failed!\n" + e.ToString(), "Error");
                        }
                    }

                    #endregion
                }
            }
            catch (ExternalException) // Java is not on the path
            {
                MessageBox.Show(
                    "No java runtime was found!\n" +
                    "Please ensure a java runtime environmet >= 1.7 is installed and accesible through the PATH variable.\n" +
                    "The Scan button will be without function.",
                    "Error");
            }
            catch (Exception)
            {
                // start will fork into a new thread
                Start();
                // we will die in a short while, nothing to be done
            }
            finally
            {
                currentTcpListener.Stop();
            }
        }