Skip to content

Commit

Permalink
Sentry in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-Maria-Janeiro committed Mar 12, 2020
1 parent 8b14374 commit e04b301
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
Binary file modified main
Binary file not shown.
28 changes: 27 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#include <string.h>
#include "server.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <math.h>
#include <sys/select.h>
#include <errno.h>

int checkIpPortValid(int, char **);
int valid_digit(char *);
int checkIp(char *);
Expand All @@ -20,12 +31,27 @@ int main(int argc, char *argv[]) {
while(1) { // The code will run until the "exit" command is summoned
char buffer[100];
// Show the user interface
printf("Available commands:\n\n new i - create a new ring with the i server only \n exit\n");
printf("Available commands:\n\n new i \n sentry i succi succi.IP succi.TCP \n exit\n");
fgets(buffer, 100 , stdin);
const char delim[2] = " ";
if(strcmp(strtok(strdup(buffer), delim), "new") == 0) { // Check if the command is the new command
sscanf(buffer, "%s %d", aux, &(myServer->key)); // Get the server key
createServer(myServer);
} else if(strcmp(strtok(strdup(buffer), delim), "sentry") == 0) { // Check if the command is the sentry command
sscanf(buffer, "%s %d %d %s %s", aux, &(myServer->key), &(myServer->nextKey), myServer->nextIp, myServer->nextPort); // Get the successor details
myServer->nextConnFD = connectToGivenServer(myServer); // Set the next server as the given server and establish a connection

int n = write(myServer->nextConnFD, "SUCCCONF\n", 10); // Tell the successor to define this server as its predecessor
if(n == -1)/*error*/exit(1);

n = read(myServer->nextConnFD, buffer, 128); // We already have all the data from the next server so we don't need to extract it here in the sentry
if(n==-1)/*error*/exit(1);

write(1,"echo: ",6); write(1,buffer,n);

// freeaddrinfo(res);
close(myServer->nextConnFD);

} else if(strcmp(buffer, "exit\n") == 0) {
exit(0);
}
Expand Down
41 changes: 39 additions & 2 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,49 @@ void createServer(Server* server) {
} else {
buffer[n] = '\0';
write(1,"received: ",11); write(1,buffer,n); // Print incoming message
n = write(newfd,"Server Response\n",17);
if(n==-1)/*error*/exit(1);
if(strcmp(buffer, "SUCCCONF\n") == 0) { // If this server is set as a successor
if(server->prevConnFD == 0) { // If the previous server is not set
server->prevConnFD = newfd; // Set the incoming request as the previous server
sprintf(buffer, "SUCC %d %s %s\n", server->key, server->myIp, server->myPort);
n = write(server->prevConnFD,buffer, strlen(buffer)); // Server gives its predecessor his info
if(n==-1)/*error*/exit(1);
} else { // If it already has a predecessor

// This two operations that follow should only be done after the NEW command, which is where we no longer need to connect to the old predecessor
// // close(server->prevConnFD); // Close connection with predecessor
// // server->prevConnFD = newfd; // Set the incoming request as the previous server

}
}
// n = write(newfd,"Server Response\n",17);
// if(n==-1)/*error*/exit(1);
}
}
}
}
freeaddrinfo(res);
close(fd);
}

int connectToGivenServer(Server* server) { // sentry 5 10 127.0.0.1 8005
fd = socket(AF_INET,SOCK_STREAM,0); //TCP socket
if (fd == -1) exit(1); //error

memset(&hints,0,sizeof hints);
hints.ai_family=AF_INET; //IPv4
hints.ai_socktype=SOCK_STREAM; //TCP socket

errcode = getaddrinfo(server->nextIp, server->nextPort,&hints,&res);
if(errcode != 0) {
printf("GET ADDRESS INFO ERROR\n");
exit(1);
}

n = connect(fd,res->ai_addr,res->ai_addrlen);
if(n == -1) {
printf("CONNECT ERROR\n");
exit(1);
}

return fd;
}
14 changes: 11 additions & 3 deletions server.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ typedef struct server {
char myPort[10];
int key;

char* nextIp;
char* previousIp;
char* doubleNextIp;
char nextIp[30];
char nextPort[10];
int nextKey;

char doubleNextIp[30];
char doubleNextPort[10];
int doubleNextKey;

int prevConnFD; // Holds the TCP connection to previous server
int nextConnFD; // Holds the TCP connection to next server
}Server;


void createServer(Server*);
int connectToGivenServer(Server*);


#endif
Binary file modified test/tester
Binary file not shown.
4 changes: 2 additions & 2 deletions test/tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main(int argc, char *argv[]) {
n=connect(fd,res->ai_addr,res->ai_addrlen);
if(n==-1)/*error*/exit(1);

// while(1) {
while(1) {
char user_input[128];
fgets(user_input, 100 , stdin);
n=write(fd,user_input, strlen(user_input));
Expand All @@ -47,7 +47,7 @@ void main(int argc, char *argv[]) {
if(n==-1)/*error*/exit(1);

write(1,"Teste: ",8); write(1,buffer,n); write(1, "\n", 3);
// }
}

freeaddrinfo(res);
close(fd);
Expand Down

0 comments on commit e04b301

Please sign in to comment.