JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) { msg_comm_t comm; comm = (msg_comm_t) (uintptr_t) env->GetLongField(jcomm, jcomm_field_Comm_bind); jboolean finished = env->GetBooleanField(jcomm, jcomm_field_Comm_finished); if (finished == JNI_TRUE) { return JNI_TRUE; } if (not comm) { jxbt_throw_null(env, "comm is null"); return JNI_FALSE; } if (MSG_comm_test(comm)) { msg_error_t status = MSG_comm_get_status(comm); if (status == MSG_OK) { jcomm_bind_task(env,jcomm); return JNI_TRUE; } else { //send the correct exception jmsg_throw_status(env,status); } } return JNI_FALSE; }
msg_error_t peer_wait_for_message(peer_t peer) { msg_error_t status; msg_comm_t comm = NULL; msg_task_t task = NULL; int idx = -1; int done = 0; while (!done) { comm = MSG_task_irecv(&task, peer->me); queue_pending_connection(comm, peer->pending_recvs); if ((idx = MSG_comm_waitany(peer->pending_recvs)) != -1) { comm = xbt_dynar_get_as(peer->pending_recvs, idx, msg_comm_t); status = MSG_comm_get_status(comm); XBT_DEBUG("peer_wait_for_message: error code = %d", status); xbt_assert(status == MSG_OK, "peer_wait_for_message() failed"); task = MSG_comm_get_task(comm); MSG_comm_destroy(comm); xbt_dynar_cursor_rm(peer->pending_recvs, (unsigned int*)&idx); done = peer_execute_task(peer, task); task_message_delete(task); task = NULL; } process_pending_connections(peer->pending_sends); } return status; }
/** * Tracker main function * @param argc number of arguments * @param argv arguments */ int tracker(int argc, char *argv[]) { int i; RngStream stream = (RngStream) MSG_host_get_property_value(MSG_host_self(), "stream"); //Checking arguments xbt_assert(argc == 2, "Wrong number of arguments for the tracker."); //Retrieving end time double deadline = atof(argv[1]); xbt_assert(deadline > 0, "Wrong deadline supplied"); //Building peers array xbt_dynar_t peers_list = xbt_dynar_new(sizeof(int), NULL); XBT_INFO("Tracker launched."); msg_comm_t comm_received = NULL; msg_task_t task_received = NULL; while (MSG_get_clock() < deadline) { if (comm_received == NULL) { comm_received = MSG_task_irecv(&task_received, TRACKER_MAILBOX); } if (MSG_comm_test(comm_received)) { //Check for correct status if (MSG_comm_get_status(comm_received) == MSG_OK) { //Retrieve the data sent by the peer. tracker_task_data_t data = MSG_task_get_data(task_received); //Add the peer to our peer list. if (!is_in_list(peers_list, data->peer_id)) { xbt_dynar_push_as(peers_list, int, data->peer_id); } //Sending peers to the peer int next_peer; int peers_length = xbt_dynar_length(peers_list); for (i = 0; i < MAXIMUM_PAIRS && i < peers_length; i++) { do { next_peer = xbt_dynar_get_as(peers_list, RngStream_RandInt(stream, 0, peers_length - 1), int); } while (is_in_list(data->peers, next_peer)); xbt_dynar_push_as(data->peers, int, next_peer); } //setting the interval data->interval = TRACKER_QUERY_INTERVAL; //sending the task back to the peer. MSG_task_dsend(task_received, data->mailbox, task_free); //destroy the communication. } MSG_comm_destroy(comm_received); comm_received = NULL; task_received = NULL; } else {
int process_pending_connections(xbt_dynar_t q) { unsigned int iter; int status; int empty = 0; msg_comm_t comm; xbt_dynar_foreach(q, iter, comm) { empty = 1; if (MSG_comm_test(comm)) { MSG_comm_destroy(comm); status = MSG_comm_get_status(comm); xbt_assert(status == MSG_OK, "process_pending_connections() failed"); xbt_dynar_cursor_rm(q, &iter); empty = 0; } }
/** * @brief Returns whether a communication is finished. * * Unlike wait(), This function always returns immediately. * * - Argument 1 (comm): a comm (previously created by isend or irecv) * - Return values (task/boolean or nil + string): if the communication is not * finished, return false. If the communication is finished and was successful, * returns the task received if you are the receiver or true if you are the * sender. If the communication is finished and has failed, returns nil * plus an error string. */ static int l_comm_test(lua_State* L) { msg_comm_t comm = sglua_check_comm(L, 1); /* comm ... */ if (!MSG_comm_test(comm)) { /* not finished yet */ lua_pushboolean(L, 0); /* comm ... false */ return 1; } else { /* finished but may have failed */ msg_error_t res = MSG_comm_get_status(comm); if (res == MSG_OK) { msg_task_t task = MSG_comm_get_task(comm); if (MSG_task_get_sender(task) == MSG_process_self()) { /* I'm the sender */ lua_pushboolean(L, 1); /* comm ... true */ return 1; } else { /* I'm the receiver: find the Lua task from the C task*/ sglua_task_unregister(L, task); /* comm ... task */ return 1; } } else { /* the communication has failed */ lua_pushnil(L); /* comm ... nil */ lua_pushstring(L, sglua_get_msg_error(res)); /* comm ... nil error */ return 2; } } }
/** * \brief Node Function * Arguments: * - my id * - the id of a guy I know in the system (except for the first node) * - the time to sleep before I join (except for the first node) * - the deadline time */ static int node(int argc, char *argv[]) { double init_time = MSG_get_clock(); msg_task_t task_received = NULL; int join_success = 0; double deadline; xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node"); s_node_t node = {0}; node.id = xbt_str_parse_int(argv[1], "Invalid ID: %s"); node.known_id = -1; node.ready = -1; node.pending_tasks = xbt_fifo_new(); get_mailbox(node.id, node.mailbox); XBT_DEBUG("New node with id %s (%08x)", node.mailbox, node.id); int i,j,d; for (i=0; i<LEVELS_COUNT; i++){ d = domain(node.id, i); for (j=0; j<LEVEL_SIZE; j++) node.routing_table[i][j] = (d==j) ? node.id : -1; } for (i=0; i<NEIGHBORHOOD_SIZE; i++) node.neighborhood_set[i] = -1; for (i=0; i<NAMESPACE_SIZE; i++) node.namespace_set[i] = -1; if (argc == 3) { // first ring XBT_DEBUG("Hey! Let's create the system."); deadline = xbt_str_parse_double(argv[2], "Invalid deadline: %s"); create(&node); join_success = 1; } else { node.known_id = xbt_str_parse_int(argv[2], "Invalid known ID: %s"); double sleep_time = xbt_str_parse_double(argv[3], "Invalid sleep time: %s"); deadline = xbt_str_parse_double(argv[4], "Invalid deadline: %s"); // sleep before starting XBT_DEBUG("Let's sleep during %f", sleep_time); MSG_process_sleep(sleep_time); XBT_DEBUG("Hey! Let's join the system."); join_success = join(&node); } if (join_success) { XBT_DEBUG("Waiting …."); while (MSG_get_clock() < init_time + deadline // && MSG_get_clock() < node.last_change_date + 1000 && MSG_get_clock() < max_simulation_time) { if (node.comm_receive == NULL) { task_received = NULL; node.comm_receive = MSG_task_irecv(&task_received, node.mailbox); // FIXME: do not make MSG_task_irecv() calls from several functions } if (!MSG_comm_test(node.comm_receive)) { MSG_process_sleep(5); } else { // a transfer has occurred msg_error_t status = MSG_comm_get_status(node.comm_receive); if (status != MSG_OK) { XBT_DEBUG("Failed to receive a task. Nevermind."); MSG_comm_destroy(node.comm_receive); node.comm_receive = NULL; } else { // the task was successfully received MSG_comm_destroy(node.comm_receive); node.comm_receive = NULL; handle_task(&node, task_received); } } } print_node(&node); } return 1; }