static int32_t callback_storage_upload(uint32_t offset, void** buf, int32_t size, void* context) { FileInfo* file_info = context; static uint8_t ubuf[2048]; if(size < 0) { printf("Storage Upload Error\n"); fflush(stdout); close(file_info->fd); free(file_info); printf("false\n"); rpc_disconnect(rpc); return 0; } if(size == 0) { printf("\nStorage Upload Completed\n"); fflush(stdout); struct timeval tv; gettimeofday(&tv, NULL); uint64_t current = tv.tv_sec * 1000 * 1000 + tv.tv_usec; printf("time = %0.3f ms\n", (float)(current - file_info->current_time) / (float)1000); close(file_info->fd); free(file_info); printf("true\n"); rpc_disconnect(rpc); return 0; } if(offset == 0) printf("Total Upload Size : %ld Byte\n", file_info->file_size); if(file_info->offset != offset) { lseek(file_info->fd, offset, SEEK_SET); file_info->offset = offset; } size = read(file_info->fd, ubuf, size > (int32_t)(file_info->file_size - file_info->offset) ? (int32_t)(file_info->file_size - file_info->offset) : size); if(size < 0) { printf("\nFile Read Error\n"); fflush(stdout); buf = NULL; rpc_disconnect(rpc); return -1; } file_info->offset += size; *buf = ubuf; printf("."); fflush(stdout); return size; }
static int callback_storage_download(uint32_t offset, void* buf, int32_t size, void* context) { FileInfo* file_info = context; if(size < 0) { printf("Storage Download Error\n"); close(file_info->fd); printf("false\n"); remove(file_info->path); free(file_info); rpc_disconnect(rpc); return 0; } if(file_info->offset != offset) { lseek(file_info->fd, offset, SEEK_SET); file_info->offset = offset; } size = write(file_info->fd, buf, size); file_info->offset += size; if(size < 0) { printf("Write Error!\n"); close(file_info->fd); printf("false\n"); remove(file_info->path); free(file_info); rpc_disconnect(rpc); return 0; } if(size == 0) { printf("\nStorage Download Completed\n"); printf("Total Size : %d Byte\n", file_info->offset); close(file_info->fd); struct timeval tv; gettimeofday(&tv, NULL); uint64_t current = tv.tv_sec * 1000 * 1000 + tv.tv_usec; printf("time = %0.3f ms\n", (float)(current - file_info->current_time) / (float)1000); printf("true\n"); free(file_info); rpc_disconnect(rpc); return 0; } printf("."); fflush(stdout); return size; }
int main(int argc, char *argv[]) { rpc_init(); RPCSession* session = rpc_session(); if(!session) { printf("RPC server not connected\n"); return ERROR_RPC_DISCONNECTED; } rpc = rpc_connect(session->host, session->port, 3, true); if(!rpc) { printf("Failed to connect RPC server\n"); return ERROR_RPC_DISCONNECTED; } int rc; if((rc = request_md5(argc, argv))) { printf("Failed to get VM storage md5 checksum. Error code : %d\n", rc); rpc_disconnect(rpc); return ERROR_CMD_EXECUTE; } while(1) { // Wait for response if(rpc_connected(rpc)) { rpc_loop(rpc); } else { free(rpc); break; } } return 0; }
int main(int argc, char *argv[]) { rpc_init(); RPCSession* session = rpc_session(); if(!session) { printf("RPC server not connected\n"); return ERROR_RPC_DISCONNECTED; } rpc = rpc_connect(session->host, session->port, 3, true); if(rpc == NULL) { printf("Failed to connect RPC server\n"); return ERROR_RPC_DISCONNECTED; } int rc; if((rc = download(argc, argv))) { printf("Failed to download file. Error code : %d\n", rc); rpc_disconnect(rpc); return ERROR_CMD_EXECUTE; } while(1) { if(rpc_connected(rpc)) { rpc_loop(rpc); } else { free(rpc); break; } } }
static bool callback_vm_create(uint32_t id, void* context) { if(id == 0) printf("Fail\n"); else printf("%d\n", id); rpc_disconnect(rpc); return false; }
static bool response_md5(bool result, uint32_t* md5, void* context) { // vm_md5_callback() implementation if(!result || !md5) printf("Failed to get VM storage md5 checksum\n"); else { // Print MD5 checksum uint8_t* md5_raw = (uint8_t*)md5; for(int i = 0; i < 16; ++i) printf("%02x", md5_raw[i]); puts(""); } rpc_disconnect(rpc); return false; }
static void free_server(struct server *server) { if (server->rpc) { rpc_disconnect(server->rpc, NULL); rpc_destroy_context(server->rpc); server->rpc = NULL; } if (server->read_event) { event_free(server->read_event); server->read_event = NULL; } if (server->write_event) { event_free(server->write_event); server->write_event = NULL; } free(server); }