/************************************************************************ The walrus. */ int main(int argc, char *argv[]) { /* default mask threshold is 1 -- any value other than black */ unsigned int thold = 1 ; /* default sense of threshold is: calculate when mask > threshold */ int thold_sense = 0 ; gras_t gr1,gr2,mask ; double a1,a2,c ; int ai,masked ; /* parse options, if given */ ai = 1 ; while ((argc > ai) && *(argv[ai]) == '-') { switch(*(argv[ai]+1)) { case 't' : if ((ai += 1) >= argc) return(usage(argv[0])) ; thold = atoi(argv[ai++]) ; break ; case 'i' : ai += 1 ; /* invert sense of threshold: calculate when mask < threshold */ thold_sense = 1 ; break ; case 'v' : if ((ai += 1) >= argc) return(usage(argv[0])) ; verbosity = atoi(argv[ai++]) ; break ; default : return(usage(argv[0])) ; } ; } ; /* load data files */ if (argc < ai + 2) return(usage(argv[0])) ; gras_init(&gr1,0,0) ; gras_init(&gr2,0,0) ; if ((tiff_load_gras(argv[ai++],&gr1) != OK) || (tiff_load_gras(argv[ai++],&gr2) != OK) ) return(FATAL) ; /* load mask, if given */ if (argc > ai) { gras_init(&mask,gr1.ny,gr1.nx) ; if (tiff_load_gras(argv[ai++],&mask) != OK) return(FATAL) ; masked = 1 ; } else { masked = 0 ; } ; /* do our calculation */ if (make_corr(&c,&a1,&a2,&gr1,&gr2, (masked ? &mask : 0),thold,thold_sense) != OK) return(FATAL) ; printf("%f\n",c) ; return(OK) ; }
int client(int argc, char *argv[]) { gras_socket_t mysock; /* socket on which I listen */ gras_socket_t toserver; /* socket used to write to the server */ gras_init(&argc, argv); gras_msgtype_declare("hello", NULL); gras_msgtype_declare("kill", NULL); mysock = gras_socket_server_range(1024, 10000, 0, 0); XBT_VERB("Client ready; listening on %d", gras_socket_my_port(mysock)); gras_os_sleep(1.5); /* sleep 1 second and half */ toserver = gras_socket_client(argv[1], atoi(argv[2])); gras_msg_send(toserver, "hello", NULL); XBT_INFO("we sent the data to the server on %s. Let's do it again for fun", gras_socket_peer_name(toserver)); gras_msg_send(toserver, "hello", NULL); XBT_INFO("Ok. Enough. Have a rest, and then kill the server"); gras_os_sleep(5); /* sleep 1 second and half */ gras_msg_send(toserver, "kill", NULL); gras_exit(); return 0; }
int client(int argc, char *argv[]) { gras_socket_t mysock; /* socket on which I listen */ client_data_t *globals; gras_init(&argc, argv); gras_msgtype_declare("hello", NULL); gras_msgtype_declare("kill", NULL); mysock = gras_socket_server_range(1024, 10000, 0, 0); XBT_VERB("Client ready; listening on %d", gras_socket_my_port(mysock)); globals = gras_userdata_new(client_data_t *); globals->done = 0; gras_os_sleep(1.5); /* sleep 1 second and half */ globals->toserver = gras_socket_client(argv[1], atoi(argv[2])); XBT_INFO("Programming the repetitive action with a frequency of 0.5 sec"); gras_timer_repeat(0.5, client_do_hello); XBT_INFO("Programming the delayed action in 5 secs"); gras_timer_delay(5, client_do_stop); while (!globals->done) { gras_msg_handle(60); } gras_exit(); return 0; }
int function(int argc, char *argv[]) { gras_init(&argc, argv); // gras_os_sleep(3); gras_exit(); return 0; }
int client(int argc, char *argv[]) { gras_socket_t mysock; /* socket on which I listen */ gras_socket_t toserver; /* socket used to write to the server */ gras_init(&argc, argv); message_declaration(); mysock = gras_socket_server_range(1024, 10000, 0, 0); XBT_VERB("Client ready; listening on %d", gras_socket_my_port(mysock)); gras_os_sleep(1.5); /* sleep 1 second and half */ toserver = gras_socket_client(argv[1], atoi(argv[2])); long long_to_convert = 4321; char *string_result; XBT_INFO("Ask to convert %ld", long_to_convert); gras_msg_rpccall(toserver, 60, "convert i2a", &long_to_convert, &string_result); XBT_INFO("The server says that %ld is equal to \"%s\".", long_to_convert, string_result); free(string_result); char *string_to_convert = "1234"; long long_result; XBT_INFO("Ask to convert %s", string_to_convert); gras_msg_rpccall(toserver, 60, "convert a2i", &string_to_convert, &long_result); XBT_INFO("The server says that \"%s\" is equal to %d.", string_to_convert, long_result); xbt_ex_t e; string_to_convert = "azerty"; TRY { gras_msg_rpccall(toserver, 60, "convert a2i", &string_to_convert, &long_result); } CATCH(e) { XBT_INFO ("The server refuses to convert %s. Here is the received exception:", string_to_convert); xbt_ex_display(&e); xbt_ex_free(e); XBT_INFO("Again, previous exception was excepted"); } gras_msg_send(toserver, "done", NULL); XBT_INFO("Stopped the server"); gras_exit(); return 0; }
int server(int argc, char *argv[]) { server_data_t *globals; int port = 4000; /* 1. Init the GRAS infrastructure and declare my globals */ gras_init(&argc, argv); globals = gras_userdata_new(server_data_t); /* 2. Get the port I should listen on from the command line, if specified */ if (argc == 2) { port = atoi(argv[1]); } XBT_INFO("Launch server (port=%d)", port); /* 3. Create my master socket */ globals->sock = gras_socket_server(port); /* 4. Register the known messages. This function is called twice here, but it's because this file also acts as regression test, no need to do so yourself of course. */ ping_register_messages(); ping_register_messages(); /* just to make sure it works ;) */ /* 5. Register my callback */ gras_cb_register("ping", &server_cb_ping_handler); XBT_INFO(">>>>>>>> Listening on port %d <<<<<<<<", gras_socket_my_port(globals->sock)); globals->endcondition = 0; /* 6. Wait up to 20 minutes for an incomming message to handle */ gras_msg_handle(20.0); /* 7. Housekeeping */ if (!globals->endcondition) XBT_WARN ("An error occured, the endcondition was not set by the callback"); /* 8. Free the allocated resources, and shut GRAS down */ gras_socket_close(globals->sock); free(globals); XBT_INFO("Done."); gras_exit(); return 0; } /* end_of_server */
/****************************************************************************** Dump a black-and-white identification mosaic image, => if (gr != 0) the mosaic raster is set to exactly the same size as gr. Otherwise the mosaic will be just big enough to hold the objects. This may be smaller than the original raster. */ rc_t mosaic_dump(const objl_t *ol, const char *fn, const gras_t *gr_o) { gras_t gr ; const char *fn_default = "ID-mosaic.tif" ; const char *me = "mosaic_dump_org" ; if (!fn || !strlen(fn)) fn = fn_unique(fn_default) ; gras_init(&gr,0,0) ; if ((mosaic_org(&gr,ol,id_bg,id_fg,id_org_flags,gr_o) != OK) || (tiff_dump_gras(fn,&gr) != OK) ) return(subfail(__FILE__,__LINE__,me)) ; gras_free(&gr) ; if (verbosity < MSG_FATAL) printf("%s [%s:%d]: wrote ID mosaic to \"%s\".\n",me,__FILE__,__LINE__,fn); return(OK) ; }
/****************************************************************************** Dump a black-and-white identification mosaic image, with the objects in one long strip sorted according to prop. */ rc_t mosaic_dump_sort(objl_t *ol, const char *fn, geo_t prop) { gras_t gr ; const char *fn_default = "ID-mosaic.tif" ; const char *me = "mosaic_dump_sort" ; if (!fn || !strlen(fn)) fn = fn_unique(fn_default) ; gras_init(&gr,0,0) ; if ((geo_sort(ol,prop,0) != OK) || (mosaic_strip(&gr,ol,id_bg,id_fg,id_str_flags) != OK) ) return(subfail(__FILE__,__LINE__,me)) ; if (tiff_dump_gras(fn,&gr) != OK) return(subfail(__FILE__,__LINE__,me)) ; gras_free(&gr) ; if (verbosity < MSG_FATAL) printf("%s [%s:%d]: wrote ID mosaic to \"%s\".\n",me,__FILE__,__LINE__,fn); return(OK) ; }
int server(int argc, char *argv[]) { gras_socket_t mysock; /* socket on which I listen */ gras_socket_t toclient; /* socket used to write to the client */ gras_init(&argc, argv); gras_msgtype_declare("hello", NULL); mysock = gras_socket_server(atoi(argv[1])); gras_msg_wait(60, "hello", &toclient, NULL /* no payload */ ); fprintf(stderr, "Cool, we received the message from %s:%d.\n", gras_socket_peer_name(toclient), gras_socket_peer_port(toclient)); gras_exit(); return 0; }
int client(int argc, char *argv[]) { gras_socket_t mysock; /* socket on which I listen */ gras_socket_t toserver; /* socket used to write to the server */ gras_init(&argc, argv); gras_msgtype_declare("hello", NULL); mysock = gras_socket_server_range(1024, 10000, 0, 0); fprintf(stderr, "Client ready; listening on %d\n", gras_socket_my_port(mysock)); gras_os_sleep(1.5); /* sleep 1 second and half */ toserver = gras_socket_client("Jacquelin", 12345); gras_msg_send(toserver, "hello", NULL); fprintf(stderr, "That's it, we sent the data to the server\n"); gras_exit(); return 0; }
int client(int argc, char *argv[]) { my_globals *globals; gras_init(&argc, argv); globals = gras_userdata_new(my_globals); globals->still_to_do = -1; XBT_INFO("Programming the repetitive_action with a frequency of %f sec", REPEAT_INTERVAL); gras_timer_repeat(REPEAT_INTERVAL, repetitive_action); XBT_INFO("Programming the delayed_action for after %f sec", DELAY_INTERVAL); gras_timer_delay(REPEAT_INTERVAL, delayed_action); XBT_INFO("Have a rest"); gras_os_sleep(DELAY_INTERVAL / 2.0); XBT_INFO("Canceling the delayed_action."); gras_timer_cancel_delay(REPEAT_INTERVAL, delayed_action); XBT_INFO("Re-programming the delayed_action for after %f sec", DELAY_INTERVAL); gras_timer_delay(REPEAT_INTERVAL, delayed_action); while (globals->still_to_do == -1 || /* Before delayed action runs */ globals->still_to_do > 0 /* after delayed_action, and not enough repetitive_action */ ) { XBT_DEBUG("Prepare to handle messages for 5 sec (still_to_do=%d)", globals->still_to_do); gras_msg_handle(5.0); } gras_exit(); return 0; } /* end_of_client */
int server(int argc, char *argv[]) { gras_socket_t mysock; /* socket on which I listen */ server_data_t *globals; gras_init(&argc, argv); globals = gras_userdata_new(server_data_t *); globals->done = 0; message_declaration(); mysock = gras_socket_server(atoi(argv[1])); gras_cb_register("convert a2i", &server_convert_a2i_cb); gras_cb_register("convert i2a", &server_convert_i2a_cb); gras_cb_register("done", &server_done_cb); while (!globals->done) { gras_msg_handle(-1); /* blocking */ } gras_exit(); return 0; }
int server(int argc, char *argv[]) { gras_socket_t mysock; /* socket on which I listen */ server_data_t *globals; gras_init(&argc, argv); globals = gras_userdata_new(server_data_t *); globals->killed = 0; gras_msgtype_declare("hello", NULL); gras_msgtype_declare("kill", NULL); mysock = gras_socket_server(atoi(argv[1])); gras_cb_register("hello", &server_hello_cb); gras_cb_register("kill", &server_kill_cb); while (!globals->killed) { gras_msg_handle(60); } gras_exit(); return 0; }
int client(int argc, char *argv[]) { xbt_ex_t e; gras_socket_t toserver = NULL; /* peer */ int connected = 0; gras_socket_t from; int ping, pong; const char *host = "127.0.0.1"; int port = 4000; /* 1. Init the GRAS's infrastructure */ gras_init(&argc, argv); /* 2. Get the server's address. The command line override defaults when specified */ if (argc == 3) { host = argv[1]; port = atoi(argv[2]); } XBT_INFO("Launch client (server on %s:%d)", host, port); /* 3. Create a socket to speak to the server */ while (!connected) { TRY { toserver = gras_socket_client(host, port); connected = 1; } CATCH(e) { if (e.category != system_error) /* dunno what happened, let the exception go through */ RETHROWF("Unable to connect to the server: %s"); xbt_ex_free(e); gras_os_sleep(0.05); } } XBT_INFO("Connected to %s:%d.", host, port); /* 4. Register the messages. See, it doesn't have to be done completely at the beginning, only before use */ ping_register_messages(); /* 5. Keep the user informed of what's going on */ XBT_INFO(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<", gras_socket_peer_name(toserver), gras_socket_peer_port(toserver)); /* 6. Prepare and send the ping message to the server */ ping = 1234; TRY { gras_msg_send(toserver, "ping", &ping); } CATCH(e) { gras_socket_close(toserver); RETHROWF("Failed to send PING to server: %s"); } XBT_INFO(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<", ping, gras_socket_peer_name(toserver), gras_socket_peer_port(toserver)); /* 7. Wait for the answer from the server, and deal with issues */ TRY { gras_msg_wait(6000, "pong", &from, &pong); } CATCH(e) { gras_socket_close(toserver); RETHROWF("Why can't I get my PONG message like everyone else: %s"); } /* 8. Keep the user informed of what's going on, again */ XBT_INFO(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<", pong, gras_socket_peer_name(from), gras_socket_peer_port(from)); /* 9. Free the allocated resources, and shut GRAS down */ gras_socket_close(toserver); XBT_INFO("Done."); gras_exit(); return 0; } /* end_of_client */