Skip to content

Commit

Permalink
Merge pull request #19 from SamN-26/feat/change-name
Browse files Browse the repository at this point in the history
Feat/change name
  • Loading branch information
SamN-26 authored Aug 26, 2023
2 parents 828e8ea + 60e41e9 commit 6ea1480
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.exe
Binary file modified Server
Binary file not shown.
Binary file removed a.out
Binary file not shown.
64 changes: 48 additions & 16 deletions server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using namespace std;

class Client;
//gloabl variables
char buffer[BUFFER_SIZE];
int clients_fd[MAX_CLIENTS];
fd_set fdSet;
vector<Client> Client_pointers;
Expand All @@ -29,18 +30,25 @@ class Client

public :

//constructors
Client(int Fd, int Uid, char* Name);
Client(int Fd, int Uid);

void setName(char* s)
{
name = s;
}
//basic functionality functions
void send_message(char* msg);
void handle_commands(char* cmd, int fd);
void setName(char* s)
{
name = s;
}
//command based functions
void handle_commands(char* cmd);
void leave_client();
void send_message_private(char* msg);
void change_name(char* name);
};

//function definations

int search_by_fd(int fd)
{
int i = 0;
Expand Down Expand Up @@ -68,9 +76,6 @@ Client::Client(int Fd, int Uid, char* Name)
name = Name;
}


//functions

void checkClients()
{
for(int i = 0; i<Client_pointers.size(); i++)
Expand Down Expand Up @@ -171,10 +176,9 @@ void extract(char* msg, char* cmd)
cmd[i] = msg[i];
}
cmd[i] = '\0';
cout<<cmd<<endl;
}

void send_message_private(char* msg, int fd)
void Client::send_message_private(char* msg)
{
char name[NAME_LENGTH];
extract(msg, name);
Expand All @@ -186,19 +190,48 @@ void send_message_private(char* msg, int fd)
}
else
{
msg = "Name not Found\n";
sprintf(msg,"Name not Found\n");
write(fd, msg, strlen(msg));
}
}

void Client::handle_commands(char* msg, int fd)
void string_to_char(char* ch, string s)
{
int i = 0;
for(; i<s.length(); i++)
{
ch[i] = s[i];
}
ch[i] = '\0';
}

void Client::change_name(char* name)
{
if((int)name[0] != 0)
{
this->name = name;
}
else{
bzero(buffer, BUFFER_SIZE);
char n[this->name.length()+1];
string_to_char(n, this->name);
sprintf(buffer, "Name : %s\n", n);
write(fd, buffer, strlen(buffer));
}
}

void Client::handle_commands(char* msg)
{
cout<<msg<<endl;
char cmd[20];
extract(msg, cmd);
cout<<cmd<<endl;
if(strcmp(cmd,"pm") == 0)
{
send_message_private(&msg[3], fd);
send_message_private(&msg[3]);
}
else if(strcmp(cmd, "name") == 0)
{
change_name(&msg[5]);
}
}

Expand All @@ -208,7 +241,6 @@ int main(int argc, char* argv[])
//setting up variables
int server_fd, max_fd, current_client, activity, portno, opt;
opt = 1;
char buffer[BUFFER_SIZE];
portno = atoi(argv[1]);

//setting up server properties
Expand Down Expand Up @@ -394,7 +426,7 @@ int main(int argc, char* argv[])
}
else if(buffer[0] == '/')
{
cli->handle_commands(&buffer[1], cli->fd);
cli->handle_commands(&buffer[1]);
}
else
{
Expand Down

0 comments on commit 6ea1480

Please sign in to comment.