Skip to content

Commit

Permalink
Fix some bugs + correct proto DL / UL + fix send ID impossible
Browse files Browse the repository at this point in the history
  • Loading branch information
HorlogeSkynet committed Nov 5, 2015
1 parent bd5065f commit 9251e3f
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 158 deletions.
12 changes: 6 additions & 6 deletions client/inc/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
#define EXIT "exit"
#define QUIT "quit"
#define STOP "stop"
#define SEND "send"
#define SENDP "sendp"
#define SEND "send "
#define SENDP "sendp "
#define CLEAR "clear"
#define CLOSE "close"
#define LOGOUT "logout"
#define TUNNEL "tunnel"
#define UPLOAD "upload"
#define DOWNLOAD "download"
#define ISPRESENT "ispresent"
#define TUNNEL "tunnel "
#define UPLOAD "upload "
#define DOWNLOAD "download "
#define ISPRESENT "ispresent "
#define DISCONNECT "disconnect"
#define INTERROGATIONPOINT "?"

Expand Down
1 change: 0 additions & 1 deletion client/src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void* serverCommunication(void *param)
{
printf("\n\n[sthread] [Client %d] is asking you for a tunnel but you're already tunneled.", _clientAsking);
}

}

else if(!strcmp(_buff, DISCONNECT))
Expand Down
20 changes: 12 additions & 8 deletions client/src/download.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void download(const char *command, int sock)

