Improved pong(d) examples.
parent
626053ad63
commit
75b407538b
|
@ -25,21 +25,28 @@ void chomp (char *str, ssize_t len)
|
||||||
|
|
||||||
struct ipc_connection_info *srv;
|
struct ipc_connection_info *srv;
|
||||||
|
|
||||||
void non_interactive (char *env[])
|
void non_interactive (int verbosity, size_t nb_msg, char *msg_str, char *env[])
|
||||||
{
|
{
|
||||||
SECURE_DECLARATION (struct ipc_message, m);
|
SECURE_DECLARATION (struct ipc_message, m);
|
||||||
|
|
||||||
// init service
|
// init service
|
||||||
TEST_IPC_QUIT_ON_ERROR (ipc_connection (env, srv, SERVICE_NAME), EXIT_FAILURE);
|
TEST_IPC_QUIT_ON_ERROR (ipc_connection (env, srv, SERVICE_NAME), EXIT_FAILURE);
|
||||||
TEST_IPC_QUIT_ON_ERROR (ipc_message_format_data (&m, 42, MSG, (ssize_t) strlen (MSG) + 1), EXIT_FAILURE);
|
|
||||||
|
|
||||||
printf ("msg to send (%ld): %.*s\n", (ssize_t) strlen (MSG) + 1, (int)strlen (MSG), MSG);
|
if (verbosity > 1) {
|
||||||
TEST_IPC_QUIT_ON_ERROR (ipc_write (srv, &m), EXIT_FAILURE);
|
printf ("msg to send (%ld): %.*s\n", (ssize_t) strlen (MSG) + 1, (int)strlen (MSG), MSG);
|
||||||
ipc_message_empty (&m);
|
}
|
||||||
TEST_IPC_QUIT_ON_ERROR (ipc_read (srv, &m), EXIT_FAILURE);
|
|
||||||
|
|
||||||
printf ("msg recv (type: %u): %s\n", m.user_type, m.payload);
|
for (size_t i = 0 ; i < nb_msg ; i++) {
|
||||||
ipc_message_empty (&m);
|
TEST_IPC_QUIT_ON_ERROR (ipc_message_format_data (&m, 42, msg_str, (ssize_t) strlen (msg_str) + 1), EXIT_FAILURE);
|
||||||
|
TEST_IPC_QUIT_ON_ERROR (ipc_write (srv, &m), EXIT_FAILURE);
|
||||||
|
ipc_message_empty (&m);
|
||||||
|
TEST_IPC_QUIT_ON_ERROR (ipc_read (srv, &m), EXIT_FAILURE);
|
||||||
|
|
||||||
|
if (verbosity > 1) {
|
||||||
|
printf ("msg recv (type: %u): %s\n", m.user_type, m.payload);
|
||||||
|
}
|
||||||
|
ipc_message_empty (&m);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_IPC_QUIT_ON_ERROR (ipc_close (srv), EXIT_FAILURE);
|
TEST_IPC_QUIT_ON_ERROR (ipc_close (srv), EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -140,7 +147,9 @@ void interactive (char *env[])
|
||||||
|
|
||||||
int main (int argc, char *argv[], char *env[])
|
int main (int argc, char *argv[], char *env[])
|
||||||
{
|
{
|
||||||
argc = argc; // warnings
|
printf("usage: %s [verbosity #messages message]", argv[0]);
|
||||||
|
|
||||||
|
// $0:
|
||||||
argv = argv; // warnings
|
argv = argv; // warnings
|
||||||
|
|
||||||
srv = malloc (sizeof (struct ipc_connection_info));
|
srv = malloc (sizeof (struct ipc_connection_info));
|
||||||
|
@ -150,8 +159,8 @@ int main (int argc, char *argv[], char *env[])
|
||||||
srv->index = 0;
|
srv->index = 0;
|
||||||
srv->version = 0;
|
srv->version = 0;
|
||||||
|
|
||||||
if (argc == 1)
|
if (argc == 4)
|
||||||
non_interactive (env);
|
non_interactive (atoi(argv[1]), (size_t) atoi(argv[2]), argv[3], env);
|
||||||
else
|
else
|
||||||
interactive (env);
|
interactive (env);
|
||||||
|
|
||||||
|
|
|
@ -14,12 +14,15 @@
|
||||||
|
|
||||||
int cpt = 0;
|
int cpt = 0;
|
||||||
|
|
||||||
|
int verbosity = 1;
|
||||||
|
|
||||||
struct ipc_connection_info *srv = NULL;
|
struct ipc_connection_info *srv = NULL;
|
||||||
struct ipc_connection_infos *clients = NULL;
|
struct ipc_connection_infos *clients = NULL;
|
||||||
|
|
||||||
void main_loop ()
|
void main_loop ()
|
||||||
{
|
{
|
||||||
long timer = 10;
|
long base_timer = 0;
|
||||||
|
long timer = base_timer;
|
||||||
SECURE_DECLARATION (struct ipc_error, ret);
|
SECURE_DECLARATION (struct ipc_error, ret);
|
||||||
|
|
||||||
clients = malloc (sizeof (struct ipc_connection_infos));
|
clients = malloc (sizeof (struct ipc_connection_infos));
|
||||||
|
@ -37,17 +40,17 @@ void main_loop ()
|
||||||
case IPC_EVENT_TYPE_CONNECTION:
|
case IPC_EVENT_TYPE_CONNECTION:
|
||||||
{
|
{
|
||||||
cpt++;
|
cpt++;
|
||||||
#ifdef PONGD_VERBOSE
|
if (verbosity > 1) {
|
||||||
printf ("connection: %d clients connected, new client is %d\n", cpt, (event.origin)->fd);
|
printf ("connection: %d clients connected, new client is %d\n", cpt, (event.origin)->fd);
|
||||||
#endif
|
}
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case IPC_EVENT_TYPE_DISCONNECTION:
|
case IPC_EVENT_TYPE_DISCONNECTION:
|
||||||
{
|
{
|
||||||
cpt--;
|
cpt--;
|
||||||
#ifdef PONGD_VERBOSE
|
if (verbosity > 1) {
|
||||||
printf ("disconnection: %d clients remaining\n", cpt);
|
printf ("disconnection: %d clients remaining\n", cpt);
|
||||||
#endif
|
}
|
||||||
|
|
||||||
// free the ipc_client structure
|
// free the ipc_client structure
|
||||||
free (event.origin);
|
free (event.origin);
|
||||||
|
@ -56,27 +59,28 @@ void main_loop ()
|
||||||
case IPC_EVENT_TYPE_MESSAGE:
|
case IPC_EVENT_TYPE_MESSAGE:
|
||||||
{
|
{
|
||||||
struct ipc_message *m = event.m;
|
struct ipc_message *m = event.m;
|
||||||
#ifdef PONGD_VERBOSE
|
if (verbosity > 1) {
|
||||||
if (m->length > 0) {
|
if (m->length > 0) {
|
||||||
printf ("message received (type %d, user type %d, size %u bytes): %.*s\n",
|
printf ("message received (type %d, user type %d, size %u bytes): %.*s\n",
|
||||||
m->type, m->user_type, m->length, m->length, m->payload);
|
m->type, m->user_type, m->length, m->length, m->payload);
|
||||||
} else {
|
} else {
|
||||||
printf ("message with a 0-byte size :(\n");
|
printf ("message with a 0-byte size :(\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ret = ipc_write (event.origin, m);
|
ret = ipc_write (event.origin, m);
|
||||||
if (ret.error_code != IPC_ERROR_NONE) {
|
if (ret.error_code != IPC_ERROR_NONE) {
|
||||||
PRINTERR (ret, "server write");
|
PRINTERR (ret, "server write");
|
||||||
}
|
}
|
||||||
printf ("message sent\n");
|
if (verbosity > 1) {
|
||||||
|
printf ("message sent\n");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case IPC_EVENT_TYPE_TIMER:{
|
case IPC_EVENT_TYPE_TIMER:{
|
||||||
printf ("timer\n");
|
printf ("timer\n");
|
||||||
|
|
||||||
timer = 10;
|
timer = base_timer;
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
case IPC_EVENT_TYPE_ERROR:
|
case IPC_EVENT_TYPE_ERROR:
|
||||||
|
@ -136,6 +140,10 @@ int main (int argc, char *argv[], char **env)
|
||||||
argc = argc; // warnings
|
argc = argc; // warnings
|
||||||
argv = argv; // warnings
|
argv = argv; // warnings
|
||||||
|
|
||||||
|
if (argc > 1) {
|
||||||
|
verbosity = atoi(argv[1]);
|
||||||
|
}
|
||||||
|
|
||||||
printf ("pid = %d\n", getpid ());
|
printf ("pid = %d\n", getpid ());
|
||||||
|
|
||||||
srv = malloc (sizeof (struct ipc_connection_info));
|
srv = malloc (sizeof (struct ipc_connection_info));
|
||||||
|
|
Reference in New Issue