126 lines
5.2 KiB
C#
126 lines
5.2 KiB
C#
using System;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace TBF.BenchControl.Network.Telnet
|
|
{
|
|
/// <summary>
|
|
/// One part of the telnet client command, which consists of a pair CmdAction action and string str.
|
|
/// </summary>
|
|
public enum CmdAction
|
|
{
|
|
VOID = 0, /// Blank (uninitialized) action. 'str' is not used.
|
|
CONNECT, /// Connect (=open a TCP socket and log in). 'str'=hostname. Notify by 'Connected' event.
|
|
SET_PROMPT, /// Set a prompt string used in comparisons when executing ..._WAIT4PROMPT. 'str'=prompt.
|
|
CLEAR_RESPONSE, /// Clear received response buffer. 'str' is not used. Note: Response buffer is cleared also after a detected prompt.
|
|
SEND_STRING, /// Send a string and do nothing else. 'str'=string to be sent.
|
|
SEND_MSRMNT_CMD, /// Send a string and extract measurements from received data (filter received data)
|
|
SEND_COMMAND, /// Send a string and WAIT for a PROMPT. 'str'=string to be sent. Notify by 'PromptReceived' event.
|
|
REBOOT_RECONNECT, /// Send 'reboot' command (if connected), wait a while and reconect. 'str' is not used. Notify by 'Connected' event.
|
|
EXIT_RECONNECT, /// Send 'exit' (terminate telnet session) and reconect. 'str' is not used. Notify using 'Connected' event.
|
|
DISCONNECT, /// Send 'exit' to disconnect, do not reconnect. 'str' is not used.
|
|
TERMINATE, /// Terminate worker thread (should be done once before TelnetClient destruction. 'str' is not used.
|
|
LABEL, /// Label marking the start of item commands, 'str' identifies the item.
|
|
NOTIFICATION, /// Notify by 'Notification' event when this command is processed. 'str'=notification ID-string
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Telnet client command. Consists of a pair 'action', 'str'
|
|
/// CmdAction 'action' specifies the action to be taken.
|
|
/// string 'str' is (in most cases) the string to be sent, however it might be a hostname or a prompt.
|
|
/// </summary>
|
|
public struct Command
|
|
{
|
|
/// Private fields
|
|
CmdAction action; /// Telnet client action to be taken, e.g. 'send a command'
|
|
string str; /// String argument, e.g. a Linux command to be sent.
|
|
int duration; /// Expected duration of the command in [s], 0 = immediate, -1 = unknown
|
|
int timeout; /// Timeout period for the command in [s], 0 = no timeout
|
|
Regex fatalRegex; /// A compiled regular expression for a fatal error, or null (null=no fatal error detection)
|
|
Regex errorRegex; /// A compiled regular expression for an error, or null (null=no error detection)
|
|
Regex warningRegex; /// A compiled regular expression for a warning, or null (null=no warning detection)
|
|
Regex successRegex; /// A compiled regular expression for a success, or null (null=no success detection)
|
|
|
|
/// Public read only properties
|
|
public CmdAction Action { get { return action; } }
|
|
public string Str { get { return str; } }
|
|
public int Duration { get { return duration; } }
|
|
public int Timeout { get { return timeout; } }
|
|
public Regex FatalRegex { get { return fatalRegex; } }
|
|
public Regex ErrorRegex { get { return errorRegex; } }
|
|
public Regex WarningRegex { get { return warningRegex; } }
|
|
public Regex SuccessRegex { get { return successRegex; } }
|
|
|
|
///
|
|
/// Various constructors:
|
|
/// - duration and timeout are in seconds
|
|
/// - if timeout and duration is not supplied, timeout=0, duration=0 (timeout switched off)
|
|
/// - if string is not supplied, null is used
|
|
///
|
|
public Command(CmdAction action, string str, int duration, int timeout,
|
|
Regex fatalRegex, Regex errorRegex, Regex warningRegex, Regex successRegex)
|
|
{
|
|
this.action = action;
|
|
this.str = str;
|
|
this.duration = duration;
|
|
this.timeout = timeout;
|
|
this.fatalRegex = fatalRegex;
|
|
this.errorRegex = errorRegex;
|
|
this.warningRegex = warningRegex;
|
|
this.successRegex = successRegex;
|
|
}
|
|
|
|
public Command(CmdAction action, string str, int duration, int timeout)
|
|
: this(action, str, duration, timeout, null, null, null, null)
|
|
{
|
|
}
|
|
|
|
public Command(CmdAction action, string str)
|
|
: this(action, str, 0, 0, null, null, null, null)
|
|
{
|
|
}
|
|
|
|
public Command(CmdAction action, int duration, int timeout)
|
|
: this(action, null, duration, timeout, null, null, null, null)
|
|
{
|
|
}
|
|
|
|
public Command(CmdAction action)
|
|
: this(action, null, 0, 0, null, null, null, null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// For debugging purposes.
|
|
/// </summary>
|
|
/// <returns>A name of Command.action</returns>
|
|
public override string ToString()
|
|
{
|
|
string result = action.ToString();
|
|
|
|
if (str != null)
|
|
{
|
|
result += ", \"" + str + "\"";
|
|
}
|
|
|
|
if (action == CmdAction.CONNECT ||
|
|
action == CmdAction.REBOOT_RECONNECT ||
|
|
action == CmdAction.EXIT_RECONNECT ||
|
|
action == CmdAction.SEND_COMMAND)
|
|
{
|
|
result += ", timeout=" + timeout.ToString() + "s";
|
|
}
|
|
|
|
if (action == CmdAction.SEND_COMMAND)
|
|
{
|
|
if (fatalRegex != null) { result += ", fatal=" + fatalRegex.ToString(); }
|
|
if (errorRegex != null) { result += ", error=" + errorRegex.ToString(); }
|
|
if (warningRegex != null) { result += ", warning=" + warningRegex.ToString(); }
|
|
if (successRegex != null) { result += ", success=" + successRegex.ToString(); }
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|