debug json before use it in socket, split socket src

This commit is contained in:
2025-09-16 20:10:37 +02:00
parent 8a4268945c
commit 12242987f3
14 changed files with 406 additions and 83 deletions
+14 -4
View File
@@ -1,3 +1,4 @@
#file: y_socket_t/Makefile
# lib: -lysocket
PROJECT_LIB=libysocket.so
CC=gcc
@@ -7,9 +8,10 @@ SOCDIR=$(PWD)
YLISTDIR=$(PWD)/../list_t
YWORKDIR=$(PWD)/../y_worker_t
YJSONDIR=$(PWD)/../yjson_t
INCLUDE=-I$(INCLUDE_DIRS) -I$(YLISTDIR)/src -I$(YWORKDIR)/include
INCLUDE=-I$(INCLUDE_DIRS) -I$(YLISTDIR)/src -I$(YWORKDIR)/include -I$(YJSONDIR)/src
CFLAGS=-g -lpthread -Wall -Werror -fpic $(INCLUDE) #"-D DEBUG=1"
#LDFLAGS=
@@ -20,7 +22,10 @@ YSOCKSRC_O=$(YSOCKSRC:.c=.o)
YNODESRC=$(PWD)/src/y_socket_t/y_node_t.c
YNODESRC_O=$(YNODESRC:.c=.o)
YFILEHANDLERSRC=$(PWD)/src/y_socket_t/y_file_handler.c
YY_STRINGSRC=$(PWD)/src/y_socket_t/y_list_string.c
YY_STRINGSRC_O=$(YY_STRINGSRC:.c=.o)
FILEHANDLERSRC=$(PWD)/src/y_socket_t/y_file_handler.c
YFILEHANDLERSRC_O=$(YFILEHANDLERSRC:.c=.o)
YLISTSRC=$(YLISTDIR)/src/list_t/list_t.c
@@ -29,14 +34,16 @@ YLISTSRC_O=$(YLISTSRC:.c=.o)
WORKSRC_0=$(YWORKDIR)/src/y_worker_t/y_worker_t.o
YTASKSRC_0=$(YWORKDIR)/src/y_worker_t/y_task_t.o
YJSONSRC=$(YJSONDIR)/src/json_t/json_t.c
YJSONSRC_O=$(YJSONSRC:.c=.o)
all: $(PROJECT_LIB)
TOPTARGETS := all clean
DEPS:=$(YLISTDIR) $(YWORKDIR)
DEPS:=$(YLISTDIR) $(YWORKDIR) $(YJSONDIR)
OBJ=$(YSOCKSRC_O) $(YNODESRC_O) $(YLISTSRC_O) $(WORKSRC_0) $(YTASKSRC_0) $(YFILEHANDLERSRC_O)
OBJ=$(YSOCKSRC_O) $(YNODESRC_O) $(YY_STRINGSRC_O) $(YLISTSRC_O) $(WORKSRC_0) $(YTASKSRC_0) $(YFILEHANDLERSRC_O) $(YJSONSRC_O)
$(TOPTARGETS): $(DEPS)
@@ -58,6 +65,9 @@ $(YSOCKSRC_O): $(YSOCKSRC) $(YNODESRC_O) $(YFILEHANDLERSRC_O)
$(YNODESRC_O): $(YNODESRC) $(YLISTSRC_O)
$(CC) -o $@ -c $< $(CFLAGS)
$(YY_STRINGSRC_O): $(YY_STRINGSRC) $(YLISTSRC_O)
$(CC) -o $@ -c $< $(CFLAGS)
$(YFILEHANDLERSRC_O): $(YFILEHANDLERSRC) $(YSOCKSRC_O)
$(CC) -o $@ -c $< $(CFLAGS)
@@ -0,0 +1,25 @@
/*file: include/y_socket_t/y_list_string.h */
#ifndef Y_PTR_STRING_T_H__C
#define Y_PTR_STRING_T_H__C
#include <string.h>
#include "list_t/list_t.h"
struct y_string{
char * buf;
size_t size;
};
typedef struct y_string * y_ptr_STRING;
struct y_string * create_y_ptr_STRING(const char *buf, size_t size);
GENERATE_LIST_ALL(y_ptr_STRING)
GEN_HEAD_PTR_LIST(y_ptr_STRING)
size_t total_size_list_y_ptr_STRING(struct main_list_y_ptr_STRING *mstr);
size_t copy_list_y_ptr_STRING_to_one_string(char **p_dst_str, struct main_list_y_ptr_STRING *mstr);
#endif /* Y_PTR_STRING_T_H__C */
+2
View File
@@ -1,3 +1,5 @@
/*file: include/y_socket_t/y_node_t.h */
#ifndef __Y_NODE_T_H__C
#define __Y_NODE_T_H__C
+10 -2
View File
@@ -1,3 +1,5 @@
/*file: include/y_socket_t/y_socket_t.h*/
#ifndef Y_SOCKET_T_H__C
#define Y_SOCKET_T_H__C
@@ -20,14 +22,16 @@
#include <errno.h>
#include <signal.h>
#include <string.h>
//#include "y_socket_t/y_socket_t.h"
#include "y_socket_t/y_node_t.h"
#include "y_socket_t/y_file_handler.h"
#include "y_worker_t/y_worker_t.h"
#include "y_worker_t/y_task_t.h"
#include "y_socket_t/y_list_string.h"
#include "json_t/json_t.h"
#define BUF_SIZE 500
#define TIMEOUT_MS 100
@@ -41,6 +45,8 @@ enum ipVersions{
extern const int af_array[nbIpVersion];//={AF_INET, AF_INET6};
/* y_ptr_STRING */
#if 0
struct y_string{
char * buf;
size_t size;
@@ -55,6 +61,8 @@ GEN_HEAD_PTR_LIST(y_ptr_STRING)
size_t total_size_list_y_ptr_STRING(struct main_list_y_ptr_STRING *mstr);
size_t copy_list_y_ptr_STRING_to_one_string(char **p_dst_str, struct main_list_y_ptr_STRING *mstr);
#endif /* y_ptr_STRING */
struct y_socket_t{
struct pollfd *fds;
size_t size_fds;
+13 -1
View File
@@ -1,3 +1,5 @@
/*file: src/y_socket_t/y_file_handler.c*/
#include "y_socket_t/y_file_handler.h"
//#include "y_socket_t/y_node_t.h"
@@ -122,7 +124,7 @@ void* y_socket_send_file_for_all_nodes(void* arg){
}
y_send_post_file_to_all_nodes(arg);
usleep(100);
usleep(1);
//for(struct list_y_NODE_T *local_list_current = nodes->begin_list; local_list_current; local_list_current=local_list_current->next )
//memset(buf_send, 0, BUF_SIZE+1);
@@ -208,7 +210,17 @@ void* y_socket_send_file_for_all_nodes(void* arg){
return NULL;
}
/*
struct arg_record_to_file{
int *is_file_to_record,
char * filename,
char *buf
};
void record_buffer_to_file(void *arg){
}
*/
void receve_from_node(struct pollfd *fds, char *msg, size_t count){
printf("\ndebug: <<<< receve_from_node %s %ld\n\n",msg,count);
char filename[500];
+63
View File
@@ -0,0 +1,63 @@
/*file: "src/y_socket_t/y_list_string.c" */
#include "y_socket_t/y_list_string.h"
struct y_string * create_y_ptr_STRING(const char *buf, size_t size){
struct y_string *string=malloc(sizeof(struct y_string));
string->buf=malloc(size+1);
if(buf){
//strncpy(string->buf, buf, size);
//snprintf(string->buf, size, "%s", buf);
memcpy(string->buf, buf, size);
//if(strlen(buf)>=size)
if(buf[size]!='\0')
string->buf[size]='\0';
// printf("\nDEBUG: ################\n%ld~~%s~~\n################\n",size,string->buf);
}
string->size=size;
return string;
}
GEN_LIST_ALL(y_ptr_STRING)
GEN_FUNC_PTR_LIST_FREE(y_ptr_STRING){
free(arg->buf);
free(arg);
}
size_t total_size_list_y_ptr_STRING(struct main_list_y_ptr_STRING *mstr){
size_t total_size=0;
for(struct list_y_ptr_STRING * local_current = mstr->begin_list; local_current; local_current = local_current->next){
total_size += local_current->value->size;
}
//printf("debug: totalsize :%ld\n",total_size);
return total_size;
}
size_t copy_list_y_ptr_STRING_to_one_string(char **p_dst_str, struct main_list_y_ptr_STRING *mstr){
if(*p_dst_str == NULL){
*p_dst_str=malloc(total_size_list_y_ptr_STRING(mstr));
}
char * dst_str = *p_dst_str;
char *cur_str = dst_str;
size_t local_size=0;
size_t count_size=0;
//for(move_current_to_begin_list_y_ptr_STRING(mstr); mstr->current_list; increment_list_y_ptr_STRING(mstr))
for(struct list_y_ptr_STRING * local_current = mstr->begin_list; local_current; local_current = local_current->next){
local_size = local_current->value->size;
//printf(">>debug: countsize :%ld/local=%ld |%s|(%s)\n",count_size,local_size,local_current->value->buf,dst_str);
//printf("debug: local_size :%ld\n",local_size);
for(size_t i=0; i<local_size; ++i){
cur_str[i]=local_current->value->buf[i];
}
count_size += local_size;
//printf("<<debug: countsize :%ld/local=%ld |%s|(%s)\n",count_size,local_size,cur_str,dst_str);
cur_str = dst_str + count_size;
}
*(dst_str + count_size - 1)='\0';
//printf("debug: |%s|\n", dst_str);
return count_size;
}
+2
View File
@@ -1,3 +1,5 @@
/*file: src/y_socket_t/y_node_t.c*/
#include "y_socket_t/y_node_t.h"
#include "y_socket_t/y_socket_t.h"
+128 -32
View File
@@ -1,7 +1,13 @@
/*file: src/y_socket_t/y_socket_t.c */
#include "y_socket_t/y_socket_t.h"
//#include "y_socket_t/y_list_string.h"
//#include "json_t/json_t.h"
const int af_array[nbIpVersion]={AF_INET, AF_INET6};
/* y_ptr_STRING */
#if 0
struct y_string * create_y_ptr_STRING(const char *buf, size_t size){
struct y_string *string=malloc(sizeof(struct y_string));
string->buf=malloc(size+1);
@@ -54,6 +60,8 @@ size_t copy_list_y_ptr_STRING_to_one_string(char **p_dst_str, struct main_list_y
}
#endif /* y_ptr_STRING */
struct y_socket_t * y_socket_create(char *port, size_t size_fds, int nb_workers){
struct y_socket_t *sock_temp=malloc(sizeof(struct y_socket_t));
if(size_fds>=nbIpVersion+1)
@@ -382,6 +390,116 @@ void* y_socket_handler_(void *arg){
return NULL;
}
void handle_input_kbd(char *buf, ssize_t buf_len ,void *arg){
struct y_socket_t * argSock = (struct y_socket_t*)arg;
struct pollfd *fds = argSock->fds;
y_NODE_T node;
int af, status;
/*
ssize_t nread, buf_len;
char buf[BUF_SIZE];
//struct main_list_y_ptr_STRING *m_str=create_var_list_y_ptr_STRING();
//printf("fd = %d\n event=%d\n\n",fds[1].fd,pollEventRec);
//fds[1].events = 0;
puts("Saisie du message : ");
memset(buf, 0, sizeof buf);
//scanf(" %"xstr(BUF_SIZE)"[^\n]%*c", buf);
buf_len = read(0,buf,BUF_SIZE);
*/
// printf("message saisi : %s\n len = %ld\n",buf, buf_len);
if(buf_len>6){
#if 1
char cmd[BUF_SIZE], dst_addr[BUF_SIZE];//, msg_buf[BUF_SIZE];
int index_buf=0, index_str=0;
for(; buf[index_buf]!=' '; ++index_buf){
cmd[index_str++]=buf[index_buf];
}
cmd[index_str]='\0';
// printf("debug : index_str= %d; cmd=[%s]\n",index_str, cmd);
index_str=0;
while(buf[index_buf]==' '){++index_buf;}
for(; buf[index_buf]!=' '; ++index_buf){
dst_addr[index_str++]=buf[index_buf];
}
dst_addr[index_str]='\0';
while(buf[index_buf]==' '){++index_buf;}
/*index_str=0;
for(; buf[index_buf]!='\n'; ++index_buf)
msg_buf[index_str++]=buf[index_buf];
msg_buf[index_str++]='\0';*/
// printf("debug : index_str=%d, dst_addr=[%s]\n", index_str, dst_addr);
#endif
if(strncmp(cmd, "sendto", 6)==0){
// printf("debug : sendto match, dst_addr=[%s]\n", dst_addr);
if(set_addr_y_NODE_T(&node, dst_addr)){
// printf("debug : set_addr_y_NODE_T done\n");
set_str_port_y_NODE_T(&node, argSock->port);
update_nodes(node, argSock->nodes);
af=(node.addr.ss_family == AF_INET6);
printf("debug : af = AF_INET=%d, af = AF_INET6=%d, vs af=[%d]\n",AF_INET, AF_INET6, af);
if(sendto(fds[af].fd, buf+index_buf , buf_len-index_buf,
// msg_buf, index_str,
0,
(struct sockaddr*)(&(node.addr)), node.addr_len) == -1){
printf("message erreur sendto : %s\n\n",buf);
perror("sendto:");
close(fds[af].fd);
return ;//NULL;
}
char dddnn[56];
put_y_NODE_T_in_string(&node, dddnn);
printf("debug: sendto : %s: msg :%s\n\n",dddnn, buf+index_buf);
}
}
}
}
void handle_buf_socket_rec(char *temp_all_buf, y_NODE_T node, struct main_list_ptr_y_WORKER_T * workers, struct argExecTasQ *argx, struct main_list_TYPE_PTR * list_arg, void * arg){
struct y_socket_t * argSock = (struct y_socket_t*)arg;
struct pollfd *fds = argSock->fds;
if(strncmp(temp_all_buf,"update standby",14)==0){
//pthread_mutex_lock(sock->mut_go_on);
//sock->go_on = 0;
//pthread_mutex_unlock(sock->mut_go_on);
standby_all_workers(workers->begin_list->value->arg);
// printf("debug: kill_all\n");
}
else if(strncmp(temp_all_buf,"update wakeup",13)==0){
//pthread_mutex_lock(sock->mut_go_on);
//sock->go_on = 0;
//pthread_mutex_unlock(sock->mut_go_on);
wakeup_all_workers(workers->begin_list->value->arg);
// printf("debug: kill_all\n");
}
else{
struct arg_handler_ *ptr_argHandl = malloc(sizeof(struct arg_handler_));
ptr_argHandl->buf = temp_all_buf;
ptr_argHandl->fds=fds;
ptr_argHandl->sock=argSock;
ptr_argHandl->node=node;
ptr_argHandl->argw=workers->begin_list->value->arg;
push_back_list_TYPE_PTR(list_arg, ptr_argHandl);
struct y_task_t task_handl = {
.func=y_socket_handler_,
.arg=ptr_argHandl,
.status=TASK_PENDING,
};
push_tasQ(argx->tasQ, task_handl);
}
}
void *y_socket_poll_fds(void *arg){
struct y_socket_t * argSock = (struct y_socket_t*)arg;
// // //
@@ -487,36 +605,8 @@ void *y_socket_poll_fds(void *arg){
copy_list_y_ptr_STRING_to_one_string(&temp_all_buf , m_str);
push_back_list_TYPE_PTR(list_arg, temp_all_buf);
if(strncmp(temp_all_buf,"update standby",14)==0){
//pthread_mutex_lock(sock->mut_go_on);
//sock->go_on = 0;
//pthread_mutex_unlock(sock->mut_go_on);
standby_all_workers(workers->begin_list->value->arg);
// printf("debug: kill_all\n");
}
else if(strncmp(temp_all_buf,"update wakeup",13)==0){
//pthread_mutex_lock(sock->mut_go_on);
//sock->go_on = 0;
//pthread_mutex_unlock(sock->mut_go_on);
wakeup_all_workers(workers->begin_list->value->arg);
// printf("debug: kill_all\n");
}
else{
struct arg_handler_ *ptr_argHandl = malloc(sizeof(struct arg_handler_));
ptr_argHandl->buf = temp_all_buf;
ptr_argHandl->fds=fds;
ptr_argHandl->sock=argSock;
ptr_argHandl->node=node;
ptr_argHandl->argw=workers->begin_list->value->arg;
push_back_list_TYPE_PTR(list_arg, ptr_argHandl);
struct y_task_t task_handl = {
.func=y_socket_handler_,
.arg=ptr_argHandl,
.status=TASK_PENDING,
};
push_tasQ(argx->tasQ, task_handl);
}
handle_buf_socket_rec(temp_all_buf, node, workers, argx, list_arg, arg);
///
//y_socket_handler_(temp_all_buf, fds, argSock);
@@ -526,13 +616,18 @@ void *y_socket_poll_fds(void *arg){
// stdin poll
if(fds[2].revents){// && POLLIN
//pollEventRec = fds[1].events;
//printf("fd = %d\n event=%d\n\n",fds[1].fd,pollEventRec);
//handle_input_kbd(arg);
//printf("fd = %d\n event=%d\n\n",fds[1].fd,pollEventRec);
//fds[1].events = 0;
puts("Saisie du message : ");
memset(buf, 0, sizeof buf);
//scanf(" %"xstr(BUF_SIZE)"[^\n]%*c", buf);
buf_len = read(0,buf,BUF_SIZE);
printf("message saisi : %s\n len = %ld\n",buf, buf_len);
// handle_input_kbd(buf, buf_len ,arg);
#if 1
if(buf_len>6){
#if 1
char cmd[BUF_SIZE], dst_addr[BUF_SIZE];//, msg_buf[BUF_SIZE];
@@ -587,7 +682,8 @@ void *y_socket_poll_fds(void *arg){
}
}
}
#endif
}
+10 -4
View File
@@ -1,4 +1,4 @@
#file: y_socket_t/test/Makefil
@@ -7,10 +7,11 @@ CC=gcc
YTESTDIR=$(PWD)/../../ytest_t
YLISTDIR=$(PWD)/../../list_t
YWORKDIR=$(PWD)/../../y_worker_t
YJSONDIR=$(PWD)/../../yjson_t
ROOT_DIR=$(PWD)/..
INCLUDE_DIR=$(ROOT_DIR)/include
CFLAGS=-I$(INCLUDE_DIR) -I$(YTESTDIR)/include_ytest/include -I$(YLISTDIR)/src -I$(YWORKDIR)/include
CFLAGS=-I$(INCLUDE_DIR) -I$(YTESTDIR)/include_ytest/include -I$(YLISTDIR)/src -I$(YWORKDIR)/include -I$(YJSONDIR)/src
LDFLAGS=-L$(YTESTDIR) -lytest -lpthread -lm -lOpenCL
#SRC_DIR=$(ROOT_DIR)/src
@@ -34,18 +35,23 @@ YFILEHANDLSRC_O=$(YFILEHANDLSRC:.c=.o)
YNODESRC=$(ROOT_DIR)/src/y_socket_t/y_node_t.c
YNODESRC_O=$(YNODESRC:.c=.o)
YY_STRINGSRC=$(ROOT_DIR)/src/y_socket_t/y_list_string.c
YY_STRINGSRC_O=$(YY_STRINGSRC:.c=.o)
YLISTSRC=$(YLISTDIR)/src/list_t/list_t.c
YLISTSRC_O=$(YLISTSRC:.c=.o)
YWORKSRC_0=$(YWORKDIR)/src/y_worker_t/y_worker_t.o
YTASKSRC_0=$(YWORKDIR)/src/y_worker_t/y_task_t.o
YJSONSRC=$(YJSONDIR)/src/json_t/json_t.c
YJSONSRC_O=$(YJSONSRC:.c=.o)
TOPTARGETS := all clean
DEPS := $(YTESTDIR) $(YLISTDIR) $(YWORKDIR) $(ROOT_DIR)
DEPS := $(YTESTDIR) $(YLISTDIR) $(YWORKDIR) $(ROOT_DIR) $(YJSONDIR)
OBJ=$(YSOCKSRC_O) $(YFILEHANDLSRC_O) $(YNODESRC_O) $(YLISTSRC_O) $(YWORKSRC_0) $(YTASKSRC_0)
OBJ=$(YSOCKSRC_O) $(YFILEHANDLSRC_O) $(YNODESRC_O) $(YY_STRINGSRC_O) $(YLISTSRC_O) $(YWORKSRC_0) $(YTASKSRC_0) $(YJSONSRC_O)
LIB_YTEST=$(YTESTDIR)/libytest.so