Archived
3
0

Improved pong(d) examples.

This commit is contained in:
Philippe PITTOLI 2020-01-28 13:39:06 +01:00
parent 626053ad63
commit 75b407538b
2 changed files with 45 additions and 28 deletions

View File

@ -25,21 +25,28 @@ void chomp (char *str, ssize_t len)
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);
// init service
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);
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 to send (%ld): %.*s\n", (ssize_t) strlen (MSG) + 1, (int)strlen (MSG), MSG);
}
printf ("msg recv (type: %u): %s\n", m.user_type, m.payload);
ipc_message_empty (&m);
for (size_t i = 0 ; i < nb_msg ; i++) {
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);
}
@ -140,7 +147,9 @@ void interactive (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
srv = malloc (sizeof (struct ipc_connection_info));
@ -150,8 +159,8 @@ int main (int argc, char *argv[], char *env[])
srv->index = 0;
srv->version = 0;
if (argc == 1)
non_interactive (env);
if (argc == 4)
non_interactive (atoi(argv[1]), (size_t) atoi(argv[2]), argv[3], env);
else
interactive (env);

View File

@ -14,12 +14,15 @@
int cpt = 0;
int verbosity = 1;
struct ipc_connection_info *srv = NULL;
struct ipc_connection_infos *clients = NULL;
void main_loop ()
{
long timer = 10;
long base_timer = 0;
long timer = base_timer;
SECURE_DECLARATION (struct ipc_error, ret);
clients = malloc (sizeof (struct ipc_connection_infos));
@ -37,17 +40,17 @@ void main_loop ()
case IPC_EVENT_TYPE_CONNECTION:
{
cpt++;
#ifdef PONGD_VERBOSE
printf ("connection: %d clients connected, new client is %d\n", cpt, (event.origin)->fd);
#endif
if (verbosity > 1) {
printf ("connection: %d clients connected, new client is %d\n", cpt, (event.origin)->fd);
}
};
break;
case IPC_EVENT_TYPE_DISCONNECTION:
{
cpt--;
#ifdef PONGD_VERBOSE
printf ("disconnection: %d clients remaining\n", cpt);
#endif
if (verbosity > 1) {
printf ("disconnection: %d clients remaining\n", cpt);
}
// free the ipc_client structure
free (event.origin);
@ -56,27 +59,28 @@ void main_loop ()
case IPC_EVENT_TYPE_MESSAGE:
{
struct ipc_message *m = event.m;
#ifdef PONGD_VERBOSE
if (m->length > 0) {
printf ("message received (type %d, user type %d, size %u bytes): %.*s\n",
m->type, m->user_type, m->length, m->length, m->payload);
} else {
printf ("message with a 0-byte size :(\n");
if (verbosity > 1) {
if (m->length > 0) {
printf ("message received (type %d, user type %d, size %u bytes): %.*s\n",
m->type, m->user_type, m->length, m->length, m->payload);
} else {
printf ("message with a 0-byte size :(\n");
}
}
#endif
ret = ipc_write (event.origin, m);
if (ret.error_code != IPC_ERROR_NONE) {
PRINTERR (ret, "server write");
}
printf ("message sent\n");
if (verbosity > 1) {
printf ("message sent\n");
}
};
break;
case IPC_EVENT_TYPE_TIMER:{
printf ("timer\n");
timer = 10;
timer = base_timer;
};
break;
case IPC_EVENT_TYPE_ERROR:
@ -136,6 +140,10 @@ int main (int argc, char *argv[], char **env)
argc = argc; // warnings
argv = argv; // warnings
if (argc > 1) {
verbosity = atoi(argv[1]);
}
printf ("pid = %d\n", getpid ());
srv = malloc (sizeof (struct ipc_connection_info));