Debian Codegraph @

c/examples/send.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include <proton/connection.h>
23 #include <proton/condition.h>
24 #include <proton/delivery.h>
25 #include <proton/link.h>
26 #include <proton/message.h>
27 #include <proton/proactor.h>
28 #include <proton/session.h>
29 #include <proton/transport.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 typedef struct app_data_t {
35 const char *host, *port;
36 const char *amqp_address;
37 const char *container_id;
38 int message_count;
39
40 pn_proactor_t *proactor;
41 pn_message_t *message;
42 pn_rwbytes_t message_buffer;
43 int sent;
44 int acknowledged;
45 } app_data_t;
46
47 static int exit_code = 0;
48
49 static void check_condition(pn_event_t *e, pn_condition_t *cond) {
50 if (pn_condition_is_set(cond)) {
51 fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)),
52 pn_condition_get_name(cond), pn_condition_get_description(cond));
53 pn_connection_close(pn_event_connection(e));
54 exit_code = 1;
55 }
56 }
57
58 /* Create a message with a map { "sequence" : number } encode it and return the encoded buffer. */
59 static void send_message(app_data_t* app, pn_link_t *sender) {
60 /* Construct a message with the map { "sequence": app.sent } */
61 pn_data_t* body;
62 pn_message_clear(app->message);
63 body = pn_message_body(app->message);
64 pn_message_set_id(app->message, (pn_atom_t){.type=PN_ULONG, .u.as_ulong=app->sent});
65 pn_data_put_map(body);
66 pn_data_enter(body);
67 pn_data_put_string(body, pn_bytes(sizeof("sequence")-1, "sequence"));
68 pn_data_put_int(body, app->sent); /* The sequence number */
69 pn_data_exit(body);
70 if (pn_message_send(app->message, sender, &app->message_buffer) < 0) {
71 fprintf(stderr, "error sending message: %s\n", pn_error_text(pn_message_error(app->message)));
72 exit(1);
73 }
74 }
75
76 /* Returns true to continue, false if finished */
77 static bool handle(app_data_t* app, pn_event_t* event) {
78 switch (pn_event_type(event)) {
79
80 case PN_CONNECTION_INIT: {
81 pn_connection_t* c = pn_event_connection(event);
82 pn_session_t* s = pn_session(pn_event_connection(event));
83 pn_connection_set_container(c, app->container_id);
84 pn_connection_open(c);
85 pn_session_open(s);
86 {
87 pn_link_t* l = pn_sender(s, "my_sender");
88 pn_terminus_set_address(pn_link_target(l), app->amqp_address);
89 pn_link_open(l);
90 break;
91 }
92 }
93
94 case PN_LINK_FLOW: {
95 /* The peer has given us some credit, now we can send messages */
96 pn_link_t *sender = pn_event_link(event);
97 while (pn_link_credit(sender) > 0 && app->sent < app->message_count) {
98 ++app->sent;
99 /* Use sent counter as unique delivery tag. */
100 pn_delivery(sender, pn_dtag((const char *)&app->sent, sizeof(app->sent)));
101 send_message(app, sender);
102 }
103 break;
104 }
105
106 case PN_DELIVERY: {
107 /* We received acknowledgement from the peer that a message was delivered. */
108 pn_delivery_t* d = pn_event_delivery(event);
109 if (pn_delivery_remote_state(d) == PN_ACCEPTED) {
110 if (++app->acknowledged == app->message_count) {
111 printf("%d messages sent and acknowledged\n", app->acknowledged);
112 pn_connection_close(pn_event_connection(event));
113 /* Continue handling events till we receive TRANSPORT_CLOSED */
114 }
115 } else {
116 fprintf(stderr, "unexpected delivery state %d\n", (int)pn_delivery_remote_state(d));
117 pn_connection_close(pn_event_connection(event));
118 exit_code=1;
119 }
120 break;
121 }
122
123 case PN_TRANSPORT_CLOSED:
124 check_condition(event, pn_transport_condition(pn_event_transport(event)));
125 break;
126
127 case PN_CONNECTION_REMOTE_CLOSE:
128 check_condition(event, pn_connection_remote_condition(pn_event_connection(event)));
129 pn_connection_close(pn_event_connection(event));
130 break;
131
132 case PN_SESSION_REMOTE_CLOSE:
133 check_condition(event, pn_session_remote_condition(pn_event_session(event)));
134 pn_connection_close(pn_event_connection(event));
135 break;
136
137 case PN_LINK_REMOTE_CLOSE:
138 case PN_LINK_REMOTE_DETACH:
139 check_condition(event, pn_link_remote_condition(pn_event_link(event)));
140 pn_connection_close(pn_event_connection(event));
141 break;
142
143 case PN_PROACTOR_INACTIVE:
144 return false;
145
146 default: break;
147 }
148 return true;
149 }
150
151 void run(app_data_t *app) {
152 /* Loop and handle events */
153 do {
154 pn_event_batch_t *events = pn_proactor_wait(app->proactor);
155 pn_event_t *e;
156 for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
157 if (!handle(app, e)) {
158 return;
159 }
160 }
161 pn_proactor_done(app->proactor, events);
162 } while(true);
163 }
164
165 int main(int argc, char **argv) {
166 struct app_data_t app = {0};
167 char addr[PN_MAX_ADDR];
168
169 app.container_id = argv[0]; /* Should be unique */
170 app.host = (argc > 1) ? argv[1] : "";
171 app.port = (argc > 2) ? argv[2] : "amqp";
172 app.amqp_address = (argc > 3) ? argv[3] : "examples";
173 app.message_count = (argc > 4) ? atoi(argv[4]) : 10;
174 app.message = pn_message();
175
176 app.proactor = pn_proactor();
177 pn_proactor_addr(addr, sizeof(addr), app.host, app.port);
178 pn_proactor_connect2(app.proactor, NULL, NULL, addr);
179 run(&app);
180 pn_proactor_free(app.proactor);
181 free(app.message_buffer.start);
182 pn_message_free(app.message);
183 return exit_code;
184 }