int pn_post_frame(pn_dispatcher_t *disp, uint16_t ch, const char *fmt, ...) { va_list ap; va_start(ap, fmt); pn_data_clear(disp->output_args); int err = pn_data_vfill(disp->output_args, fmt, ap); va_end(ap); if (err) { pn_transport_logf(disp->transport, "error posting frame: %s, %s: %s", fmt, pn_code(err), pn_error_text(pn_data_error(disp->output_args))); return PN_ERR; } pn_do_trace(disp, ch, OUT, disp->output_args, disp->output_payload, disp->output_size); encode_performatives: pn_buffer_clear( disp->frame ); pn_bytes_t buf = pn_buffer_bytes( disp->frame ); buf.size = pn_buffer_available( disp->frame ); ssize_t wr = pn_data_encode( disp->output_args, buf.start, buf.size ); if (wr < 0) { if (wr == PN_OVERFLOW) { pn_buffer_ensure( disp->frame, pn_buffer_available( disp->frame ) * 2 ); goto encode_performatives; } pn_transport_logf(disp->transport, "error posting frame: %s", pn_code(wr)); return PN_ERR; } pn_frame_t frame = {disp->frame_type}; frame.channel = ch; frame.payload = buf.start; frame.size = wr; size_t n; while (!(n = pn_write_frame(disp->output + disp->available, disp->capacity - disp->available, frame))) { disp->capacity *= 2; disp->output = (char *) realloc(disp->output, disp->capacity); } disp->output_frames_ct += 1; if (disp->trace & PN_TRACE_RAW) { pn_string_set(disp->scratch, "RAW: \""); pn_quote(disp->scratch, disp->output + disp->available, n); pn_string_addf(disp->scratch, "\""); pn_transport_log(disp->transport, pn_string_get(disp->scratch)); } disp->available += n; return 0; }
int buffer(int argc, char **argv) { pn_buffer_t *buf = pn_buffer(16); pn_buffer_append(buf, "abcd", 4); pn_buffer_print(buf); printf("\n"); pn_buffer_prepend(buf, "012", 3); pn_buffer_print(buf); printf("\n"); pn_buffer_prepend(buf, "z", 1); pn_buffer_print(buf); printf("\n"); pn_buffer_append(buf, "efg", 3); pn_buffer_print(buf); printf("\n"); pn_buffer_append(buf, "hijklm", 6); pn_buffer_print(buf); printf("\n"); pn_buffer_defrag(buf); pn_buffer_print(buf); printf("\n"); pn_buffer_trim(buf, 1, 1); pn_buffer_print(buf); printf("\n"); pn_buffer_trim(buf, 4, 0); pn_buffer_print(buf); printf("\n"); pn_buffer_clear(buf); pn_buffer_print(buf); printf("\n"); pn_buffer_free(buf); pn_data_t *data = pn_data(16); int err = pn_data_fill(data, "Ds[iSi]", "desc", 1, "two", 3); if (err) { printf("%s\n", pn_code(err)); } pn_data_print(data); printf("\n"); pn_bytes_t str; err = pn_data_scan(data, "D.[.S.]", &str); if (err) { printf("%s\n", pn_code(err)); } else { printf("%.*s\n", (int) str.size, str.start); } pn_data_clear(data); pn_data_fill(data, "DL[SIonn?DL[S]?DL[S]nnI]", ATTACH, "asdf", 1, true, true, SOURCE, "queue", true, TARGET, "queue", 0); pn_data_print(data); printf("\n"); pn_data_free(data); return 0; }
void protonError(int err, char *step, pn_messenger_t *messenger) { const char *errMsg = NULL; if (0 == err) { return; } printf("ERROR: PROTON API: %s Errno: %d (%s)\n", step, err, pn_code(err)); #if (PN_VERSION_MINOR == 4) /* ** Proton-C 0.4 returns an error message directly */ errMsg = pn_messenger_error(messenger); if (NULL == errMsg) { errMsg = "pn_messenger_error() returned NULL"; } #else /* ** Proton-C 0.5 and later returns an error structure which ** you retrieve the error message from as a separate step. */ { pn_error_t *errInfo = pn_messenger_error(messenger); errMsg = pn_error_text(errInfo); } #endif printf("ERROR: PROTON Msg: %s\n", (NULL == errMsg) ? "NULL" : errMsg); }
connection_engine::connection_engine(class handler &h, const connection_options& opts) { connection_ = proton::connection(take_ownership(pn_connection()).get()); pn_ptr<pn_transport_t> transport = take_ownership(pn_transport()); pn_ptr<pn_collector_t> collector = take_ownership(pn_collector()); if (!connection_ || !transport || !collector) throw proton::error("engine create"); int err = pn_transport_bind(transport.get(), connection_.pn_object()); if (err) throw error(msg() << "transport bind:" << pn_code(err)); pn_connection_collect(connection_.pn_object(), collector.get()); ctx_ = &connection_engine_context::get(connection_); // Creates context ctx_->engine_handler = &h; ctx_->transport = transport.release(); ctx_->collector = collector.release(); opts.apply(connection_); // Provide defaults for connection_id and link_prefix if not set. std::string cid = connection_.container_id(); if (cid.empty()) { cid = make_id(); pn_connection_set_container(connection_.pn_object(), cid.c_str()); } id_generator &link_gen = connection_context::get(connection_).link_gen; if (link_gen.prefix().empty()) { link_gen.prefix(make_id()+"/"); } }
int pn_dispatch_frame(pn_dispatcher_t *disp, pn_frame_t frame) { if (frame.size == 0) { // ignore null frames if (disp->trace & PN_TRACE_FRM) pn_transport_logf(disp->transport, "%u <- (EMPTY FRAME)\n", frame.channel); return 0; } ssize_t dsize = pn_data_decode(disp->args, frame.payload, frame.size); if (dsize < 0) { pn_string_format(disp->scratch, "Error decoding frame: %s %s\n", pn_code(dsize), pn_error_text(pn_data_error(disp->args))); pn_quote(disp->scratch, frame.payload, frame.size); pn_transport_log(disp->transport, pn_string_get(disp->scratch)); return dsize; } disp->channel = frame.channel; // XXX: assuming numeric uint64_t lcode; bool scanned; int e = pn_data_scan(disp->args, "D?L.", &scanned, &lcode); if (e) { pn_transport_log(disp->transport, "Scan error"); return e; } if (!scanned) { pn_transport_log(disp->transport, "Error dispatching frame"); return PN_ERR; } uint8_t code = lcode; disp->code = code; disp->size = frame.size - dsize; if (disp->size) disp->payload = frame.payload + dsize; pn_do_trace(disp, disp->channel, IN, disp->args, disp->payload, disp->size); pn_action_t *action = disp->actions[code]; int err = action(disp); disp->channel = 0; disp->code = 0; pn_data_clear(disp->args); disp->size = 0; disp->payload = NULL; return err; }
void pn_fprint_data(FILE *stream, const char *bytes, size_t size) { char buf[256]; ssize_t n = pn_quote_data(buf, 256, bytes, size); if (n >= 0) { fputs(buf, stream); } else { if (n == PN_OVERFLOW) { fputs(buf, stream); fputs("... (truncated)", stream); } else fprintf(stderr, "pn_quote_data: %s\n", pn_code(n)); } }
int value(int argc, char **argv) { pn_atom_t data[1024]; pn_atoms_t atoms = {1024, data}; int err = pn_fill_atoms(&atoms, "DDsL[i[i]i]{sfsf}@DLT[sss]", "blam", 21, 1, 2, 3, "pi", 3.14159265359, "e", 2.7, (uint64_t) 42, PN_SYMBOL, "one", "two", "three"); if (err) { printf("err = %s\n", pn_code(err)); } else { pn_print_atoms(&atoms); printf("\n"); } pn_bytes_t blam; uint64_t n; int32_t one, two, three; pn_bytes_t key1; float val1; pn_bytes_t key2; float val2; uint64_t al; pn_type_t type; bool threeq; pn_bytes_t sym; err = pn_scan_atoms(&atoms, "DDsL[i[i]?i]{sfsf}@DLT[.s.]", &blam, &n, &one, &two, &threeq, &three, &key1, &val1, &key2, &val2, &al, &type, &sym); if (err) { printf("err = %s\n", pn_code(err)); } else { printf("scan=%.*s %" PRIu64 " %i %i %i %.*s %f %.*s %f %" PRIu64 " %i %.*s\n", (int) blam.size, blam.start, n, one, two, three, (int) key1.size, key1.start, val1, (int) key2.size, key2.start, val2, al, threeq, (int) sym.size, sym.start); } char str[1024*10]; int ch, idx = 0; while ((ch = getchar()) != EOF) { str[idx++] = ch; } str[idx] = '\0'; /* str[0] = '\0'; for (int i = 2; i < argc; i++) { strcat(str, argv[i]); if (i < argc - 1) strcat(str, " "); }*/ printf("JSON: %s\n", str); pn_atom_t parserdata[1024]; pn_atoms_t parserd = {1024, parserdata}; printf("\n"); pn_parser_t *parser = pn_parser(); err = pn_parser_parse(parser, str, &parserd); if (err) { printf("parse err=%s, %s\n", pn_code(err), pn_parser_error(parser)); } else { printf("--\n"); for (int i = 0; i < parserd.size; i++) { printf("%s: ", pn_type_str(parserd.start[i].type)); err = pn_print_atom(parserd.start[i]); if (err) printf("err=%s", pn_code(err)); printf("\n"); } printf("--\n"); err = pn_print_atoms(&parserd); if (err) printf("print err=%s", pn_code(err)); printf("\n"); } pn_parser_free(parser); return 0; }
int pn_post_transfer_frame(pn_dispatcher_t *disp, uint16_t ch, uint32_t handle, pn_sequence_t id, const pn_bytes_t *tag, uint32_t message_format, bool settled, bool more, pn_sequence_t frame_limit) { bool more_flag = more; int framecount = 0; // create preformatives, assuming 'more' flag need not change compute_performatives: pn_data_clear(disp->output_args); int err = pn_data_fill(disp->output_args, "DL[IIzIoo]", TRANSFER, handle, id, tag->size, tag->start, message_format, settled, more_flag); if (err) { pn_transport_logf(disp->transport, "error posting transfer frame: %s: %s", pn_code(err), pn_error_text(pn_data_error(disp->output_args))); return PN_ERR; } do { // send as many frames as possible without changing the 'more' flag... encode_performatives: pn_buffer_clear( disp->frame ); pn_bytes_t buf = pn_buffer_bytes( disp->frame ); buf.size = pn_buffer_available( disp->frame ); ssize_t wr = pn_data_encode(disp->output_args, buf.start, buf.size); if (wr < 0) { if (wr == PN_OVERFLOW) { pn_buffer_ensure( disp->frame, pn_buffer_available( disp->frame ) * 2 ); goto encode_performatives; } pn_transport_logf(disp->transport, "error posting frame: %s", pn_code(wr)); return PN_ERR; } buf.size = wr; // check if we need to break up the outbound frame size_t available = disp->output_size; if (disp->remote_max_frame) { if ((available + buf.size) > disp->remote_max_frame - 8) { available = disp->remote_max_frame - 8 - buf.size; if (more_flag == false) { more_flag = true; goto compute_performatives; // deal with flag change } } else if (more_flag == true && more == false) { // caller has no more, and this is the last frame more_flag = false; goto compute_performatives; } } if (pn_buffer_available( disp->frame ) < (available + buf.size)) { // not enough room for payload - try again... pn_buffer_ensure( disp->frame, available + buf.size ); goto encode_performatives; } pn_do_trace(disp, ch, OUT, disp->output_args, disp->output_payload, available); memmove( buf.start + buf.size, disp->output_payload, available); disp->output_payload += available; disp->output_size -= available; buf.size += available; pn_frame_t frame = {disp->frame_type}; frame.channel = ch; frame.payload = buf.start; frame.size = buf.size; size_t n; while (!(n = pn_write_frame(disp->output + disp->available, disp->capacity - disp->available, frame))) { disp->capacity *= 2; disp->output = (char *) realloc(disp->output, disp->capacity); } disp->output_frames_ct += 1; framecount++; if (disp->trace & PN_TRACE_RAW) { pn_string_set(disp->scratch, "RAW: \""); pn_quote(disp->scratch, disp->output + disp->available, n); pn_string_addf(disp->scratch, "\""); pn_transport_log(disp->transport, pn_string_get(disp->scratch)); } disp->available += n; } while (disp->output_size > 0 && framecount < frame_limit); disp->output_payload = NULL; return framecount; }