Add Initial Code Import
[dmaap/messagerouter/dmaapclient.git] / src / main / cpp / samplePostClient.cpp
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22
23 #include <stdio.h>
24 #include "cambria.h"
25
26 void handleResponse ( const CAMBRIA_CLIENT cc, const cambriaSendResponse* response )
27 {
28         if ( response )
29         {
30                 ::printf ( "\t%d %s\n", response->statusCode, ( response->statusMessage ? response->statusMessage : "" ) );
31                 ::printf ( "\t%s\n", response->responseBody ? response->responseBody : "" );
32
33                 // destroy the response (or it'll leak)
34                 ::cambriaDestroySendResponse ( cc, response );
35         }
36         else
37         {
38                 ::fprintf ( stderr, "No response object.\n" );
39         }
40 }
41
42 int main ( int argc, const char* argv[] )
43 {
44         ////////////////////////////////////////////////////////////////////////////
45         ////////////////////////////////////////////////////////////////////////////
46         ////////////////////////////////////////////////////////////////////////////
47         
48         // you can send single message in one call...
49         ::printf ( "Sending single message...\n" );
50         int sent = ::cambriaSimpleSend ( "localhost", 8080, "topic", "streamName",
51                 "{ \"field\":\"this is a JSON formatted alarm\" }" );
52         ::printf ( "\t%d sent\n\n", sent );
53
54         // you can also send multiple messages in one call with cambriaSimpleSendMultiple.
55         // the message argument becomes an array of strings, and you pass an array
56         // count too.
57         const char* msgs[] =
58         {
59                 "{\"format\":\"json\"}",
60                 "<format>xml</format>",
61                 "or whatever. they're just strings."
62         };
63         sent = ::cambriaSimpleSendMultiple ( "localhost", 8080, "topic", "streamName", msgs, 3 );
64         ::printf ( "\t%d sent\n\n", sent );
65
66         ////////////////////////////////////////////////////////////////////////////
67         ////////////////////////////////////////////////////////////////////////////
68         ////////////////////////////////////////////////////////////////////////////
69
70         // you can also create a client instance to keep around and make multiple
71         // send requests to. Chunked sending isn't supported right now, so each
72         // call to cambriaSendMessage results in a full socket open / post / close
73         // cycle, but hopefully we can improve this with chunking so that subsequent
74         // sends just push the message into the socket.
75
76         // create a client
77         const CAMBRIA_CLIENT cc = ::cambriaCreateClient ( "localhost", 8080, "topic", CAMBRIA_NATIVE_FORMAT );
78         if ( !cc )
79         {
80                 ::printf ( "Couldn't create client.\n" );
81                 return 1;
82         }
83
84         ////////////////////////////////////////////////////////////////////////////
85         // send a single message
86         ::printf ( "Sending single message...\n" );
87         const cambriaSendResponse* response = ::cambriaSendMessage ( cc, "streamName", "{\"foo\":\"bar\"}" );
88         handleResponse ( cc, response );
89
90         ////////////////////////////////////////////////////////////////////////////
91         // send a few messages at once
92         const char* msgs2[] =
93         {
94                 "{\"foo\":\"bar\"}",
95                 "{\"bar\":\"baz\"}",
96                 "{\"zoo\":\"zee\"}",
97                 "{\"foo\":\"bar\"}",
98                 "{\"foo\":\"bar\"}",
99                 "{\"foo\":\"bar\"}",
100         };
101         unsigned int count = sizeof(msgs2)/sizeof(const char*);
102
103         ::printf ( "Sending %d messages...\n", count );
104         response = ::cambriaSendMessages ( cc, "streamName", msgs2, count );
105         handleResponse ( cc, response );
106
107         ////////////////////////////////////////////////////////////////////////////
108         // destroy the client (or it'll leak)
109         ::cambriaDestroyClient ( cc );
110
111         return 0;
112 }