Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / MQFortuneClient.c
1 /* MQFortuneClient.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 Cleint - Using Message Queues
23  *
24  * Author: Simone Piccardi
25  * Oct. 2002
26  *
27  * Usage: fortune -h give all info
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #include <sys/types.h>   /* primitive system data types */
34 #include <sys/stat.h>    /* file characteristics constants and functions */
35 #include <unistd.h>      /* unix standard library */
36 #include <stdio.h>       /* standard I/O library */
37 #include <stdlib.h>      /* C standard library */
38 #include <string.h>      /* C strings library */
39 #include <errno.h>       /* error definitions and routines */
40 #include <signal.h>      /* signal constants, types and functions */
41 #include <sys/ipc.h>     /* SysV IPC functions */
42 #include <sys/msg.h>     /* SysV message queues */
43
44
45 #include "macros.h"
46
47 /* Maximum message size */
48 #define MSGMAX 8192
49
50 /* Subroutines declaration */
51 void usage(void);
52
53 int main(int argc, char *argv[])
54 {
55 /* Variables definition */
56     int i;
57     struct msgbuf_read {          /* message struct to ask fortune to server */
58         long mtype;                               /* message type, must be 1 */
59         long pid;             /* message data, must be the pid of the client */
60     } msg_read;
61     struct msgbuf_write {        /* message struct to get result from server */
62         long mtype;            /* message type, will be the pid of the client*/
63         char mtext[MSGMAX];             /* message data, will be the fortune */
64     } msg_write;
65     int msgid;                                   /* Message queue identifier */
66     key_t key;                                          /* Message queue key */
67     int size;                                                /* message size */
68     /*
69      * Input section: decode parameters passed in the calling 
70      * Use getopt function
71      */
72     opterr = 0;                              /* don't want writing to stderr */
73     while ( (i = getopt(argc, argv, "h")) != -1) {
74         switch (i) {
75         /* 
76          * Handling options 
77          */ 
78         case 'h':  
79             usage();
80             return(0);
81             break;
82         case '?':                                    /* unrecognized options */
83             printf("Unrecognized options -%c\n", optopt);
84             usage();
85         default:                                       /* should not reached */
86             usage();
87         }
88     }
89     /* ***********************************************************
90      * 
91      *           Options processing completed
92      *
93      *                Main code beginning
94      * 
95      * ***********************************************************/
96     /* 
97      * Comunication section 
98      */
99     key = ftok("./MQFortuneServer.c", 1); 
100     msgid = msgget(key, 0); 
101     if (msgid < 0) {
102         perror("Cannot find message queue");
103         exit(1);
104     }
105
106     /* Main body: do request and write result */
107     msg_read.mtype = 1;                      /* type for request is always 1 */
108     msg_read.pid = getpid();                   /* use pid for communications */
109     size = sizeof(msg_read.pid);  
110     msgsnd(msgid, &msg_read, size, 0);               /* send request message */
111     debug("sent request from %d\n", msg_read.pid);
112     msgrcv(msgid, &msg_write, MSGMAX, msg_read.pid, MSG_NOERROR);
113     printf("%s", msg_write.mtext);
114 }
115 /*
116  * routine to print usage info and exit
117  */
118 void usage(void) {
119     printf("Elementary fortune server\n");
120     printf("Usage:\n");
121     printf("  fortuned [-h] [-f] -n XXX \n");
122     printf("  -h   print this help\n");
123     printf("  -f filename   set file for fortunes\n");
124     printf("  -n XXX        set pool depth\n");
125     exit(1);
126 }