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/pingpong.c

109 lines
2.6 KiB
C
Raw Normal View History

2016-12-20 23:36:00 +01:00
#include "../../core/communication.h"
2016-10-27 16:27:04 +02:00
#include <sys/socket.h>
#include <sys/un.h>
2016-12-20 23:36:00 +01:00
#include "../../core/process.h"
#include <unistd.h>
2016-06-05 20:48:13 +02:00
#define PONGD_SERVICE_NAME "pongd"
2016-10-27 16:27:04 +02:00
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
/*
* main loop
*
* opens the application pipes,
* reads then writes the same message,
* then closes the pipes
*/
2016-10-27 16:27:04 +02:00
void main_loop (struct service *srv)
{
2016-10-27 16:27:04 +02:00
2016-12-20 23:36:00 +01:00
size_t msize = BUFSIZ;
char *buf = NULL;
if ( (buf = malloc (BUFSIZ)) == NULL) {
handle_error ("malloc");
2016-10-27 16:27:04 +02:00
}
2016-12-20 23:36:00 +01:00
memset (buf, 0, BUFSIZ);
2016-10-27 16:27:04 +02:00
2016-12-20 23:36:00 +01:00
int i,ret, cpt = 0;
2016-10-27 16:27:04 +02:00
2016-12-20 23:36:00 +01:00
struct array_proc ap;
memset(&ap, 0, sizeof(struct array_proc));
2016-12-20 23:36:00 +01:00
struct process *p2 = malloc(sizeof(struct process));
2016-11-04 16:51:17 +01:00
2016-12-20 23:36:00 +01:00
while(cpt < 5) {
ret = srv_select(&ap, srv, &p2);
if (ret == CONNECTION) {
struct process *p = malloc(sizeof(struct process));
memset(p, 0, sizeof(struct process));
if (srv_accept (srv, p) < 0) {
handle_error("srv_accept < 0");
}else {
printf("new connection\n");
}
2016-10-27 16:27:04 +02:00
2016-12-20 23:36:00 +01:00
if (add_proc(&ap, p) < 0) {
handle_error("add_proc < 0");
}
cpt++;
} else {
if (srv_read(p2, &buf, &msize) < 0) {
handle_error("srv_read < 0");
}
if (srv_write (p2, buf, msize) < 0) {
handle_error("srv_write < 0");
2016-10-27 16:27:04 +02:00
}
}
}
2016-12-20 23:36:00 +01:00
for (i = 0; i < ap.size; i++) {
if (srv_close_proc (ap.tab_proc[i]) < 0) {
handle_error( "srv_close_proc < 0");
}
}
}
2016-11-04 16:51:17 +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>
*/
2016-06-12 14:41:25 +02:00
int main(int argc, char * argv[], char **env)
{
2016-06-05 20:48:13 +02:00
struct service srv;
memset (&srv, 0, sizeof (struct service));
2016-12-20 23:36:00 +01:00
srv.index = 0;
srv.version = 0;
unlink("/tmp/ipc/pongd-0-0");
if (srv_init (argc, argv, env, &srv, PONGD_SERVICE_NAME) < 0) {
handle_error("srv_init < 0");
return EXIT_FAILURE;
}
2016-06-05 20:48:13 +02:00
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
2016-06-05 20:48:13 +02:00
main_loop (&srv);
// the application will shut down, and remove the service named pipe
2016-12-20 23:36:00 +01:00
if (srv_close (&srv) < 0) {
handle_error("srv_close < 0");
}
return EXIT_SUCCESS;
}