-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
139 lines (110 loc) · 4.15 KB
/
main.cpp
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
#include "TCPConnection.h"
#include "TCPOctetStream.h"
#include <iostream>
#include <string>
#include <limits>
#include <iomanip>
#include "bad_if.h"
#include "bad_switch.h"
#include "Menu.h"
using std::cout;
void bad_if_else();
void bad_switch_case();
void run();
int main() {
run();
// bad_switch_case(); // Führt die zusätzliche Funktion aus
// bad_if_else();
return 0;
}
void run() {
TCPConnection connection;
Menu menu;
menu.printMenu(); // Zeige das Menü
while (true) {
cout << "\nAktueller Zustand: \033[33m[ " << connection.GetStateName() <<" ]\033[0m "
<< "Wähle Auswahl: ";
switch (menu.getChoice()) {
case MenuChoice::ACTIVEOPEN:
connection.ActiveOpen();
break;
case MenuChoice::PASSIVEOPEN:
connection.PassiveOpen();
break;
case MenuChoice::SEND:
connection.Send();
break;
case MenuChoice::CLOSE:
connection.Close();
break;
case MenuChoice::ACK:
connection.Acknowledge();
break;
case MenuChoice::SYNC:
connection.Synchronize();
break;
case MenuChoice::OCTET: {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Geben Sie den Text ein: ";
std::string text;
std::getline(std::cin, text);
if(!text.empty() && text[0] == ' ') {
text.erase(0,1);
}
TCPOctetStream stream(text);
connection.ProcessOctet(&stream);
break;
}
case MenuChoice::SHOWSTATE:
cout << "\033[34mAktueller Zustand: " << connection.GetStateName() << "\033[0m\n";
break;
case MenuChoice::SHOWSTATS: {
// Ruft die erweiterte Statistik-Anzeige im aktuellen Zustand auf
connection.ShowStatistics();
break;
}
case MenuChoice::MENU:
menu.printMenu();
break;
case MenuChoice::CONNECT:
cout << "\033[32mVerbinde mit lokalem Server...\033[0m\n";
// connection.Connect();
break;
case MenuChoice::EXIT:
cout << "\033[31mBeende Programm...\033[0m\n";
return;
case MenuChoice::UNKNOWN:
default:
cout << "\033[31mUnbekannte Auswahl, bitte versuchen Sie es erneut.\033[0m\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
break;
}
}
}
void bad_if_else() {
// Instanz der Klasse TCPConnect erstellen
bad_if connection;
// Demo: Methoden aufrufen
std::cout << "=== TCPConnect Demo ===" << std::endl;
std::cout << "\n1. ActiveOpen:" << std::endl;
connection.ActiveOpen(); // Sollte in den ESTABLISHED-Zustand wechseln.
std::cout << "\n2. Close:" << std::endl;
connection.Close(); // Sollte zurück in den CLOSED-Zustand wechseln.
std::cout << "\n3. PassiveOpen:" << std::endl;
connection.PassiveOpen(); // Sollte in den LISTEN-Zustand wechseln.
std::cout << "\n4. Send (im LISTEN-Zustand):" << std::endl;
connection.Send(); // Sollte eine Fehlermeldung ausgeben, da Senden nur im ESTABLISHED-Zustand erlaubt ist.
std::cout << "\n5. Close (im LISTEN-Zustand):" << std::endl;
connection.Close(); // Sollte zurück in den CLOSED-Zustand wechseln.
}
void bad_switch_case() {
bad_switch connection;
cout << "\n=== Start: Test von bad_switch ===\n";
connection.HandleEvent("ActiveOpen"); // Sollte in ESTABLISHED wechseln
connection.HandleEvent("Send"); // Senden im ESTABLISHED-Zustand
connection.HandleEvent("Close"); // Zurück in CLOSED wechseln
connection.HandleEvent("PassiveOpen");// Sollte in LISTEN wechseln
connection.HandleEvent("Close"); // Zurück in CLOSED wechseln
cout << "\n=== Ende des Tests ===\n";
}