rendered paste body#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gm/gm.h>
#define GM_PRIORITY GM_LOW_PRIORITY
#define GM_SIZE gm_min_size_for_length(sizeof(gm_message_t))
#define GM_BUFFER_COUNT 1
#define GM_BUFFER_LENGTH (gm_max_length_for_size(GM_SIZE))
#define MAX_BUF 4096
typedef struct gm_message {
unsigned long sector;
unsigned long nr;
char data[MAX_BUF];
} gm_message_t;
void handle (void *buffer) {
printf("%ld\t", ((gm_message_t *)buffer) -> sector);
printf("%ld\n", ((gm_message_t *)buffer) -> nr);
printf("%s\n", ((gm_message_t *)buffer) -> data);
}
int copy_buffer (void*p1, void*p2) {
((gm_message_t * ) p1) -> sector = ((gm_message_t * ) p2) -> sector;
((gm_message_t * ) p1) -> nr = ((gm_message_t * ) p2) -> nr;
strcpy(((gm_message_t * ) p1) -> data, ((gm_message_t * ) p2) -> data);
return sizeof(gm_message_t);
}
gm_status_t gm_init_mine (struct gm_port **port, unsigned int board_num, unsigned int port_id, char *name, enum gm_api_version ver)
{
gm_init();
return gm_open(port, board_num, port_id, name, ver);
}
void gm_close_mine (gm_status_t status) {
gm_finalize();
gm_exit (status);
return;
}
gm_status_t my_send(struct gm_port *port, void *buffer, gm_u16_t target_node_id) {
unsigned int send_length;
void *out_buffer;
gm_status_t status;
/* gm_init_for receiving finished. Now allocate buffers */
if (!(out_buffer = gm_dma_calloc (port, GM_BUFFER_COUNT, GM_BUFFER_LENGTH)))
gm_perror("Could not allocate in_buffer", GM_OUT_OF_MEMORY);
send_length = copy_buffer (out_buffer, buffer);
// send_length = sizeof(out_buffer);
gm_send_to_peer_with_callback (port, out_buffer, GM_SIZE, send_length, GM_PRIORITY, target_node_id, NULL, NULL);
return status;
}
gm_status_t my_recv (struct gm_port *port, void *incbuffer) {
gm_status_t status;
unsigned int size, test = 1;
void *in_buffer,*buffer;
gm_recv_event_t *event;
if (!(in_buffer = gm_dma_calloc (port, GM_BUFFER_COUNT, GM_BUFFER_LENGTH)))
gm_perror("Could not allocate in_buffer", GM_OUT_OF_MEMORY);
// gm_allow_remote_memory_access (port);
gm_provide_receive_buffer (port, in_buffer, GM_SIZE, GM_PRIORITY);
while (test == 1 ) {
event = gm_blocking_receive(port);
switch (GM_RECV_EVENT_TYPE(event)) {
case GM_RECV_EVENT:
case GM_PEER_RECV_EVENT:
case GM_FAST_PEER_RECV_EVENT:
//printf("\n %ld \n", (unsigned long) ((gm_message_t*)gm_ntohp (event->recv.buffer))->sector);
copy_buffer(incbuffer, (char *) gm_ntohp (event->recv.buffer));
// sprintf(incbuffer, (char *) gm_ntohp (event->recv.message));
buffer = gm_ntohp (event->recv.buffer);
size = (unsigned int)gm_ntoh_u8 (event->recv.size);
gm_provide_receive_buffer (port, buffer, size, GM_PRIORITY);
test = 0;
break;
case GM_NO_RECV_EVENT:
break;
case _GM_SLEEP_EVENT: //printf("pire sleep\n");
default:
gm_unknown (port, event);
}
}
gm_dma_free(port,in_buffer);
return status;
}
int main (int argc, char ** argv) {
struct gm_port *port;
gm_status_t status;
gm_u32_t target_node_id, local_node_id;
unsigned int global_id = 0;
gm_message_t *mybuff = (gm_message_t *) malloc (sizeof(gm_message_t));
gm_message_t *mymsg = (gm_message_t *) malloc (sizeof(gm_message_t));
/* init gm */
if ((status = gm_init_mine (&port, 0, 4, "my_send_port", (enum gm_api_version) GM_API_VERSION_1_1)) != GM_SUCCESS)
gm_perror("Cannot open Port",status);
if ((status = gm_get_node_id (port, &local_node_id)) != GM_SUCCESS)
gm_perror("Cannot get my node_id",status);
if ((status = gm_node_id_to_global_id (port, local_node_id, &global_id)) != GM_SUCCESS)
gm_perror("Cannot convert node ids",status);
target_node_id = gm_host_name_to_node_id (port,argv[1]);
((gm_message_t *) mymsg)-> sector = atoi(argv[3]);
((gm_message_t *) mymsg)-> nr = atoi(argv[4]);
sprintf(((gm_message_t *) mymsg)-> data,argv[2]);
my_send (port, mymsg, target_node_id);
my_recv (port, mybuff);
handle(mybuff);
free(mybuff); free(mymsg);
/* finalise gm */
gm_close_mine(status);
return 0;
}