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;
|
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
void handle_new_connection (struct ipc_service *srv, struct ipc_client_array *ap)
|
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");
|
|
|
|
}
|
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
if (ipc_client_add (ap, p) < 0) {
|
|
|
|
handle_error("ipc_client_add < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cpt++;
|
|
|
|
printf ("%d client(s)\n", cpt);
|
|
|
|
}
|
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
void handle_new_msg (struct ipc_client_array *ap, struct ipc_client_array *proc_to_read)
|
2016-12-23 01:33:52 +01:00
|
|
|
{
|
2018-10-03 22:02:37 +02:00
|
|
|
struct ipc_message m;
|
|
|
|
memset (&m, 0, sizeof (struct ipc_message));
|
2016-12-23 01:33:52 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < proc_to_read->size; i++) {
|
|
|
|
// printf ("loop handle_new_msg\n");
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_server_read (proc_to_read->tab_proc[i], &m) < 0) {
|
|
|
|
handle_error("server_read < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
// close the client then delete it from the client array
|
2017-01-19 22:07:52 +01:00
|
|
|
if (m.type == MSG_TYPE_CLOSE) {
|
2016-12-23 01:33:52 +01:00
|
|
|
cpt--;
|
|
|
|
printf ("disconnection => %d client(s) remaining\n", cpt);
|
|
|
|
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_server_close_proc (proc_to_read->tab_proc[i]) < 0)
|
|
|
|
handle_err( "handle_new_msg", "server_close_proc < 0");
|
2018-10-04 01:22:50 +02:00
|
|
|
if (ipc_client_del (ap, proc_to_read->tab_proc[i]) < 0)
|
|
|
|
handle_err( "handle_new_msg", "ipc_client_del < 0");
|
|
|
|
if (ipc_client_del (proc_to_read, proc_to_read->tab_proc[i]) < 0)
|
|
|
|
handle_err( "handle_new_msg", "ipc_client_del < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("new message : %s", m.val);
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_server_write (proc_to_read->tab_proc[i], &m) < 0) {
|
|
|
|
handle_err( "handle_new_msg", "server_write < 0");
|
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
|
|
|
*/
|
|
|
|
|
2018-10-04 00:30:47 +02:00
|
|
|
void main_loop (struct ipc_service *srv)
|
2016-12-23 01:33:52 +01:00
|
|
|
{
|
|
|
|
int i, ret = 0;
|
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
struct ipc_client_array ap;
|
|
|
|
memset(&ap, 0, sizeof(struct ipc_client_array));
|
2016-12-23 01:33:52 +01:00
|
|
|
|
2018-10-04 01:22:50 +02:00
|
|
|
struct ipc_client_array proc_to_read;
|
|
|
|
memset(&proc_to_read, 0, sizeof(struct ipc_client_array));
|
2016-12-23 01:33:52 +01:00
|
|
|
|
|
|
|
while(1) {
|
2018-10-03 21:24:20 +02:00
|
|
|
ret = ipc_server_select (&ap, srv, &proc_to_read);
|
2018-10-04 01:22:50 +02:00
|
|
|
// printf ("on peut lire ces client:\n");
|
|
|
|
// ipc_client_array_print (&proc_to_read);
|
2016-12-23 01:33:52 +01:00
|
|
|
// printf ("-- \n\n");
|
|
|
|
|
|
|
|
if (ret == CONNECTION) {
|
|
|
|
handle_new_connection (srv, &ap);
|
|
|
|
} else if (ret == APPLICATION) {
|
|
|
|
handle_new_msg (&ap, &proc_to_read);
|
|
|
|
} else { // both new connection and new msg from at least one client
|
|
|
|
handle_new_connection (srv, &ap);
|
|
|
|
handle_new_msg (&ap, &proc_to_read);
|
|
|
|
}
|
2018-10-04 01:22:50 +02:00
|
|
|
ipc_client_array_free (&proc_to_read);
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ap.size; i++) {
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_server_close_proc (ap.tab_proc[i]) < 0) {
|
|
|
|
handle_error( "server_close_proc < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* service ping-pong
|
|
|
|
*
|
|
|
|
* 1. creates the named pipe /tmp/<service>, then listens
|
|
|
|
* 2. opens the named pipes in & out
|
|
|
|
* 3. talks with the (test) program
|
|
|
|
* 4. closes the test program named pipes
|
|
|
|
* 5. removes the named pipe /tmp/<service>
|
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char * argv[], char **env)
|
|
|
|
{
|
2018-10-04 00:30:47 +02:00
|
|
|
struct ipc_service srv;
|
|
|
|
memset (&srv, 0, sizeof (struct ipc_service));
|
2016-12-23 01:33:52 +01:00
|
|
|
srv.index = 0;
|
|
|
|
srv.version = 0;
|
|
|
|
|
|
|
|
// unlink("/tmp/ipc/pongd-0-0");
|
|
|
|
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_server_init (argc, argv, env, &srv, PONGD_SERVICE_NAME) < 0) {
|
|
|
|
handle_error("server_init < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
printf ("Listening on %s.\n", srv.spath);
|
|
|
|
|
|
|
|
printf("MAIN: server created\n" );
|
|
|
|
|
|
|
|
// the service will loop until the end of time, a specific message, a signal
|
|
|
|
main_loop (&srv);
|
|
|
|
|
|
|
|
// the application will shut down, and remove the service named pipe
|
2018-10-03 21:24:20 +02:00
|
|
|
if (ipc_server_close (&srv) < 0) {
|
|
|
|
handle_error("server_close < 0");
|
2016-12-23 01:33:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|