Obsolete
/
libipc-old
Archived
3
0
Fork 0
pollfd
Karchnu 2020-06-29 21:07:19 +02:00
parent 2a8e9a3707
commit a6ca358847
4 changed files with 63 additions and 72 deletions

View File

@ -4,8 +4,6 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> // error numbers #include <errno.h> // error numbers
#include <poll.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -9,6 +9,8 @@
#include <errno.h> // error numbers #include <errno.h> // error numbers
#include <time.h> #include <time.h>
#include <poll.h>
/*** /***
* global defaults * global defaults
**/ **/

View File

@ -13,48 +13,47 @@
fprintf(stderr, "error while %s: %s\n", msg, err);\ fprintf(stderr, "error while %s: %s\n", msg, err);\
} }
void non_interactive (char *env[]) void non_interactive ()
{ {
struct ipc_message m; SECURE_DECLARATION(struct ipc_message, m);
memset (&m, 0, sizeof (struct ipc_message)); SECURE_DECLARATION(struct ipc_ctx, ctx);
SECURE_DECLARATION(struct ipc_connection_info, srv);
// init service // init service
TEST_IPC_Q(ipc_connection (env, &srv, SERVICE_NAME), EXIT_FAILURE); TEST_IPC_Q(ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
int server_fd = ctx.pollfd[0].fd;
printf ("msg to send (%ld): %.*s\n", (ssize_t) strlen(MSG) +1, (int) strlen(MSG), MSG); printf ("msg to send (%ld): %.*s\n", (ssize_t) strlen(MSG) +1, (int) strlen(MSG), MSG);
TEST_IPC_Q(ipc_message_format_data (&m, /* type */ 'a', MSG, (ssize_t) strlen(MSG) +1), EXIT_FAILURE); TEST_IPC_Q(ipc_message_format_data (&m, /* type */ 'a', MSG, (ssize_t) strlen(MSG) +1), EXIT_FAILURE);
TEST_IPC_Q(ipc_write (&srv, &m), EXIT_FAILURE);
TEST_IPC_Q(ipc_read (&srv, &m), EXIT_FAILURE); m.fd = server_fd;
TEST_IPC_Q(ipc_write (&ctx, &m), EXIT_FAILURE);
TEST_IPC_Q(ipc_read (&ctx, 0 /* only one option */, &m), EXIT_FAILURE);
printf ("msg recv: %s\n", m.payload); printf ("msg recv: %s\n", m.payload);
ipc_message_empty (&m); ipc_message_empty (&m);
TEST_IPC_Q(ipc_close (&srv), EXIT_FAILURE); TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE);
} }
void interactive (char *env[]) #if 0
void interactive ()
{ {
SECURE_DECLARATION(struct ipc_connection_info, srv); SECURE_DECLARATION(struct ipc_ctx, ctx);
// index and version should be filled
srv.index = 0;
srv.version = 0;
// init service // init service
TEST_IPC_Q(ipc_connection (env, &srv, SERVICE_NAME), EXIT_FAILURE); TEST_IPC_Q(ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
int server_fd = ctx.pollfd[0].fd;
SECURE_DECLARATION(struct ipc_event, event); SECURE_DECLARATION(struct ipc_event, event);
SECURE_DECLARATION(struct ipc_ctx, services);
TEST_IPC_Q(ipc_add (&services, &srv), EXIT_FAILURE);
long timer = 10; long timer = 10;
while (1) { while (1) {
printf ("msg to send: "); printf ("msg to send: ");
fflush (stdout); fflush (stdout);
TEST_IPC_Q(ipc_wait_event (&services, NULL, &event, &timer), EXIT_FAILURE); TEST_IPC_Q(ipc_events_loop (&ctx, &event, &timer), EXIT_FAILURE);
switch (event.type) { switch (event.type) {
case IPC_EVENT_TYPE_TIMER: { case IPC_EVENT_TYPE_TIMER: {
@ -71,14 +70,16 @@ void interactive (char *env[])
ipc_message_empty (m); ipc_message_empty (m);
free (m); free (m);
ipc_connections_free (&services); TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE);
TEST_IPC_Q(ipc_close (&srv), EXIT_FAILURE); ipc_connections_free (&ctx);
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }
TEST_IPC_Q(ipc_write (&srv, m), EXIT_FAILURE); m->fd = server_fd;
TEST_IPC_Q(ipc_write (&ctx, m), EXIT_FAILURE);
} }
break; break;
case IPC_EVENT_TYPE_MESSAGE: case IPC_EVENT_TYPE_MESSAGE:
@ -96,16 +97,19 @@ void interactive (char *env[])
} }
} }
} }
#endif
int main (int argc, char *argv[], char *env[]) //int main (int argc, char *argv[])
int main (void)
{ {
argc = argc; // warnings non_interactive ();
argv = argv; // warnings
#if 0
if (argc == 1) if (argc == 1)
non_interactive (env); non_interactive ();
else else
interactive (env); interactive ();
#endif
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -14,52 +14,49 @@
int cpt = 0; int cpt = 0;
struct ipc_connection_info *srv = 0; struct ipc_ctx *ctx;
struct ipc_ctx *clients;
void main_loop () void main_loop ()
{ {
SECURE_DECLARATION(struct ipc_error, ret); SECURE_DECLARATION(struct ipc_error, ret);
clients = malloc (sizeof (struct ipc_ctx)); ctx = malloc (sizeof (struct ipc_ctx));
memset(clients, 0, sizeof(struct ipc_ctx)); memset(ctx, 0, sizeof(struct ipc_ctx));
SECURE_DECLARATION(struct ipc_event,event); SECURE_DECLARATION(struct ipc_event, event);
long timer = 10; int timer = 10000;
while(1) { while(1) {
// ipc_wait_event provides one event at a time // ipc_wait_event provides one event at a time
// warning: event->m is free'ed if not NULL // warning: event->m is free'ed if not NULL
ret = ipc_wait_event (clients, srv, &event, &timer); ret = ipc_events_loop (ctx, &event, &timer);
if (ret.error_code != IPC_ERROR_NONE && ret.error_code != IPC_ERROR_CLOSED_RECIPIENT) { if (ret.error_code != IPC_ERROR_NONE && ret.error_code != IPC_ERROR_CLOSED_RECIPIENT) {
PRINTERR(ret,"service poll event"); PRINTERR(ret,"service poll event");
// the application will shut down, and close the service // the application will shut down, and close the service
TEST_IPC_Q(ipc_server_close (srv), EXIT_FAILURE); TEST_IPC_Q(ipc_close_all (ctx), EXIT_FAILURE);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
switch (event.type) { switch (event.type) {
case IPC_EVENT_TYPE_TIMER: { case IPC_EVENT_TYPE_TIMER: {
fprintf(stderr, "time up!\n"); fprintf(stderr, "time up!\n");
timer = 10; timer = 10000;
}; };
break; break;
case IPC_EVENT_TYPE_CONNECTION: { case IPC_EVENT_TYPE_CONNECTION: {
cpt++; cpt++;
printf ("connection: %d clients connected\n", cpt); printf ("connection: %d ctx connected\n", cpt);
printf ("new client has the fd %d\n", ((struct ipc_connection_info*) event.origin)->fd); printf ("new client has the fd %d\n", event.origin);
}; };
break; break;
case IPC_EVENT_TYPE_DISCONNECTION: case IPC_EVENT_TYPE_DISCONNECTION:
{ {
cpt--; cpt--;
printf ("disconnection: %d clients remaining\n", cpt); printf ("disconnection: %d clients remaining\n", cpt);
// TODO: something?
// free the ipc_connection_info structure
free (event.origin);
}; };
break; break;
case IPC_EVENT_TYPE_MESSAGE: case IPC_EVENT_TYPE_MESSAGE:
@ -69,16 +66,20 @@ void main_loop ()
printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload); printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload);
} }
ret = ipc_write (event.origin, m); ret = ipc_write (ctx, m);
if (ret.error_code != IPC_ERROR_NONE) { if (ret.error_code != IPC_ERROR_NONE) {
PRINTERR(ret,"server write"); PRINTERR(ret,"server write");
} }
}; };
break; break;
case IPC_EVENT_TYPE_TX:
{
printf ("a message was sent\n");
}
break;
case IPC_EVENT_TYPE_ERROR: case IPC_EVENT_TYPE_ERROR:
{ {
fprintf (stderr, "a problem happened with client %d\n" fprintf (stderr, "a problem happened with client %d\n", event.origin);
, ((struct ipc_connection_info*) event.origin)->fd);
}; };
break; break;
default : default :
@ -97,48 +98,34 @@ void exit_program(int signal)
{ {
printf("Quitting, signal: %d\n", signal); printf("Quitting, signal: %d\n", signal);
// free remaining clients
for (size_t i = 0; i < clients->size ; i++) {
struct ipc_connection_info *cli = clients->cinfos[i];
if (cli != NULL) {
free (cli);
}
clients->cinfos[i] = NULL;
}
ipc_connections_free (clients);
free (clients);
// the application will shut down, and close the service // the application will shut down, and close the service
TEST_IPC_Q(ipc_server_close (srv), EXIT_FAILURE); TEST_IPC_Q(ipc_close_all (ctx), EXIT_FAILURE);
free (srv);
// free remaining ctx
ipc_connections_free (ctx);
free (ctx);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
/* /*
* service ping-pong: send back everything sent by the clients * service ping-pong: send back everything sent by the ctx
* stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals * stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals
*/ */
int main(int argc, char * argv[], char **env) int main(void)
{ {
argc = argc; // warnings
argv = argv; // warnings
printf ("pid = %d\n", getpid ()); printf ("pid = %d\n", getpid ());
srv = malloc (sizeof (struct ipc_connection_info)); ctx = malloc (sizeof (struct ipc_ctx));
if (srv == NULL) { if (ctx == NULL) {
exit (1); exit (1);
} }
memset (srv, 0, sizeof (struct ipc_connection_info)); memset (ctx, 0, sizeof (struct ipc_ctx));
srv->index = 0;
srv->version = 0;
TEST_IPC_Q(ipc_server_init (env, srv, PONGD_SERVICE_NAME), EXIT_FAILURE); TEST_IPC_Q(ipc_server_init (ctx, PONGD_SERVICE_NAME), EXIT_FAILURE);
struct ipc_connection_info * srv = &ctx->cinfos[0];
printf ("Listening on %s.\n", srv->spath); printf ("Listening on %s.\n", srv->spath);
printf("MAIN: server created\n" ); printf("MAIN: server created\n" );