void join(char * str) { if (!is_inroom() && is_logged()) { if (allocate_mem(ROOM, &room_data)) { room_data->type = ROOM; room_data->operation_type = ENTER_ROOM; strcpy(room_data->user_name, username); strcpy(room_data->room_name, str); if (send_message(room_data->type, room_data) != FAIL && wait_until_received(RESPONSE) != FAIL) { if (response_data.response_type == ENTERED_ROOM_SUCCESS) { char buf[50]; inroom = 1; strcpy(roomname, room_data->room_name); sprintf(buf, "%s: %s", "You've successfully entered room", roomname); writestr(buf); } else { writestr("Couldn't enter room."); } } else { writestr("Server didn't answer."); } free_mem(room_data); } } else if (is_inroom() && is_logged()) { if (strcmp(str, roomname)) { /* User didn't choose the same room to log in again */ if (allocate_mem(ROOM, &room_data)) { room_data->type = ROOM; room_data->operation_type = CHANGE_ROOM; strcpy(room_data->user_name, username); strcpy(room_data->room_name, str); if (send_message(room_data->type, room_data) != FAIL && wait_until_received(RESPONSE) != FAIL) { if (response_data.response_type == CHANGE_ROOM_SUCCESS) { char buf[50]; strcpy(roomname, room_data->room_name); sprintf(buf, "%s: %s", "You've successfully changed room for", roomname); writestr(buf); } else { writestr("Couldn't change room."); } } else { writestr("Server didn't answer"); } free_mem(room_data); } else { writestr("Couldn't allocate room_data structure."); } } else { writestr("No use in entering the same room again."); } } else { writestr("To perform it, you have to be logged in."); } }
void request(const int rtype) { /* room_users_list is not handled */ if (is_logged() && rtype >= USERS_LIST && rtype <= ROOM_USERS_LIST ) { if (allocate_mem(REQUEST, &request_data)) { request_data->type = REQUEST; request_data->request_type = rtype; strcpy(request_data->user_name, username); if (send_message(request_data->type, request_data) != FAIL) { int mtype = (rtype == USERS_LIST) ? USERS : (rtype == ROOMS_LIST) ? ROOMS : ROOM_USERS; if (allocate_mem(mtype, &request_response_data)) { if (wait_until_received(mtype) != FAIL) { display_request_result(); } else { writestr("Request wasn't proceeded."); } free_mem(request_response_data); } else { writestr("Couldn't allocate data structure."); } } free_mem(request_data); } else { writestr("Couldn't allocate request_data structure."); } } else { writestr("To perform this, you have to be logged in."); } }
void send_priv(char * str) { if (is_logged()) { int username_length; if ((username_length = get_username(str)) != FAIL) { chatmsg_data.type = MESSAGE; strcpy(chatmsg_data.receiver, str); strcpy(chatmsg_data.sender, username); strcpy(chatmsg_data.message, &str[username_length+1]); if (chatmsg_data.message[0]) { get_time(chatmsg_data.send_time); chatmsg_data.msg_type = PRIVATE; if (send_message(chatmsg_data.type, &chatmsg_data) != FAIL && wait_until_received(RESPONSE) != FAIL) { if (response_data.response_type == MSG_SEND) { print_msg(0); } else { writestr("message wasn't sent"); } } } else { writestr("No use in sending empty message."); } } else { writestr("This priv doesn't make sense."); } } else { writestr("To perform this, you have to be logged in."); } }
void Media::upload_image_ajax() { contents::ajax::media::UploadImage c; forms::media::UploadImage form; form.load(context()); form.image.load(context()); response().content_type("application/json"); if (!is_logged()) { c.add_error( MEDIA_NOT_LOGIN_ERROR_CODE, MEDIA_NOT_LOGIN_ERROR_TEXT ); render("ajax","media_upload_image", c); return; } // if the form is not valid if ( !form.validate() && !form.image.validate() ) { c.add_error( MEDIA_SAVE_INVALID_ERROR_CODE, MEDIA_SAVE_INVALID_ERROR_TEXT ); } else { std::string fileURL = uploadsModel->save( form.image.value() ); // if we got an internal proble while saving if (fileURL.empty()) { c.add_error( MEDIA_SAVE_INTERNAL_ERROR_CODE, MEDIA_SAVE_INTERNAL_ERROR_TEXT ); } else { c.url = fileURL; } } render("ajax","media_upload_image", c); }
decree replica::last_prepared_decree() const { ballot lastBallot = 0; decree start = last_committed_decree(); while (true) { auto mu = _prepare_list->get_mutation_by_decree(start + 1); if (mu == nullptr || mu->data.header.ballot < lastBallot || (!mu->is_logged() && !_options.prepare_ack_on_secondary_before_logging_allowed) ) break; start++; lastBallot = mu->data.header.ballot; } return start; }
decree replica::last_prepared_decree() const { ballot lastBallot = 0; decree start = last_committed_decree(); while (true) { auto mu = _prepare_list->get_mutation_by_decree(start + 1); if (mu == nullptr || mu->data.header.ballot < lastBallot || !mu->is_logged() ) break; start++; lastBallot = mu->data.header.ballot; } return start; }
bool Controller::check_permission() { //TODO for the moment we do not handle case // when you're logged but you're current group has not // enough priviledges if (!is_logged()) { std::ostringstream oss; oss << cppcms::filters::urlencode( request().path_info() ); response().set_redirect_header( "/users/login" "?from=" + oss.str() ); return false; } return true; }
void send_chatmsg(char * str) { if (is_inroom() && is_logged()) { chatmsg_data.type = MESSAGE; chatmsg_data.msg_type = PUBLIC; get_time(chatmsg_data.send_time); strcpy(chatmsg_data.sender, username); strcpy(chatmsg_data.receiver, roomname); strcpy(chatmsg_data.message, str); if (send_message(chatmsg_data.type, &chatmsg_data) != FAIL && wait_until_received(RESPONSE) != FAIL) { if (response_data.response_type == MSG_SEND) { print_msg(0); } else { fprintf(stderr, "%d", response_data.response_type); writestr("message wasn't sent."); } } } else { writestr("To perform it, you have to be logged in and be in room."); } }
void replica::on_prepare(dsn_message_t request) { check_hashed_access(); replica_configuration rconfig; mutation_ptr mu; { rpc_read_stream reader(request); unmarshall(reader, rconfig); mu = mutation::read_from(reader, request); } decree decree = mu->data.header.decree; dinfo("%s: mutation %s on_prepare", name(), mu->name()); dassert(mu->data.header.ballot == rconfig.ballot, ""); if (mu->data.header.ballot < get_ballot()) { derror("%s: mutation %s on_prepare skipped due to old view", name(), mu->name()); // no need response because the rpc should have been cancelled on primary in this case return; } // update configuration when necessary else if (rconfig.ballot > get_ballot()) { if (!update_local_configuration(rconfig)) { derror( "%s: mutation %s on_prepare failed as update local configuration failed, state = %s", name(), mu->name(), enum_to_string(status()) ); ack_prepare_message(ERR_INVALID_STATE, mu); return; } } if (PS_INACTIVE == status() || PS_ERROR == status()) { derror( "%s: mutation %s on_prepare failed as invalid replica state, state = %s", name(), mu->name(), enum_to_string(status()) ); ack_prepare_message( (PS_INACTIVE == status() && _inactive_is_transient) ? ERR_INACTIVE_STATE : ERR_INVALID_STATE, mu ); return; } else if (PS_POTENTIAL_SECONDARY == status()) { // new learning process if (rconfig.learner_signature != _potential_secondary_states.learning_signature) { init_learn(rconfig.learner_signature); // no need response as rpc is already gone return; } if (!(_potential_secondary_states.learning_status == LearningWithPrepare || _potential_secondary_states.learning_status == LearningSucceeded)) { derror( "%s: mutation %s on_prepare skipped as invalid learning status, state = %s, learning_status = %s", name(), mu->name(), enum_to_string(status()), enum_to_string(_potential_secondary_states.learning_status) ); // no need response as rpc is already gone return; } } dassert (rconfig.status == status(), ""); if (decree <= last_committed_decree()) { ack_prepare_message(ERR_OK, mu); return; } // real prepare start auto mu2 = _prepare_list->get_mutation_by_decree(decree); if (mu2 != nullptr && mu2->data.header.ballot == mu->data.header.ballot) { if (mu2->is_logged()) { ack_prepare_message(ERR_OK, mu); } else { derror("%s: mutation %s on_prepare skipped as it is duplicate", name(), mu->name()); // response will be unnecessary when we add retry logic in rpc engine. // the retried rpc will use the same id therefore it will be considered responsed // even the response is for a previous try. } return; } error_code err = _prepare_list->prepare(mu, status()); dassert (err == ERR_OK, ""); if (PS_POTENTIAL_SECONDARY == status()) { dassert (mu->data.header.decree <= last_committed_decree() + _options->max_mutation_count_in_prepare_list, ""); } else { dassert (PS_SECONDARY == status(), ""); dassert (mu->data.header.decree <= last_committed_decree() + _options->staleness_for_commit, ""); } dassert(mu->log_task() == nullptr, ""); mu->log_task() = _stub->_log->append(mu, LPC_WRITE_REPLICATION_LOG, this, std::bind(&replica::on_append_log_completed, this, mu, std::placeholders::_1, std::placeholders::_2), gpid_to_hash(get_gpid()) ); }
void replica::on_prepare(dsn_message_t request) { check_hashed_access(); replica_configuration rconfig; mutation_ptr mu; { msg_binary_reader reader(request); unmarshall(reader, rconfig); mu = mutation::read_from(reader, request); } decree decree = mu->data.header.decree; ddebug( "%s: mutation %s on_prepare", name(), mu->name()); dassert (mu->data.header.ballot == rconfig.ballot, ""); if (mu->data.header.ballot < get_ballot()) { ddebug( "%s: mutation %s on_prepare skipped due to old view", name(), mu->name()); return; } // update configuration when necessary else if (rconfig.ballot > get_ballot()) { if (!update_local_configuration(rconfig)) { ddebug( "%s: mutation %s on_prepare to %s failed as update local configuration failed", name(), mu->name(), enum_to_string(status()) ); ack_prepare_message(ERR_INVALID_STATE, mu); return; } } if (PS_INACTIVE == status() || PS_ERROR == status()) { ddebug( "%s: mutation %s on_prepare to %s skipped", name(), mu->name(), enum_to_string(status()) ); ack_prepare_message( (PS_INACTIVE == status() && _inactive_is_transient) ? ERR_INACTIVE_STATE : ERR_INVALID_STATE, mu ); return; } else if (PS_POTENTIAL_SECONDARY == status()) { if (_potential_secondary_states.learning_status != LearningWithPrepare && _potential_secondary_states.learning_status != LearningSucceeded) { ddebug( "%s: mutation %s on_prepare to %s skipped, learnings state = %s", name(), mu->name(), enum_to_string(status()), enum_to_string(_potential_secondary_states.learning_status) ); // do not retry as there may retries later return; } } dassert (rconfig.status == status(), ""); if (decree <= last_committed_decree()) { ack_prepare_message(ERR_OK, mu); return; } // real prepare start auto mu2 = _prepare_list->get_mutation_by_decree(decree); if (mu2 != nullptr && mu2->data.header.ballot == mu->data.header.ballot) { ddebug( "%s: mutation %s redundant prepare skipped", name(), mu->name()); if (mu2->is_logged() || _options.prepare_ack_on_secondary_before_logging_allowed) { ack_prepare_message(ERR_OK, mu); } return; } error_code err = _prepare_list->prepare(mu, status()); dassert (err == ERR_OK, ""); if (PS_POTENTIAL_SECONDARY == status()) { dassert (mu->data.header.decree <= last_committed_decree() + _options.staleness_for_start_prepare_for_potential_secondary, ""); } else { dassert (PS_SECONDARY == status(), ""); dassert (mu->data.header.decree <= last_committed_decree() + _options.staleness_for_commit, ""); } // ack without logging if (_options.prepare_ack_on_secondary_before_logging_allowed) { ack_prepare_message(err, mu); } // write log dassert (mu->log_task() == nullptr, ""); mu->log_task() = _stub->_log->append(mu, LPC_WRITE_REPLICATION_LOG, this, std::bind(&replica::on_append_log_completed, this, mu, std::placeholders::_1, std::placeholders::_2), gpid_to_hash(get_gpid()) ); dassert(mu->log_task() != nullptr, ""); }