if(rename(_destFile, _temp) == -1)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be renamed as well.\033[0m\n\n", _path);
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be renamed as well.\033[0m\n\n", _fileName);
closeFile(_file, _fileName);
return;
}
Expand All @@ -47,31 +47,35 @@ void download(const char *command, int sock)
recv(sock, _buff, BUFFER, false);
if(!strcmp(_buff, FAIL))
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be uploaded by remote [Client %hd].\033[0m\n", _fileName, _foo);
closeFile(_file, _fileName);
if(remove(_destFile) == -1)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be removed as well.\033[0m\n", _fileName);
}

return;
}
else
{
long int _fsize = 0;
sscanf(_buff, "size: %ld", &_fsize);
printf("\n\033[32m[WIFSS] Reception of \"%s\" (%ld bytes) started in \"~%s/\" !\033[0m\n\n", _fileName, _path);
}

printf("\n\033[32m[WIFSS] Reception of \"%s\" started in \"~%s/\" !\033[0m\n\n", _fileName, _path);

int res;
int _res;
while(1)
{
do
{
res = recv(sock, _buff, BUFFER, false);
if(res <= 0)
_res = recv(sock, _buff, BUFFER, false);
if(_res <= 0)
{
printf("\n\033[31m[WIFSS] Error: \"%s\" could not be downloaded completely.\033[0m\n", _fileName);
closeFile(_file, _fileName);
return;
}

} while(res != BUFFER);
} while(_res != BUFFER);

if(!strcmp(_buff, FINISHED))
{
Expand Down
20 changes: 10 additions & 10 deletions client/src/upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ void upload(const char *command, int sock)
char _buff[BUFFER] = "";
char _fileName[PATHSIZE] = "";
char _destFile[PATHSIZE] = "";
const char *_path = "/Downloads/WIFSS/";
const char *_path = "/Downloads/WIFSS/client0/"; /* /!\ REMOVE "CLIENT0/" BEFORE FINISH /!\ */

sscanf(command, "upload %[^\n]", _fileName);
printf("\n\n[sthread] [Server] is asking us to upload: \"%s\". Trying to upload it...", _fileName);
printf("\n\n[sthread] [Server] is asking us to upload: \"%s\". Trying to upload it...\n", _fileName);

strcpy(_destFile, getenv("HOME"));
strcat(_destFile, _path);
strcat(_destFile, _fileName);
strcpy(_destFile, getenv("HOME")); //_destFile = "$HOME"
strcat(_destFile, _path); //_destFile = "$HOME/Downloads/WIFSS/"
strcat(_destFile, _fileName); //_destFile = "$HOME/Downloads/WIFSS/fileName"

FILE *_file = NULL;

Expand All @@ -24,7 +24,7 @@ void upload(const char *command, int sock)

if(_file == NULL)
{
printf("\n\n\033[31m[WIFSS] \"%s\" asked is unreachable.\033[0m\n", _fileName);
printf("\n\033[31m[WIFSS] \"%s\" asked is unreachable.\033[0m\n", _fileName);
sprintf(_buff, "%s", FAIL);
send(sock, _buff, BUFFER, false);
return;
Expand All @@ -44,22 +44,22 @@ void upload(const char *command, int sock)

recv(sock, _buff, BUFFER, false); //Receive "OK", cue-role

int res;
int _res;
while(ftell(_file) != SEEK_END)
{
memset(_buff, 0, BUFFER);
fread(_buff, sizeof(char), BUFFER, _file);

do
{
res = send(sock, _buff, BUFFER, false);
if(res <= 0)
_res = send(sock, _buff, BUFFER, false);
if(_res <= 0)
{
printf("\n\n[WIFSS] File could not be uploaded completely.\n");
break;
}

} while(res != BUFFER);
} while(_res != BUFFER);
}

memset(_buff, 0, BUFFER);
Expand Down
5 changes: 2 additions & 3 deletions server/inc/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
#define HALT "halt"
#define EXIT "exit"
#define HELP "help"
#define SEND "send"
#define SENDP "sendp"
#define SEND "send "
#define SENDP "sendp "
#define CLOSE "close"
#define CLEAR "clear"
#define UPLOAD "upload "
#define DOWNLOAD "download "
#define ISPRESENT "ispresent "
#define DATACLIENT "dataclient"
#define DISCONNECT "disconnect"
#define INTERROGATIONPOINT "?"

Expand Down
2 changes: 1 addition & 1 deletion server/inc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void download(const char*, int);
void disconnect(const char*);
void commandCursor(void);
void startServer(void);
void closeServer(void);
void closeServer(int);


#endif
36 changes: 17 additions & 19 deletions server/src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,33 @@ void* command_handler(void* data)
if(!strcmp(_buffer, QUIT) || !strcmp(_buffer, STOP)|| !strcmp(_buffer, HALT) || !strcmp(_buffer, EXIT) || !strcmp(_buffer, CLOSE))
{
broadcast(SID, "[Server] is going to shutdown !");
close_all_connections();
if(close(*listen_socket) == -1)
{
printf("\n\033[35m[WIFSS] Socket of server couldn't be successfully closed.\033[0m\n");
}
for(short int _i = 0; _i < MAX_CLIENTS; _i++)
{
pthread_cancel(threads[_i]);
}
break;
}

else if(str_beginwith(_buffer, SENDP))
else if(str_beginwith(_buffer, SEND))
{
memset(_cpy, 0, BUFFER);
char _buffTemp[BUFFER] = "";
short int _idTemp = -1;
sscanf(_buffer, "sendp %hd %[^\n]", &_idTemp, _cpy);
sprintf(_buffTemp, "[Server] whispers to you: \"%s\".", _cpy);
send(g_clients[_idTemp].sock, _buffTemp, BUFFER, false);
sscanf(_buffer, "send %[^\n]", _cpy);
sprintf(_buffTemp, "[Server] says: \"%s\".", _cpy);
broadcast(SID, _buffTemp);
}

else if(str_beginwith(_buffer, SEND))
else if(str_beginwith(_buffer, SENDP))
{
memset(_cpy, 0, BUFFER);
char _buffTemp[BUFFER] = "";
sscanf(_buffer, "send %[^\n]", _cpy);
sprintf(_buffTemp, "[Server] says: \"%s\".", _cpy);
broadcast(SID, _buffTemp);
short int _idTemp = -1;
sscanf(_buffer, "sendp %hd %[^\n]", &_idTemp, _cpy);
if(g_clients[_idTemp].status == TAKEN &&_idTemp >= 0 && _idTemp < MAX_CLIENTS)
{
sprintf(_buffTemp, "[Server] whispers to you: \"%s\".", _cpy);
send(g_clients[_idTemp].sock, _buffTemp, BUFFER, false);
}
else
{
printf("\n[WIFSS] This client identifier is invalid.\n\n");
}
}

else if(str_beginwith(_buffer, DISCONNECT) && str_validation(_buffer, ARGDCL))
Expand Down Expand Up @@ -91,7 +89,7 @@ void* command_handler(void* data)
}
}

closeServer();
closeServer(*listen_socket);

