-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.c
213 lines (176 loc) · 4.93 KB
/
peer.c
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
#include <stdlib.h>
#include "peer.h"
#include "sockwrap.h"
/* ------------------------- api ------------------------- */
localnode* createlocalnode(int ipa, int ipb, int ipc, int ipd, int bipa, int bipb, int bipc, int bipd)
{
localnode* ln = (localnode*)malloc(sizeof(localnode));
SockAddr saself;
saself.ip.a = ipa;
saself.ip.b = ipb;
saself.ip.c = ipc;
saself.ip.d = ipd;
//saself.port = port;
//set bootstrap ip
SockAddr saboot;
saboot.ip.a = bipa;
saboot.ip.b = bipb;
saboot.ip.c = bipc;
saboot.ip.d = bipd;
//saboot.port = bport;
ln->addr = saself;
ln->bootAddr = saboot;
return ln;
}
void boot(localnode* n)
{
printf("1\n");
//SockAddr bootAddr; //addr of bootstrap node
SockAddr thisaddr_upper; //addr of this node for upper layer access
//listen for connections
n->addr.port = NODELOCAL_DEFAULT_PORT_ROUTING_LAYER_PEER;
n->bootAddr.port = NODELOCAL_DEFAULT_PORT_ROUTING_LAYER_PEER;
n->port_upper = NODELOCAL_DEFAULT_PORT_ROUTING_LAYER_UPPER;
//get bootstrap node address
//getBootstraplocalnodeIp(&bootAddr);
if(ipaddr_cmp(&(n->bootAddr.ip), &n->addr.ip)==1 && n->bootAddr.port == n->addr.port) {
printf("cmp true, boot = this\n");
//listen on peer port
sw_listen(&n->addr);
thisaddr_upper = n->addr;
thisaddr_upper.port = n->port_upper;
//listen on upper port
sw_listen(&thisaddr_upper);
}
else {
//connect and save socket fd
//n->bsSfd = sw_conn(bootAddr);
//sw_listen(&n->addr); //mmm:uncomment it!
//n->bootAddr = bootAddr;
printf("before join\n");
join(n);
}
}
void join(localnode* n)
{
//send join msg to bootstrap node
msgheader* h = (msgheader*)malloc(sizeof(msgheader));
h->type = MT_JOINREQ;
//h->srcIp = n->ip;
//h->srcId = n->id;
msg_join_req m;
m.h = *h;
m.srcId = n->id;
m.srcAddr = n->addr;
m.fwdId = n->id;
m.fwdAddr = n->addr;
send_join_req(n, &m, &n->bootAddr); //mmm:right? temp var?
}
void leave(localnode* n)
{
msg_leave_req m;
m.srcId = n->id;
m.srcAddr = n->addr;
send_leave_req(n, &m, &n->succAddr);
}
/* ------------------------- message sending utilities ------------------------- */
void send_join_req(localnode* n, msg_join_req* m, SockAddr* addr)
{
int sockfd;
sockfd = sw_conn(addr);
sw_send(sockfd, (char*)m, sizeof(msg_join_req));
}
void fwd_join_req(localnode* n, msg_join_req* m, SockAddr* ip)
{
;
}
void send_join_resp(localnode* n, msg_join_resp* m, SockAddr* ip)
{
;
}
//send messages
void send_leave_req(localnode* n, msg_leave_req* m, SockAddr* ip)
{
}
//send messages
void fwd_leave_req(localnode* n, msg_leave_req* m, SockAddr* ip)
{
}
//send messages
void send_leave_resp(localnode* n, msg_leave_resp* m, SockAddr* ip)
{
}
/* ------------------------- message handling ------------------------- */
void on_receive_join_resp(localnode* n, msg_join_resp* m)
{
//get join response
/* if destId not id */
n->succId = m->succId;
n->succAddr = m->succAddr;
//update successor
}
void on_receive_join_req(localnode* n, msg_join_req* m)
{
msgheader* _h;
msg_join_resp* _m;
if(m->fwdId == m->srcId) {
//sent from joining node
m->fwdId = n->id;
m->fwdAddr = n->addr;
//mmm:toimpl
//forward join
}
else {
//forwarded
if(m->fwdId>m->srcId) {
m->fwdId = n->id;
m->fwdAddr = n->addr;
//mmm:toimpl--forward
}
else {
//fwdId < srcId
if(n->id < m->srcId) {
m->fwdId = n->id;
m->fwdAddr = n->addr;
}
else if(n->id > m->srcId){
_h = (msgheader*)malloc(sizeof(msgheader));
_h->type = MT_JOINRESP;
_m = (msg_join_resp*)malloc(sizeof(msg_join_resp));
_m->succId = n->id;
_m->succAddr = n->addr;
send_join_resp(n, _m, &m->srcAddr);
//mmm:toimpl send
}
else {
printf("dup hash values!\n");
}
//mmm:toimpl forward
}
}
}
void on_receive_leave_req(localnode* n, msg_leave_req* m)
{
msg_leave_resp* _m;
msgheader* _h = (msgheader*)malloc(sizeof(msgheader));
_h->type = MT_LEAVEREQ;
if( m->srcId == n->succId ) {
//update succId
//n->succId = //get succ's succ
//send leave message
_m = (msg_leave_resp*)malloc(sizeof(msg_leave_resp));
_m->h = *_h;
_m->srcId = n->id;
_m->srcAddr = n->addr;
send_leave_resp(n, _m, &m->srcAddr);
}
else {
fwd_leave_req(n, m, &n->succAddr);
}
}
void on_receive_leave_resp(localnode* n, msg_leave_resp* m)
{
//validate src(may it be its pred's pred?)
//can't validate!!
printf("node %d exit exit successfully\n", n->id);
}