Esempi di server con le message queue
[gapil.git] / sources / MQFortuneServer.c
1 /* MQFortuneServer.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program fortuned 
22  * Fortune server - Using Message Queues
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: fortuned -h give all info
28  *
29  * $Id: MQFortuneServer.c,v 1.2 2002/10/20 22:40:34 piccardi Exp $
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #include <sys/types.h>   /* predefined types */
36 #include <sys/stat.h>    /*  */
37 #include <unistd.h>      /* include unix standard library */
38 #include <stdio.h>       /* include standard I/O library */
39 #include <stdlib.h>      /* standard library */
40 #include <string.h>      /* ANSI C standard string */
41 #include <errno.h>       /* errorstring */
42 #include <signal.h>      /* signals */
43 #include <sys/ipc.h>
44 #include <sys/msg.h>
45
46 #include "macros.h"
47 #include "wrappers.h"
48
49 /* Maximum message size */
50 #define MSGMAX 8192
51
52 /* Subroutines declaration */
53 void usage(void);
54 void HandSIGTERM(int signo);
55 int FortuneParse(char *file, char **fortune, int n);
56
57 int msgid;                                       /* Message queue identifier */
58 int main(int argc, char *argv[])
59 {
60 /* Variables definition */
61     int i, n = 0;
62     char **fortune;                       /* array of fortune message string */
63     char *fortunefilename;                              /* fortune file name */
64     struct msgbuf_read {      /* message struct to read request from clients */
65         long mtype;                               /* message type, must be 1 */
66         long pid;             /* message data, must be the pid of the client */
67     } msg_read;
68     struct msgbuf_write {       /* message struct to write result to clients */
69         long mtype;            /* message type, will be the pid of the client*/
70         char mtext[MSGMAX];             /* message data, will be the fortune */
71     } msg_write;
72     key_t key;                                          /* Message queue key */
73     int size;                                                /* message size */
74     /*
75      * Input section: decode parameters passed in the calling 
76      * Use getopt function
77      */
78     opterr = 0;                              /* don't want writing to stderr */
79     while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
80         switch (i) {
81         /* 
82          * Handling options 
83          */ 
84         case 'h':                                       /* print usage infos */
85             usage();
86             return(0);
87             break;
88         case 'f':                                   /* set fortune file name */
89             fortunefilename = optarg;
90             break;
91         case 'n':                     /* set number of fortune string to use */
92             n = strtol(optarg, NULL, 10);
93             fortune = (char **) calloc(sizeof(*fortune), n);
94             break;
95         case '?':                                    /* unrecognized options */
96             printf("Unrecognized options -%c\n", optopt);
97             usage();
98         default:                                       /* should not reached */
99             usage();
100         }
101     }
102     /* ***********************************************************
103      * 
104      *           Options processing completed
105      *
106      *                Main code beginning
107      * 
108      * ***********************************************************/
109     if (n==0) usage();          /* if no pool depth exit printing usage info */
110     Signal(SIGTERM, HandSIGTERM);            /* set handlers for termination */
111     Signal(SIGINT, HandSIGTERM);
112     Signal(SIGQUIT, HandSIGTERM);
113     i = FortuneParse(fortunefilename, fortune, n);          /* parse phrases */
114     for (n=0; n<i; n++) debug("%s%%\n", fortune[n]);
115     /* 
116      * Comunication section 
117      */
118     key = ftok("./MQFortuneServer.c", 1); 
119     msgid = msgget(key, IPC_CREAT|0666);
120     if (msgid < 0) {
121         perror("Cannot create message queue");
122         exit(1);
123     }
124
125     /* Main body: loop over requests */
126     while (1) {
127         msgrcv(msgid, &msg_read, sizeof(int), 1, MSG_NOERROR);
128         debug("received request from %d\n", msg_read.pid);
129         n = random() % i;                             /* select random value */
130         strncpy(msg_write.mtext, fortune[n], MSGMAX);
131         size = min(strlen(fortune[n])+1, MSGMAX);  
132         msg_write.mtype=msg_read.pid;             /* use request pid as type */
133         msgsnd(msgid, &msg_write, size, 0);
134     }
135     debug("Exiting for unknown reasons\n");
136 }
137 /*
138  * routine to print usage info and exit
139  */
140 void usage(void) {
141     printf("Elementary fortune server\n");
142     printf("Usage:\n");
143     printf("  fortuned [-h] [-f] -n XXX \n");
144     printf("  -h   print this help\n");
145     printf("  -f filename   set file for fortunes\n");
146     printf("  -n NNN        set pool depth\n");
147     exit(1);
148 }
149 /*
150  * Signal Handler to manage termination
151  */
152 void HandSIGTERM(int signo) {
153     debug("Terminated by %s\n", strsignal(signo));
154     msgctl(msgid, IPC_RMID, NULL);                   /* remove message queue */
155     exit(0);
156 }