void sender_context_init(sender_context_t *sc, Options_t *opts, Statistics_t *stats) { sc->opts = opts; sc->stats = stats; sc->sent = 0; sc->received = 0; sc->id.type = PN_ULONG; sc->reply_message = 0; // 4096 extra bytes should easily cover the message metadata sc->encoded_data_size = sc->opts->msg_size + 4096; sc->encoded_data = (char *)calloc(1, sc->encoded_data_size); check(sc->encoded_data, "failed to allocate encoding buffer"); sc->container_id = pn_string("reactor-send"); // prefer uuid-like name sc->reply_message = (sc->opts->get_replies) ? pn_message() : 0; sc->message = pn_message(); check(sc->message, "failed to allocate a message"); pn_string_t *rpto = pn_string("amqp://"); pn_string_addf(rpto, "%s", pn_string_get(sc->container_id)); pn_message_set_reply_to(sc->message, pn_string_get(rpto)); pn_free(rpto); pn_data_t *body = pn_message_body(sc->message); // borrow the encoding buffer this one time char *data = sc->encoded_data; pn_data_put_binary(body, pn_bytes(sc->opts->msg_size, data)); check(sc->opts->targets.count > 0, "no specified address"); sc->send_url = pn_url_parse(sc->opts->targets.addresses[0]); const char *host = pn_url_get_host(sc->send_url); const char *port = pn_url_get_port(sc->send_url); sc->hostname = pn_string(host); if (port && strlen(port)) pn_string_addf(sc->hostname, ":%s", port); }
pn_rule_t *pn_rule(const char *pattern, const char *substitution) { static pn_class_t clazz = PN_CLASS(pn_rule); pn_rule_t *rule = (pn_rule_t *) pn_new(sizeof(pn_rule_t), &clazz); rule->pattern = pn_string(pattern); rule->substitution = pn_string(substitution); return rule; }
void pn_subscription_initialize(void *obj) { pn_subscription_t *sub = (pn_subscription_t *) obj; sub->messenger = NULL; sub->scheme = pn_string(NULL); sub->host = pn_string(NULL); sub->port = pn_string(NULL); sub->address = pn_string(NULL); sub->context = NULL; }
void test_inspect(void *o, const char *expected) { pn_string_t *dst = pn_string(NULL); pn_inspect(o, dst); assert(pn_strequals(pn_string_get(dst), expected)); pn_free(dst); }
pn_dispatcher_t *pn_dispatcher(uint8_t frame_type, pn_transport_t *transport) { pn_dispatcher_t *disp = (pn_dispatcher_t *) calloc(sizeof(pn_dispatcher_t), 1); disp->frame_type = frame_type; disp->transport = transport; disp->trace = (pn_env_bool("PN_TRACE_RAW") ? PN_TRACE_RAW : PN_TRACE_OFF) | (pn_env_bool("PN_TRACE_FRM") ? PN_TRACE_FRM : PN_TRACE_OFF) | (pn_env_bool("PN_TRACE_DRV") ? PN_TRACE_DRV : PN_TRACE_OFF); disp->input = pn_buffer(1024); disp->fragment = 0; disp->channel = 0; disp->code = 0; disp->args = pn_data(16); disp->payload = NULL; disp->size = 0; disp->output_args = pn_data(16); disp->frame = pn_buffer( 4*1024 ); // XXX disp->capacity = 4*1024; disp->output = (char *) malloc(disp->capacity); disp->available = 0; disp->halt = false; disp->batch = true; disp->scratch = pn_string(NULL); return disp; }
PN_EXTERN pn_url_t *pn_url() { static const pn_class_t clazz = PN_CLASS(pn_url); pn_url_t *url = (pn_url_t*) pn_class_new(&clazz, sizeof(pn_url_t)); if (!url) return NULL; memset(url, 0, sizeof(*url)); url->str = pn_string(NULL); return url; }
static void test_build_list(void) { pn_list_t *l = build_list(0, pn_string("one"), pn_string("two"), pn_string("three"), END); assert(pn_list_size(l) == 3); assert(pn_strequals(pn_string_get((pn_string_t *) pn_list_get(l, 0)), "one")); assert(pn_strequals(pn_string_get((pn_string_t *) pn_list_get(l, 1)), "two")); assert(pn_strequals(pn_string_get((pn_string_t *) pn_list_get(l, 2)), "three")); pn_free(l); }
static void test_string_addf(void) { pn_string_t *str = pn_string("hello "); assert(str); int err = pn_string_addf(str, "%s", "this is a string that should be long " "enough to force growth but just in case we'll " "tack this other really long string on for the " "heck of it"); assert(err == 0); pn_free(str); }
void test_list_inspect(void) { pn_list_t *l = build_list(0, END); test_inspect(l, "[]"); pn_free(l); l = build_list(0, pn_string("one"), END); test_inspect(l, "[\"one\"]"); pn_free(l); l = build_list(0, pn_string("one"), pn_string("two"), END); test_inspect(l, "[\"one\", \"two\"]"); pn_free(l); l = build_list(0, pn_string("one"), pn_string("two"), pn_string("three"), END); test_inspect(l, "[\"one\", \"two\", \"three\"]"); pn_free(l); }
static void test_build_map_odd(void) { pn_map_t *m = build_map(0, 0.75, pn_string("key"), pn_string("value"), pn_string("key2"), pn_string("value2"), pn_string("key3"), END); assert(pn_map_size(m) == 3); pn_string_t *key = pn_string(NULL); pn_string_set(key, "key"); assert(pn_strequals(pn_string_get((pn_string_t *) pn_map_get(m, key)), "value")); pn_string_set(key, "key2"); assert(pn_strequals(pn_string_get((pn_string_t *) pn_map_get(m, key)), "value2")); pn_string_set(key, "key3"); assert(pn_map_get(m, key) == NULL); pn_free(m); pn_free(key); }
void test_iterator(void) { pn_list_t *list = build_list(0, pn_string("one"), pn_string("two"), pn_string("three"), pn_string("four"), END); pn_iterator_t *it = pn_iterator(); pn_it_state_t *state = (pn_it_state_t *) pn_iterator_start (it, pn_it_next, sizeof(pn_it_state_t)); state->list = list; state->index = 0; void *obj; int index = 0; while ((obj = pn_iterator_next(it))) { assert(obj == pn_list_get(list, index++)); } assert(index == 4); pn_free(list); pn_free(it); }
static void test_string(const char *value) { size_t size = value ? strlen(value) : 0; pn_string_t *str = pn_string(value); assert(equals(pn_string_get(str), value)); assert(pn_string_size(str) == size); pn_string_t *strn = pn_stringn(value, size); assert(equals(pn_string_get(strn), value)); assert(pn_string_size(strn) == size); pn_string_t *strset = pn_string(NULL); pn_string_set(strset, value); assert(equals(pn_string_get(strset), value)); assert(pn_string_size(strset) == size); pn_string_t *strsetn = pn_string(NULL); pn_string_setn(strsetn, value, size); assert(equals(pn_string_get(strsetn), value)); assert(pn_string_size(strsetn) == size); assert(pn_hashcode(str) == pn_hashcode(strn)); assert(pn_hashcode(str) == pn_hashcode(strset)); assert(pn_hashcode(str) == pn_hashcode(strsetn)); assert(!pn_compare(str, str)); assert(!pn_compare(str, strn)); assert(!pn_compare(str, strset)); assert(!pn_compare(str, strsetn)); pn_free(str); pn_free(strn); pn_free(strset); pn_free(strsetn); }
static void test_stringn(const char *value, size_t size) { pn_string_t *strn = pn_stringn(value, size); assert(equals(pn_string_get(strn), value)); assert(pn_string_size(strn) == size); pn_string_t *strsetn = pn_string(NULL); pn_string_setn(strsetn, value, size); assert(equals(pn_string_get(strsetn), value)); assert(pn_string_size(strsetn) == size); assert(pn_hashcode(strn) == pn_hashcode(strsetn)); assert(!pn_compare(strn, strsetn)); pn_free(strn); pn_free(strsetn); }
// Received client side int pn_do_mechanisms(pn_transport_t *transport, uint8_t frame_type, uint16_t channel, pn_data_t *args, const pn_bytes_t *payload) { pni_sasl_t *sasl = transport->sasl; // If we already pretended we got the ANONYMOUS mech then ignore if (sasl->last_state==SASL_PRETEND_OUTCOME) return 0; // This scanning relies on pn_data_scan leaving the pn_data_t cursors // where they are after finishing the scan int err = pn_data_scan(args, "D.[@["); if (err) return err; pn_string_t *mechs = pn_string(""); // Now keep checking for end of array and pull a symbol while(pn_data_next(args)) { pn_bytes_t s = pn_data_get_symbol(args); if (pni_included_mech(transport->sasl->included_mechanisms, s)) { pn_string_addf(mechs, "%*s ", (int)s.size, s.start); } } if (pn_string_size(mechs)) { pn_string_buffer(mechs)[pn_string_size(mechs)-1] = 0; } if (pni_init_client(transport) && pni_process_mechanisms(transport, pn_string_get(mechs))) { pni_sasl_set_desired_state(transport, SASL_POSTED_INIT); } else { sasl->outcome = PN_SASL_PERM; pni_sasl_set_desired_state(transport, SASL_RECVED_OUTCOME_FAIL); } pn_free(mechs); return 0; }
static void test_list_index(void) { pn_list_t *l = pn_list(PN_WEAKREF, 0); void *one = pn_string("one"); void *two = pn_string("two"); void *three = pn_string("three"); void *dup1 = pn_string("dup"); void *dup2 = pn_string("dup"); void *last = pn_string("last"); pn_list_add(l, one); pn_list_add(l, two); pn_list_add(l, three); pn_list_add(l, dup1); pn_list_add(l, dup2); pn_list_add(l, last); check_list_index(l, one, 0); check_list_index(l, two, 1); check_list_index(l, three, 2); check_list_index(l, dup1, 3); check_list_index(l, dup2, 3); check_list_index(l, last, 5); void *nonexistent = pn_string("nonexistent"); check_list_index(l, nonexistent, -1); pn_free(l); pn_free(one); pn_free(two); pn_free(three); pn_free(dup1); pn_free(dup2); pn_free(last); pn_free(nonexistent); }
int main(int argv, char **args){ pn_engine_t *engine = NULL; sdb_ret_attr_t av; /* pn_str_t raw = pn_string("ATTR uid domain item1 color 3\r\nred_green\r\n"); */ pn_str_t raw = pn_string("ATTR uid domain item1 color 3\r\nred\r\n"); /* pn_str_t raw = pn_string("ATTR uid domain item1 colo"); */ pn_chain_t ch; pn_buf_t buf; pn_int rc; rc = pn_engine_create(NULL, &engine, its_attr); if (PN_OK != rc) { pnlogerr("create engine failed"); return rc; } memset(&av, 0, sizeof(sdb_ret_attr_t)); pn_engine_set_data(engine, &av, sizeof(sdb_ret_attr_t)); buf.pos = raw.data; buf.last = raw.data + raw.len; ch.buf = &buf; ch.next = NULL; pn_engine_set_raw(engine, &ch); test_ret_attr_cut(engine); pnlog("test done"); exit(0); rc = pn_engine_parse(engine); if (PN_OK != rc) { pnlogerr("parse failed with rc[%d]", rc); return rc; } pnlog("uid:%.*s, domain:%.*s, itemname:%.*s, attrname:%.*s, size:%d, attrval_str:%.*s", strp(av.uid), strp(av.domain), strp(av.itemname), strp(av.attrname), av.size, strp(av.attrval_str)); return 0; }
void pni_handle_bound(pn_reactor_t *reactor, pn_event_t *event) { assert(reactor); assert(event); pn_connection_t *conn = pn_event_connection(event); pn_transport_t *transport = pn_event_transport(event); pn_record_t *record = pn_connection_attachments(conn); pn_url_t *url = (pn_url_t *)pn_record_get(record, PNI_CONN_PEER_ADDRESS); const char *host = NULL; const char *port = "5672"; pn_string_t *str = NULL; // link the new transport to its reactor: pni_record_init_reactor(pn_transport_attachments(transport), reactor); if (pn_connection_acceptor(conn) != NULL) { // this connection was created by the acceptor. There is already a // socket assigned to this connection. Nothing needs to be done. return; } if (url) { host = pn_url_get_host(url); const char *uport = pn_url_get_port(url); if (uport) { port = uport; } else { const char *scheme = pn_url_get_scheme(url); if (scheme && strcmp(scheme, "amqps") == 0) { port = "5671"; } } if (!pn_connection_get_user(conn)) { // user did not manually set auth info const char *user = pn_url_get_username(url); if (user) pn_connection_set_user(conn, user); const char *passwd = pn_url_get_password(url); if (passwd) pn_connection_set_password(conn, passwd); } } else { // for backward compatibility, see if the connection's hostname can be // used for the remote address. See JIRA PROTON-1133 const char *hostname = pn_connection_get_hostname(conn); if (hostname) { str = pn_string(hostname); char *h = pn_string_buffer(str); // see if a port has been included in the hostname. This is not // allowed by the spec, but the old reactor interface allowed it. char *colon = strrchr(h, ':'); if (colon) { *colon = '\0'; port = colon + 1; } host = h; } } if (!host) { // error: no address configured pn_condition_t *cond = pn_transport_condition(transport); pn_condition_set_name(cond, "proton:io"); pn_condition_set_description(cond, "Connection failed: no address configured"); pn_transport_close_tail(transport); pn_transport_close_head(transport); } else { pn_socket_t sock = pn_connect(pni_reactor_io(reactor), host, port); // invalid sockets are ignored by poll, so we need to do this manually if (sock == PN_INVALID_SOCKET) { pn_condition_t *cond = pn_transport_condition(transport); pn_condition_set_name(cond, "proton:io"); pn_condition_set_description(cond, pn_error_text(pn_reactor_error(reactor))); pn_transport_close_tail(transport); pn_transport_close_head(transport); } else { pn_reactor_selectable_transport(reactor, sock, transport); } } pn_free(str); }
void test_map_inspect(void) { // note that when there is more than one entry in a map, the order // of the entries is dependent on the hashes involved, it will be // deterministic though pn_map_t *m = build_map(0, 0.75, END); test_inspect(m, "{}"); pn_free(m); m = build_map(0, 0.75, pn_string("key"), pn_string("value"), END); test_inspect(m, "{\"key\": \"value\"}"); pn_free(m); m = build_map(0, 0.75, pn_string("k1"), pn_string("v1"), pn_string("k2"), pn_string("v2"), END); test_inspect(m, "{\"k1\": \"v1\", \"k2\": \"v2\"}"); pn_free(m); m = build_map(0, 0.75, pn_string("k1"), pn_string("v1"), pn_string("k2"), pn_string("v2"), pn_string("k3"), pn_string("v3"), END); test_inspect(m, "{\"k3\": \"v3\", \"k1\": \"v1\", \"k2\": \"v2\"}"); pn_free(m); }
static void test_map(void) { void *one = pn_class_new(PN_OBJECT, 0); void *two = pn_class_new(PN_OBJECT, 0); void *three = pn_class_new(PN_OBJECT, 0); pn_map_t *map = pn_map(PN_OBJECT, PN_OBJECT, 4, 0.75); assert(pn_map_size(map) == 0); pn_string_t *key = pn_string("key"); pn_string_t *dup = pn_string("key"); pn_string_t *key1 = pn_string("key1"); pn_string_t *key2 = pn_string("key2"); assert(!pn_map_put(map, key, one)); assert(pn_map_size(map) == 1); assert(!pn_map_put(map, key1, two)); assert(pn_map_size(map) == 2); assert(!pn_map_put(map, key2, three)); assert(pn_map_size(map) == 3); assert(pn_map_get(map, dup) == one); assert(!pn_map_put(map, dup, one)); assert(pn_map_size(map) == 3); assert(!pn_map_put(map, dup, two)); assert(pn_map_size(map) == 3); assert(pn_map_get(map, dup) == two); assert(pn_refcount(key) == 2); assert(pn_refcount(dup) == 1); assert(pn_refcount(key1) == 2); assert(pn_refcount(key2) == 2); assert(pn_refcount(one) == 1); assert(pn_refcount(two) == 3); assert(pn_refcount(three) == 2); pn_map_del(map, key1); assert(pn_map_size(map) == 2); assert(pn_refcount(key) == 2); assert(pn_refcount(dup) == 1); assert(pn_refcount(key1) == 1); assert(pn_refcount(key2) == 2); assert(pn_refcount(one) == 1); assert(pn_refcount(two) == 2); assert(pn_refcount(three) == 2); pn_decref(one); pn_decref(two); pn_decref(three); pn_decref(key); pn_decref(dup); pn_decref(key1); pn_decref(key2); pn_decref(map); }