1 /* Copyright (c) 2006, 2007, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"My little example");
16 int server_kill_cb(gras_msg_cb_ctx_t ctx, void *payload) {
17 gras_socket_t client = gras_msg_cb_ctx_from(ctx);
18 server_data_t *globals=(server_data_t*)gras_userdata_get();
20 CRITICAL2("Argh, killed by %s:%d! Bye folks, I'm out of here...",
21 gras_socket_peer_name(client), gras_socket_peer_port(client));
26 } /* end_of_kill_callback */
28 int server(int argc, char *argv[]) {
29 gras_socket_t mysock; /* socket on which I listen */
30 server_data_t *globals;
32 gras_init(&argc,argv);
34 globals=gras_userdata_new(server_data_t*);
37 gras_msgtype_declare("kill", NULL);
38 gras_cb_register("kill",&server_kill_cb);
40 if (argc>1 && !strcmp(argv[1],"--cheat")) {
41 mysock = gras_socket_server(9999);
42 INFO0("Hi! hi! I'm not in the search range, but in 9999...");
44 mysock = gras_socket_server((rand() % 10) + 3000);
45 INFO1("Ok, I'm hidden on port %d. Hope for the best.", gras_socket_my_port(mysock));
48 while (!globals->killed) {
49 gras_msg_handle(-1); /* blocking */
56 int client(int argc, char *argv[]) {
57 gras_socket_t mysock; /* socket on which I listen */
58 gras_socket_t toserver; /* socket used to write to the server */
59 int found; /* whether we found peer */
60 int port; /* where we think that the server is */
63 gras_init(&argc,argv);
65 gras_msgtype_declare("kill", NULL);
66 mysock = gras_socket_server_range(1024, 10000, 0, 0);
68 VERB0("Run little server, run. I'll get you. (sleep 1.5 sec)");
71 for (port=3000, found=0; port<3010 && !found; port++) {
73 toserver = gras_socket_client(argv[1], port);
74 gras_msg_send(toserver,"kill", NULL);
75 gras_socket_close(toserver);
77 INFO1("Yeah! I found the server on %d! It's eradicated by now.",port);
82 INFO1("Damn, the server is not on %d",port);
86 THROW0(not_found_error, 0, "Damn, I failed to find the server! I cannot survive this humilliation.");