Skip to content

Commit

Permalink
commiting
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme-Viegas committed Apr 4, 2020
1 parent e952921 commit baab734
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 42 deletions.
Binary file modified main
Binary file not shown.
21 changes: 10 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void flush_stdin();
char aux[100];
char c;
int n;
char buff[100];

struct addrinfo *res;
UdpData* udp;
Expand All @@ -41,17 +42,16 @@ int main(int argc, char *argv[]) {
strcpy(myServer->myPort, argv[2]);


while(1) { // The code will run until the "exit" command is summoned
char buff[100];
//while(1) { // The code will run until the "exit" command is summoned
// Show the user interface
printf("\n\nAvailable commands:\n\n new i \n sentry i succi succi.IP succi.TCP\n entry i succi succie.IP succi.TCP\n show\n find k\n leave\n exit\n");

flush_stdin(); //TODO not doing what we want
printf("\n\nAvailable commands:\n\n new i \n sentry i succi succi.IP succi.TCP\n entry i succi succie.IP succi.TCP\n leave\n exit\n");
strcpy(buff, "\0");
fgets(buff, 100 , stdin);
const char delim[2] = " ";
if(strcmp(strtok(strdup(buff), delim), "new") == 0) { // If its the "new" command then create a ring with that server
sscanf(buff, "%s %d", aux, &(myServer->myKey)); // Get the server key
createServer(myServer);
createServer(myServer, 0);
} else if(strcmp(strtok(strdup(buff), delim), "sentry") == 0) { // If its the sentry command open a connection with nextServer
sscanf(buff, "%s %d %d %s %s", aux, &(myServer->myKey), &(myServer->nextKey), myServer->nextIp, myServer->nextPort); // Get the successor details
printf("Trying to enter\n");
Expand All @@ -61,8 +61,7 @@ int main(int argc, char *argv[]) {
int n = write(myServer->nextConnFD, buff, strlen(buff)); // Give the successor your details
if(n == -1)/*error*/exit(1);

createServer(myServer); //Now that the entry connections are established and stable it's time enter in listening mode
flush_stdin();
createServer(myServer, 1); //Now that the entry connections are established and stable it's time enter in listening mode
} else if(strcmp(strtok(strdup(buff), delim), "entry") == 0) {
char connectIp[100], connectPort[100];
int connectKey;
Expand All @@ -85,14 +84,14 @@ int main(int argc, char *argv[]) {
int n = write(myServer->nextConnFD, buff, strlen(buff)); // Give the successor your details
if(n == -1)/*error*/exit(1);

createServer(myServer); //Now that the entry connections are established and stable it's time enter in listening mode
createServer(myServer, 1); //Now that the entry connections are established and stable it's time enter in listening mode

freeaddrinfo(udp->res);
close(udp->fd);
} else if(strcmp(strtok(strdup(buff), delim), "exit") == 0) {
} else if(strstr(buff, "exit") != NULL) {
exit(0);
}
}
//}

return 0;
}
Expand Down
Binary file added main4
Binary file not shown.
134 changes: 106 additions & 28 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int tmp;
int searchFlag = 0;


void createServer(Server* server) {
void createServer(Server* server, int _onGoingOperation) {
fd=socket(AF_INET,SOCK_STREAM,0); //TCP socket
if (fd==-1) exit(1); //error

Expand Down Expand Up @@ -67,22 +67,27 @@ void createServer(Server* server) {
n = bind(udpFd,res->ai_addr,res->ai_addrlen);
if(n==-1) /*error*/ exit(1);

int ongoingOperation = _onGoingOperation;
int serverInRing = 1;

while(1) {
// Set all sockets to 0
FD_ZERO(&rfds);

// Add the main socket to set
FD_SET(fd, &rfds);
maxfd = fd;

// Add main UDP socket to set
FD_SET(udpFd, &rfds);
maxfd = max(fd, udpFd);
// We only check the main sockets if the server is in the ring,
// if a server is not in the ring it cannot receive connections
if(serverInRing == 1) {
// Add the main socket to set
FD_SET(fd, &rfds);
maxfd = fd;

// Add main UDP socket to set
FD_SET(udpFd, &rfds);
maxfd = max(fd, udpFd);
}

FD_SET(STDIN_FILENO , &rfds);

maxfd = max(STDIN_FILENO, maxfd);
// Add all available child sockets to set
// If there are available sockets
if(client_sockets[0] > 0) {
Expand All @@ -94,32 +99,73 @@ void createServer(Server* server) {
if(server->prevConnFD > 0) {
client_sockets[1] = server->prevConnFD;
FD_SET(server->prevConnFD, &rfds);
} else {
client_sockets[1] = -1;
}
maxfd = max(server->prevConnFD, maxfd);

// If there's an available next connection, save it in the file descriptors array and set it
if(server->nextConnFD > 0) {
client_sockets[2] = server->nextConnFD;
FD_SET(server->nextConnFD, &rfds);
} else {
client_sockets[2] = -1;
}
maxfd = max(server->nextConnFD, maxfd);

if(ongoingOperation == 0) { //If there's no operation going on at the moment, print the menu
if(serverInRing == 1)
printf("\n show\n find k\n leave\n");
else
printf("\nAvailable commands:\n\n new i \n sentry i succi succi.IP succi.TCP\n entry i succi succie.IP succi.TCP\n exit\n");
} else {
printf("Linha 122\n");
}

counter = select(maxfd + 1, &rfds, (fd_set*)NULL, (fd_set*)NULL, (struct timeval *)NULL);
if(counter <=0) exit(1);
else if(FD_ISSET(STDIN_FILENO , &rfds)) { // If there's something available to read on the keyboard
fgets(buffer, 100 , stdin);
if(strstr(buffer, "leave") != NULL) {
close(server->prevConnFD);
close(server->nextConnFD);
break;
} else if(strstr(buffer, "find") != NULL) {
sscanf(buffer, "%s %d", str, &searchKey);
startKeySearch(server, searchKey, 0, NULL, 0, 0);
} else if(strstr(buffer, "send") != NULL) {
n = write(server->prevConnFD, "Prev\n", 6);
n = write(server->nextConnFD, "Next\n", 6);
} else if(strstr(buffer, "show") != NULL) {
printServerData(server);
if(serverInRing == 1) {
if(strstr(buffer, "leave") != NULL) {
if((server->nextConnFD > 0) && (server->prevConnFD > 0)) { // If there's only one server in the ring
close(server->prevConnFD);
close(server->nextConnFD);
}
serverInRing = 0;
printf("Linha 136\n");
cleanServer(server);
} else if(strstr(buffer, "find") != NULL) {
sscanf(buffer, "%s %d", str, &searchKey);
ongoingOperation = 1;
startKeySearch(server, searchKey, 0, NULL, 0, 0, &ongoingOperation);
} else if(strstr(buffer, "send") != NULL) {
n = write(server->prevConnFD, "Prev\n", 6);
n = write(server->nextConnFD, "Next\n", 6);
} else if(strstr(buffer, "show") != NULL) {
printServerData(server);
}
} else {
printf("Linha 142\n");
if(strstr(buffer, "new") != NULL) {
sscanf(buffer, "%s %d", str, &(server->myKey));
serverInRing = 1;
} else if(strstr(buffer, "sentry ")) {
sscanf(buffer, "%s %d %d %s %s", str, &(server->myKey), &(server->nextKey), server->nextIp, server->nextPort); // Get the successor details
printf("Trying to enter\n");
server->nextConnFD = connectToNextServer(server); // Set the next server as the given server and establish a connection

sprintf(buffer, "NEW %d %s %s\n", server->myKey, server->myIp, server->myPort);
int n = write(server->nextConnFD, buffer, strlen(buffer)); // Give the successor your details
if(n == -1)/*error*/exit(1);

serverInRing = 1;
ongoingOperation = 1;
} else if(strstr(buffer, "entry ")) {

} else if(strstr(buffer, "exit")) {

}
}
continue;
}
Expand All @@ -140,7 +186,7 @@ void createServer(Server* server) {
n=recvfrom(udpFd,buffer,128,0, (struct sockaddr*)&udpAddr,&udpAddrlen);
if(n==-1)/*error*/exit(1);
sscanf(buffer, "%s %d", str, &searchKey);
startKeySearch(server, searchKey, 1, (struct sockaddr*)&udpAddr,udpAddrlen, udpFd);
startKeySearch(server, searchKey, 1, (struct sockaddr*)&udpAddr,udpAddrlen, udpFd, &ongoingOperation);
searchFlag = 1;
}

Expand All @@ -149,6 +195,12 @@ void createServer(Server* server) {
client_sockets[0] = 0;
}

for(int i = 0; i < 3; i++) {
printf("Fd: %d\n", client_sockets[i]);
}

printf("Next: %d | Prev: %d", server->nextConnFD, server->prevConnFD);


//Else its on some other socket
for(int i = 0; i < 3; i++) {
Expand All @@ -160,6 +212,9 @@ void createServer(Server* server) {
//If there are only 2 servers in the ring...
close(server->nextConnFD);
server->nextConnFD = -1;
int auxKey = server->myKey;
cleanServer(server);
server->myKey = auxKey;
} else if(newfd == server->nextConnFD) {
close(server->nextConnFD);
copyDoubleToNext(server);
Expand All @@ -176,6 +231,7 @@ void createServer(Server* server) {
client_sockets[i] = 0;
} else {
buffer[n] = '\0';
write(1, "Received: ", 11); write(1, buffer, sizeof(buffer));
//Now check what the incoming message is asking for
if(strstr(buffer, "NEW") != NULL) {
if(newfd == server->nextConnFD) { // If the connection is comming from the next server
Expand All @@ -196,7 +252,7 @@ void createServer(Server* server) {
} else {
if((server->nextConnFD <= 0) && (server->prevConnFD <= 0)) { //If there's only 1 server in the ring
// Set the double next as myself
copyDoubleToSelf(server);
copySelfToDouble(server);

// Create a second connection to the incoming server to be used as next
sscanf(buffer, "%s %d %s %s", str, &(server->nextKey), server->nextIp, server->nextPort);
Expand All @@ -219,6 +275,8 @@ void createServer(Server* server) {
} else {
server->prevConnFD = newfd;
}
ongoingOperation = 0;
printf("Linha 266\n");
} else if(strstr(buffer, "FND ") != NULL) {
char ip[30], port[10];
printf("%s \n", buffer);
Expand All @@ -237,10 +295,10 @@ void createServer(Server* server) {
int connectKey;
char connectIp[30], connectPort[10];
sscanf(buffer, "%s %d %d %s %s", str, &searchKey, &connectKey, connectIp, connectPort);
printf("Received from %d\n", tmp);

sprintf(buffer, "EKEY %d %d %s %s\n", searchKey, connectKey, connectIp, connectPort);
n = sendto(udpFd,buffer,strlen(buffer),0, (struct sockaddr*)&udpAddr, udpAddrlen);
ongoingOperation = 0;
}
}
}
Expand Down Expand Up @@ -319,7 +377,7 @@ void copyNextToDouble(Server *server) {
strcpy(server->doubleNextPort, server->nextPort);
}

void copyDoubleToSelf(Server *server) {
void copySelfToDouble(Server *server) {
server->doubleNextKey = server->myKey;
strcpy(server->doubleNextIp, server->myIp);
strcpy(server->doubleNextPort, server->myPort);
Expand Down Expand Up @@ -353,9 +411,14 @@ UdpData* connectToUdpServer(char ip[30], char port[10]) {
return udp;
}

void startKeySearch(Server * server, int searchKey, int entry, struct sockaddr* udpAddr, socklen_t udpAddrlen, int fd) {
if((server->nextConnFD <= 0) && (server->prevConnFD <= 0)) { //If only 1 server in the received
void startKeySearch(Server * server, int searchKey, int entry, struct sockaddr* udpAddr, socklen_t udpAddrlen, int fd, int *_onGoingOperation) {
if((server->nextConnFD <= 0) && (server->prevConnFD <= 0)) { //If only 1 server in the ring
printf("I'm the nearest server (my key: %d) to %d key\n", server->myKey, searchKey);
if(entry == 1) {
sprintf(buffer, "EKEY %d %d %s %s\n", searchKey, server->myKey, server->myIp, server->myPort);
n = sendto(fd,buffer,strlen(buffer),0, udpAddr, udpAddrlen);
}
*_onGoingOperation = 0;
} else if(distance(searchKey, server->nextKey) > distance(searchKey, server->myKey)) {
//Send FND for the successor
sprintf(str, "FND %d %d %s %s\n", searchKey, server->myKey, server->myIp, server->myPort);
Expand All @@ -368,4 +431,19 @@ void startKeySearch(Server * server, int searchKey, int entry, struct sockaddr*
n = sendto(fd,buffer,strlen(buffer),0, udpAddr, udpAddrlen);
}
}
}
}

void cleanServer(Server * server) {
server->myKey = 0;

strcpy(server->nextIp, "");
strcpy(server->nextPort, "");
server->nextKey = 0;

strcpy(server->doubleNextIp, "");
strcpy(server->doubleNextPort, "");
server->doubleNextKey = 0;

server->prevConnFD = 0;
server->nextConnFD = 0;
}
7 changes: 4 additions & 3 deletions server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ typedef struct udpData {
}UdpData;


void createServer(Server*);
void createServer(Server*, int);
int connectToNextServer(Server*);
void serverIsEntering(char[128], int *, Server*);
void printServerData(Server*);
void copyDoubleToNext(Server *);
int distance(int, int);
int connectToGivenServer(char[30], char[10]);
UdpData* connectToUdpServer(char ip[30], char port[10]);
void startKeySearch(Server *, int, int, struct sockaddr*, socklen_t, int);
void startKeySearch(Server *, int, int, struct sockaddr*, socklen_t, int, int *);
void copyNextToDouble(Server *server);
void copyDoubleToSelf(Server *server);
void copySelfToDouble(Server *server);
void cleanServer(Server * server);

#endif

0 comments on commit baab734

Please sign in to comment.