Example #1
0
        /// <summary>
        /// Spawn an Expect session.
        /// </summary>
        /// <example>
        /// sess = Expect.Spawn(new ProcessSpawnable("cmd.exe"), new Regex(@"[a-zA-Z]:[^>\n]*?>"));
        /// </example>
        /// <param name="spawnable">Instance of ISpawnable</param>
        /// <param name="default_cmd_regex">Default regular expression for Cmd()</param>
        /// <param name="default_timeout">Default timeout to expect, unit: seconds</param>
        public static Session Spawn(ISpawnable spawnable, Regex default_cmd_regex, float default_timeout = 0f)
        {
            spawnable.Init();
            var sess = new Session(spawnable);

            if (default_timeout > 0)
            {
                sess.Timeout = (int)(default_timeout * 1000);
            }
            sess.DefCmdRegex = default_cmd_regex;
            return(sess);
        }
Example #2
0
        /// <summary>
        /// Restart the spawned process of the session.
        /// </summary>
        /// <remarks>
        /// if the process has already exited, it will start process only.
        /// </remarks>
        /// <returns>true if restart successfully, or false</returns>
        /// <example>
        /// result = Reset();
        /// </example>
        public bool Reset()
        {
            var result = false;

            if (Process.HasExited)
            {
                Logging.WriteLine(String.Format("Session process {0} has already exited at {1} with error code {2}!", Process.StartInfo.FileName, Process.ExitTime.ToString(), Process.ExitCode));
            }
            else
            {
                try
                {
                    Process.Kill();
                }
                catch (Win32Exception wexp)
                {
                    Logging.WriteLine(String.Format("Session process {0} failed to be killed: {1}!", Process.StartInfo.FileName, wexp));
                    return(result);
                }
                catch (NotSupportedException nexp)
                {
                    Logging.WriteLine(String.Format("Session process {0} don't support 'kill': {1}!", Process.StartInfo.FileName, nexp));
                    return(result);
                }
                catch (InvalidOperationException iexp)
                {
                    Logging.WriteLine(String.Format("Session process {0} is not existed or already exited: {1}!", Process.StartInfo.FileName, iexp));
                }
            }
            Process p = new Process();

            p.StartInfo.FileName  = Process.StartInfo.FileName;
            p.StartInfo.Arguments = Process.StartInfo.Arguments;
            _spawnable            = new ProcessSpawnable(p);
            _spawnable.Init();
            Logging.WriteLine(String.Format("Session program {0} has restarted, and clear the previous session's buffer <#{1}#>", Process.StartInfo.FileName, _output_buffer));
            _output_buffer = "";
            result         = true;
            return(result);
        }
Example #3
0
 public static Session Spawn(ISpawnable spawnable, string line_terminator)
 {
     spawnable.Init();
     return(new Session(spawnable, line_terminator));
 }
Example #4
0
 public static Session Spawn(ISpawnable spawnable)
 {
     spawnable.Init();
     return(new Session(spawnable));
 }
Example #5
0
 public static Session Spawn(ISpawnable spawnable, string line_terminator, CancellationToken ct)
 {
     spawnable.Init();
     return(new Session(spawnable, line_terminator, ct));
 }