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)
18 gras_socket_t client = gras_msg_cb_ctx_from(ctx);
19 server_data_t *globals = (server_data_t *) gras_userdata_get();
21 XBT_CRITICAL("Argh, killed by %s:%d! Bye folks, I'm out of here...",
22 gras_socket_peer_name(client), gras_socket_peer_port(client));
27 } /* end_of_kill_callback */
29 int server(int argc, char *argv[])
31 gras_socket_t mysock; /* socket on which I listen */
32 server_data_t *globals;
34 gras_init(&argc, argv);
36 globals = gras_userdata_new(server_data_t *);
39 gras_msgtype_declare("kill", NULL);
40 gras_cb_register("kill", &server_kill_cb);
42 if (argc > 1 && !strcmp(argv[1], "--cheat")) {
43 mysock = gras_socket_server(9999);
44 XBT_INFO("Hi! hi! I'm not in the search range, but in 9999...");
46 mysock = gras_socket_server((rand() % 10) + 3000);
47 XBT_INFO("Ok, I'm hidden on port %d. Hope for the best.",
48 gras_socket_my_port(mysock));
51 while (!globals->killed) {
52 gras_msg_handle(-1); /* blocking */
59 int client(int argc, char *argv[])
61 gras_socket_t mysock; /* socket on which I listen */
62 gras_socket_t toserver; /* socket used to write to the server */
63 int found; /* whether we found peer */
64 int port; /* where we think that the server is */
67 gras_init(&argc, argv);
69 gras_msgtype_declare("kill", NULL);
70 mysock = gras_socket_server_range(1024, 10000, 0, 0);
72 XBT_VERB("Run little server, run. I'll get you. (sleep 1.5 sec)");
75 for (port = 3000, found = 0; port < 3010 && !found; port++) {
77 toserver = gras_socket_client(argv[1], port);
78 gras_msg_send(toserver, "kill", NULL);
79 gras_socket_close(toserver);
81 XBT_INFO("Yeah! I found the server on %d! It's eradicated by now.",
88 XBT_INFO("Damn, the server is not on %d", port);
92 THROWF(not_found_error, 0,
93 "Damn, I failed to find the server! I cannot survive this humilliation.");