-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataValueRetentionFile.cs
More file actions
155 lines (140 loc) · 5.6 KB
/
Copy pathDataValueRetentionFile.cs
File metadata and controls
155 lines (140 loc) · 5.6 KB
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
// Copyright (c) 2004-2008 Userwise Solutions LLC. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace NetCams
{
public class DataValueRetentionFile
{
public DataValueRetentionFile(TestContext testContext)
{
mTestContext = testContext;
mUpdateTimer.Interval = 1000;
mUpdateTimer.Elapsed += new System.Timers.ElapsedEventHandler(mUpdateTimer_Elapsed);
}
void mUpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs eventArgs)
{
try
{
mUpdateTimer.Enabled = false;
if (mFileIsOutdated)
{
SaveDataToFile();
}
}
catch (Exception e)
{
mTestContext.LogError("Problem updating retentive data file for " + mTestContext.Name + ". Message=" + e.Message);
}
finally
{
mUpdateTimer.Enabled = true;
}
}
private TestContext mTestContext;
private List<GlobalValue> mGlobalValues = new List<GlobalValue>();
public void RegisterGlobalValue(GlobalValue theValue)
{
// NOTE: timer isn't enabled until the first value is registered. It is possible to be re-enabled during a save, but that shouldn't be a problem unless the timer interval is REALLY short
mUpdateTimer.Enabled = true;
theValue.GlobalValueChanged += new GlobalValue.GlobalValueDelegate(GlobalValueChanged);
}
public void UnregisterGlobalValue(GlobalValue theValue)
{
theValue.GlobalValueChanged -= new GlobalValue.GlobalValueDelegate(GlobalValueChanged);
if (mTestContext.mGlobalValues.Count == 0)
{
// if there are no globals left after this one, disable the timer
// warning: this assumes they are unregistered from mTestContext.mGlobalValues before we get here
mUpdateTimer.Enabled = false;
}
}
private bool mFileIsOutdated = false;
public void GlobalValueChanged(GlobalValue globalValue)
{
if (mTestContext.FullyInitialized) mFileIsOutdated = true;
}
private System.Timers.Timer mUpdateTimer = new System.Timers.Timer();
public void SaveDataToFile()
{
if (writer == null) OpenFileForWriting();
foreach (GlobalValue globalValue in mTestContext.mGlobalValues)
{
if (globalValue.IsRetentive)
{
writer.WriteLine(globalValue.Name + " = " + globalValue.Value);
}
}
CloseFileFromWriting();
mFileIsOutdated = false;
}
private StreamWriter writer = null;
private StreamReader reader = null;
public void OpenFileForWriting()
{
if (writer != null) CloseFileFromWriting();
writer = new StreamWriter(mTestContext.Name + ".ret");
}
public void CloseFileFromWriting()
{
writer.Close();
//File.Delete(mFile);
//File.Move(mFile + ".tmp", mFile);
writer.Dispose();
writer = null;
}
private GlobalValue.DataScope mDataScope = GlobalValue.DataScope.NotDefined;
public void LoadDataFromFile()
{
if (writer != null) CloseFileFromWriting();
try
{
mTestContext.LogMessage("Loading retentive file...");
reader = new StreamReader(mTestContext.Name + ".ret");
do
{
// csline is case sensitive, while line is in lowercase
string line = reader.ReadLine().Trim();
try
{
int assignmentIndex = line.IndexOf("=");
if (assignmentIndex < 0)
{
mTestContext.LogError("Line in Data Value Retention File is missing an '=' sign. Line='" + line + "'");
continue;
}
string dataValueName = line.Substring(0, assignmentIndex).Trim();
string dataValueAsString = line.Substring(assignmentIndex + 1).Trim();
GlobalValue globalValue = mTestContext.GetGlobalValueIfExists(dataValueName);
if (globalValue == null)
{
mTestContext.LogWarning("GlobalValue '" + dataValueName + "' exists in the retentive file, but not in the test context.");
}
else if (globalValue.IsRetentive)
{
globalValue.Value = dataValueAsString;
}
}
catch (Exception e)
{
mTestContext.LogError("Problem processing line in data value retentive file. Line='" + line + "'. Message=" + e.Message);
}
} while (reader.Peek() != -1);
}
catch (Exception e)
{
mTestContext.LogError("Problem reading data value retentive file. Message=" + e.Message);
}
finally
{
if (reader != null)
{
reader.Close();
reader.Dispose();
reader = null;
}
}
}
}
}