void send_list(char* ip) { int skt = sbp_connect(ip, CNODE_SERVICE_PORT); u64_t blnum = getlist(); struct sbpfs_head head; char* head_data; int head_data_len; head.data = NULL; head.entry_num = 0; head.title = PROTOCOL; char block_num_s[32]; sprintf(block_num_s, "%lld", blnum); char c_len[32]; sprintf(c_len, "%lld", blnum * 8); char dnodename[38]; sprintf(dnodename, "%s%s", "DNode_", dname); mkent(head,CONTENT_LEN,c_len); mkent(head,USER,dnodename); mkent(head,"BlockNum",block_num_s); if (make_head(&head_data, &head_data_len, &head) == -1) { printf("mkhead failed\n"); free(head_data); return; } if (sbp_send(skt, head_data, head_data_len) < 0) { printf("send head err failed\n"); sbp_perror("a"); perror("a"); free(head_data); return; } if (sbp_send(skt, (char*) blolist, bnum * 8) < 0) { printf("send head err failed\n"); sbp_perror("a"); perror("a"); free(head_data); return; } printf("sendlist succeed\n"); if (get_reply(skt) < 0) { printf("error sendlist \n"); return; } printf("reply succeed\n"); }
int send_copy(char*ip, u64_t blocknum) { struct sbpfs_head head; char* head_data; int head_data_len; char* data = (char*) malloc(1024 * 1024 * 16); if (data == NULL) { printf("malloc data failed\n"); return -1; } int succeed = readblock(blocknum, 0, 1024 * 1024 * 16, data); printf("succeed: %d\n", succeed); int skt = sbp_connect(ip, DNODE_SERVICE_PORT); printf("%d", skt); if (succeed < 0) send_err(skt, "copy_block", "cannot read block"); else { printf("send start\n"); head.data = NULL; head.entry_num = 0; head.title = REQUEST_OK; mkent(head,METHOD,"Copy"); mkent(head,ARGC,"3"); char blocknum_s[32]; sprintf(blocknum_s, "%lld", blocknum); mkent(head,"Arg0",blocknum_s); mkent(head,"Arg1","0"); char data_len_s[32]; sprintf(data_len_s, "%d", 1024 * 1024 * 16); mkent(head,"Arg2",data_len_s); char data_length_s[32]; sprintf(data_length_s, "%d", 1024 * 1024 * 16); mkent(head,CONTENT_LEN,data_length_s); if (make_head(&head_data, &head_data_len, &head) == -1) { printf("mkhead failed\n"); free(data); free(head_data); return -1; } if (sbp_send(skt, head_data, head_data_len) < 0) { printf("send head err failed\n"); sbp_perror("a"); perror("a"); free(data); free(head_data); return -1; } if (sbp_send(skt, data, 1024 * 1024 * 16) < 0) { printf("send data err failed\n"); sbp_perror("a"); perror("a"); free(data); free(head_data); return -1; } printf("send succeed\n"); free(data); free(head_data); return 0; } return 0; }
static s64_t write_block(struct block_entry* ents, int offset_in_ent_dnode, char* buf, char* auth_code) { s64_t ret = 0; struct sbpfs_head head; char* usr; char* pass; char* data; char* rec_data; char tran_usr[TRAN_USERNAME_LEN]; char tran_blocknum[32]; char tran_offset[32]; char tran_len[32]; if (sbp_getUandP(&usr, &pass) == -1) { printf("Can not get username and password\n"); return -1; } sprintf(tran_usr, "Client_%s", usr); s64_t sockfd; u64_t rec_len = 0; int data_len = 0; head.data = NULL; head.title = PROTOCOL; head.entry_num = 0; mkent(head,USER,tran_usr); mkent(head,PASS,pass); mkent(head,AUTH_CODE,auth_code); mkent(head,METHOD,"WRITE"); mkent(head,ARGC,"3"); bzero(tran_blocknum, 32); bzero(tran_offset, 32); bzero(tran_len, 32); sprintf(tran_blocknum, "%lld", ents->block_id); sprintf(tran_offset, "%d", ents->offset_in_block); sprintf(tran_len, "%d", ents->length_in_block); mkent(head,"Arg0",tran_blocknum); mkent(head,"Arg1",tran_offset); mkent(head,"Arg2",tran_len); mkent(head,CONTENT_LEN,tran_len); if (make_head(&data, &data_len, &head) == -1) { seterr(HEAD_ERR,MAKE_HEAD); goto err_exit; }/*data need free*/ if ((sockfd = sbp_connect(get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode]), DNODE_SERVICE_PORT)) < 0) { printf("connect ip: %s err\n", get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode])); seterr(SOCKET_ERR, EST_SOCK); goto err_exit1; } if (sbp_send(sockfd, data, data_len) < 0) { printf("send head to ip: %s err\n", get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode])); seterr(SOCKET_ERR, SEND); goto err_exit1; } if (sbp_send(sockfd, buf, ents->length_in_block) < 0) { printf("send data to ip: %s err\n", get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode])); seterr(SOCKET_ERR, SEND); goto err_exit1; } if ((sbp_recv(sockfd, &rec_data, &rec_len)) < 0) { printf("receive from ip: %s err\n", get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode])); seterr(SOCKET_ERR, RECV); goto err_exit1; } if (strlen(rec_data) == 0) { seterr(SOCKET_ERR,RECV); goto err_exit2; } if (decode_head(rec_data, rec_len, &head) == -1) { seterr("Data Error", "Can not decode SBPFS_HEAD"); goto err_exit2; }/*head need free*/ if (strncmp(head.title, REQUEST_OK, strlen(REQUEST_OK)) == 0) { ret = ents->length_in_block; free(data); free(rec_data); free_head(&head); // printf("succeeded write to %s block: %lld length:%d \n", // get_dnode_hostname(ents->d_nodes[offset_in_ent_dnode]), // ents->block_id, ents->length_in_block); } else if (strncmp(head.title, REQUEST_ERR, strlen(REQUEST_ERR)) == 0) { sbp_update_err(&head); goto err_exit3; } else { seterr(HEAD_ERR, UNKNOWN_HEAD); } goto ok_exit; err_exit3: free_head(&head); err_exit2: free(rec_data); err_exit1: free(data); err_exit: free(usr); free(pass); return -1; ok_exit: free(usr); free(pass); return ret; }