-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrafficDataPrinter.cs
111 lines (106 loc) · 6.96 KB
/
TrafficDataPrinter.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
using System;
using System.Collections.Generic;
using System.IO;
using static NeTraf.HelperMethods;
using static System.Console;
namespace NeTraf
{
public static class TrafficDataPrinter
{
public static void PrintToOutputFile(List<TrafficDataRowSet> trafficDataRowSets,
string outputFilePath,
TrafficDataType trafficDataType,
TrafficMeasurementType trafficMeasurementType)
{
var stream = File.OpenWrite(outputFilePath);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(stream))
{
var unit = GetBytesConvertionTargetUnit(trafficDataRowSets,trafficDataType,trafficMeasurementType);
trafficDataRowSets.ForEach(trafficDataRowSet =>
{
file.WriteLine(TrafficDataToOutput(trafficDataRowSet,
trafficDataRowSet.CumulatedRunningTime,
trafficDataType,
trafficMeasurementType,
unit));
});
}
}
private static string TrafficDataToOutput(TrafficDataRowSet trafficDataRowSet,
double cumulatedRunningTime,
TrafficDataType trafficDataType,
TrafficMeasurementType trafficMeasurementType,
TrafficUnitType trafficUnitType)
{
switch (trafficDataType)
{
case TrafficDataType.TotalData :
{
switch (trafficMeasurementType)
{
case TrafficMeasurementType.Packets : return $"{trafficDataRowSet.TotalTotalTrafficData.Item1},{ConvertTime(cumulatedRunningTime)}";
case TrafficMeasurementType.Bytes : return $"{String.Format("{0:0.00}", ConvertBytes(trafficDataRowSet.TotalTotalTrafficData.Item2, trafficUnitType).Item1)},{ConvertTime(cumulatedRunningTime)}";
case TrafficMeasurementType.Rate : return $"{String.Format("{0:0.00}", ConvertBytes(trafficDataRowSet.TotalTotalTrafficData.Item3, trafficUnitType).Item1)},{ConvertTime(cumulatedRunningTime)}";
default: return $"{trafficDataRowSet.TotalIncomingTrafficData.Item1},{ConvertTime(cumulatedRunningTime)}";
}
}
case TrafficDataType.IncomingData :
{
switch (trafficMeasurementType)
{
case TrafficMeasurementType.Packets : return $"{trafficDataRowSet.TotalIncomingTrafficData.Item1},{ConvertTime(cumulatedRunningTime)}";
case TrafficMeasurementType.Bytes : return $"{String.Format("{0:0.00}", ConvertBytes(trafficDataRowSet.TotalIncomingTrafficData.Item2, trafficUnitType).Item1)},{ConvertTime(cumulatedRunningTime)}";
case TrafficMeasurementType.Rate : return $"{String.Format("{0:0.00}", ConvertBytes(trafficDataRowSet.TotalIncomingTrafficData.Item3, trafficUnitType).Item1)},{ConvertTime(cumulatedRunningTime)}";
default: return $"{trafficDataRowSet.TotalIncomingTrafficData.Item1},{ConvertTime(cumulatedRunningTime)}";
}
}
case TrafficDataType.OutgoingData :
{
switch (trafficMeasurementType)
{
case TrafficMeasurementType.Packets : return $"{trafficDataRowSet.TotalOutgoingTrafficData.Item1},{ConvertTime(cumulatedRunningTime)}";
case TrafficMeasurementType.Bytes : return $"{String.Format("{0:0.00}", ConvertBytes(trafficDataRowSet.TotalOutgoingTrafficData.Item2, trafficUnitType).Item1)},{ConvertTime(cumulatedRunningTime)}";
case TrafficMeasurementType.Rate : return $"{String.Format("{0:0.00}", ConvertBytes(trafficDataRowSet.TotalOutgoingTrafficData.Item3, trafficUnitType).Item1)},{ConvertTime(cumulatedRunningTime)}";
default: return $"{trafficDataRowSet.TotalOutgoingTrafficData.Item1},{ConvertTime(cumulatedRunningTime)}";
}
}
default:
{
switch (trafficMeasurementType)
{
case TrafficMeasurementType.Packets : return $"{trafficDataRowSet.TotalTotalTrafficData.Item1},{ConvertTime(cumulatedRunningTime)}";
case TrafficMeasurementType.Bytes : return $"{String.Format("{0:0.00}", ConvertBytes(trafficDataRowSet.TotalTotalTrafficData.Item2, trafficUnitType).Item1)},{ConvertTime(cumulatedRunningTime)}";
case TrafficMeasurementType.Rate : return $"{String.Format("{0:0.00}", ConvertBytes(trafficDataRowSet.TotalTotalTrafficData.Item3, trafficUnitType).Item1)},{ConvertTime(cumulatedRunningTime)}";
default: return $"{trafficDataRowSet.TotalTotalTrafficData.Item1},{ConvertTime(cumulatedRunningTime)}";
}
}
}
}
public static void PrintToConsole(List<TrafficDataRowSet> trafficDataRowSets)
{
var bytesUnit = TrafficUnitType.Bytes;
var rateUnit = TrafficUnitType.Bytes;
trafficDataRowSets.ForEach(trafficDataRowSet =>
{
WriteLine();
WriteLine($"During {ConvertTime(trafficDataRowSet.RunningTime)} :");
WriteLine();
foreach (TrafficDataType trafficDataType in Enum.GetValues(typeof(TrafficDataType)))
{
bytesUnit = GetBytesConvertionTargetUnit(trafficDataRowSets,trafficDataType,TrafficMeasurementType.Bytes);
rateUnit = GetBytesConvertionTargetUnit(trafficDataRowSets,trafficDataType,TrafficMeasurementType.Rate);
ForegroundColor = ConsoleColor.White;
switch (trafficDataType)
{
case TrafficDataType.TotalData : Write("TotalTrafficData : "); break;
case TrafficDataType.IncomingData : ForegroundColor = ConsoleColor.Green; Write("IncomingTrafficData : "); break;
case TrafficDataType.OutgoingData : ForegroundColor = ConsoleColor.Red; Write("OutgoingTrafficData : "); break;
}
WriteLine(trafficDataRowSet.Print(trafficDataType,bytesUnit,rateUnit ,false));
}
ForegroundColor = ConsoleColor.White;
WriteLine("__________________________________________________________________________");
});
}
}
}