def handle_inputs(readable_sockets, server_socket): """ This functions purpose is to read all inputs given by users, and create new requests accordingly :param readable_sockets: a list of sockets which are waiting to be read :param server_socket: the socket of the server """ for cur_socket in readable_sockets: if cur_socket is server_socket: new_socket, new_ip = server_socket.accept() inputs.append(new_socket) answers_queue.append( Answer(new_socket, Answer.HND_TYPE, 9, "handshake")) if new_socket not in outputs: outputs.append(new_socket) else: rtype = cur_socket.recv(1) if rtype is not None: create_answer(cur_socket, rtype) if cur_socket not in outputs: outputs.append(cur_socket) else: inputs.remove(cur_socket) if cur_socket in outputs: outputs.remove(cur_socket) cur_socket.close()
def get_next_question(id, answer, attempt): answer_entry = Answer.answer(id=answer) attempt_entry = ScriptAttempt.Attempt(id=attempt) history = AttemptsResult.AttemptsResult(answer_entry, attempt_entry) result = Question.question(id) if len(result.answers) == 0: ScriptAttempt.Attempt.complete(id=attempt) return jsonify(result.__dict__)
def send_account_info(dest_sock, username_length): username_length = int(username_length) username = dest_sock.recv(username_length) user = User(username) if user.exists(): user.by_file() msg = (user.get_info()) log("Sent user info of user " + username) answers_queue.append(Answer(dest_sock, Answer.ACC_TYPE, len(msg), msg)) if dest_sock not in outputs: outputs.append(dest_sock) inputs.remove(dest_sock)
def send_login_msg(dest_sock, data_len): data_len = int(data_len) username = dest_sock.recv(data_len) usr = User(username) if usr.exists(): usr.by_file() msg = "Welcome back " + username log("User logged in : " + username) else: usr.new_user() log("User created : " + username) msg = "Welcome aboard " + username answers_queue.append(Answer(dest_sock, Answer.LOG_TYPE, len(msg), msg)) if dest_sock not in outputs: outputs.append(dest_sock) inputs.remove(dest_sock)
def send_error(error_msg, dest_sock): answers_queue.append( Answer(dest_sock, Answer.ERR_TYPE, len(error_msg), error_msg)) if dest_sock not in outputs: outputs.append(dest_sock) inputs.remove(dest_sock)
def get_answers(self): answers = mongo_client.db.answersToQuestionRelations.find({"question": ObjectId(self.id)}) result = [] if answers is not None: result = [Answer.answer(item).__dict__ for item in answers] return result