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

264 lines
7.3 KiB
C
Raw Normal View History

#include "../../lib/communication.h"
#include <pthread.h>
2016-10-27 16:27:04 +02:00
#include <sys/socket.h>
#include <sys/un.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)
/* control the file descriptor*/
void * pongd_thread(void * pdata) {
2016-11-03 22:44:35 +01:00
//struct process *proc = (struct process*) pdata;
int *sockclient = (int*) pdata;
// about the message
2016-10-27 16:27:04 +02:00
char *buf = malloc(BUFSIZ);
if (buf == NULL)
{
handle_error("malloc");
}
memset(buf, 0, BUFSIZ);
int nbytes;
// init unix socket
while (1) {
2016-11-03 22:44:35 +01:00
if ((nbytes = file_read (*sockclient, &buf)) == -1) {
2016-10-27 16:27:04 +02:00
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", nbytes);
}
2016-10-28 13:58:04 +02:00
if (nbytes == 0 || strncmp ("exit", buf, 4) == 0){
2016-10-27 16:27:04 +02:00
printf("------thread shutdown------------\n");
2016-11-03 22:44:35 +01:00
//close(cfd);
2016-11-04 16:51:17 +01:00
close(*sockclient);
2016-10-27 16:27:04 +02:00
free(buf);
break;
2016-10-28 13:58:04 +02:00
}else {
printf ("read, size %d : %s\n", nbytes, buf);
2016-11-03 22:44:35 +01:00
if ((nbytes = file_write (*sockclient, buf, nbytes)) == -1) {
2016-10-27 16:27:04 +02:00
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", nbytes);
}
}
}
return NULL;
}
2016-10-27 16:27:04 +02:00
/*
* 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)
{
int ret;
struct process tab_proc[10];
//thread
pthread_t tab_thread[10];
2016-10-27 16:27:04 +02:00
int cnt = 0;
//init socket unix for server
int sfd;
struct sockaddr_un peer_addr;
socklen_t peer_addr_size;
sfd = set_listen_socket(srv->spath);
if (sfd == -1){
handle_error("set_listen_socket");
}
/* master file descriptor list */
fd_set master;
/* temp file descriptor list for select() */
fd_set read_fds;
/* maximum file descriptor number */
int fdmax;
/* listening socket descriptor */
int listener = sfd;
/* newly accept()ed socket descriptor */
int newfd;
/* buffer for client data */
char *buf = malloc(BUFSIZ);
if (buf == NULL)
{
handle_error("malloc");
}
memset(buf, 0, BUFSIZ);
int nbytes;
int i;
2016-10-27 16:27:04 +02:00
/* clear the master and temp sets */
FD_ZERO(&master);
FD_ZERO(&read_fds);
/* add the listener to the master set */
FD_SET(listener, &master);
//FD_SET(sfd, &master);
2016-10-27 16:27:04 +02:00
/* keep track of the biggest file descriptor */
fdmax = sfd; /* so far, it's this one*/
for(;;) {
/* copy it */
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
{
perror("Server-select() error lol!");
exit(1);
}
2016-10-28 13:58:04 +02:00
//printf("Server-select...OK\n");
2016-10-27 16:27:04 +02:00
/*run through the existing connections looking for data to be read*/
for(i = 0; i <= fdmax; i++) {
if(FD_ISSET(i, &read_fds)) {
/* we got one... */
if(i == listener) {
/* handle new connections */
peer_addr_size = sizeof(struct sockaddr_un);
newfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
if (newfd == -1) {
handle_error("accept");
}
else
{
printf("Server-accept() is OK...\n");
2016-11-04 16:51:17 +01:00
//FD_SET(newfd, &master); /* add to master set */
//if(newfd > fdmax)
//{ /* keep track of the maximum */
//fdmax = newfd;
//}
nbytes = file_read (newfd, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (buf, &tab_proc[cnt]);
if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
}
srv_process_print (&tab_proc[cnt]);
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &newfd);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf ("\n-------New thread created---------\n");
}
printf ("%d applications to serve\n",cnt);
cnt++;
}
}
/*else {
2016-10-27 16:27:04 +02:00
nbytes = file_read (i, &buf);
if ( nbytes == -1) {
handle_error("file_read");
2016-11-04 16:51:17 +01:00
} else if( nbytes == 0) {*/
2016-10-27 16:27:04 +02:00
/* close it... */
2016-11-04 16:51:17 +01:00
//close(i);
2016-10-27 16:27:04 +02:00
/* remove from master set */
2016-11-04 16:51:17 +01:00
/*FD_CLR(i, &master);
2016-10-27 16:27:04 +02:00
}else {
2016-10-28 13:58:04 +02:00
buf[BUFSIZ - 1] = '\0';
2016-10-27 16:27:04 +02:00
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
2016-10-27 16:27:04 +02:00
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (buf, &tab_proc[cnt]);
2016-10-27 16:27:04 +02:00
if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
}
srv_process_print (&tab_proc[cnt]);
2016-11-03 22:44:35 +01:00
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &i);
2016-10-27 16:27:04 +02:00
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf ("\n-------New thread created---------\n");
}
printf ("%d applications to serve\n",cnt);
cnt++;
2016-11-04 16:51:17 +01:00
}*/
2016-10-27 16:27:04 +02:00
}
}
}
if (strncmp ("exit", buf, 4) == 0) {
break;
}
}
for (i = 0; i < cnt; i++) {
2016-10-14 17:47:08 +02:00
pthread_join(tab_thread[i], NULL);
}
2016-10-27 16:27:04 +02:00
free(buf);
close(sfd);
}
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));
srv->index = 0;
srv->version = 0;
2016-06-13 09:47:19 +02:00
srv_init (argc, argv, env, &srv, PONGD_SERVICE_NAME, NULL);
2016-06-05 20:48:13 +02:00
printf ("Listening on %s.\n", srv.spath);
// creates the service named pipe, that listens to client applications
int ret;
2016-06-05 20:48:13 +02:00
if ((ret = srv_create (&srv))) {
fprintf(stdout, "error service_create %d\n", ret);
exit (1);
}
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-06-05 20:48:13 +02:00
if ((ret = srv_close (&srv))) {
fprintf(stdout, "error service_close %d\n", ret);
exit (1);
}
return EXIT_SUCCESS;
}