-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit9.cpp
More file actions
231 lines (183 loc) · 5.92 KB
/
Copy pathUnit9.cpp
File metadata and controls
231 lines (183 loc) · 5.92 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//---------------------------------------------------------------------------
#include <vcl.h>
#include "System.IniFiles.hpp"
#include <algorithm>
#pragma hdrstop
#include "Unit9.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm9 *Form9;
//---------------------------------------------------------------------------
__fastcall TForm9::TForm9(TComponent* Owner) : TForm(Owner)
{
Caption = "Multicast Port Monitor - " + String(__DATE__) + " at " + String(__TIME__) + String();
pPortStatsList.reserve(20);
ReadIniFile();
}
//---------------------------------------------------------------------------
//this is a printf for the Debug Memo widget
void TForm9::pme(const char* fmt, ...)
{
//Check length and clear after 1000 lines
if (MemoDebug->Lines->Count > 1000) MemoDebug->Lines->Clear();
va_list args;
va_start(args, fmt);
char buf[200];
vsprintf(buf,fmt,args);
MemoDebug->Lines->Add(buf);
va_end(args);
//scroll to bottom of text
MemoDebug->SelStart = MemoDebug->Lines->Text.Length();
MemoDebug->SelLength = 1;
MemoDebug->ClearSelection();
}
void TForm9::pme(String s)
{
//Check length and clear after 1000 lines
if (MemoDebug->Lines->Count > 1000) MemoDebug->Lines->Clear();
MemoDebug->Lines->Add(s);
//scroll to bottom of text
MemoDebug->SelStart = MemoDebug->Lines->Text.Length();
MemoDebug->SelLength = 1;
MemoDebug->ClearSelection();
}
char buf[300];
void __fastcall TForm9::IdIPMCastClient1IPMCastRead(TObject *Sender, const TIdBytes AData, TIdSocketHandle *ABinding)
{
if (CB_Debug->Checked) {
pme("Rx Multicast==============================================================================================");
pme("Rx packet size: %d",AData.size());
MemoDebug->Lines->Add("On Port:" + IntToStr(ABinding->Port));
}
int bindPort = ABinding->Port;
for (int i = 1; i < SG_StreamList->RowCount; i++)
{
int p = StrToInt(SG_StreamList->Cells[0][i]);
if (p==bindPort) {
int a = StrToInt(SG_StreamList->Cells[2][i]);
SG_StreamList->Cells[2][i]= IntToStr(a+1);
SG_StreamList->Cells[3][i]= "0"; // timeSinceLast=0;
return;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm9::BN_ListenClick(TObject *Sender)
{
if (b_Listening)
{
b_Listening = false;
BN_Listen->Caption = "Start";
for (auto a : socketList) a->Active=false;
}
else
{
b_Listening = true;
BN_Listen->Caption = "Stop";
socketList.clear();
int n = SG_StreamList->RowCount;
for (int i = 1; i < n; i++) {
int p = StrToInt(SG_StreamList->Cells[0][i]);
String IP = SG_StreamList->Cells[1][i];
pme("Adding %d on %s",p,IP.c_str() );
pme(IP);
TIdIPMCastClient *mcs = new TIdIPMCastClient();
mcs->OnIPMCastRead = &IdIPMCastClient1IPMCastRead;
mcs->ReuseSocket = rsTrue;
mcs->DefaultPort = p;
mcs->MulticastGroup = IP;
mcs->Active=true;
socketList.push_back(mcs);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm9::BN_QuitClick(TObject *Sender)
{
for (auto a : pPortStatsList) a->ClientSocket->Active=false;
WriteIniFile();
Close();
}
void __fastcall TForm9::ReadIniFile()
{
if (FileExists(iniFile))
{
TIniFile *ini = new TIniFile(iniFile);
numPorts = ini->ReadInteger( "Form", "NumPorts", 0);
SG_StreamList->RowCount = numPorts+1;
pme("Ini file %s found with %d ports to monitor",iniFile, numPorts);
SG_StreamList->Cells[0][0]= L"Port";
SG_StreamList->Cells[1][0]= L"IP";
SG_StreamList->Cells[2][0]= L"Count Rx";
SG_StreamList->Cells[3][0]= L"Time Since Last";
SG_StreamList->Cells[4][0]= L"Name";
for (int i=1;i<=numPorts;i++) {
char portnum[10];
sprintf(portnum,"Port%02d",i);
int port = ini->ReadInteger( "Form", portnum, 0);
char name[20];
sprintf(name,"Name%02d",i);
auto s = ini->ReadString( "Form", name, "--");
char IP[20];
sprintf(IP,"IP%02d",i);
auto I = ini->ReadString( "Form", IP, "192.168.1.255");
SG_StreamList->Cells[0][i]= IntToStr(port);
SG_StreamList->Cells[1][i]= I;
SG_StreamList->Cells[2][i]= L"0";
SG_StreamList->Cells[3][i]= L"0";
SG_StreamList->Cells[4][i]= s;
}
int width = ini->ReadInteger( "Form", "width", 780);
int height = ini->ReadInteger( "Form", "height", 400);
Form9->Width = std::clamp(width,440,2000);
Form9->Height = std::clamp(height,270,1000);
}
else pme("No ini file. Using defaults");
}
void __fastcall TForm9::WriteIniFile()
{
if (FileExists(iniFile)) pme("Ini file found");
else pme("No ini file. Creating New ini");
TIniFile *ini = new TIniFile(iniFile);
ini->WriteString ( "Form", "NumPorts", numPorts);
int width = Form9->Width;
int height = Form9->Height;
pme("Width/Height: %d %d",width,height);
ini->WriteString ( "Form", "Width", width);
ini->WriteString ( "Form", "Height", height);
for (int i=1;i<=numPorts;i++) {
char name[20];
sprintf(name,"Name%02d",i);
ini->WriteString( "Form", name, SG_StreamList->Cells[4][i]);
char portnum[10];
sprintf(portnum,"Port%02d",i);
auto pn = SG_StreamList->Cells[0][i];
ini->WriteInteger( "Form", portnum,StrToInt(pn));
char IP[20];
sprintf(IP,"IP%02d",i);
ini->WriteString( "Form", IP, SG_StreamList->Cells[1][i]);
}
}
//timer is 250ms. Show stats every 250ms, update timeSinceLast every 4th timer so once per second
int frameCounter=0;
void __fastcall TForm9::Timer1Timer(TObject *Sender)
{
for (int i = 1; i < SG_StreamList->RowCount; i++)
{
int c = StrToInt(SG_StreamList->Cells[2][i]);
if (c!=0) {
int tsl = StrToInt(SG_StreamList->Cells[3][i]);
SG_StreamList->Cells[3][i] = IntToStr(tsl+1);
}
}
}
void __fastcall TForm9::BN_ClearClick(TObject *Sender)
{
for (int i = 1; i < SG_StreamList->RowCount; i++)
{
SG_StreamList->Cells[2][i]= "0"; //count
SG_StreamList->Cells[3][i]= "0"; // timeSinceLast=0;
}
}
//---------------------------------------------------------------------------