Altre correzioni su memory mapping ed esempi
[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  * $Id: MQFortuneClient.c,v 1.2 2002/12/03 11:06:05 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
48 /* Maximum message size */
49 #define MSGMAX 8192
50
51 /* Subroutines declaration */
52 void usage(void);
53
54 int main(int argc, char *argv[])
55 {
56 /* Variables definition */
57     int i;
58     struct msgbuf_read {          /* message struct to ask fortune to server */
59         long mtype;                               /* message type, must be 1 */
60         long pid;             /* message data, must be the pid of the client */
61     } msg_read;
62     struct msgbuf_write {        /* message struct to get result from server */
63         long mtype;            /* message type, will be the pid of the client*/
64         char mtext[MSGMAX];             /* message data, will be the fortune */
65     } msg_write;
66     int msgid;                                   /* Message queue identifier */
67     key_t key;                                          /* Message queue key */
68     int size;                                                /* message size */
69     /*
70      * Input section: decode parameters passed in the calling 
71      * Use getopt function
72      */
73     opterr = 0;                              /* don't want writing to stderr */
74     while ( (i = getopt(argc, argv, "h")) != -1) {
75         switch (i) {
76         /* 
77          * Handling options 
78          */ 
79         case 'h':  
80             usage();
81             return(0);
82             break;
83         case '?':                                    /* unrecognized options */
84             printf("Unrecognized options -%c\n", optopt);
85             usage();
86         default:                                       /* should not reached */
87             usage();
88         }
89     }
90     /* ***********************************************************
91      * 
92      *           Options processing completed
93      *
94      *                Main code beginning
95      * 
96      * ***********************************************************/
97     /* 
98      * Comunication section 
99      */
100     key = ftok("./MQFortuneServer.c", 1); 
101     msgid = msgget(key, 0); 
102     if (msgid < 0) {
103         perror("Cannot find message queue");
104         exit(1);
105     }
106
107     /* Main body: do request and write result */
108     msg_read.mtype = 1;                      /* type for request is always 1 */
109     msg_read.pid = getpid();                   /* use pid for communications */
110     size = sizeof(msg_read.pid);  
111     msgsnd(msgid, &msg_read, size, 0);               /* send request message */
112     debug("sent request from %d\n", msg_read.pid);
113     msgrcv(msgid, &msg_write, MSGMAX, msg_read.pid, MSG_NOERROR);
114     printf("%s", msg_write.mtext);
115 }
116 /*
117  * routine to print usage info and exit
118  */
119 void usage(void) {
120     printf("Elementary fortune server\n");
121     printf("Usage:\n");
122     printf("  fortuned [-h] [-f] -n XXX \n");
123     printf("  -h   print this help\n");
124     printf("  -f filename   set file for fortunes\n");
125     printf("  -n XXX        set pool depth\n");
126     exit(1);
127 }