exit(false);
}
1 change: 0 additions & 1 deletion server/src/disconnect.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ void disconnect(const char *buffer)
{
if(g_clients[_idTemp].status == TAKEN)
{
send(g_clients[_idTemp].sock, "Server wants your disconnection.", BUFFER, false);
send(g_clients[_idTemp].sock, DISCONNECT, BUFFER, false);
close(g_clients[_idTemp].sock);
}
Expand Down
59 changes: 31 additions & 28 deletions server/src/download.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ void download(const char *command, int sender_id)
{
char _cpy[BUFFER] = "";
char _buffer[BUFFER] = "";
char filename[BUFFER] = "";
char _filename[BUFFER] = "";
long int _fsize = 0;
short int remote_id = -1;
short int _remote_id = -1;

sscanf(command, "download %hd %[^\n]", &remote_id, filename);
printf("\n\n[Client %d] is asking the uploading of [Client %d]'s \"%s\".\n\n", sender_id, remote_id, filename);
sscanf(command, "download %hd %[^\n]", &_remote_id, _filename);
printf("\n\n[Client %d] is asking the uploading of [Client %d]'s \"%s\".\n", sender_id, _remote_id, _filename);

if((remote_id > (MAX_CLIENTS - 1)) || (remote_id < 0) || (remote_id == sender_id))
if((_remote_id > (MAX_CLIENTS - 1)) || (_remote_id < 0) || (_remote_id == sender_id))
{
memset(filename, 0, BUFFER);
sprintf(filename, "%s", "Error: Client wanted is invalid...");
send(g_clients[sender_id].sock, filename, BUFFER, false);
memset(_filename, 0, BUFFER);
sprintf(_filename, "%s", "Error: Client wanted is invalid...");
send(g_clients[sender_id].sock, _filename, BUFFER, false);
return;
}

if(g_clients[remote_id].sock <= 0)
if(g_clients[_remote_id].sock <= 0)
{
memset(filename, 0, BUFFER);
sprintf(filename, "%s", "Error: Client asked is not connected...");
send(g_clients[sender_id].sock, filename, BUFFER, false);
memset(_filename, 0, BUFFER);
sprintf(_filename, "%s", "Error: Client asked is not connected...");
send(g_clients[sender_id].sock, _filename, BUFFER, false);
return;
}

sprintf(_cpy, "upload %s", filename);
send(g_clients[remote_id].sock, _cpy, BUFFER, false);
sprintf(_cpy, "upload %s", _filename);
send(g_clients[_remote_id].sock, _cpy, BUFFER, false); //LEAVES THE FUNCTION HERE ## BUG ##
/* Waiting for ACK... */
recv(g_clients[remote_id].sock, _buffer, BUFFER, false);
recv(g_clients[_remote_id].sock, _buffer, BUFFER, false);
if(!strcmp(_buffer, FAIL))
{
printf("\n\n[WIFSS] A problem occured with the file during its opening.\n");
Expand All @@ -43,26 +43,29 @@ void download(const char *command, int sender_id)
{
sscanf(_buffer, "size: %ld", &_fsize);
printf("\n\n[WIFSS] File size: %ld bytes.\n", _fsize);
memset(_buffer, 0, BUFFER);
sprintf(_buffer, "%s", OK);
send(g_clients[sender_id].sock, _buffer, BUFFER, false);
/* Waiting for ACK... */
memset(_buffer, 0, BUFFER);
recv(g_clients[sender_id].sock, _buffer, BUFFER, false);
//Receive and Send "OK" (cue-role) from sender, to remote
send(g_clients[_remote_id].sock, _buffer, BUFFER, false);
}

int res;
int _res;
while(1)
{
memset(_buffer, 0, BUFFER);

do
{
res = recv(g_clients[remote_id].sock, _buffer, BUFFER, false);
if(res <= 0)
_res = recv(g_clients[_remote_id].sock, _buffer, BUFFER, false);
if(_res <= 0)
{
printf("\n\n[WIFSS] File could not be downloaded completely.\n");
break;
printf("\n[WIFSS] File could not be downloaded completely.\n\n");
return;
}

} while(res != BUFFER);
} while(_res != BUFFER);

if(!strcmp(_buffer, ENDT))
{
Expand All @@ -72,14 +75,14 @@ void download(const char *command, int sender_id)
{
do
{
res = send(g_clients[sender_id].sock, _buffer, BUFFER, false);
if(res <= 0)
_res = send(g_clients[sender_id].sock, _buffer, BUFFER, false);
if(_res <= 0)
{
printf("\n\n[WIFSS] File could not be downloaded completely.\n");
break;
printf("\n[WIFSS] File could not be downloaded completely.\n\n");
return;
}

} while(res != BUFFER);
} while(_res != BUFFER);
}
}
}
56 changes: 56 additions & 0 deletions server/src/fctmanagement.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <server.h>

void broadcast(int sender, const char *msg)
{
for(short int _i = 0; _i < MAX_CLIENTS; _i++)
{
if(_i != sender)
{
send(g_clients[_i].sock, msg, BUFFER, false);
}
}
}

void close_all_connections()
{
printf("\n[WIFSS] Closing all connections...");

char _buffer[BUFFER] = "";
sprintf(_buffer, "%s", DISCONNECT);

for(short int _i = 0; _i < MAX_CLIENTS; _i++)
{
if(g_clients[_i].status == TAKEN)
{
send(g_clients[_i].sock, _buffer, BUFFER, false);
}
printf(".");
}
printf(" done.\n");
}

void closeServer(int listen_socket)
{
close_all_connections();

if(close(listen_socket) == -1)
{
printf("\n\033[35m[WIFSS] Socket of server couldn't be successfully closed.\033[0m\n");
}
else
{
printf("\n\033[32m[WIFSS] Socket of server successfully closed.\033[0m\n");
}

for(short int _i = 0; _i < MAX_CLIENTS; _i++)
{
pthread_cancel(threads[_i]);
}

printf("[WIFSS] Server stopped.\n");
for(short int _k = 0; _k < 60; _k++)
{
printf("=");
}
printf("\n\n");
}
Loading

0 comments on commit 9251e3f

Please sign in to comment.