Mod aaa-12 修改user_hashtable.c并移动文件到Product/user下

RCA:
SOL:
修改人:chenling
检视人:
This commit is contained in:
ChenLing 2019-07-25 12:15:40 +08:00
parent 41d9adad79
commit 7b7e5a1ad9
2 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,77 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=userhash
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = DLL
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = APP
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../../Product/user/user_auth
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = user_hashtable.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../../Product/common
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
PLAT_ARM64_LDFLAGS := -fPIC -shared
PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
#gcc libs
ARM64_LIBS := -lcjson -lpthread -lm
LINUX_LIBS := -lcjson -lpthread -lm
ifeq ($(PLAT_ARM64), TRUE)
DEPEND_LIB +=
USER_CLEAN_ITEMS +=
endif
ifeq ($(PLAT_LINUX), TRUE)
DEPEND_LIB +=
USER_CLEAN_ITEMS +=
endif
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifneq ($(MAKECMDGOALS), clean)
ifneq ($(MAKECMDGOALS), cleanall)
ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB)))
$(shell $(CP) $(DEPEND_LIB) ./)
endif
endif
endif
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,169 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "hlist.h"
#include "user_hashtable.h"
/*链表全局变量 */
struct hlist_head *hash;
USER_INFO *pNode ;
/*计算hash值 */
struct hlist_head * call_hash(struct hlist_head *hash, uint32_t ip)
{
unsigned int val = ip % 100;
printf("val =%d\n", val);
return &hash[val];
}
/*初始化函数 */
int Init_hash()
{
int i = 0;
/*创建hash头 */
hash = (struct hlist_head*)malloc(sizeof(*hash)*100);
if(NULL == hash)
{
printf("alloc error\n");
return -1;
}
/*初始化hash表头 */
for(i = 0; i < 100; i++)
INIT_HLIST_HEAD(&hash[i]);
/*hsah桶普通节点分配内存 */
pNode = (struct user_info *)malloc(sizeof(struct user_info));
if (NULL == pNode)
{
printf("alloc error\n");
return -1;
}
/*初始化hash桶的普通结点 */
memset(pNode,0,sizeof(struct user_info));
INIT_HLIST_NODE(&pNode->hnode);
}
/*查找用户信息*/
USER_INFO * ufind_user(uint32_t user_ip)
{
struct hlist_node *p = NULL, *n = NULL ;
/* 这个实际上就是一个for循环从头到尾遍历链表。
* posstruct hlist_node类型的一个指针
* nstruct hlist_node类型的一个指针
* headstruct hlist_head类型的一个指针hlist链表的头结点
*/
hlist_for_each_safe(p,n,call_hash(hash,user_ip))
{
/* p表示struct hlist_node类型的一个地址。
* struct user_info
* hnodetype结构体中的hlist_node成员变量的名称
* p所指地址的这个结构体的首地址
*/
pNode = hlist_entry(p, struct user_info ,hnode);
if(pNode != NULL)
printf("user_id :%d\n",pNode->id);
return pNode;
}
}
/*增加用户信息*/
int uadd_user(uint32_t user_ip, int user_id)
{
struct hlist_node *pos = NULL, *n = NULL ;
pNode = NULL;
struct hlist_head* pVal = call_hash(hash, user_ip);
printf("pVal = %p, pVal->first = %p\n", pVal, pVal->first);
//hlist_for_each_safe(p,n,call_hash(hash, user_ip)) /*查找ip是否存在hash表中 */
for (pos = pVal->first; pos && ({ n = pos->next; 1; }); pos = n)
{
pNode = hlist_entry(pos, struct user_info ,hnode);
if(pNode != NULL)
printf("IP ALEADY EXISTED\n");
}
if (pNode == NULL)
{
pNode = (struct user_info *)malloc(sizeof(struct user_info));
if (NULL == pNode)
{
return -1;
}
memset(pNode,0,sizeof(struct user_info));
INIT_HLIST_NODE(&pNode->hnode);
pNode->ip = user_ip;
hlist_add_head(&pNode->hnode, call_hash(hash, user_ip));
}
pNode->id = user_id;
}
/*删除用户信息 */
void udelete_user(int user_ip)
{
struct hlist_node *p = NULL, *n = NULL ;
int i = 0;
hlist_for_each_safe(p,n,call_hash(hash,user_ip))
{
pNode = hlist_entry(p, struct user_info ,hnode);
hlist_del(&pNode->hnode);
free(pNode);
}
}
/*删除所有的hash节点 */
void udelete_all()
{
struct hlist_node *p = NULL, *n = NULL ;
int i = 0;
for(i = 0; i < 100; i++)
{
hlist_for_each_safe(p,n,&hash[i])
{
pNode = hlist_entry(p, struct user_info ,hnode);
hlist_del(&pNode->hnode);
free(pNode);
}
}
}
/*打印所有信息信息 */
void uprintf_users()
{
struct hlist_node *p = NULL, *n = NULL ;
int i = 0;
for(i = 0; i < 100; i++)
{
hlist_for_each_safe(p,n,&hash[i])
{
char str[32];
pNode = hlist_entry(p, struct user_info ,hnode);
if(pNode != NULL)
inet_ntop(AF_INET, (void *)&pNode->ip, str, 32);
printf("user_ip :%s user_id:%d\n", str, pNode->id);
}
}
}