inline void addMessageCheck(Chat& chat, const Message& msg) { if(!chat.addMessage(msg)) cout << "message could not be added to chat" << endl; else cout << "message added to chat" << endl; }
int handle(int socket, const string& str, const User& user) { vector<string> tokens; string answer; string cmd; string arg_1; string arg_2; tokens = preProcess(str); switch(tokens.size()) { case 0: answer = INVALID_CMD_ERR; break; case 1: cmd = lower(tokens[0]); if(cmd == EXIT_CMD) answer = OK; if(cmd == HELP_CMD) answer = HELP; else answer = INVALID_CMD_ERR; break; case 2: cmd = lower(tokens[0]); arg_1 = tokens[1]; if(cmd == JOIN_GROUP_CMD) answer = OK; break; //number of args >= 3 default: cmd = lower(tokens[0]); arg_1 = tokens[1]; if(cmd == SEND_MSG_CMD) { string msg_content = join(tokens, " ", 2); Message msg(user.getName(), arg_1, msg_content); cout << "message: " << msg.getContent() << endl; chat_mtx.lock(); if(!chat.hasUser(msg.getSrcUserName())) answer = NO_SRC_USER_ERR; else if(!chat.hasUser(msg.getDstUserName())) answer = NO_DST_USER_ERR; else if(!chat.addMessage(msg)) answer = MSG_EXISTS_ERR; else answer = MSG_QUEUED; chat_mtx.unlock(); } else answer = INVALID_CMD_ERR; break; } if(send(socket, answer) < 0) error("send"); if(send(socket, "hur brbrb\n") < 0) error("send"); return 0; }
int handle(int socket, const string& str, const User& user) { char header; string answer; int ret = 0; header = netToHostHeader(str); switch(header) { case EXIT: { answer = hostToNetMsg(OK); ret = -1; break; } case WHO: { stringstream ss; vector<string> header = {"user", "status"}; chat_mtx.lock(); ChatView view(chat, ss); view.printUsersFromGroup(ONLINE_GROUP, header); view.printUsersFromGroup(OFFLINE_GROUP, false); chat_mtx.unlock(); answer = hostToNetUsersList(ss.str()); break; } case SEND_MSG: { Message msg = netToHostSendMsg(str); /*cout << "src | dst | content: " << msg.getSrcUserName() << " | " << msg.getDstUserName() << " | " << msg.getContent() << endl;*/ if(msg.getSrcUserName().empty()) { answer = hostToNetMsg(INVALID_REQUEST_ERR); break; } bool has_user, add_msg=true; chat_mtx.lock(); has_user = chat.hasUser(msg.getDstUserName()); if(has_user) add_msg = chat.addMessage(msg); chat_mtx.unlock(); if(!has_user) answer = hostToNetMsg(NO_MSG_DST_ERR); else if(!add_msg) answer = hostToNetMsg(MSG_EXISTS); else { size_t msg_id; msg_id = hash<Message>{}(msg); //cout << msg_id << endl; answer = hostToNetMsgQueued(msg_id); } break; } case SEND_FILE: { Message msg = netToHostSendFile(str); /*cout << "src | dst | content: " << msg.getSrcUserName() << " | " << msg.getDstUserName() << " | " << msg.getContent() << endl;*/ if(msg.getSrcUserName().empty()) { answer = hostToNetMsg(INVALID_REQUEST_ERR); break; } bool has_user, add_msg=true; chat_mtx.lock(); has_user = chat.hasUser(msg.getDstUserName()); if(has_user) add_msg = chat.addMessage(msg); chat_mtx.unlock(); if(!has_user) answer = hostToNetMsg(NO_MSG_DST_ERR); else if(!add_msg) answer = hostToNetMsg(MSG_EXISTS); else { size_t msg_id; msg_id = hash<Message>{}(msg); //cout << msg_id << endl; answer = hostToNetFileQueued(msg_id); } break; } case CREATE_GROUP: { string group_name = netToHostCreateGroup(str); if(group_name.empty()) { answer = hostToNetMsg(INVALID_REQUEST_ERR); break; } bool has_group; chat_mtx.lock(); has_group = chat.hasGroup(group_name); chat.addGroup(group_name); chat_mtx.unlock(); if(has_group) answer = hostToNetMsg(GROUP_EXISTS); else answer = hostToNetMsg(GROUP_CREATED); break; } case JOIN_GROUP: { string group_name = netToHostJoinGroup(str); if(group_name.empty()) { answer = hostToNetMsg(INVALID_REQUEST_ERR); break; } bool has_group, add_user; chat_mtx.lock(); has_group = chat.hasGroup(group_name); add_user = chat.addUserToGroup(user.getName(), group_name); chat_mtx.unlock(); if(!has_group) answer = hostToNetMsg(NO_GROUP_ERR); else if(!add_user) answer = hostToNetMsg(USER_ALREADY_IN_GROUP); else answer = hostToNetMsg(USER_ADDED_TO_GROUP); break; } case SEND_GROUP: { pair<string, string> group_msg = netToHostSendGroup(str); if(group_msg.first.empty()) { answer = hostToNetMsg(INVALID_REQUEST_ERR); break; } Group group; bool in_group, has_group; string user_name = user.getName(); string group_name = group_msg.first; string group_msg_c = group_msg.second; chat_mtx.lock(); has_group = chat.hasGroup(group_name); if(has_group) { group = chat.getGroup(group_name); in_group = group.has(user_name); if(in_group) for(auto const& name: group.getUsersNames()) if(name != user_name) { Message msg = Message(user_name, name, group_msg_c); chat.addMessage(msg); } } chat_mtx.unlock(); if(has_group) { if(!in_group) answer = hostToNetMsg(NOT_IN_GROUP_ERR); else { size_t msg_id; Message msg = Message(user_name, group_name, group_msg_c); msg_id = hash<Message>{}(msg); //cout << msg_id << endl; answer = hostToNetMsgQueued(msg_id); } } else answer = hostToNetMsg(NO_GROUP_ERR); break; } default: answer = hostToNetMsg(INVALID_REQUEST_ERR); break; } if(send(socket, answer) < 0) error("send"); return ret; }