-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmq-send.c
More file actions
164 lines (151 loc) · 3.6 KB
/
Copy pathmq-send.c
File metadata and controls
164 lines (151 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* libmq: A library for interacting with message queues
*
* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright (c) 2014-2015 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "libmq.h"
static const char *progname = "mq-send";
static void usage(void);
int
main(int argc, char **argv)
{
MQ *connection;
MQMESSAGE *msg;
const char *type, *subj;
char *buffer, *p;
size_t bufsize, buflen;
ssize_t r;
int c;
progname = argv[0];
type = NULL;
subj = NULL;
while((c = getopt(argc, argv, "ht:s:")) != -1)
{
switch(c)
{
case 'h':
usage();
return 0;
case 't':
type = optarg;
break;
case 's':
subj = optarg;
break;
default:
usage();
return 1;
}
}
argc -= optind;
argv += optind;
if(!argc)
{
usage();
return 1;
}
connection = mq_connect_send(argv[0], NULL, NULL);
if(!connection)
{
fprintf(stderr, "%s: cannot connect to '%s'\n", progname, argv[0]);
return 1;
}
if(mq_error(connection))
{
fprintf(stderr, "%s: cannot connect to '%s': %s\n", progname, argv[0], mq_errmsg(connection));
mq_disconnect(connection);
return 1;
}
/* Read stdin into the buffer, extending as needed */
buffer = NULL;
bufsize = 0;
buflen = 0;
while(!feof(stdin))
{
if(bufsize - buflen < 1024)
{
p = (char *) realloc(buffer, bufsize + 1024);
if(!p)
{
fprintf(stderr, "%s: failed to reallocate buffer from %u bytes to %u bytes\n", progname, (unsigned) bufsize, (unsigned) bufsize + 1024);
return 1;
}
buffer = p;
bufsize += 1024;
}
r = fread(&(buffer[buflen]), 1, 1023, stdin);
if(r < 0)
{
fprintf(stderr, "%s: error reading from standard input: %s\n", progname, strerror(errno));
free(buffer);
return -1;
}
buflen += r;
buffer[buflen] = 0;
}
/* Wrap the buffer in a message and send it */
msg = mq_message_create(connection);
if(subj)
{
mq_message_set_subject(msg, subj);
}
if(type)
{
mq_message_set_type(msg, type);
}
fprintf(stderr, "%s: sending %s message '%s' to <%s>\n", progname, type, subj, argv[0]);
if(mq_message_add_bytes(msg, (unsigned char *) buffer, buflen))
{
fprintf(stderr, "%s: failed to add to outgoing message buffer: %s\n", progname, mq_errmsg(connection));
return 1;
}
if(mq_message_send(msg))
{
fprintf(stderr, "%s: failed to send outgoing message: %s\n", progname, mq_errmsg(connection));
return 1;
}
/* Deliver any messages in the local queue */
if(mq_deliver(connection))
{
fprintf(stderr, "%s: failed to deliver pending messages: %s\n", progname, mq_errmsg(connection));
return 1;
}
/* Clean up and exit */
mq_message_free(msg);
free(buffer);
mq_disconnect(connection);
return 0;
}
static void
usage(void)
{
fprintf(stderr, "Usage: %s [OPTIONS] DEST-URI < FILE\n"
"\n"
"OPTIONS is one or more of:\n\n"
" -h Print this notice and exit\n"
" -t TYPE Specify the message type\n"
" -s SUBJECT Specify a subject for the message\n"
"\n",
progname);
}