/***************************************************************************** * setup_pkt_channels *****************************************************************************/ int setup_pkt_channels(int task_id) { mcapi_status_t status; mcapi_request_t request; size_t dummy; /* create/ get endpoints */ if (setup_echo_endpoints(task_id) != 0) { return -1; } /* connect the write channel only */ mcapi_pktchan_connect_i(local_endpts[task_id].endpts[WRITE_IDX], remote_endpts[task_id], &request, &status); if (status != MCAPI_SUCCESS) { th_log_error("Task id %d failed to initialize (1): %s\n", task_id, mcapi_display_status(status,status_buff,sizeof(status_buff))); return -1; } mcapi_wait(&request, &dummy, TIMEOUT,&status); if (status != MCAPI_SUCCESS) { th_log_error("Task id %d failed to initialize (2): %s\n", task_id, mcapi_display_status(status,status_buff,sizeof(status_buff))); return -1; } /* open the write channel */ mcapi_pktchan_send_open_i(&(pkt_hndls[task_id].send), local_endpts[task_id].endpts[WRITE_IDX], &request, &status); if (status != MCAPI_SUCCESS) { th_log_error("Task id %d failed to initialize (3) : %s\n", task_id, mcapi_display_status(status,status_buff,sizeof(status_buff))); return -1; } mcapi_wait(&request, &dummy, TIMEOUT,&status); if (status != MCAPI_SUCCESS) { th_log_error("Task id %d failed to initialize (4) : %s\n", task_id, mcapi_display_status(status,status_buff,sizeof(status_buff))); return -1; } /* open the read channel */ /* the read channel may not yet be connected */ mcapi_pktchan_recv_open_i(&(pkt_hndls[task_id].recv), local_endpts[task_id].endpts[READ_IDX], &request, &status); mcapi_wait(&request, &dummy, TIMEOUT,&status); if (status != MCAPI_SUCCESS) { th_log_error("Task id %d failed to initialize (5) : %s\n", task_id, mcapi_display_status(status,status_buff,sizeof(status_buff))); return -1; } return 0; }
int main(int argc, char *argv[]) { //the endpoints used in message-oriented communication mcapi_endpoint_t red_msg_point; mcapi_endpoint_t yellow_msg_point; mcapi_endpoint_t green_msg_point; //the endpoints used in channel-oriented communication mcapi_endpoint_t yellow_sin_chan; mcapi_endpoint_t yellow_cos_chan; mcapi_endpoint_t yellow_pkt_chan; //status message received in almost all MCAPI-calls mcapi_status_t status; //info-struct received in initialization mcapi_info_t info; //buffer for incoming messages char recv_buf[MAX_MSG_LEN]; //the status code converted to string char status_msg[MCAPI_MAX_STATUS_MSG_LEN]; //size parameter required in some calls size_t size = 1; //an iterator used in loops unsigned int i = 0; //request handle is used to operate wait-calls mcapi_request_t request; //a second request handle! just so that we see it works :) mcapi_request_t request2; //sandles used in channel-messaging mcapi_sclchan_recv_hndl_t sin_handle; mcapi_sclchan_recv_hndl_t cos_handle; mcapi_pktchan_send_hndl_t pkt_handle; //how many scalars we are expecting char count = 0; //buffer of data sent in messages unsigned char* send_buf; printf(COLOR "here\n"); //We are yellow! initialize accordingly mcapi_initialize( THE_DOMAIN, YELLOW_NODE, 0, 0, &info, &status ); check( MCAPI_SUCCESS, status ); //create our side of messaging yellow_msg_point = mcapi_endpoint_create( YELLOW_MSG, &status ); check( MCAPI_SUCCESS, status ); //obtain the red message point red_msg_point = mcapi_endpoint_get( THE_DOMAIN, RED_NODE, RED_MSG, TIMEOUT, &status ); check( MCAPI_SUCCESS, status ); printf(COLOR "start-up messaging\n"); //wait for the amount to come mcapi_msg_recv( yellow_msg_point, recv_buf, MAX_MSG_LEN, &size, &status ); check( MCAPI_SUCCESS, status ); //read the count from first byte count = recv_buf[0]; //surprise! this process reserves the buffer with malloc send_buf = (char*)malloc(count*2); //send ack send_buf[0] = 'a'; mcapi_msg_send( yellow_msg_point, red_msg_point, send_buf, 1, 0, &status ); check( MCAPI_SUCCESS, status ); printf(COLOR "start-up messaged with %u bytes. expecting %u scalars\n", size, count ); //open our channel endpoint to sin yellow_sin_chan = mcapi_endpoint_create( YELLOW_SIN, &status ); check( MCAPI_SUCCESS, status ); //open our channel endpoint to cos yellow_cos_chan = mcapi_endpoint_create( YELLOW_COS, &status ); check( MCAPI_SUCCESS, status ); //open our ends, let senders form connection mcapi_sclchan_recv_open_i( &sin_handle, yellow_sin_chan, &request, &status ); check( MCAPI_PENDING, status ); mcapi_sclchan_recv_open_i( &cos_handle, yellow_cos_chan, &request2, &status ); check( MCAPI_PENDING, status ); //wait for it to happen mcapi_wait( &request, &size, TIMEOUT, &status ); check( MCAPI_SUCCESS, status ); mcapi_wait( &request2, &size, TIMEOUT, &status ); check( MCAPI_SUCCESS, status ); printf(COLOR "beginning the receive value\n"); for ( i = 0; i < 15000; ++i ) { //an iterator used in loops unsigned int j = 0; //start to receveive stuff //i+=2 because our values are two bytes while buf is one byte for ( j = 0; j < count; ++j ) { //receive cos scalar short cval = mcapi_sclchan_recv_uint16( cos_handle, &status ); check( MCAPI_SUCCESS, status ); //receive sin scalar short sval = mcapi_sclchan_recv_uint16( sin_handle, &status ); check( MCAPI_SUCCESS, status ); //addition short sumval = sval + cval; //put to buf send_buf[j*2] = sumval; send_buf[j*2+1] = sumval >> 8; //printf( COLOR "%hX %hhX %hhX\n", sumval, send_buf[j*2], //send_buf[j*2+1] ); } } printf(COLOR "receiving done, closing scalar channels\n"); //close our ends mcapi_sclchan_recv_close_i( sin_handle, &request, &status ); check( MCAPI_PENDING, status ); mcapi_sclchan_recv_close_i( cos_handle, &request2, &status ); check( MCAPI_PENDING, status ); //wait for it to happen mcapi_wait( &request, &size, TIMEOUT, &status ); check( MCAPI_SUCCESS, status ); mcapi_wait( &request2, &size, TIMEOUT, &status ); check( MCAPI_SUCCESS, status ); printf(COLOR "closed, informing green\n"); //obtain their endpoint green_msg_point = mcapi_endpoint_get( THE_DOMAIN, GREEN_NODE, GREEN_MSG, TIMEOUT, &status ); check( MCAPI_SUCCESS, status ); //send the message mcapi_msg_send( yellow_msg_point, green_msg_point, send_buf, 1, 0, &status ); check( MCAPI_SUCCESS, status ); printf(COLOR "informed, opening packet channel\n"); //open our channel endpoint to green yellow_pkt_chan = mcapi_endpoint_create( YELLOW_PKT, &status ); check( MCAPI_SUCCESS, status ); //open our end, let receiver form connection mcapi_pktchan_send_open_i( &pkt_handle, yellow_pkt_chan, &request, &status ); check( MCAPI_PENDING, status ); //wait for it to happen mcapi_wait( &request, &size, TIMEOUT, &status ); check( MCAPI_SUCCESS, status ); //now send printf(COLOR "sending the packet\n"); mcapi_pktchan_send( pkt_handle, send_buf, count, &status ); //and now close printf(COLOR "sent the packet, closing\n"); mcapi_pktchan_send_close_i( pkt_handle, &request, &status ); check( MCAPI_PENDING, status ); mcapi_wait( &request, &size, TIMEOUT, &status ); check( MCAPI_SUCCESS, status ); printf(COLOR "closed, shutdown\n"); //free buf free( send_buf ); //shut-down mcapi_finalize( &status ); check( MCAPI_SUCCESS, status ); return EXIT_SUCCESS; }
int main(int argc, char** argv) { mcapi_boolean_t recv_done = MCAPI_FALSE; mcapi_boolean_t send_done = MCAPI_FALSE; mca_status_t status; mcapi_request_t request; size_t size; int num_sends,num_recvs,order,i; struct timeval tv; unsigned int seed; mcapi_endpoint_t recv_endpt; mcapi_endpoint_t send_endpt; int rc = 0; mcapi_param_t parms; mcapi_info_t version; mcapi_set_debug_level(6); memset(&send_requests,0,sizeof(send_requests)); memset(&recv_requests,0,sizeof(recv_requests)); /* initialize the random number generator with a seed */ if (argc == 2) { seed = atoi(argv[1]); } else { gettimeofday(&tv,0); seed = tv.tv_sec + tv.tv_usec; } printf("Using seed:[%u]\n",seed); srand(seed); /* create a node */ mcapi_initialize(DOMAIN,NODE,NULL,&parms,&version,&status); if (status != MCAPI_SUCCESS) { fprintf(stderr,"\nERROR: Failed to initialize (0): %s\n",mcapi_display_status(status,status_buff,sizeof(status_buff))); rc++; mcapi_finalize(&status); return rc; } /* create the endpoints */ send_endpt = mcapi_endpoint_create(1,&status); recv_endpt = mcapi_endpoint_create(2,&status); /*************************** connect the channels *********************/ do { mcapi_pktchan_connect_i(send_endpt,recv_endpt,&request,&status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); while (!mcapi_test(&request,&size,&status)) {} if (status != MCAPI_SUCCESS) { fprintf(stderr,"\nERROR: Failed to initialize (1): %s\n",mcapi_display_status(status,status_buff,sizeof(status_buff))); rc++; mcapi_finalize(&status); return rc; } /*************************** open the channels *********************/ do { mcapi_pktchan_recv_open_i(&r1 /*recv_handle*/,recv_endpt, &request, &status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); while (!mcapi_test(&request,&size,&status)) {} if (status != MCAPI_SUCCESS) { fprintf(stderr,"\nERROR: Failed to initialize (2): %s\n",mcapi_display_status(status,status_buff,sizeof(status_buff))); rc++; mcapi_finalize(&status); return rc; } do { mcapi_pktchan_send_open_i(&s1 /*send_handle*/,send_endpt, &request, &status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); while (!mcapi_test(&request,&size,&status)) {} if (status != MCAPI_SUCCESS) { fprintf(stderr,"\nERROR: Failed to initialize (3): %s\n",mcapi_display_status(status,status_buff,sizeof(status_buff))); rc++; mcapi_finalize(&status); return rc; } /* randomly issue sends and receives */ printf("sending and receiving num_msgs=%d\n",NUM_MSGS); while (!(recv_done && send_done)) { if (send_done) { order = 1; } else if (recv_done) { order = 0; } else { order = get_random(); } if (order % 2) { /* do some receives */ num_recvs = get_random(); printf("issuing %i receives...\n",num_recvs); for (i = 0; i < num_recvs; i++) { recv_done = receiver(); } } else { /* do some sends */ num_sends = get_random(); printf("issuing %i sends...\n",num_sends); for (i = 0; i < num_sends; i++) { send_done = sender(); } } } rc =check_results(); mcapi_finalize(&status); if (status != MCAPI_SUCCESS) { fprintf(stderr,"\nERROR: Failed to finalize: %s\n",mcapi_display_status(status,status_buff,sizeof(status_buff))); rc++; } if (rc == 0) { printf(" Test PASSED\n"); } else { printf(" Test FAILED\n"); } return rc; }
int main () { size_t size; mcapi_endpoint_t ep1,ep2; mcapi_pktchan_send_hndl_t s1; /* s1 = ep1->ep2 */ mcapi_pktchan_recv_hndl_t r1; /* r1 = ep1->ep2 */ int i = 0; int rc = 1; mca_status_t status; mcapi_request_t request; mcapi_param_t parms; mcapi_info_t version; mcapi_set_debug_level(6); /* create a node */ mcapi_initialize(DOMAIN,NODE,NULL,&parms,&version,&status); if (status != MCAPI_SUCCESS) { WRONG } /* create endpoints */ ep1 = mcapi_endpoint_create(PORT_NUM1,&status); if (status != MCAPI_SUCCESS) { WRONG } ep2 = mcapi_endpoint_create(PORT_NUM2,&status); if (status != MCAPI_SUCCESS) { WRONG } /*************************** connect the channels *********************/ do { mcapi_pktchan_connect_i(ep1,ep2,&request,&status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); while (!mcapi_test(&request,&size,&status)) {} if (status != MCAPI_SUCCESS) { WRONG } /*************************** open the channels *********************/ do { mcapi_pktchan_recv_open_i(&r1 /*recv_handle*/,ep2, &request, &status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); while (!mcapi_test(&request,&size,&status)) {} if (status != MCAPI_SUCCESS) { WRONG } do { mcapi_pktchan_send_open_i(&s1 /*send_handle*/,ep1, &request, &status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); while (!mcapi_test(&request,&size,&status)) {} if (status != MCAPI_SUCCESS) { WRONG } /* tests: recv send send recv obeys FIFO */ do { mcapi_pktchan_recv_i(r1,(void **)((void*)&requests[i].buffer),&requests[i].request,&requests[i].status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); i++; do { mcapi_pktchan_send_i(s1,"one",sizeof("one"),&requests[i].request,&requests[i].status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); i++; do { mcapi_pktchan_send_i(s1,"two",sizeof("two"),&requests[i].request,&requests[i].status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); i++; do { mcapi_pktchan_recv_i(r1,(void **)((void*)&requests[i].buffer),&requests[i].request,&requests[i].status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); i++; rc = check_results(); /* close the channels */ do { mcapi_pktchan_recv_close_i(r1,&request,&status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); while (!mcapi_test(&request,&size,&status)) {} if (status != MCAPI_SUCCESS) { WRONG } do { mcapi_pktchan_send_close_i(s1,&request,&status); //retry if all request handles are in-use } while (status == MCAPI_ERR_REQUEST_LIMIT); while (!mcapi_test(&request,&size,&status)) {} if (status != MCAPI_SUCCESS) { WRONG } mcapi_finalize(&status); if (rc == 0) { printf(" Test PASSED\n"); } else { printf(" Test FAILED\n"); } return rc; }