forked from CSS-D/Net-Task2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (119 loc) · 3.11 KB
/
Copy pathmain.cpp
File metadata and controls
123 lines (119 loc) · 3.11 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
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <winsock2.h>
#include <string>
#include <cstring>
#include <windows.h>
#include <cmath>
#include "Router.h"
#define HEADER "Router-> "
#define LEN 20
using namespace std;
/**
* @description: run the router
* @param {Router*} lpParam
* @return {*}
*/
DWORD WINAPI run(LPVOID lpParam)
{
Router *router_ptr = (Router *)lpParam;
router_ptr->run();
}
/**
* @description: run the timer
* @param {Rouer*} lpParam
* @return {*}
*/
DWORD WINAPI timer(LPVOID lpParam)
{
Router *router_ptr = (Router *)lpParam;
router_ptr->timer();
}
int main()
{
Router *router = new Router();
router->initial();
//Input the initial my_next_routers information
cout << "Initialization:" << endl;
//input ip
string str_local_ip_addr;
cout << "Local IP address: ";
cin >> str_local_ip_addr;
router->local_ip_addr = inet_addr(str_local_ip_addr.c_str());
//input algorithm
cout << "Routing Algorithm(DV/LS): ";
string str_algorithm;
cin >> str_algorithm;
if (str_algorithm == "DV")
router->algorithm = Router::DV;
else if (str_algorithm == "LS")
router->algorithm = Router::LS;
else
{
cout << "Invalid Type" << endl;
return 0;
}
//cmd
string command;
while (1)
{
cout << HEADER;
cin >> command;
if (command == "ADD" || command == "add")
{
string str_ip_addr;
u_short port;
int cost;
cin >> str_ip_addr >> port >> cost;
router->add(inet_addr(str_ip_addr.c_str()), port, cost);
}
else if (command == "Run" || command == "run")
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)run, (void *)router, 0, NULL);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)timer, (void *)router, 0, NULL);
}
else if (command == "show")
{
string str;
getline(cin, str);
if (str == " route table")
{
router->show_route_table();
}
else if (str == " next router")
{
router->show_next_routers();
}
else if (str == " link state")
{
router->show_link_state();
}
}
else if (command == "send")
{
Message message;
string str;
cin >> str;
message.message_type = Message::DATA_MESSAGE;
message.source_ip_addr = router->local_ip_addr;
message.dest_ip_addr = inet_addr(str.c_str());
message.cost = 0;
cin >> str;
memset(message.data, 0, sizeof(message.data));
strcpy(message.data, str.c_str());
router->send_data_message(message);
}
else if (command == "delete")
{
string str_ip_addr;
cin >> str_ip_addr;
router->delete_next_router(inet_addr(str_ip_addr.c_str()));
}
else if (command == "exit")
{
break;
}
}
system("pause");
}