Reputation: 11
iwant Eliminate duplicate account,i have insert the keyptr into the hashmap and it works but when i use HASH_FIND_STR to find the str, exit_entry is always null,i cant figure it out... this is my code
// mutex
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// hash node
struct hash_buf_struct {
char *account_hash;
char *password_hash;
char *option_hash;
UT_hash_handle hh;
};
// hash init
struct hash_buf_struct *buffer = NULL;
struct hash_buf_struct *s = NULL;
// Producer thread func
void *producer()
{
while (1)
{
char rcvbuf[1500];
memset(rcvbuf, '\0', sizeof(rcvbuf));
recvfrom(sockfd, rcvbuf, sizeof(rcvbuf), 0, (struct sockaddr *)&send_to, (socklen_t *)&send_to_len);
regmatch_t matches[4];
if (regexec(®ex, rcvbuf, 4, matches, 0) == 0) {
char account[256];
char password[256];
char operation[5];
regmatch_t account_match = matches[1];
regmatch_t password_match = matches[2];
regmatch_t operation_match = matches[3];
snprintf( account, account_match.rm_eo - account_match.rm_so + 1, "%s", rcvbuf + account_match.rm_so);
snprintf( password, password_match.rm_eo - password_match.rm_so + 1, "%s", rcvbuf + password_match.rm_so);
snprintf(operation, operation_match.rm_eo - operation_match.rm_so + 1, "%s", rcvbuf + operation_match.rm_so);
// sendto(sockfd, "Done!\n", 7, 0, (const struct sockaddr *)&send_to, (socklen_t)sizeof(send_to));
s = (struct hash_buf_struct*)calloc(sizeof(struct hash_buf_struct), sizeof(char));
s->option_hash = (char *)calloc(strlen(operation) + 1, sizeof(char));
s->password_hash = (char *)calloc(strlen(password) + 1, sizeof(char));
s->account_hash = (char *)calloc(strlen(account) + 2, sizeof(char));
strcat (s->password_hash, password);
strcat (s->option_hash, operation);
strcat (s->account_hash, account);
struct hash_buf_struct *hash = NULL;
printf("---------------print test--------------\n");
for (hash = buffer; hash != NULL; hash = (struct hash_buf_struct *)(hash->hh.next))
printf("account = %s\n",hash->account_hash);
printf("\n");
struct hash_buf_struct *exit_entry = (struct hash_buf_struct *)calloc(strlen(account) + 1, sizeof(char));
HASH_FIND_STR(buffer, s->account_hash, exit_entry); // cant work
if(exit_entry == NULL)
{
pthread_mutex_lock(&mutex);
HASH_ADD_KEYPTR(hh, buffer, s->account_hash, strlen(account) + 1, s);
pthread_mutex_unlock(&mutex);
} else {
if (strcmp(s->option_hash, exit_entry->option_hash) == 0) printf("Repetitive operation");
else {
pthread_mutex_lock(&mutex);
HASH_ADD_KEYPTR(hh, buffer, s->account_hash, strlen(account) + 1, s);
pthread_mutex_unlock(&mutex);
}
}
} else printf("----------------------------------------------------------\nValid format: %s\n\
Please type like: ACCOUNT PASSWORD OPTION(close|open)\n----------------------------------------------------------\n", rcvbuf);
}
}
What am I missing,the buffer is Global variables.Logically speaking the HASH_FIND_STR can work..i have try to print the total hashmap,everything is all right but still cant find the account iwant
Upvotes: 1
Views: 57