Newer
Older
using System;
using System.IO;
using System.Xml.Serialization;
Niko Ehrenfeuchter
committed
namespace AutoTx.XmlWrapper
Niko Ehrenfeuchter
committed
public class ServiceStatus
{
[NonSerialized] string _storageFile; // remember where we came from
private DateTime _lastStatusUpdate;
private DateTime _lastStorageNotification;
private DateTime _lastAdminNotification;
string _currentTransferSrc;
string _currentTargetTmp;
bool _filecopyFinished;
private bool _serviceSuspended;
private bool _cleanShutdown;
Niko Ehrenfeuchter
committed
private long _currentTransferSize;
[XmlElement("LastStatusUpdate", DataType = "dateTime")]
public DateTime LastStatusUpdate {
get { return _lastStatusUpdate; }
set { _lastStatusUpdate = value; }
}
[XmlElement("LastStorageNotification", DataType = "dateTime")]
public DateTime LastStorageNotification {
get { return _lastStorageNotification; }
set {
_lastStorageNotification = value;
Serialize();
}
}
[XmlElement("LastAdminNotification", DataType = "dateTime")]
public DateTime LastAdminNotification {
get { return _lastAdminNotification; }
set {
_lastAdminNotification = value;
Serialize();
}
}
public string LimitReason {
get { return _limitReason; }
set {
_limitReason = value;
log("LimitReason was updated (" + value + "), calling Serialize()...");
Serialize();
}
}
public string CurrentTransferSrc {
get { return _currentTransferSrc; }
set {
_currentTransferSrc = value;
log("CurrentTransferSrc was updated (" + value + "), calling Serialize()...");
Serialize();
}
}
public string CurrentTargetTmp {
get { return _currentTargetTmp; }
set {
_currentTargetTmp = value;
log("CurrentTargetTmp was updated (" + value + "), calling Serialize()...");
Serialize();
}
}
public bool ServiceSuspended {
get { return _serviceSuspended; }
set {
_serviceSuspended = value;
log("ServiceSuspended was updated (" + value + "), calling Serialize()...");
Serialize();
}
}
public bool FilecopyFinished {
get { return _filecopyFinished; }
set {
_filecopyFinished = value;
log("FilecopyFinished was updated (" + value + "), calling Serialize()...");
Serialize();
}
}
/// <summary>
/// Indicates whether the service was cleanly shut down (false while the service is running).
/// </summary>
public bool CleanShutdown {
get { return _cleanShutdown; }
set {
_cleanShutdown = value;
Serialize();
}
}
Niko Ehrenfeuchter
committed
public long CurrentTransferSize {
get { return _currentTransferSize; }
Niko Ehrenfeuchter
committed
_currentTransferSize = value;
log("CurrentTransferSize was updated (" + value + "), calling Serialize()...");
Serialize();
}
}
Niko Ehrenfeuchter
committed
public ServiceStatus() {
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
156
_currentTransferSrc = "";
_currentTargetTmp = "";
_filecopyFinished = true;
}
public void Serialize() {
/* During de-serialization, the setter methods get called as well but
* we should not serialize until the deserialization has completed.
* As the storage file name will only be set after this, it is sufficient
* to test for this (plus, we can't serialize anyway without it).
*/
if (_storageFile == null) {
log("File name for XML serialization is not set, doing nothing.");
return;
}
// update the timestamp:
LastStatusUpdate = DateTime.Now;
try {
var xs = new XmlSerializer(GetType());
var writer = File.CreateText(_storageFile);
xs.Serialize(writer, this);
writer.Flush();
writer.Close();
}
catch (Exception ex) {
log("Error in Serialize(): " + ex.Message);
}
log("Finished serializing " + _storageFile);
}
static void log(string message) {
// use Console.WriteLine until proper logging is there (running as a system
// service means those messages will disappear):
Console.WriteLine(message);
/*
using (var sw = File.AppendText(@"C:\Tools\AutoTx\console.log")) {
sw.WriteLine(message);
}
*/
}
Niko Ehrenfeuchter
committed
public static ServiceStatus Deserialize(string file) {
ServiceStatus status;
var xs = new XmlSerializer(typeof(ServiceStatus));
try {
var reader = File.OpenText(file);
Niko Ehrenfeuchter
committed
status = (ServiceStatus) xs.Deserialize(reader);
reader.Close();
}
catch (Exception) {
// if reading the status XML fails, we return an empty (new) one
Niko Ehrenfeuchter
committed
status = new ServiceStatus();
}
// now set the storage filename:
status._storageFile = file;
return status;
}
}
}