y_socket: add post file and receive file

This commit is contained in:
2025-09-02 22:18:37 +02:00
parent 1739a3d8a3
commit c1b52ca658
2 changed files with 104 additions and 6 deletions
+82 -4
View File
@@ -1,5 +1,7 @@
#include "y_socket_t/y_file_handler.h" #include "y_socket_t/y_file_handler.h"
//#include "y_socket_t/y_node_t.h"
void fileNameDateScore(char* filename, char * pre, char* post,size_t score){ void fileNameDateScore(char* filename, char * pre, char* post,size_t score){
@@ -19,7 +21,70 @@ struct arg_send_file{
char * filename; char * filename;
}; };
#endif #endif
/* */
void y_send_post_file_to_all_nodes(void *arg){
struct arg_send_file *argS=(struct arg_send_file*)arg;
struct pollfd *fds=argS->fds;
struct main_list_y_NODE_T *nodes=argS->nodes;
char * filename=argS->filename;
#if TEMP_ADDR
char tempAddr[BUF_SIZE+1];
#endif
int c_af;
// char host[NI_MAXHOST], service[NI_MAXSERV];
char buf_send[BUF_SIZE+1];
int fd_file;
int retsprintf = sprintf(buf_send,"post file %s", filename );
for(struct list_y_NODE_T *local_list_current = nodes->begin_list; local_list_current; local_list_current=local_list_current->next ){
//memset(tempAddr, 0, BUF_SIZE+1);
c_af=(local_list_current->value).addr.ss_family;
#if TEMP_ADDR
if(c_af==AF_INET){
if(NULL == inet_ntop(c_af,
&(GET_IN_type_ADDR(&(local_list_current->value),)),
tempAddr, BUF_SIZE/*(argSock->local_list_current->value).addr_len*/)){
fprintf(stderr, "error inet_ntop v4\n");
}
}else if(c_af==AF_INET6){
if(NULL == inet_ntop(c_af,
&(GET_IN_type_ADDR(&(local_list_current->value),6)),
tempAddr, BUF_SIZE /*(argSock->local_list_current->value).addr_len*/)){
fprintf(stderr, "error inet_ntop v6 :errno=%d\n",errno);
}
}
#endif
#if 0
off_t offset = 0;
ssize_t ret_sendfile ;
while((ret_sendfile = sendfile(fds[(c_af==AF_INET6)].fd ,fd_file, &offset, BUF_SIZE))>0){
}
#endif
/// printf("debug: destination %s :\n",tempAddr);
if(sendto(fds[(c_af==AF_INET6)].fd,
buf_send, retsprintf,
/*msgRet, len_msgRet,*/
0,
(struct sockaddr*)&((local_list_current->value).addr),
(local_list_current->value).addr_len) !=
retsprintf
/*len_msgRet*/
){
#if TEMP_ADDR
fprintf(stderr, "Error sending response to %s\n",tempAddr);
#endif
}else{
#if TEMP_ADDR
printf("debug: sending response to < %s >",tempAddr);
#endif
}
}
}
/* */
#define TEMP_ADDR 1 #define TEMP_ADDR 1
//void y_socket_send_file_for_all_nodes(struct pollfd *fds, struct main_list_y_NODE_T *nodes, char * filename) //void y_socket_send_file_for_all_nodes(struct pollfd *fds, struct main_list_y_NODE_T *nodes, char * filename)
@@ -55,6 +120,7 @@ void* y_socket_send_file_for_all_nodes(void* arg){
return NULL; return NULL;
} }
y_send_post_file_to_all_nodes(arg);
//memset(buf_send, 0, BUF_SIZE+1); //memset(buf_send, 0, BUF_SIZE+1);
while((retread = read(fd_file, buf_send, BUF_SIZE) ) > 0 ){ while((retread = read(fd_file, buf_send, BUF_SIZE) ) > 0 ){
@@ -122,20 +188,31 @@ void* y_socket_send_file_for_all_nodes(void* arg){
} }
void recevefromnode(){ void receve_from_node(struct pollfd *fds, char *msg, size_t count){
char filename[500];
int fd_file;
long int nread;
char buf[BUF_SIZE];
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len = sizeof(struct sockaddr_storage);
//update_nodes(y_NODE_T node, struct main_list_y_NODE_T *nodes);
fileNameDateScore(filename, "_",msg,count); fileNameDateScore(filename, "_",msg,count);
if((fd_file = open(filename, O_WRONLY | O_CREAT , if((fd_file = open(filename, O_WRONLY | O_CREAT ,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1){ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1){
fprintf(stderr,"erreur write %s\n",filename); fprintf(stderr,"erreur write %s\n",filename);
return NULL; return ; //NULL;
} }
memset(buf,0, BUF_SIZE); memset(buf,0, BUF_SIZE);
while((nread = recvfrom(fds[1].fd, buf, BUF_SIZE, 0, while((nread = recvfrom(fds[1].fd, buf, BUF_SIZE, 0,
(struct sockaddr *)&peer_addr, &peer_addr_len)) == BUF_SIZE){ (struct sockaddr *)&peer_addr, &peer_addr_len
//(struct sockaddr *)&(node.addr), &(node.addr_len)
)) == BUF_SIZE){
if(nread == -1) if(nread == -1)
{ {
fprintf(stderr,"error nread\n"); fprintf(stderr,"error nread\n");
} }
else { else {
printf("msg: %s\n",buf); printf("msg: %s\n",buf);
@@ -144,6 +221,7 @@ void recevefromnode(){
} }
printf("nread==%ld\n",nread); printf("nread==%ld\n",nread);
} }
if(nread >0 && nread <BUF_SIZE){ if(nread >0 && nread <BUF_SIZE){
printf("msg: %s\n",buf); printf("msg: %s\n",buf);
+21 -1
View File
@@ -351,7 +351,7 @@ void* y_socket_handler_(void *arg){
//y_socket_send_file_for_all_nodes(fds, nodes, filename) ; //y_socket_send_file_for_all_nodes(fds, nodes, filename) ;
} }
} }
if(strncmp(buf, "update", 6)==0){ else if(strncmp(buf, "update", 6)==0){
if(strncmp(buf+7,"kill",4)==0){ if(strncmp(buf+7,"kill",4)==0){
pthread_mutex_lock(sock->mut_go_on); pthread_mutex_lock(sock->mut_go_on);
sock->go_on = 0; sock->go_on = 0;
@@ -361,6 +361,26 @@ void* y_socket_handler_(void *arg){
} }
} }
else if(strncmp(buf, "post", 4)==0){
if(strncmp(buf+5,"file",4)==0){
char filename[BUF_SIZE];
strcpy(filename, buf + 9);
receve_from_node(fds, filename, strlen(filename));
/*
pthread_mutex_lock(sock->mut_go_on);
sock->go_on = 0;
pthread_mutex_unlock(sock->mut_go_on);
*/
// kill_all_workers(argw);
// printf("debug: kill_all\n");
}
}
return NULL; return NULL;
} }
void *y_socket_poll_fds(void *arg){ void *y_socket_poll_fds(void *arg){