Esempio n. 1
0
ta_identity_t *ta_identity_create(char *name, xt_core_log_t *log)
{
  assert(name);
  assert(log);
  ta_identity_t *identity;
  xt_core_bool_t so_far_so_good;

  identity = malloc(sizeof *identity);
  if (identity) {
    identity->log = log;
    identity->phrases_qutex = NULL;
    identity->phrases = NULL;
    identity->name = xt_core_string_copy(name);
    if (identity->name) {
      so_far_so_good = xt_core_bool_true;
    } else {
      so_far_so_good = xt_core_bool_false;
      xt_core_log_trace(log, " ta ", "xt_core_string_copy");
    }
  } else {
    so_far_so_good = xt_core_bool_false;
    xt_core_log_trace(log, " ta ", "malloc");
  }

  if (so_far_so_good) {
    identity->phrases = xt_case_list_create(ta_phrase_compare, ta_phrase_copy,
        ta_phrase_destroy);
    if (identity->phrases) {
      so_far_so_good = xt_core_bool_true;
      xt_case_list_set_size_limit(identity->phrases, MAX_PHRASES_SIZE);
    } else {
      so_far_so_good = xt_core_bool_false;
      xt_core_log_enter(log, " ta ", "xt_case_list_create");
    }
  }

  if (so_far_so_good) {
    identity->phrases_qutex = xt_sync_qutex_create();
    if (!identity->phrases_qutex) {
      so_far_so_good = xt_core_bool_false;
      xt_core_log_enter(log, " ta ", "xt_sync_qutex_create");
    }
  }

  if (identity && !so_far_so_good) {
    if (identity->name) {
      xt_core_string_destroy(identity->name);
    }
    if (identity->phrases) {
      xt_case_list_destroy(identity->phrases);
    }
    if (identity->phrases_qutex) {
      xt_sync_qutex_destroy(identity->phrases_qutex);
    }
    free(identity);
    identity = NULL;
  }

  return identity;
}
Esempio n. 2
0
File: system.c Progetto: xtools/xt
xt_core_bool_t xt_net_post_system_create_outbox(xt_net_post_system_t *post)
{
    xt_core_bool_t success;

    post->outbox = xt_case_list_create(XT_CORE_OBJECT_NO_COMPARE_F,
                                       xt_core_message_copy, xt_core_message_destroy);
    if (post->outbox) {
        success = xt_core_bool_true;
    } else {
        xt_core_trace("x_case_list_create");
        success = xt_core_bool_false;
    }

    return success;
}
Esempio n. 3
0
xt_case_list_t *ta_identity_listen(ta_identity_t *identity,
    unsigned long next_phrase_id)
{
  assert(identity);
  unsigned long i;
  xt_case_list_t *phrases;
  ta_phrase_t *phrase;
  ta_phrase_t *phrase_copy;
  unsigned long phrases_size;

  phrases = xt_case_list_create(ta_phrase_compare, ta_phrase_copy,
      ta_phrase_destroy);
  if (phrases) {
    xt_sync_qutex_lock_shared(identity->phrases_qutex);
    phrases_size = xt_case_list_get_size(identity->phrases);
    check_range_of_next_phrase_id(&next_phrase_id, phrases_size);
    for (i = next_phrase_id; i < phrases_size; i++) {
      phrase = xt_case_list_find_at(identity->phrases, i);
      assert(phrase);
      phrase_copy = ta_phrase_copy(phrase);
      if (phrase_copy) {
        if (!xt_case_list_add_last(phrases, phrase_copy)) {
          xt_core_log_trace(identity->log, " ta ", "xt_case_list_add_last");
          ta_phrase_destroy(phrase_copy);
          break;
        }
      } else {
        xt_core_log_trace(identity->log, " ta ", "ta_phrase_copy");
        break;
      }
    }
    xt_sync_qutex_unlock_shared(identity->phrases_qutex);
  } else {
    xt_core_log_trace(identity->log, " ta ", "xt_case_list_create");
  }

  return phrases;
}
Esempio n. 4
0
File: system.c Progetto: xtools/xt
xt_case_list_t *xt_net_post_system_take_unsent_messages(xt_net_post_system_t *post)
{
    assert(post);
    xt_case_list_t *list;
    xt_core_message_t *message;

    list = xt_case_list_create(XT_CORE_OBJECT_NO_COMPARE_F,
                               xt_core_message_copy, XT_CORE_OBJECT_NO_DESTROY_F);
    if (list) {
        xt_case_list_iterate_start(post->outbox);
        while ((message = xt_case_list_iterate_next(post->outbox))) {
            if (xt_case_list_add_last(list, message)) {
                xt_case_list_iterate_remove(post->outbox);
            } else {
                xt_core_trace("x_case_list_add_last");
            }
        }
    } else {
        xt_core_trace("x_case_list_create");
    }

    return list;
}
Esempio n. 5
0
File: post.c Progetto: xtools/xt
void *xt_net_http_post_create(int socket)
{
  xt_net_http_post_t *http_post;
  xt_core_bool_t so_far_so_good;

  http_post = malloc(sizeof *http_post);
  if (http_post) {
    http_post->socket = socket;
    http_post->last_receive_activity_time = time(NULL);
    http_post->socket_closed = xt_core_bool_false;

    http_post->in_buffer_have_status_line = xt_core_bool_false;
    http_post->in_buffer_have_headers = xt_core_bool_false;
    http_post->in_buffer_have_body = xt_core_bool_false;
    http_post->in_buffer_have_complete_message = xt_core_bool_false;
    http_post->in_buffer_receive_position = 0;
    http_post->in_buffer_parse_position = 0;
    http_post->in_resource_path = NULL;
    http_post->in_body = NULL;

    http_post->out_buffer = NULL;
    http_post->out_buffer_size = 0;
    http_post->currently_sending_out_buffer = xt_core_bool_false;
    http_post->out_buffer_send_position = 0;

    xt_net_post_stats_init(&http_post->stats);

    so_far_so_good = xt_core_bool_true;
  } else {
    so_far_so_good = xt_core_bool_false;
    xt_core_trace("malloc");
  }

  if (so_far_so_good) {
    xt_core_nameobject_init_iobject(&http_post->nameobject_iobject);
    http_post->in_http_headers
      = xt_case_set_create(&http_post->nameobject_iobject);
    if (!http_post->in_http_headers) {
      xt_core_trace("x_case_set_create");
      so_far_so_good = xt_core_bool_false;
    }
  }

  if (so_far_so_good) {
    http_post->inbox = xt_case_list_create(XT_CORE_OBJECT_NO_COMPARE_F,
        XT_CORE_OBJECT_NO_COPY_F, XT_CORE_OBJECT_NO_DESTROY_F);
    if (!http_post->inbox) {
      so_far_so_good = xt_core_bool_false;
    }
  }

  if (so_far_so_good) {
    http_post->outbox = xt_case_list_create(XT_CORE_OBJECT_NO_COMPARE_F,
        XT_CORE_OBJECT_NO_COPY_F, xt_net_http_message_destroy);
    if (!http_post->outbox) {
      so_far_so_good = xt_core_bool_false;
    }
  }

  if (!so_far_so_good && http_post) {
    xt_net_http_post_create_rollback(http_post);
    http_post = NULL;
  }

  return http_post;
}
Esempio n. 6
0
File: system.c Progetto: xtools/xt
xt_net_client_system_t *xt_net_client_system_create
(const char *server_ip_address, unsigned short server_min_port,
    unsigned short server_max_port, xt_net_engine_get_name_f get_engine_name,
    void *custom_client_context, xt_core_log_t *log)
{
  assert(server_ip_address);
  xt_net_client_system_t *client;
  xt_core_bool_t success;
  unsigned long each_engine_id;
  xt_core_bool_t messaging_mutex_needs_destroy;
  xt_core_bool_t connected;

  messaging_mutex_needs_destroy = xt_core_bool_false;
  connected = xt_core_bool_false;

  client = malloc(sizeof *client);
  if (client) {
    client->log = log;
    client->post = NULL;
    client->get_engine_name = get_engine_name;
    client->custom_client_context = custom_client_context;
    client->server_min_port = server_min_port;
    client->server_max_port = server_max_port;
    client->server_socket_closed = xt_core_bool_true;
    for (each_engine_id = 0; each_engine_id < XT_NET_ENGINE_TYPE_COUNT;
         each_engine_id++) {
      *(client->engines_array + each_engine_id) = NULL;
    }
    xt_net_post_ipost_init(&client->ipost, xt_net_post_system_compare,
        xt_net_post_system_create, xt_net_post_system_create_decoy,
        xt_net_post_system_destroy, xt_net_post_system_destroy_decoy,
        xt_net_post_system_get_last_receive_activity_time,
        xt_net_post_system_get_socket, xt_net_post_system_get_stats,
        xt_net_post_system_receive_message,
        xt_net_post_system_receive_messages, xt_net_post_system_send_message,
        xt_net_post_system_send_messages,
        xt_net_post_system_is_socket_closed, xt_net_post_system_mod,
        xt_net_post_system_compare_equal);
    success = xt_core_bool_true;
  } else {
    success = xt_core_bool_false;
    xt_core_log_trace(client->log, "xnet", "malloc");
  }

  if (success) {
    client->server_ip_address = strdup(server_ip_address);
    if (!client->server_ip_address) {
      success = xt_core_bool_false;
    }
  }

  if (success) {
    client->exchange = xt_net_exchange_create(&client->ipost);
    if (!client->exchange) {
      success = xt_core_bool_false;
    }
  }

  if (success) {
    client->engines = xt_case_list_create(XT_CORE_OBJECT_NO_COMPARE_F,
        XT_CORE_OBJECT_NO_COPY_F, destroy_engine_container);
    if (!client->engines) {
      success = xt_core_bool_false;
    }
  }

  if (success) {
    if (0 == pthread_mutex_init(&client->messaging_mutex, NULL)) {
      messaging_mutex_needs_destroy = xt_core_bool_true;
    } else {
      success = xt_core_bool_false;
      xt_core_log_trace(client->log, "xnet", "pthread_mutex_init");
    }
  }

  if (success) {
    connected = connect_to_server(client);
    if (!connected) {
      success = xt_core_bool_false;
      xt_core_log_trace(client->log, "xnet", "connect_to_server");
    }
  }

  if (!success && client) {
    if (connected) {
      xt_net_client_socket_destroy(client->socket);
    }
    if (client->exchange) {
      xt_net_exchange_destroy(client->exchange);
    }
    if (client->server_ip_address) {
      free(client->server_ip_address);
    }
    if (client->engines) {
      xt_case_list_destroy(client->engines);
    }
    if (messaging_mutex_needs_destroy) {
      pthread_mutex_destroy(&client->messaging_mutex);
    }
    free(client);
    client = NULL;
  }

  return client;
}