17 using System.Collections.Generic;
33 public sealed
class AcSession : IFormattable, IEquatable<AcSession>, IComparable<AcSession>, IComparable
35 #region class variables
46 #region Equality comparison
57 if (ReferenceEquals(other, null))
return false;
58 if (ReferenceEquals(
this, other))
return true;
60 var right = Tuple.Create(other.
Name, other.
Host);
61 return left.Equals(right);
68 public override bool Equals(
object other)
70 if (ReferenceEquals(other, null))
return false;
71 if (ReferenceEquals(
this, other))
return true;
72 if (GetType() != other.GetType())
return false;
83 return hash.GetHashCode();
88 #region Order comparison
101 if (
AcSession.ReferenceEquals(
this, other))
107 result =
Name.CompareTo(other.
Name);
120 int IComparable.CompareTo(
object other)
123 throw new ArgumentException(
"Argument is not an AcSession",
"other");
135 get {
return _name ?? String.Empty; }
136 internal set { _name = value; }
144 get {
return _host ?? String.Empty; }
145 internal set { _host = value; }
154 get {
return _duration; }
155 internal set { _duration = value; }
159 public string ToString(
string format, IFormatProvider provider)
175 if (provider != null)
177 ICustomFormatter fmt = provider.GetFormat(this.GetType()) as ICustomFormatter;
179 return fmt.Format(format,
this, provider);
182 if (String.IsNullOrEmpty(format))
185 switch (format.ToUpperInvariant())
188 return $
"{Name}, {Host}, {Duration}";
196 throw new FormatException($
"The {format} format string is not supported.");
201 public string ToString(
string format)
220 #region class variables
221 [NonSerialized]
private readonly
object _locker =
new object();
224 #region object construction:
262 if (r != null && r.
RetVal == 0)
264 XElement xml = XElement.Parse(r.
CmdResult);
265 foreach (XElement e
in xml.Elements(
"Element"))
268 session.
Name = (string)e.Attribute(
"Username");
269 session.
Host = (string)e.Attribute(
"Host");
270 string temp = (string)e.Attribute(
"Duration");
271 if (!String.Equals(temp,
"(timed out)"))
273 double duration =
double.Parse(temp);
274 session.
Duration = TimeSpan.FromMinutes(duration);
277 lock (_locker) { Add(session); }
286 AcDebug.
Log($
"AcUtilsException caught and logged in AcSessions.initAsync{Environment.NewLine}{ecx.Message}");
289 catch (Exception ecx)
291 AcDebug.
Log($
"Exception caught and logged in AcSessions.initAsync{Environment.NewLine}{ecx.Message}");
string Host
IP address of the host machine.
AccuRev program return value and command result.
override bool Equals(object other)
Overridden to determine equality.
AcSession()
Constructor used during AcSessions list construction. It is called internally and not by user code...
Wrapper around TimeSpan so we can return our own formatted elapsed time string and still sort correct...
AcDuration Duration
Length of time the user has been logged on.
A container for AcSession objects that define AccuRev user sessions.
Holds the attributes for an AccuRev user session: principal name, host machine IP address...
string CmdResult
The command result (usually XML) emitted by AccuRev.
int RetVal
The AccuRev program return value for the command, otherwise minus one (-1) on error.
static void Log(string message, bool formatting=true)
Write the message text to STDOUT, to weekly log files located in %LOCALAPPDATA%\AcTools\Logs, and to trigger.log in the AccuRev server's ..storage\site_slice\logs folder in the case of triggers.
int CompareTo(AcDuration other)
Generic IComparable implementation (default) for comparing AcDuration objects to sort by timespan...
string ToString(string format, IFormatProvider provider)
The ToString implementation.
int CompareTo(AcSession other)
Generic IComparable implementation (default) for comparing AcSession objects to sort by Duration and ...
Exception thrown when an AccuRev command fails. The AccuRev program return value is zero (0) on succe...
string Name
User's AccuRev principal name.
AcSessions()
A container for AcSession objects that define AccuRev user sessions.
AccuRev command processing.
static async Task< AcResult > runAsync(string command, ICmdValidate validator=null)
Run the AccuRev command asynchronously with non-blocking I/O.
Use to log and display error and general purpose text messages, and to save the XML param data sent b...
async Task< bool > initAsync()
Populate this container with AcSession objects, the currently active login sessions.
string ToString(string format, IFormatProvider provider)
The ToString implementation.
override int GetHashCode()
Override appropriate for type AcSession.
bool Equals(AcSession other)
IEquatable implementation to determine the equality of instances of type AcSession. Uses AccuRev principal name and IP address of the host machine to compare instances.