00001 00002 00003 /// This is an example that shows how to use SQS functions of AWS4C 00004 /// The example performs the following actions 00005 /// 00006 /// Initializes the library 00007 /// Creates the queue 00008 /// Obtains the full URL of the queue 00009 /// Gets get queue parameters 00010 /// Sets visibility timeout of the queue 00011 /// Puts a number of messages into the queue 00012 /// Reads a number of messages from the queue 00013 /// Delets the processed messages 00014 00015 #include <stdio.h> 00016 #include <getopt.h> 00017 #include <stdlib.h> 00018 #include <string.h> 00019 #include "aws4c.h" 00020 00021 00022 /// a helper function that prints out a result of the operation 00023 /// and data returned by the server 00024 void DumpResult ( int rv, IOBuf * bf ) 00025 { 00026 printf ( "RV %d\n", rv ); 00027 printf ( "CODE [%d] \n", bf->code ); 00028 printf ( "RESULT [%s] \n", bf->result ); 00029 printf ( "LEN [%d] \n", bf->len ); 00030 if ( bf->lastMod) printf ( "LASTMOD [%s] \n", bf->lastMod ); 00031 if ( bf->eTag) printf ( "ETAG [%s] \n", bf->eTag ); 00032 00033 while(-1) 00034 { 00035 char Ln[1024]; 00036 int sz = aws_iobuf_getline ( bf, Ln, sizeof(Ln)); 00037 if ( Ln[0] == 0 ) break; 00038 printf ( "S[%3d] %s", sz, Ln ); 00039 } 00040 00041 } 00042 00043 00044 int main ( int argc, char * argv[] ) 00045 { 00046 char * queueURL; 00047 00048 /// Initialized the library 00049 aws_init (); 00050 aws_set_debug ( 0 ); 00051 int rc = aws_read_config ( "sample" ); 00052 if ( rc ) 00053 { 00054 puts ( "Could not found a credential in the the config file" ); 00055 puts ( "Make your ~/.awsAuth file is correct" ); 00056 exit ( 1 ); 00057 } 00058 00059 00060 00061 00062 00063 /// Create the Queue 00064 IOBuf * bf = aws_iobuf_new (); 00065 int rv = sqs_create_queue ( bf, "AWS-SQS-Sample" ); 00066 if ( rv || bf->code != 200 ) 00067 { 00068 /// Note failure to create the queue means either genuine 00069 /// error or simply the fact that the queue already exist 00070 /// for most applications it is okay to continue even if this 00071 /// operation fails 00072 puts ( "Failed to create queue\n" ); 00073 DumpResult ( rv, bf ); 00074 } 00075 aws_iobuf_free(bf); 00076 00077 00078 00079 /// Get the URL of the queue 00080 /// Most applications require the full URL of the queue which might be 00081 /// different from the queue name passwd to the SQSCreate queue 00082 bf = aws_iobuf_new (); 00083 rv = sqs_list_queues ( bf, "AWS-SQS-Sample" ); 00084 if ( rv || bf->code != 200 ) 00085 { 00086 puts ( "Failed to retrive list of queue\n" ); 00087 DumpResult ( rv, bf ); 00088 exit(0); 00089 } 00090 00091 while(-1) 00092 { 00093 char Ln[1024]; 00094 int sz = aws_iobuf_getline ( bf, Ln, sizeof(Ln)); 00095 if ( Ln[0] == 0 ) break; 00096 Ln[strlen(Ln)-1] = 0; 00097 printf ( "Queue: [%s]\n", Ln ); 00098 queueURL = strdup(Ln); 00099 } 00100 00101 if ( queueURL == NULL ) 00102 { printf ( "Could not find our queue" ); exit(0); } 00103 00104 bf = aws_iobuf_new (); 00105 int timeOut, qLength; 00106 rv = sqs_get_queueattributes ( bf, queueURL , &timeOut, &qLength ); 00107 if ( !rv && bf->code == 200 ) 00108 { 00109 printf ( "Queue Timeout = %d Approximate Lenght = %d \n", 00110 timeOut, qLength ); 00111 } 00112 else 00113 { 00114 DumpResult(rv,bf); exit(0); 00115 } 00116 00117 bf = aws_iobuf_new (); 00118 rv = sqs_set_queuevisibilitytimeout ( bf, queueURL , 16 ); 00119 if ( rv || bf->code != 200 ) 00120 { 00121 DumpResult(rv,bf); exit(0); 00122 } 00123 00124 /// Send a few messages 00125 int i; 00126 for ( i = 0 ; i < 18 ; i ++ ) 00127 { 00128 puts ( "Send Message" ); 00129 bf = aws_iobuf_new (); 00130 char Message[256]; 00131 snprintf(Message,sizeof(Message),"Msg #%d \n\n L=%d\n <A><</ResponseMetaData>", i,i); 00132 rv = sqs_send_message ( bf, queueURL, Message ); 00133 if ( rv || bf->code != 200 ) { DumpResult(rv,bf); exit(0); } 00134 } 00135 /// Now receive them back 00136 for ( i = 0 ; i < 500 ; i ++ ) 00137 { 00138 puts ( "Get Message" ); 00139 bf = aws_iobuf_new (); 00140 char receipt[1024]; 00141 memset ( receipt, 0 , sizeof(receipt)); 00142 rv = sqs_get_message ( bf, queueURL, receipt ); 00143 DumpResult(rv,bf); 00144 puts ("\n----"); 00145 printf ( "ID: %s\n", receipt ); 00146 aws_iobuf_free(bf); 00147 00148 if ( receipt[0] != 0 ) 00149 { 00150 bf = aws_iobuf_new (); 00151 rv = sqs_delete_message ( bf, queueURL, receipt ); 00152 if ( rv || bf->code != 200 ) DumpResult(rv,bf); 00153 } 00154 else { puts ( "Empty Queue" ); break; } 00155 } 00156 puts ("\n---"); 00157 }
Generated by Doxygen