Пример #1
0
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;
}
Пример #2
0
END_TEST

START_TEST (test_cometd_should_retry_recv)
{
    cometd_advice* handshake_advice = cometd_advice_new();
    handshake_advice->reconnect = COMETD_RECONNECT_HANDSHAKE;

    cometd_advice* retry_advice = cometd_advice_new();
    retry_advice->reconnect = COMETD_RECONNECT_RETRY;

    cometd_conn* conn = g_instance->conn;

    cometd_conn_set_state(conn, COMETD_DISCONNECTED);
    fail_if(cometd_should_retry_recv(g_instance));

    cometd_conn_set_state(conn, COMETD_UNCONNECTED);
    cometd_conn_take_advice(conn, handshake_advice);
    fail_if(cometd_should_retry_recv(g_instance));

    cometd_conn_set_state(conn, COMETD_UNCONNECTED);
    cometd_conn_take_advice(conn, NULL);
    fail_if(cometd_should_retry_recv(g_instance));

    cometd_conn_set_state(conn, COMETD_UNCONNECTED);
    cometd_conn_take_advice(conn, retry_advice);
    fail_unless(cometd_should_retry_recv(g_instance));
}
Пример #3
0
END_TEST

START_TEST (test_cometd_should_recv)
{
    cometd_conn* conn = g_instance->conn;

    cometd_conn_set_state(conn, COMETD_HANDSHAKE_SUCCESS);
    fail_unless(cometd_should_recv(g_instance));

    cometd_conn_set_state(conn, COMETD_CONNECTED);
    fail_unless(cometd_should_recv(g_instance));

    cometd_conn_set_state(conn, COMETD_DISCONNECTED);
    fail_if(cometd_should_recv(g_instance));
}
Пример #4
0
END_TEST

START_TEST(test_cometd_should_handshake)
{
    cometd_conn_set_state(g_instance->conn, COMETD_HANDSHAKE_SUCCESS);
    fail_if(cometd_should_handshake(g_instance));

    cometd_conn_set_state(g_instance->conn, COMETD_CONNECTED);
    fail_if(cometd_should_handshake(g_instance));

    cometd_conn_set_state(g_instance->conn, COMETD_UNCONNECTED);
    cometd_advice* advice = cometd_advice_new();
    advice->reconnect = COMETD_RECONNECT_HANDSHAKE;
    cometd_conn_take_advice(g_instance->conn, advice);

    fail_unless(cometd_should_handshake(g_instance));

    cometd_conn_take_advice(g_instance->conn, NULL);
}
Пример #5
0
int
cometd_disconnect(const cometd* h, int wait_for_server)
{
  if (wait_for_server){
    puts("default here");
  } else {
    cometd_conn_set_state(h->conn, COMETD_DISCONNECTED);
  } 
  cometd_loop_stop(h->loop);
  
  return COMETD_SUCCESS;
}
Пример #6
0
gpointer
cometd_loop_gthread_run(gpointer data)
{
  const cometd* h = (const cometd*) data;

  cometd_conn* conn = h->conn;
  cometd_loop* loop = h->loop;

  JsonNode *connect = NULL, *payload = NULL;

  long backoff = 0;

  guint attempt;
  for (attempt = 1; cometd_should_recv(h); ++attempt)
  {
    cometd_loop_wait(loop, backoff);

    payload = cometd_recv(h);
    connect = cometd_msg_extract_connect(payload);

    cometd_inbox_push(h->inbox, payload);
    cometd_process_msg(h, connect);

    if (cometd_should_retry_recv(h))
      backoff = cometd_get_backoff(h, attempt);
    else
      backoff = attempt = 0;

    json_node_free(payload);
    json_node_free(connect);

    // bail out if we should no longer backoff
    if (backoff == -1)
      break;
  }

  // If we've bailed from the loop, it's because we gave up
  // on the COMETD_UNCONNECTED state and we are no longer retrying or we
  // have intentially disconnected.
  //
  // If we gave up retrying, then this ensures that our state gets set correctly
  // and it should be a signal to the inbox queue to stop waiting.
  cometd_conn_set_state(conn, COMETD_DISCONNECTED);

  return NULL;
}
Пример #7
0
int
cometd_connect(const cometd* h)
{
  int error_code = cometd_handshake(h, NULL);

  if (error_code != COMETD_SUCCESS){
    goto error;
  }

  if (cometd_loop_start(h->loop)){
    error_code = cometd_error(h, ECOMETD_INIT_LOOP,
                                 "could not initialize connection loop");
    goto error;
  }

  cometd_conn_set_state(h->conn, COMETD_CONNECTED);

error:
  return error_code;
}
Пример #8
0
int	cometd_process_handshake(const cometd* h, JsonNode* msg)
{
  cometd_conn* conn = h->conn;
  cometd_config* config = h->config;

  cometd_transport* t = cometd_transport_negotiate(config->transports, msg);

  int code = COMETD_SUCCESS;
  
  if (t) {
    gchar* client_id = cometd_msg_client_id(msg);
    cometd_conn_set_transport(conn, t);
    cometd_conn_set_client_id(conn, client_id);
    cometd_conn_set_state(conn, COMETD_HANDSHAKE_SUCCESS);
    g_free(client_id);
  } else {
    code = ECOMETD_NO_TRANSPORT;
  }
  cometd_conn_take_advice(conn, cometd_msg_advice(msg));

  return code;
}