Debian Codegraph @

c/examples/raw_connect.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/condition.h>
23 #include <proton/raw_connection.h>
24 #include <proton/listener.h>
25 #include <proton/netaddr.h>
26 #include <proton/proactor.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 typedef struct app_data_t {
33 const char *host, *port;
34 const char *amqp_address;
35
36 pn_proactor_t *proactor;
37 pn_listener_t *listener;
38 pn_raw_connection_t *towake;
39
40 int connects;
41 int disconnects;
42
43 /* Sender values */
44
45 /* Receiver values */
46 } app_data_t;
47
48 typedef struct connection_data_t {
49 bool sender;
50 } connection_data_t;
51
52 static int exit_code = 0;
53
54 /* Close the connection and the listener so so we will get a
55 * PN_PROACTOR_INACTIVE event and exit, once all outstanding events
56 * are processed.
57 */
58 static void close_all(pn_raw_connection_t *c, app_data_t *app) {
59 if (c) pn_raw_connection_close(c);
60 if (app->listener) pn_listener_close(app->listener);
61 }
62
63 static int check_condition(pn_event_t *e, pn_condition_t *cond, app_data_t *app) {
64 if (pn_condition_is_set(cond)) {
65 fprintf(stderr, "%s: %s: %s\n", pn_event_type_name(pn_event_type(e)),
66 pn_condition_get_name(cond), pn_condition_get_description(cond));
67 close_all(pn_event_raw_connection(e), app);
68 return 1;
69 }
70 return 0;
71 }
72
73 static void send_message(pn_raw_connection_t *c, const char* msg) {
74 pn_raw_buffer_t buffer;
75 uint32_t len = strlen(msg);
76 char *buf = (char*) malloc(1024);
77 memcpy(buf, msg, len);
78 buffer.bytes = buf;
79 buffer.capacity = 1024;
80 buffer.offset = 0;
81 buffer.size = len;
82 pn_raw_connection_write_buffers(c, &buffer, 1);
83 }
84
85 static void recv_message(pn_raw_buffer_t buf) {
86 fwrite(buf.bytes, buf.size, 1, stdout);
87 }
88
89 connection_data_t *make_receiver_data(void) {
90 connection_data_t *cd = (connection_data_t*) malloc(sizeof(connection_data_t));
91 cd->sender = false;
92 return cd;
93 }
94
95 connection_data_t *make_sender_data(void) {
96 connection_data_t *cd = (connection_data_t*) malloc(sizeof(connection_data_t));
97 cd->sender = true;
98 return cd;
99 }
100
101 #define READ_BUFFERS 4
102
103 /* This function handles events when we are acting as the receiver */
104 static void handle_receive(app_data_t *app, pn_event_t* event) {
105 switch (pn_event_type(event)) {
106
107 case PN_RAW_CONNECTION_CONNECTED: {
108 pn_raw_connection_t *c = pn_event_raw_connection(event);
109 pn_raw_buffer_t buffers[READ_BUFFERS] = {{0}};
110 int i = READ_BUFFERS;
111 for (; i; --i) {
112 pn_raw_buffer_t *buff = &buffers[READ_BUFFERS-i];
113 buff->bytes = (char*) malloc(1024);
114 buff->capacity = 1024;
115 buff->size = 0;
116 buff->offset = 0;
117 }
118 pn_raw_connection_give_read_buffers(c, buffers, READ_BUFFERS);
119 } break;
120
121 case PN_RAW_CONNECTION_NEED_READ_BUFFERS: {
122 } break;
123
124 default:
125 break;
126 }
127 }
128
129 #define WRITE_BUFFERS 4
130
131 static void free_buffers(pn_raw_buffer_t buffs[], size_t n) {
132 unsigned i;
133 for (i=0; i<n; ++i) {
134 free(buffs[i].bytes);
135 }
136 }
137
138 /* This function handles events when we are acting as the sender */
139 static void handle_send(app_data_t* app, pn_event_t* event) {
140 switch (pn_event_type(event)) {
141
142 case PN_RAW_CONNECTION_CONNECTED: {
143 printf("**raw connection connected\n");
144 app->connects++;
145 } break;
146
147 case PN_RAW_CONNECTION_DISCONNECTED: {
148 pn_raw_connection_t *c = pn_event_raw_connection(event);
149 connection_data_t *cd = (connection_data_t*) pn_raw_connection_get_context(c);
150 pn_raw_connection_set_context(c, NULL);
151 free(cd);
152 printf("**raw connection disconnected\n");
153 pn_proactor_cancel_timeout(app->proactor);
154 app->disconnects++;
155 check_condition(event, pn_raw_connection_condition(c), app);
156 } break;
157
158 case PN_RAW_CONNECTION_DRAIN_BUFFERS: {
159 pn_raw_connection_t *c = pn_event_raw_connection(event);
160 pn_raw_buffer_t buffs[READ_BUFFERS];
161 size_t n;
162 while ( (n = pn_raw_connection_take_read_buffers(c, buffs, READ_BUFFERS)) ) {
163 free_buffers(buffs, n);
164 }
165 while ( (n = pn_raw_connection_take_written_buffers(c, buffs, READ_BUFFERS)) ) {
166 free_buffers(buffs, n);
167 }
168 }
169
170 case PN_RAW_CONNECTION_NEED_WRITE_BUFFERS: {
171 pn_raw_connection_t *c = pn_event_raw_connection(event);
172 char line[120];
173 if (fgets(line, sizeof(line), stdin)) {
174 send_message(c, line);
175 } else {
176 /* On end of file, close for write then wait 2 sec for response */
177 pn_raw_connection_write_close(c);
178 app->towake = c;
179 pn_proactor_set_timeout(app->proactor, 2000);
180 }
181 } break;
182
183 /* This path handles received bytes */
184 case PN_RAW_CONNECTION_READ: {
185 pn_raw_connection_t *c = pn_event_raw_connection(event);
186 pn_raw_buffer_t buffs[READ_BUFFERS];
187 size_t n;
188 while ( (n = pn_raw_connection_take_read_buffers(c, buffs, READ_BUFFERS)) ) {
189 unsigned i;
190 for (i=0; i<n && buffs[i].bytes; ++i) {
191 recv_message(buffs[i]);
192 free(buffs[i].bytes);
193 }
194 }
195 } break;
196
197 /* This is signal to close 2 secs after input closes */
198 case PN_RAW_CONNECTION_WAKE:
199
200 /* This is signal the other end of the network closed */
201 case PN_RAW_CONNECTION_CLOSED_READ: {
202 pn_raw_connection_t *c = pn_event_raw_connection(event);
203 pn_raw_connection_close(c);
204 } break;
205
206 case PN_RAW_CONNECTION_WRITTEN: {
207 pn_raw_connection_t *c = pn_event_raw_connection(event);
208 pn_raw_buffer_t buffs[READ_BUFFERS];
209 size_t n;
210 while ( (n = pn_raw_connection_take_written_buffers(c, buffs, READ_BUFFERS)) ) {
211 if (!pn_raw_connection_is_read_closed(c)) {
212 pn_raw_connection_give_read_buffers(c, buffs, n);
213 } else {
214 unsigned i;
215 for (i=0; i<n && buffs[i].bytes; ++i) {
216 free(buffs[i].bytes);
217 }
218 }
219 };
220 } break;
221
222 default:
223 break;
224 }
225 }
226
227 /* Handle all events, delegate to handle_send or handle_receive
228 Return true to continue, false to exit
229 */
230 static bool handle(app_data_t* app, pn_event_t* event) {
231 switch (pn_event_type(event)) {
232
233 case PN_PROACTOR_TIMEOUT: {
234 if (app->towake) {
235 pn_raw_connection_wake(app->towake);
236 app->towake = NULL;
237 }
238 } break;
239
240 case PN_PROACTOR_INACTIVE: {
241 return false;
242 } break;
243
244 default: {
245 pn_raw_connection_t *c = pn_event_raw_connection(event);
246 if (c) {
247 connection_data_t *cd = (connection_data_t*) pn_raw_connection_get_context(c);
248 if (cd && cd->sender) {
249 handle_send(app, event);
250 } else {
251 handle_receive(app, event);
252 }
253 }
254 }
255 }
256 return exit_code == 0;
257 }
258
259 void run(app_data_t *app) {
260 /* Loop and handle events */
261 do {
262 pn_event_batch_t *events = pn_proactor_wait(app->proactor);
263 pn_event_t *e;
264 for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) {
265 if (!handle(app, e)) {
266 return;
267 }
268 }
269 pn_proactor_done(app->proactor, events);
270 } while(true);
271 }
272
273 int main(int argc, char **argv) {
274 struct app_data_t app = {0};
275 char addr[PN_MAX_ADDR];
276 pn_raw_connection_t *c = pn_raw_connection();
277 connection_data_t *cd = make_sender_data();
278
279 app.host = (argc > 1) ? argv[1] : "";
280 app.port = (argc > 2) ? argv[2] : "amqp";
281
282 /* Create the proactor and connect */
283 app.proactor = pn_proactor();
284 pn_raw_connection_set_context(c, cd);
285 pn_proactor_addr(addr, sizeof(addr), app.host, app.port);
286 pn_proactor_raw_connect(app.proactor, c, addr);
287 run(&app);
288 pn_proactor_free(app.proactor);
289 return exit_code;
290 }