/[cl-ipc]/cl-ipc/sample-client.c
ViewVC logotype

Contents of /cl-ipc/sample-client.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Tue Sep 20 15:57:12 2005 UTC (8 years, 6 months ago) by skamphausen
Branch: MAIN
CVS Tags: release-1-0, HEAD
File MIME type: text/plain
added sample C client and ignore the binary
1 /* -*- C -*- */
2 /*
3 * $Id: sample-client.c,v 1.1 2005/09/20 15:57:12 skamphausen Exp $
4 *
5 * Copyright (C) 2005 by Stefan Kamphausen <http://www.skamphausen.de>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * Description:
33 * A sample client implementation for use with IPC calls.
34 */
35 # include <stdio.h>
36 # include <errno.h>
37 # include <string.h>
38 # include <unistd.h>
39 # include <stdlib.h>
40 # include <getopt.h>
41
42 # include <sys/types.h>
43 # include <sys/ipc.h>
44 # include <sys/msg.h>
45
46 #define STR_LEN 256
47 #define LINE_LEN 256
48
49 /* All messages get the same message type */
50 #define MSG_TYPE 1
51
52
53 /* This'll hold the data that's written into the queue */
54 struct msg_st {
55 long int type;
56 char line1[LINE_LEN];
57 };
58
59 /* What can happen after parsing the command lines */
60 enum operation_modes {
61 CLEAN,
62 WRITE
63 };
64
65 void usage(void) {
66 printf("sample-client -k KEY -c clean ipc queue with key KEY.\n");
67 printf("sample-client -k KEY -p STR Pass STR to the ipc line with key KEY.\n");
68 }
69
70 int get_message_queue(int key) {
71 int msgid;
72
73 if ((msgid = msgget((key_t) key, 0)) == -1) {
74 fprintf(stderr, "msgget failed for key %d: %s\n",key,strerror(errno));
75 exit(-5);
76 } else {
77 return msgid;
78 }
79 }
80
81 void cleanup_stale_queue(int key) {
82 int result;
83 int msgid;
84
85 printf("removing queue with key %d\n",key);
86 msgid = get_message_queue(key);
87
88 result = msgctl(msgid,IPC_RMID,NULL);
89
90 if (result == -1) {
91 fprintf(stderr,"Could not remove queue: %s\n",strerror(errno));
92 }
93
94 }
95
96
97 void send_message_to_queue(int key, char* msg) {
98 int msgid;
99 struct msg_st queue_msg;
100
101 printf("sending \"%s\" to queue with key %d\n",msg,key);
102 msgid = get_message_queue(key);
103
104 /* prepare data */
105 snprintf(queue_msg.line1,STR_LEN, "%s", msg);
106 queue_msg.type = MSG_TYPE;
107
108 /* pass the data to the queue. */
109 if (msgsnd(msgid, (void *) &queue_msg, sizeof(struct msg_st) - sizeof(long int), IPC_NOWAIT) == -1) {
110 fprintf(stderr, "could not send message: %s\n",strerror(errno));
111 exit(-6);
112 }
113
114 }
115
116
117 int main(int argc, char *argv[]) {
118 int mode = -1;
119 int c;
120 int this_option_optind;
121 int option_index = 0;
122 char opt_message[STR_LEN];
123 int opt_key = -1;
124
125 /* parse options */
126 while (1) {
127 this_option_optind = optind ? optind : 1;
128 option_index = 0;
129 /* no long options */
130 static struct option long_options[] = {
131 {0,0,0}
132 };
133 c = getopt_long(argc,argv,"ruwsp:ck:", long_options, &option_index);
134 if (c == -1) {
135 break;
136 }
137 switch(c) {
138 case 0:
139 /* printf("option %s\n",long_options[option_index].name); */
140 break;
141 case 'k':
142 opt_key = strtol(optarg,NULL,10);
143 break;
144 case 'c':
145 mode = CLEAN;
146 break;
147 case 'p':
148 mode = WRITE;
149 strncpy(opt_message,optarg,STR_LEN);
150 break;
151 default:
152 printf("Can't handle option '%c'\n",c);
153 usage();
154 exit(-1);
155 }
156 }
157
158 switch (mode) {
159 case CLEAN:
160 if (opt_key == -1) {
161 fprintf(stderr,"no key given\n");
162 usage();
163 exit(-4);
164 }
165 cleanup_stale_queue(opt_key);
166 break;
167 case WRITE:
168 if (opt_key == -1) {
169 fprintf(stderr,"no key given\n");
170 usage();
171 exit(-4);
172 }
173 send_message_to_queue(opt_key,opt_message);
174 break;
175 }
176 exit(0);
177 }

  ViewVC Help
Powered by ViewVC 1.1.5