Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/pong/app/pongd.c

190 lines
4.5 KiB
C
Raw Normal View History

2016-12-23 01:33:52 +01:00
#include "../../core/communication.h"
2018-10-04 01:22:50 +02:00
#include "../../core/client.h"
2016-12-23 01:33:52 +01:00
#include "../../core/error.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define PONGD_SERVICE_NAME "pongd"
int cpt = 0;
struct ipc_service *srv = 0;
void handle_new_connection (struct ipc_clients *clients)
2016-12-23 01:33:52 +01:00
{
2018-10-04 00:30:47 +02:00
struct ipc_client *p = malloc(sizeof(struct ipc_client));
memset(p, 0, sizeof(struct ipc_client));
2016-12-23 01:33:52 +01:00
2018-10-03 21:24:20 +02:00
if (ipc_server_accept (srv, p) < 0) {
handle_error("server_accept < 0");
2016-12-23 01:33:52 +01:00
} else {
printf("new connection\n");
}
if (ipc_client_add (clients, p) < 0) {
2018-10-04 01:22:50 +02:00
handle_error("ipc_client_add < 0");
2016-12-23 01:33:52 +01:00
}
cpt++;
printf ("%d client(s)\n", cpt);
}
void handle_new_msg (struct ipc_clients *clients, struct ipc_clients *clients_talking)
2016-12-23 01:33:52 +01:00
{
2018-10-08 15:18:56 +02:00
int ret = 0;
2018-10-03 22:02:37 +02:00
struct ipc_message m;
memset (&m, 0, sizeof (struct ipc_message));
2018-10-08 15:18:56 +02:00
int i = 0;
for (i = 0; i < clients_talking->size; i++) {
2016-12-23 01:33:52 +01:00
// printf ("loop handle_new_msg\n");
2018-10-08 16:15:35 +02:00
// current talking client
struct ipc_client *pc = clients_talking->clients[i];
ret = ipc_server_read (pc, &m);
2018-10-08 15:18:56 +02:00
if (ret < 0) {
2018-10-03 21:24:20 +02:00
handle_error("server_read < 0");
2016-12-23 01:33:52 +01:00
}
2018-10-08 16:15:35 +02:00
// close the client then delete it from clients
2018-10-08 15:18:56 +02:00
if (ret == 1) {
2016-12-23 01:33:52 +01:00
cpt--;
printf ("disconnection => %d client(s) remaining\n", cpt);
2018-10-10 23:18:15 +02:00
if (ipc_server_close_client (pc) < 0) {
handle_err( "handle_new_msg", "server_close_client < 0");
}
if (ipc_client_del (clients, pc) < 0) {
handle_err( "handle_new_msg", "ipc_client_del < 0");
}
if (ipc_client_del (clients_talking, pc) < 0) {
handle_err( "handle_new_msg", "ipc_client_del < 0");
}
i--;
2018-10-08 16:15:35 +02:00
// free the ipc_client structure
free (pc);
2016-12-23 01:33:52 +01:00
continue;
}
if (m.type == MSG_TYPE_SERVER_CLOSE) {
2018-10-08 16:15:35 +02:00
// free remaining clients
for (int y = 0; y < clients->size ; y++) {
struct ipc_client *cli = clients->clients[y];
// TODO: replace with specific ipc_client_empty function
if (cli != NULL)
free (cli);
clients->clients[y] = NULL;
}
ipc_clients_free (clients);
ipc_clients_free (clients_talking);
if (ipc_server_close (srv) < 0) {
handle_error("server_close < 0");
}
2018-10-08 16:15:35 +02:00
ipc_message_empty (&m);
free (srv);
exit (0);
}
2016-12-23 01:33:52 +01:00
2018-10-12 01:59:56 +02:00
if (m.length > 0) {
printf ("new message : %.*s", m.length, m.payload);
}
2018-10-08 16:15:35 +02:00
if (ipc_server_write (pc, &m) < 0) {
2018-10-03 21:24:20 +02:00
handle_err( "handle_new_msg", "server_write < 0");
2016-12-23 01:33:52 +01:00
}
2018-10-08 16:15:35 +02:00
// empty the message structure
ipc_message_empty (&m);
memset (&m, 0, sizeof m);
2016-12-23 01:33:52 +01:00
}
}
/*
* main loop
*
* accept new application connections
* read a message and send it back
2017-01-19 22:07:52 +01:00
* close a connection if MSG_TYPE_CLOSE received
2016-12-23 01:33:52 +01:00
*/
void main_loop ()
2016-12-23 01:33:52 +01:00
{
2018-10-08 16:15:35 +02:00
int ret = 0;
2016-12-23 01:33:52 +01:00
struct ipc_clients clients;
memset(&clients, 0, sizeof(struct ipc_clients));
2016-12-23 01:33:52 +01:00
struct ipc_clients clients_talking;
memset(&clients_talking, 0, sizeof(struct ipc_clients));
2016-12-23 01:33:52 +01:00
while(1) {
int new_connection = 0;
ret = ipc_server_select (&clients, srv, &clients_talking, &new_connection);
if (ret < 0) {
handle_error("ipc_server_select < 0");
}
if (new_connection) {
handle_new_connection (&clients);
}
if (clients_talking.size > 0) {
handle_new_msg (&clients, &clients_talking);
2016-12-23 01:33:52 +01:00
}
2018-10-08 16:15:35 +02:00
ipc_clients_free (&clients_talking);
2016-12-23 01:33:52 +01:00
}
2018-10-08 16:15:35 +02:00
// should never go there
exit (1);
2016-12-23 01:33:52 +01:00
}
/*
* service ping-pong
*
2018-10-08 15:18:56 +02:00
* 1. creates the unix socket /run/ipc/<service>.sock, then listens
* 2. listen for new clients
* 3. then accept a new client, and send back everything it sends
* 4. close any client that closes its socket
*
* and finally, stop the program once a client sends a SERVER CLOSE command
2016-12-23 01:33:52 +01:00
*/
int main(int argc, char * argv[], char **env)
{
argc = argc; // warnings
argv = argv; // warnings
srv = malloc (sizeof (struct ipc_service));
if (srv == NULL) {
exit (1);
}
memset (srv, 0, sizeof (struct ipc_service));
srv->index = 0;
srv->version = 0;
2016-12-23 01:33:52 +01:00
// unlink("/tmp/ipc/pongd-0-0");
if (ipc_server_init (env, srv, PONGD_SERVICE_NAME) < 0) {
2018-10-03 21:24:20 +02:00
handle_error("server_init < 0");
2016-12-23 01:33:52 +01:00
return EXIT_FAILURE;
}
printf ("Listening on %s.\n", srv->spath);
2016-12-23 01:33:52 +01:00
printf("MAIN: server created\n" );
// the service will loop until the end of time, a specific message, a signal
main_loop ();
2016-12-23 01:33:52 +01:00
// the application will shut down, and remove the service named pipe
if (ipc_server_close (srv) < 0) {
2018-10-03 21:24:20 +02:00
handle_error("server_close < 0");
2016-12-23 01:33:52 +01:00
}
return EXIT_SUCCESS;
}