//----------------------------------------------------------------------------
//
// Copyright © 2009-2012, Intel Corporation. All rights reserved.
//
// File: Connection.cs
//
//----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using Intel.Management.Wsman;
namespace UCT
{
[Serializable]
///
/// This class hold the basic information data.
/// Used for the remeber the machines wich we connect them on the past.
///
public class Connection : ISerializable
{
#region - Members -
///
/// The amt hostname
///
public string HostName { set; get; }
///
/// AMT User name
///
public string UserName { set; get; }
///
/// Indicate if Kerberos is enable
///
public bool KerberosEnable { set; get; }
///
/// Indicate if TLS is enable
///
public bool TLSEnable { set; get; }
///
/// Indicate if TLS is enable
///
public bool MTLSEnable { set; get; }
///
/// Hold the client ceritficate name
///
public string ClientCertificate { set; get; }
///
/// Indicate if Cira is enable
///
public bool CiraEnable { set; get; }
///
/// Hold the proxy host name
///
public string ProxyHost { set; get; }
///
/// Hold the proxy port
///
public ushort ProxyPort { set; get; }
///
/// Hold the proxy user name
///
public string ProxyUserName { set; get; }
#endregion
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
throw new NotImplementedException();
}
#endregion
#region - Constructors -
public Connection(string host, string username, bool kerberos, bool tls, bool mtls, string cert,
bool cira, string proxyHost, ushort proxyPort, string proxyUser, string proxyPass)
{
HostName = host;
UserName = username;
KerberosEnable = kerberos;
TLSEnable = tls;
MTLSEnable = mtls;
ClientCertificate = cert;
CiraEnable = cira;
ProxyHost = proxyHost;
ProxyPort = proxyPort;
ProxyUserName = proxyUser;
}
public Connection() { }
#endregion
#region - Operators -
public static bool operator ==(Connection x, Connection y)
{
// If one is null, but not both, return false.
if (((object)x == null) || ((object)y == null))
{
return false;
}
if (x.HostName == y.HostName &&
x.UserName == y.UserName &&
x.KerberosEnable == y.KerberosEnable &&
x.TLSEnable == y.TLSEnable &&
x.ClientCertificate == y.ClientCertificate &&
x.MTLSEnable == y.MTLSEnable &&
x.CiraEnable == y.CiraEnable &&
x.ProxyHost == y.ProxyHost &&
x.ProxyPort == y.ProxyPort &&
x.ProxyUserName == y.ProxyUserName )
{
return true;
}
return false;
}
public static bool operator !=(Connection x, Connection y)
{
return !(x == y);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
#endregion
}
[Serializable]
public class ConnectionHistory
{
// This attribute enables the ArrayList to be serialized:
[XmlArray("Items")]
[XmlArrayItem("ConnectionItem", typeof(Connection))]
///
/// Host all the connections
///
private List _connections;
[XmlIgnore]
private XmlSerializer _serializer;
///
/// Constuctor
///
public ConnectionHistory()
{
if (_connections == null)
{
_connections = new List();
_serializer = new XmlSerializer(typeof(List));
}
}
///
/// Add new connection information
///
///
public bool CheckExistingConnectionInfo(Connection con)
{
if (_connections.Exists(e => e.HostName == con.HostName) == false)
{
AddConnectionInfo(con);
return false;
}
if (_connections.Find(e => e.HostName == con.HostName) == con)
{
return false;
}
return true;
}
///
/// Add new connection information
///
///
public void AddConnectionInfo(Connection conToAdd)
{
_connections.Add(conToAdd);
SaveConnections();
}
///
/// Remove new connection information
///
///
public void ReplaceConnectionInfo(Connection conToAdd)
{
_connections.Remove(_connections.Find(e => e.HostName == conToAdd.HostName));
AddConnectionInfo(conToAdd);
}
///
/// Save all the connection
///
private void SaveConnections()
{
try
{
// Serialize the connections list
using (StringWriter writer = new StringWriter())
{
_serializer.Serialize(writer, _connections);
string serializedXml = writer.ToString();
writer.Close();
// Save the serialized xml
Properties.Settings.Default.CONNECTION_HISTORY = serializedXml;
Properties.Settings.Default.Save();
}
}
catch { }
}
///
/// Save all the connection
///
public List LoadConnections()
{
try
{
if (Properties.Settings.Default.CONNECTION_HISTORY != string.Empty)
{
_connections = (List)_serializer.Deserialize(new StringReader(Properties.Settings.Default.CONNECTION_HISTORY));
}
}
catch { }
return _connections;
}
}
}