int cometd_process_connect(const cometd* h, JsonNode* msg) { cometd_advice* advice; cometd_conn* conn = h->conn; // If we are trying to disconnect cleanly then we should not // reset the connected/connecting status on response. if (cometd_conn_is_state(conn, COMETD_DISCONNECTING | COMETD_DISCONNECTED)) return COMETD_SUCCESS; // Always set advice if it is offered and reuse old advice if new advice DNE advice = cometd_msg_advice(msg); if (advice) cometd_conn_take_advice(conn, advice); if (!cometd_msg_is_successful(msg)) { cometd_conn_set_state(conn, COMETD_UNCONNECTED); cometd_handle_advice(h, advice); } else cometd_conn_set_state(conn, COMETD_CONNECTED); return COMETD_SUCCESS; }
END_TEST START_TEST (test_cometd_process_connect_unsuccessful) { cometd_conn* conn = g_instance->conn; cometd_conn_set_client_id(conn, "testid"); cometd_conn_set_transport(conn, &TEST_TRANSPORT); fail_unless(cometd_conn_is_state(conn, COMETD_UNINITIALIZED)); JsonNode* msg = cometd_msg_connect_new(g_instance); cometd_msg_set_boolean_member(msg, "successful", FALSE); int code = cometd_process_connect(g_instance, msg); fail_unless(cometd_conn_is_state(conn, COMETD_UNCONNECTED)); ck_assert_int_eq(COMETD_SUCCESS, code); }
gboolean cometd_should_handshake(const cometd* h) { cometd_conn* conn = h->conn; if (cometd_conn_is_state(conn, COMETD_HANDSHAKE_SUCCESS | COMETD_CONNECTED)) return FALSE; return conn->advice == NULL || cometd_advice_is_handshake(conn->advice); }
END_TEST START_TEST (test_cometd_process_handshake_success) { cometd_conn* conn = g_instance->conn; JsonNode* n = json_from_fixture("handshake_resp_lp"); fail_unless(cometd_conn_is_state(conn, COMETD_UNINITIALIZED)); fail_unless(cometd_current_transport(g_instance) == NULL); fail_unless(cometd_conn_client_id(conn) == NULL); int code = cometd_process_handshake(g_instance, n); fail_unless(code == COMETD_SUCCESS); fail_unless(cometd_conn_is_state(conn, COMETD_HANDSHAKE_SUCCESS)); fail_if(cometd_current_transport(g_instance) == NULL); fail_if(cometd_conn_client_id(conn) == NULL); json_node_free(n); }
END_TEST START_TEST (test_cometd_process_handshake_no_transport) { cometd_conn* conn = g_instance->conn; JsonNode* n = json_from_fixture("handshake_resp_unsupported_transports"); fail_unless(cometd_conn_is_state(conn, COMETD_UNINITIALIZED)); fail_unless(cometd_current_transport(g_instance) == NULL); fail_unless(cometd_conn_client_id(conn) == NULL); int code = cometd_process_handshake(g_instance, n); fail_unless(code == ECOMETD_NO_TRANSPORT); fail_unless(cometd_conn_is_state(conn, COMETD_UNINITIALIZED)); fail_unless(cometd_current_transport(g_instance) == NULL); fail_unless(cometd_conn_client_id(conn) == NULL); fail_if(cometd_conn_advice(conn) == NULL); json_node_free(n); }
gboolean cometd_should_retry_recv(const cometd* h) { g_return_val_if_fail(h, FALSE); g_return_val_if_fail(h->conn, FALSE); cometd_conn* conn = h->conn; const cometd_advice* advice = cometd_conn_advice(conn); return cometd_conn_is_state(conn, COMETD_UNCONNECTED) && (advice != NULL && cometd_advice_is_retry(advice)); }
gboolean cometd_should_recv(const cometd* h) { g_return_val_if_fail(h, FALSE); g_return_val_if_fail(h->conn, FALSE); cometd_conn* conn = h->conn; const int state = COMETD_CONNECTED | COMETD_HANDSHAKE_SUCCESS | COMETD_UNCONNECTED; return cometd_conn_is_state(conn, state); }
END_TEST START_TEST (test_cometd_new) { fail_unless(cometd_conn_is_state(g_instance->conn, COMETD_UNINITIALIZED)); ck_assert_int_eq(COMETD_SUCCESS, g_instance->last_error->code); char* actual_url = "http://example.com/cometd/"; cometd_configure(g_instance, COMETDOPT_URL, actual_url); cometd_config* config = g_instance->config; ck_assert_str_eq(actual_url, config->url); ck_assert_int_eq(DEFAULT_BACKOFF_INCREMENT, config->backoff_increment); ck_assert_int_eq(DEFAULT_REQUEST_TIMEOUT, config->request_timeout); fail_if(cometd_find_transport(config, "long-polling") == NULL); }
/** * Reads JsonNodes that are received by the inbox thread. * If a handler wishes to to keep a JsonNode in memory * longer than the lifetime of a cometd_callback, then it * must increase the reference count to the node. */ void cometd_listen(const cometd* h) { JsonNode* msg; // TODO: COMETD_UNINITIALIZED is a useless state const long stop = COMETD_DISCONNECTED | COMETD_DISCONNECTING | COMETD_UNINITIALIZED; // TODO: Add tests that demostrate that cometd_listen actually returns while (!cometd_conn_is_state(h->conn, stop)) { while (msg == cometd_inbox_take(h->inbox)) if (msg != NULL) { cometd_process_msg(h, msg); // We need are responsible for destroying the message // after it has been taken from the queue and processed. json_node_free(msg); } } }