forked from lstratman/EasyConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoricalConnection.cs
155 lines (134 loc) · 4.56 KB
/
HistoricalConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using EasyConnect.Protocols;
namespace EasyConnect
{
/// <summary>
/// Wraps a connection that the user made and the time that they made it.
/// </summary>
public class HistoricalConnection : ISerializable, IXmlSerializable
{
/// <summary>
/// Default constructor.
/// </summary>
public HistoricalConnection()
{
}
/// <summary>
/// Constructor for <see cref="ISerializable"/>.
/// </summary>
/// <param name="info">Container for serialized data.</param>
/// <param name="context">Serialization context.</param>
// ReSharper disable UnusedParameter.Local
public HistoricalConnection(SerializationInfo info, StreamingContext context)
// ReSharper restore UnusedParameter.Local
{
LastConnection = info.GetDateTime("LastConnection");
Connection = (IConnection) info.GetValue("Connection", typeof (IConnection));
}
/// <summary>
/// Constructor that initializes <see cref="Connection"/>.
/// </summary>
/// <param name="connection">Connection that the user made.</param>
public HistoricalConnection(IConnection connection)
{
Connection = connection;
}
/// <summary>
/// Connection that the user made.
/// </summary>
public IConnection Connection
{
get;
set;
}
/// <summary>
/// Date and time that the user made the connection.
/// </summary>
public DateTime LastConnection
{
get;
set;
}
/// <summary>
/// Serialization method for <see cref="ISerializable"/>.
/// </summary>
/// <param name="info">Container for serialized data.</param>
/// <param name="context">Serialization context.</param>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("LastConnection", LastConnection);
info.AddValue("Connection", Connection);
}
/// <summary>
/// Schema method for <see cref="IXmlSerializable"/>.
/// </summary>
/// <returns>Null, always.</returns>
public XmlSchema GetSchema()
{
return null;
}
/// <summary>
/// Deserializes the historical connection data from an XML source.
/// </summary>
/// <param name="reader">XML source that we are deserializing from.</param>
public void ReadXml(XmlReader reader)
{
// If we are dealing with a legacy historical connection, deserialize it into LegacyHistoricalConnection
if (reader.GetAttribute("isLegacy") != "false")
{
XmlSerializer serializer = new XmlSerializer(typeof (LegacyHistoricalConnection));
LegacyHistoricalConnection legacyHistoricalConnection = (LegacyHistoricalConnection) serializer.Deserialize(reader);
LastConnection = legacyHistoricalConnection.LastConnection;
Connection = legacyHistoricalConnection;
}
// Otherwise, get the type of connection from the node name under Connection and deserialize it using that
else
{
reader.Read();
while (reader.MoveToContent() == XmlNodeType.Element)
{
switch (reader.LocalName)
{
case "LastConnection":
LastConnection = DateTime.Parse(reader.ReadElementContentAsString());
break;
case "Connection":
reader.Read();
while (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
}
XmlSerializer serializer =
new XmlSerializer(
reader.LocalName == "HistoricalConnection"
? typeof (LegacyHistoricalConnection)
: ConnectionFactory.GetProtocols().First(p => p.ConnectionType.Name == reader.LocalName).ConnectionType);
Connection = (IConnection) serializer.Deserialize(reader);
reader.Read();
break;
}
}
reader.Read();
}
}
/// <summary>
/// Serializes the connection data to XML.
/// </summary>
/// <param name="writer">XML destination that we're serializing to.</param>
public void WriteXml(XmlWriter writer)
{
// Write an explicit isLegacy="false" attribute so that we don't try to deserialize this using LegacyHistoricalConnection.
writer.WriteAttributeString("isLegacy", "false");
writer.WriteElementString("LastConnection", LastConnection.ToString());
writer.WriteStartElement("Connection");
XmlSerializer serializer = new XmlSerializer(Connection.GetType());
serializer.Serialize(writer, Connection);
writer.WriteEndElement();
}
}
}