Exemple #1
0
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle,
                                    drizzle_return_t *ret_ptr)
{
  drizzle_options_t options;
  drizzle_query_st *query;
  drizzle_con_st *con;

  if (drizzle->query_new == 0 && drizzle->query_running == 0)
  {
    *ret_ptr= DRIZZLE_RETURN_OK;
    return NULL;
  }

  options= drizzle->options;
  drizzle->options|= DRIZZLE_NON_BLOCKING;

  /* Check to see if any queries need to be started. */
  if (drizzle->query_new > 0)
  {
    for (query= drizzle->query_list; query != NULL; query= query->next)
    {
      if (query->state != DRIZZLE_QUERY_STATE_INIT)
        continue;

      drizzle->query_new--;
      drizzle->query_running++;
      assert(query->con->query == NULL);
      query->con->query= query;

      drizzle_query_run_state(query, ret_ptr);
      if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
      {
        assert(query->state == DRIZZLE_QUERY_STATE_DONE);
        drizzle->query_running--;
        drizzle->options= options;
        query->con->query= NULL;
        if (*ret_ptr == DRIZZLE_RETURN_ERROR_CODE || *ret_ptr == DRIZZLE_RETURN_OK)
        {
          return query;
        }
        return NULL;
      }
    }
    assert(drizzle->query_new == 0);
  }

  while (1)
  {
    /* Loop through each active connection. */
    while ((con= drizzle_con_ready(drizzle)) != NULL)
    {
      query= con->query;
      drizzle_query_run_state(query, ret_ptr);
      if (query->state == DRIZZLE_QUERY_STATE_DONE)
      {
        drizzle->query_running--;
        drizzle->options= options;
        con->query= NULL;
        return query;
      }
      assert(*ret_ptr == DRIZZLE_RETURN_IO_WAIT);
    }

    if (options & DRIZZLE_NON_BLOCKING)
    {
      *ret_ptr= DRIZZLE_RETURN_IO_WAIT;
      return NULL;
    }

    *ret_ptr= drizzle_con_wait(drizzle);
    if (*ret_ptr != DRIZZLE_RETURN_OK)
    {
      drizzle->options= options;
      return NULL;
    }
  }
}
Exemple #2
0
int main(void)
{
  close(STDOUT_FILENO);

  drizzle_st drizzle;
  drizzle_con_st listen_con;
  drizzle_con_st client;
  drizzle_con_st server;
  drizzle_return_t ret;
  bool server_accepted = false;
  server_state_st server_state;
  client_state_st client_state;

  drizzle_test("drizzle_create");
  if (drizzle_create(&drizzle) == NULL)
  {
    drizzle_test_error("returned NULL");
  }

  drizzle_test("drizzle_con_add_tcp_listen");
  if (drizzle_con_add_tcp_listen(&drizzle, &listen_con, DRIZZLE_TEST_HOST,
                                 DRIZZLE_TEST_PORT, 1,
                                 DRIZZLE_CON_NONE) == NULL)
  {
    drizzle_test_error("returned NULL");
  }

  drizzle_test("drizzle_con_listen");
  ret= drizzle_con_listen(&listen_con);
  if (ret != DRIZZLE_RETURN_OK)
  {
    drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
  }

  drizzle_test("drizzle_con_add_tcp");
  if (drizzle_con_add_tcp(&drizzle, &client, DRIZZLE_TEST_HOST,
                          DRIZZLE_TEST_PORT, NULL, NULL, NULL,
                          DRIZZLE_CON_NONE) == NULL)
  {
    drizzle_test_error("returned NULL");
  }

  drizzle_test("drizzle_add_options");
  drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING);

  server_state.state= SERVER_STATE_START;
  client_state.state= CLIENT_STATE_START;

  while (true)
  {
    if (server_accepted == false)
    {
      drizzle_test("drizzle_con_accept");
      (void)drizzle_con_accept(&drizzle, &server, &ret);
      if (ret == DRIZZLE_RETURN_OK)
      {
        server_accepted = true;
      }
      else if (ret == DRIZZLE_RETURN_COULD_NOT_CONNECT || ret == DRIZZLE_RETURN_NO_ACTIVE_CONNECTIONS)
      {
        break;
      }
      else if (ret != DRIZZLE_RETURN_IO_WAIT)
      {
        drizzle_test_error("returned %s (%s)", drizzle_error(&drizzle), drizzle_strerror(ret));
      }
    }

    if (server_accepted)
    {
      _server(&server, &server_state);
    }

    _client(&client, &client_state);

    if (server_state.state == SERVER_STATE_DONE &&
        client_state.state == CLIENT_STATE_DONE)
    {
      break;
    }

    drizzle_test("drizzle_con_wait");
    ret= drizzle_con_wait(&drizzle);
    if (ret == DRIZZLE_RETURN_COULD_NOT_CONNECT || ret == DRIZZLE_RETURN_NO_ACTIVE_CONNECTIONS)
    {
      break;
    }
    else if (ret != DRIZZLE_RETURN_OK)
    {
      drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret);
    }
  }

  if (server_accepted)
  {
    drizzle_test("drizzle_con_free");
    drizzle_con_free(&server);
  }

  drizzle_test("drizzle_con_free");
  drizzle_con_free(&client);

  drizzle_test("drizzle_con_free");
  drizzle_con_free(&listen_con);

  drizzle_test("drizzle_free");
  drizzle_free(&drizzle);

  return 0;
}
Exemple #3
0
int
io_wait(drizzle_con_st *con, drizzle_return_t ret)
{
    drizzle_return_t dret;
    int fd = 0, events = 0;
    PyObject *fileno, *state, *args, *res;

    if (ret == DRIZZLE_RETURN_OK) {
        return 0;
    }else if (ret == DRIZZLE_RETURN_IO_WAIT) {
        events = con->events;

        YDEBUG("IO_WAIT con:%p events:%d", con, events);
        if (external_io_wait) {
            fd = drizzle_con_fd(con);
            if (fd == -1){
                return -1;
            }

            fileno = PyLong_FromLong((long)fd);
            if (fileno == NULL) {
                return -1;
            }
            state = PyLong_FromLong((long)events);
            if (state == NULL) {
                Py_DECREF(fileno);
                return -1;
            }

            args = PyTuple_Pack(2, fileno, state);
            if (args == NULL) {
                Py_DECREF(fileno);
                Py_DECREF(state);
                return -1;
            }

            YDEBUG("call external_io_wait ...");
            res = PyObject_CallObject(external_io_wait, args);
            Py_DECREF(args);

            if (res == NULL) {
                return -1;
            }
            Py_XDECREF(res);
            dret = drizzle_con_set_revents(con, events);
            if (dret != DRIZZLE_RETURN_OK){
                RDEBUG("ret %d:%s", dret, drizzle_error(drizzle));
                return -1;
            }
            return 1;
        } else {
            DEBUG("call drizzle_con_wait ...");
            dret = drizzle_con_wait(drizzle);

            if (dret != DRIZZLE_RETURN_OK){
                RDEBUG("ret %d:%s", dret, drizzle_error(drizzle));
                return -1;
            }
            return 1;
        }
    }else{
        RDEBUG("ret %d:%s", ret, drizzle_error(drizzle));
        return -1;
    }
    return 0;
}