-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamEntry.cs
More file actions
125 lines (88 loc) · 3.34 KB
/
camEntry.cs
File metadata and controls
125 lines (88 loc) · 3.34 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace PSIP_Udvardi_csh
{
class camEntry
{
private string macAddress;
private int port;
private Timer entryTimeout;
DateTime timerStart;
private double timeoutValueSeconds;
public Timer EntryTimeout { get => entryTimeout; set => entryTimeout = value; }
public int Port { get => port; set => port = value; }
public camEntry(string macAddress, int port_recv, double timeSeconds)
{
this.macAddress = macAddress;
port = port_recv;
CamTable.CamTableDict.TryAdd(macAddress, this);
entryTimeout = new Timer();
entryTimeout.Interval = TimeSpan.FromSeconds(timeSeconds).TotalMilliseconds;
entryTimeout.Elapsed += removeMe;
timerStart = DateTime.Now;
entryTimeout.Enabled = true;
timeoutValueSeconds = timeSeconds;
}
private void initializeNewTimer(double timeSeconds)
{
entryTimeout.Stop();
entryTimeout.Dispose();
try
{
entryTimeout = new Timer();
entryTimeout.Interval = TimeSpan.FromSeconds(timeSeconds).TotalMilliseconds;
entryTimeout.Elapsed += removeMe;
timerStart = DateTime.Now;
entryTimeout.Enabled = true;
timeoutValueSeconds = timeSeconds;
}
catch (System.ObjectDisposedException) //If object is disposed during new initialization. Try again.
{
entryTimeout = new Timer();
entryTimeout.Interval = TimeSpan.FromSeconds(timeSeconds).TotalMilliseconds;
entryTimeout.Elapsed += removeMe;
timerStart = DateTime.Now;
entryTimeout.Enabled = true;
timeoutValueSeconds = timeSeconds;
}
}
public double getTimeRemaining()
{
TimeSpan timerRunningFor = DateTime.Now - timerStart;
return timeoutValueSeconds - timerRunningFor.TotalSeconds;
}
public double getTimeElapsed()
{
TimeSpan timerRunningFor = DateTime.Now - timerStart;
return timerRunningFor.TotalSeconds;
}
//Entry removes itself from table, when its timer elapses.
private void removeMe(object sender, EventArgs e)
{
if(CamTable.CamTableDict.TryRemove(macAddress, out camEntry deletedEntry))
{
entryTimeout.Stop();
entryTimeout.Dispose();
Console.WriteLine(" Successfully deleted:" + deletedEntry.macAddress);
}
}
public void stopAndDisposeTimer()
{
entryTimeout.Stop();
entryTimeout.Dispose();
}
public void resetTimer(double newTimeoutSeconds)
{
initializeNewTimer(newTimeoutSeconds);
}
//For displaying entries in CAM table.
public override string ToString()
{
return macAddress + " Port:" + port + " Timeout: " + Math.Floor(getTimeRemaining()) + "s";
}
}
}