#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node{
char jipnc_id[16];
int status;
int fd;
pthread_t thread_id;
struct node *next;
}NODE;
typedef struct {
NODE *head;
NODE *tail;
int num;
}JIPNC_LIST;
void list_init(JIPNC_LIST *list)
{
list->head = NULL;
list->tail = NULL;
list->num = 0;
return;
}
void insert_node(JIPNC_LIST *list, char *jipnc_id, int fd, pthread_t thread_id)
{
NODE *new_node = (NODE *)malloc(sizeof(NODE) + 4);
memset(new_node, 0, sizeof(NODE) + 4);
memcpy(new_node->jipnc_id, jipnc_id, 16);
new_node->status = JMS_JIPNC_FREE;
new_node->fd = fd;
new_node->thread_id = thread_id;
new_node->next = NULL;
if(list->num == 0){
list->head = new_node;
list->tail = new_node;
list->num++;
}
else{
list->tail->next = new_node;
list->tail = new_node;
new_node->next = NULL;
list->num++;
}
return;
}
void delete_node(JIPNC_LIST *list, int fd)
{
int i;
int num = list->num;
NODE * pre_node = NULL;
NODE * cur_node = list->head;
if(list == NULL){
return;
}
for(i = 0;i < num;i++){
if(cur_node->fd == fd){
break;
}
pre_node = cur_node;
cur_node = cur_node->next;
}
if(cur_node == NULL){
return;
}
if(cur_node == list->head){
list->head = list->head->next;
if(list->head == NULL){
list->tail = NULL;
}
if(cur_node != NULL){
free(cur_node);
cur_node = NULL;
}
list->num--;
}
else{
pre_node->next = cur_node->next;
if(pre_node->next == NULL){
list->tail = pre_node;
}
if(cur_node != NULL){
free(cur_node);
cur_node = NULL;
}
list->num--;
}
return;
}
void modify_node_status(JIPNC_LIST *list, char *jipnc_id, int value)
{
int i;
int num = list->num;
NODE * cur_node = list->head;
if(jipnc_id == NULL || strlen(jipnc_id) == 0){
return;
}
for(i = 0;i < num;i++){
if(0 == strncmp(cur_node->jipnc_id, jipnc_id, 16)){
cur_node->status = value;
return;
}
cur_node = cur_node->next;
}
}
int get_node_status(JIPNC_LIST *list, char *jipnc_id)
{
int i;
int num = list->num;
NODE * cur_node = list->head;
if(jipnc_id == NULL || strlen(jipnc_id) == 0){
return -1;
}
for(i = 0;i < num;i++){
if(0 == strncmp(cur_node->jipnc_id, jipnc_id, 16)){
return cur_node->status;;
}
cur_node = cur_node->next;
}
return -2;
}
int get_node_fd(JIPNC_LIST *list, char *jipnc_id)
{
int i;
int num = list->num;
NODE * cur_node = list->head;
if(jipnc_id == NULL || strlen(jipnc_id) == 0){
return -1;
}
for(i = 0;i < num;i++){
if(0 == strncmp(cur_node->jipnc_id, jipnc_id, 16)){
return cur_node->fd;
}
cur_node = cur_node->next;
}
return -1;
}
pthread_t get_node_thread_id(JIPNC_LIST *list, int fd)
{
int i;
int num = list->num;
NODE * cur_node = list->head;
for(i = 0;i < num;i++){
if(cur_node->fd == fd){
return cur_node->thread_id;
}
cur_node = cur_node->next;
}
return -1;
}
void free_list(JIPNC_LIST *list)
{
int i;
int num = list->num;
NODE * tmp_node = NULL;
for(i = 0; i < num; i++){
tmp_node = list->head;
list->head = list->head->next;
list->num--;
free(tmp_node);
tmp_node = NULL;
}
list->head = NULL;
list->tail = NULL;
list->num = 0;
return;
}
void print_list(JIPNC_LIST *list)
{
int i;
NODE *cur = list->head;
jms_print("-------------------------------------------------------\n");
jms_print("online num = %d\n",list->num);
for(i = 0;i < list->num;i++){
jms_print("id:%16s\tpic_static:%2d\tfd:%4d\n",cur->jipnc_id,cur->status,cur->fd);
cur = cur->next;
}
jms_print("\n\n");
}