-
Notifications
You must be signed in to change notification settings - Fork 12
/
sqs_example.c
173 lines (149 loc) · 4.81 KB
/
sqs_example.c
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
163
164
165
166
167
168
169
170
171
172
173
/*
*
* Copyright(c) 2009, Vlad Korolev, <vlad[@]v-lad.org >
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://www.gnu.org/licenses/lgpl-3.0.txt
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*/
/// \file sqs_example.c
/// This is an example that shows how to use SQS functions of AWS4C.
/// The example performs the following actions
///
/// Initializes the library
/// Creates the queue
/// Obtains the full URL of the queue
/// Gets the queue parameters
/// Sets the visibility timeout of the queue
/// Puts a number of messages into the queue
/// Reads a number of messages from the queue
/// Deletes the processed messages
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include "aws4c.h"
/// This helper function that prints out a result of the operation
/// and data returned by the server
void DumpResult ( int rv, IOBuf * bf )
{
printf ( "RV %d\n", rv );
printf ( "CODE [%d] \n", bf->code );
printf ( "RESULT [%s] \n", bf->result );
printf ( "LEN [%d] \n", bf->len );
if ( bf->lastMod) printf ( "LASTMOD [%s] \n", bf->lastMod );
if ( bf->eTag) printf ( "ETAG [%s] \n", bf->eTag );
while(-1)
{
char Ln[1024];
int sz = aws_iobuf_getline ( bf, Ln, sizeof(Ln));
if ( Ln[0] == 0 ) break;
printf ( "S[%3d] %s", sz, Ln );
}
}
int main ( int argc, char * argv[] )
{
char * queueURL;
/// Initialize the library.
aws_init ();
aws_set_debug ( 0 );
int rc = aws_read_config ( "ncred" );
if ( rc )
{
puts ( "Could not find a credential in the config file" );
puts ( "Make sure your ~/.awsAuth file is correct" );
exit ( 1 );
}
/// Create the queue
IOBuf * bf = aws_iobuf_new ();
int rv = sqs_create_queue ( bf, "AWSCSQSSampleXX4" );
if ( rv || bf->code != 200 )
{
/// Note that failure to create the queue suggests either a genuine
/// error or simply that the queue already exists.
/// For most applications, it is okay to continue even if this
/// operation fails.
puts ( "Failed to create queue\n" );
DumpResult ( rv, bf );
}
aws_iobuf_free(bf);
/// Get the URL of the queue.
/// Most applications require the full URL of the queue thath might be
/// different from the queue name passed to the SQSCreate queue
bf = aws_iobuf_new ();
rv = sqs_list_queues ( bf, "AWSCSQSSampleXX4" );
if ( rv || bf->code != 200 )
{
puts ( "Failed to retrieve list of queues.\n" );
DumpResult ( rv, bf );
exit(0);
}
while(-1)
{
char Ln[1024];
aws_iobuf_getline ( bf, Ln, sizeof(Ln));
if ( Ln[0] == 0 ) break;
Ln[strlen(Ln)-1] = 0;
printf ( "Queue: [%s]\n", Ln );
queueURL = strdup(Ln);
}
if ( queueURL == NULL )
{ printf ( "Could not find our queue" ); exit(0); }
bf = aws_iobuf_new ();
int timeOut, qLength;
rv = sqs_get_queueattributes ( bf, queueURL , &timeOut, &qLength );
if ( !rv && bf->code == 200 )
{
printf ( "Queue Timeout = %d Approximate Length = %d \n",
timeOut, qLength );
}
else
{
DumpResult(rv,bf); exit(0);
}
bf = aws_iobuf_new ();
rv = sqs_set_queuevisibilitytimeout ( bf, queueURL , 16 );
if ( rv || bf->code != 200 )
{
DumpResult(rv,bf); exit(0);
}
/// Send a few messages
int i;
for ( i = 0 ; i < 18 ; i ++ )
{
puts ( "Send message" );
bf = aws_iobuf_new ();
char Message[256];
snprintf(Message,sizeof(Message),"Msg #%d \n\n L=%d\n <A><</ResponseMetaData>", i,i);
rv = sqs_send_message ( bf, queueURL, Message );
if ( rv || bf->code != 200 ) { DumpResult(rv,bf); exit(0); }
}
/// Retrieve messages.
for ( i = 0 ; i < 500 ; i ++ )
{
puts ( "Get Message" );
bf = aws_iobuf_new ();
char receipt[1024];
memset ( receipt, 0 , sizeof(receipt));
rv = sqs_get_message ( bf, queueURL, receipt );
DumpResult(rv,bf);
puts ("\n----");
printf ( "ID: %s\n", receipt );
aws_iobuf_free(bf);
if ( receipt[0] != 0 )
{
bf = aws_iobuf_new ();
rv = sqs_delete_message ( bf, queueURL, receipt );
if ( rv || bf->code != 200 ) DumpResult(rv,bf);
}
else { puts ( "Empty queue" ); break; }
}
puts ("\n---");
return 0;
}