AcUtils
A high performance abstraction layer for AccuRev
AcSessions.cs
Go to the documentation of this file.
1 
16 using System;
17 using System.Collections.Generic;
18 using System.Threading.Tasks;
19 using System.Xml.Linq;
20 
21 namespace AcUtils
22 {
28 
32  [Serializable]
33  public sealed class AcSession : IFormattable, IEquatable<AcSession>, IComparable<AcSession>, IComparable
34  {
35  #region class variables
36  private string _name; // user's principal name
37  private string _host; // IP address of the host machine
38  private AcDuration _duration; // length of time user has been logged on
39  #endregion
40 
44  internal AcSession() { }
45 
46  #region Equality comparison
47 
49  public bool Equals(AcSession other)
56  {
57  if (ReferenceEquals(other, null)) return false;
58  if (ReferenceEquals(this, other)) return true;
59  var left = Tuple.Create(Name, Host);
60  var right = Tuple.Create(other.Name, other.Host);
61  return left.Equals(right);
62  }
63 
68  public override bool Equals(object other)
69  {
70  if (ReferenceEquals(other, null)) return false;
71  if (ReferenceEquals(this, other)) return true;
72  if (GetType() != other.GetType()) return false;
73  return this.Equals(other as AcSession);
74  }
75 
80  public override int GetHashCode()
81  {
82  var hash = Tuple.Create(Name, Host);
83  return hash.GetHashCode();
84  }
86  #endregion
87 
88  #region Order comparison
89 
91 
98  public int CompareTo(AcSession other)
99  {
100  int result;
101  if (AcSession.ReferenceEquals(this, other))
102  result = 0;
103  else
104  {
105  result = Duration.CompareTo(other.Duration);
106  if (result == 0)
107  result = Name.CompareTo(other.Name);
108  }
109 
110  //return -1 * result; // sort in reverse order to show longer sessions first
111  return result;
112  }
113 
120  int IComparable.CompareTo(object other)
121  {
122  if (!(other is AcSession))
123  throw new ArgumentException("Argument is not an AcSession", "other");
124  AcSession o = (AcSession)other;
125  return this.CompareTo(o);
126  }
128  #endregion
129 
133  public string Name
134  {
135  get { return _name ?? String.Empty; }
136  internal set { _name = value; }
137  }
138 
142  public string Host
143  {
144  get { return _host ?? String.Empty; }
145  internal set { _host = value; }
146  }
147 
151 
152  public AcDuration Duration
153  {
154  get { return _duration; }
155  internal set { _duration = value; }
156  }
157 
158  #region ToString
159  public string ToString(string format, IFormatProvider provider)
174  {
175  if (provider != null)
176  {
177  ICustomFormatter fmt = provider.GetFormat(this.GetType()) as ICustomFormatter;
178  if (fmt != null)
179  return fmt.Format(format, this, provider);
180  }
181 
182  if (String.IsNullOrEmpty(format))
183  format = "G";
184 
185  switch (format.ToUpperInvariant())
186  {
187  case "G": // default when not using a format specifier
188  return $"{Name}, {Host}, {Duration}";
189  case "N": // user's AccuRev principal name
190  return Name;
191  case "H": // IP address of the host machine
192  return Host;
193  case "D": // length of time user has been logged on
194  return Duration.ToString();
195  default:
196  throw new FormatException($"The {format} format string is not supported.");
197  }
198  }
199 
200  // Calls ToString(string, IFormatProvider) version with a null IFormatProvider argument.
201  public string ToString(string format)
202  {
203  return ToString(format, null);
204  }
205 
206  // Calls ToString(string, IFormatProvider) version with the general format and a null IFormatProvider argument.
207  public override string ToString()
208  {
209  return ToString("G", null);
210  }
211  #endregion ToString
212  }
213 
217  [Serializable]
218  public sealed class AcSessions : List<AcSession>
219  {
220  #region class variables
221  [NonSerialized] private readonly object _locker = new object();
222  #endregion
223 
224  #region object construction:
225 
227 
246  public AcSessions() { }
247 
255 
256  public async Task<bool> initAsync()
257  {
258  bool ret = false; // assume failure
259  try
260  {
261  AcResult r = await AcCommand.runAsync("show -fx sessions").ConfigureAwait(false);
262  if (r != null && r.RetVal == 0)
263  {
264  XElement xml = XElement.Parse(r.CmdResult);
265  foreach (XElement e in xml.Elements("Element"))
266  {
267  AcSession session = new AcSession();
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)"))
272  {
273  double duration = double.Parse(temp);
274  session.Duration = TimeSpan.FromMinutes(duration);
275  }
276 
277  lock (_locker) { Add(session); }
278  }
279 
280  ret = true; // operation succeeded
281  }
282  }
283 
284  catch (AcUtilsException ecx)
285  {
286  AcDebug.Log($"AcUtilsException caught and logged in AcSessions.initAsync{Environment.NewLine}{ecx.Message}");
287  }
288 
289  catch (Exception ecx)
290  {
291  AcDebug.Log($"Exception caught and logged in AcSessions.initAsync{Environment.NewLine}{ecx.Message}");
292  }
293 
294  return ret;
295  }
297  #endregion
298  }
299 }
string Host
IP address of the host machine.
Definition: AcSessions.cs:143
AccuRev program return value and command result.
Definition: AcCommand.cs:29
override bool Equals(object other)
Overridden to determine equality.
Definition: AcSessions.cs:68
AcSession()
Constructor used during AcSessions list construction. It is called internally and not by user code...
Definition: AcSessions.cs:44
Wrapper around TimeSpan so we can return our own formatted elapsed time string and still sort correct...
Definition: AcDuration.cs:25
AcDuration Duration
Length of time the user has been logged on.
Definition: AcSessions.cs:153
A container for AcSession objects that define AccuRev user sessions.
Definition: AcSessions.cs:218
Holds the attributes for an AccuRev user session: principal name, host machine IP address...
Definition: AcSessions.cs:33
string CmdResult
The command result (usually XML) emitted by AccuRev.
Definition: AcCommand.cs:70
int RetVal
The AccuRev program return value for the command, otherwise minus one (-1) on error.
Definition: AcCommand.cs:61
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.
Definition: AcDebug.cs:378
int CompareTo(AcDuration other)
Generic IComparable implementation (default) for comparing AcDuration objects to sort by timespan...
Definition: AcDuration.cs:99
string ToString(string format, IFormatProvider provider)
The ToString implementation.
Definition: AcDuration.cs:140
int CompareTo(AcSession other)
Generic IComparable implementation (default) for comparing AcSession objects to sort by Duration and ...
Definition: AcSessions.cs:98
Exception thrown when an AccuRev command fails. The AccuRev program return value is zero (0) on succe...
string Name
User's AccuRev principal name.
Definition: AcSessions.cs:134
AcSessions()
A container for AcSession objects that define AccuRev user sessions.
Definition: AcSessions.cs:246
AccuRev command processing.
Definition: AcCommand.cs:138
static async Task< AcResult > runAsync(string command, ICmdValidate validator=null)
Run the AccuRev command asynchronously with non-blocking I/O.
Definition: AcCommand.cs:184
Use to log and display error and general purpose text messages, and to save the XML param data sent b...
Definition: AcDebug.cs:100
async Task< bool > initAsync()
Populate this container with AcSession objects, the currently active login sessions.
Definition: AcSessions.cs:256
string ToString(string format, IFormatProvider provider)
The ToString implementation.
Definition: AcSessions.cs:173
override int GetHashCode()
Override appropriate for type AcSession.
Definition: AcSessions.cs:80
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.
Definition: AcSessions.cs:55