@@ -23,21 +23,20 @@ typedef struct {
23
23
24
24
25
25
extern int errno ;
26
- stateInfo server ;
26
+ stateInfo server , aux_succi ;
27
27
28
28
29
29
int main (int argc , char * argv []){
30
30
31
- int fd , newfd , afd = 0 ;
31
+ int fd , newfd , afd = 0 , maxfd , counter , i = 0 , k = 0 ;
32
32
ssize_t n ;
33
33
socklen_t addrlen ;
34
34
struct addrinfo hints , * res ;
35
35
struct sockaddr_in addr ;
36
- char buffer [128 ], text [128 ], host [20 ];
36
+ char buffer [128 ], text [128 ], host [20 ], info [ 4 ][ 20 ] ;
37
37
fd_set rfds ;
38
38
enum {idle , busy } state ;
39
- int maxfd , counter ;
40
- int i = 0 ;
39
+ char * token ;
41
40
42
41
43
42
if (argc < 3 ){
@@ -117,6 +116,27 @@ int main(int argc, char *argv[]){
117
116
if (n == -1 ) exit (1 );
118
117
write (1 , "received: " ,10 );
119
118
write (1 , buffer , n );
119
+
120
+ token = strtok (buffer ," " );
121
+
122
+ if (strcmp (token ,"NEW" )== 0 && k < 4 ){ //a condição k<4 é só para ignorar o \n que está no buffer
123
+ //atualiza a info sobre o seu sucessor
124
+ while ( token != NULL ){
125
+ strcpy (info [k ],token );
126
+ token = strtok (NULL ," " );
127
+ k ++ ;
128
+ }
129
+
130
+ printf (" info 1:%s \n info 2:%s \n info 3:%s \n" ,info [1 ],info [2 ],info [3 ] );
131
+
132
+ server .succ_key = atoi (info [1 ]);
133
+ strcpy (server .succIP , info [2 ]);
134
+ strcat (server .succIP , ":" );
135
+ strcat (server .succIP , info [3 ]);
136
+
137
+
138
+ }
139
+
120
140
}
121
141
else {
122
142
close (afd );
@@ -127,7 +147,7 @@ int main(int argc, char *argv[]){
127
147
128
148
129
149
if (FD_ISSET (0 ,& rfds )){
130
- i = UserInput ();
150
+ i = UserInput (fd , res ); // NOTA: ACHO QUE NÃO É ESTE fd QUE QUEREMOS <--- CHECKAR
131
151
if (i == 1 ){
132
152
printf ("Invalid command\n" );
133
153
}
@@ -148,17 +168,18 @@ int main(int argc, char *argv[]){
148
168
exit (0 );
149
169
}
150
170
151
- int UserInput (){
171
+ int UserInput (int fd , struct addrinfo * res ){
152
172
153
- int i = -1 ;
154
- char option [10 ]= "\0" , input [128 ]= "\0" ;
173
+ int i = -1 , succi = 0 , succiPORTO = 0 ;
174
+ char option [10 ]= "\0" , input [128 ]= "\0" , succiIP [ 9 ] = "\n" ;
155
175
156
176
if (fgets (input , 128 , stdin )== NULL ){
157
177
return 1 ;
158
178
}
159
179
160
180
if (!sscanf (input , " %s" , option )) return 1 ;
161
181
182
+
162
183
if (strcmp (option , "new" )== 0 ){
163
184
164
185
if (sscanf (input , " %s %d" , option , & i )== 2 && i < MAX_KEY ){
@@ -178,8 +199,12 @@ int UserInput(){
178
199
179
200
else if (strcmp (option , "sentry" )== 0 ){
180
201
181
- printf ("%s selected\n" , option );
182
- return 0 ;
202
+ if (sscanf (input , " %s %d %d %s %d" , option , & i ,& succi , succiIP , & succiPORTO )== 5 && i < MAX_KEY ){
203
+ printf ("%s selected\n" , option );
204
+ sEntry (fd ,i , succi , succiIP , succiPORTO );
205
+ return 0 ;
206
+ }
207
+ return 1 ;
183
208
}
184
209
185
210
else if (strcmp (option , "leave" )== 0 ){
@@ -207,8 +232,8 @@ int UserInput(){
207
232
return -1 ;
208
233
}
209
234
210
- return 1 ;
211
235
236
+ return 1 ;
212
237
}
213
238
214
239
int CreateRing (int i ){
@@ -238,4 +263,41 @@ void ShowState(){
238
263
printf ("\t \t succ2_key: %d\n" , server .succ2_key );
239
264
}
240
265
return ;
266
+ }
267
+ void sEntry (int fd , int i , int succi , char * succiIP , int succiPORTO ){
268
+ int n = 0 ;
269
+ char text [50 ], PORT [9 ];
270
+ struct addrinfo hints , * res ;
271
+ char * token ,* token1 ;
272
+
273
+ printf ("%d %d %s %d\n" ,i ,succi , succiIP ,succiPORTO );
274
+
275
+ fd = socket (AF_INET ,SOCK_STREAM ,0 );//TCP socket
276
+ if (fd == -1 ){printf ("couldn't create socket\n" ); exit (1 );} //error
277
+
278
+ memset (& hints , 0 , sizeof (hints ));
279
+ hints .ai_family = AF_INET ;
280
+ hints .ai_socktype = SOCK_STREAM ;
281
+ hints .ai_flags = AI_PASSIVE ;
282
+
283
+ //transforma o succiPORTO numa string para usar no getadrinfo
284
+ sprintf (PORT ,"%d" ,succiPORTO );
285
+
286
+ errno = getaddrinfo (succiIP ,PORT ,& hints ,& res ) ;
287
+ if (errno != 0 ) /*error*/ exit (1 );
288
+
289
+ n = connect (fd ,res -> ai_addr ,res -> ai_addrlen );
290
+ if (n == -1 )/*error*/ exit (1 );
291
+ printf ("connected\n" );
292
+
293
+ token = strtok (server .nodeIP ,":" );
294
+ token1 = strtok (NULL , ":" );
295
+ sprintf (text ,"NEW %d %s %s \n" ,i , token , token1 );
296
+ printf ("text= %s\n" ,text );
297
+
298
+ n = write (fd ,text ,strlen (text ));
299
+ if (n == -1 )/*error*/ exit (1 );
300
+
301
+ //freeaddrinfo(res);
302
+ //close (fd);
241
303
}
0 commit comments