-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsdv.cpp
144 lines (114 loc) · 2.77 KB
/
dsdv.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
140
141
142
143
144
#include "rip.h"
#include "socket.h"
#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include <mutex>
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include <arpa/inet.h>
#include <sys/socket.h>
using namespace std;
mutex mtx;
//global variable
//store the table from disk temporarily
map<string, struct RouterTab> myTable;
//store adjacent nods map
map<string, struct RouterTab> adjBuf;
//store the route table
map<string, struct RouterTab> routerTable;
//store the map from port number to the name of router
map<int, string> portToName;
//store the host message
struct Host host;
//store the socket fd
int fd;
//store the print time
int printNum;
void* receive(void* ptr)
{
//cout<<"thread 1\n";
while(1)
{
string content;
int fromPort = socketReceive(fd, content);
//cout<<"receive from: "<<fromPort<<endl;
//cout<<"receive content: "<<content<<endl;
//cout<<"========================\n";
//update route table
string src = portToName.find(fromPort)->second;
Host srcRouter;
srcRouter.name = src;
map<string, struct RouterTab> srcTab;
//cout<<"========================\n";
//cout<<"receive from: "<<src<<endl;
//cout<<"receive content: "<<content<<endl;
//cout<<"========================\n";
ripReceive(content, srcTab, srcRouter);
mtx.lock();
ripUpdate(srcRouter, srcTab, adjBuf, routerTable, host);
mtx.unlock();
/*
string sentContent;
ripSend(sentContent, routerTable);
//cout<<sentContent;
if(fromPort != -1)
socketSend(fd, fromPort, sentContent);
*/
}
}
int main(int argc, char *argv[])
{
if(argc != 3)
{
printf("usage: %s <prot_number> <file_name>\n", argv[0]);
exit(-1);
}
int port = atoi(argv[1]);
if(port <= 0)
{
printf("error: invalid <port_number>\n");
exit(-1);
}
string filename = string(argv[2]);
try
{
//init
ripInit(filename, routerTable, portToName, host);
ripInit(filename, adjBuf, portToName, host);
cout<<"This is router "<<host.name<<" listening port "<<port<<endl;
fd = socketBind(port);
int fromPort = -1;
pthread_t child;
pthread_create(&child, NULL, receive, NULL);
while(1)
{
//refresh the table from data
Host temp;
myTable.clear();
mtx.lock();
ripInit(filename, myTable, portToName, temp);
ripRefresh(adjBuf, myTable, routerTable, host);
mtx.unlock();
//print table
ripPrintTable(routerTable, printNum, host);
//prepare to send
string sendContent;
ripSend(sendContent, routerTable, host);
//send router table to adjacent nodes
map<int, string>::iterator it = portToName.begin();
for(; it != portToName.end(); it ++)
{
socketSend(fd, it->first, sendContent);
}
sleep(1);
}
close(fd);
}
catch(int e)
{
cout<<"Oops! error code: "<<e<<endl;
}
return 0;
}