static int client_init(char * remotehost, char * port) { int result = 1; int ret, sock; struct addrinfo * ai; struct addrinfo hints; memset (&hints, '\0', sizeof(hints)); hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; if(ret = getaddrinfo (remotehost, port, &hints, &ai)) printf_die(stderr, "[ERROR] getaddrinfo('%s', '%s'): %s\n", EXIT_FAILURE, remotehost, port, gai_strerror(ret)); if(!ai) printf_die(stderr, "[ERROR] getaddrinf(): couldn't fill the struct!\n", EXIT_FAILURE); struct addrinfo *runp = ai; while (runp) { sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol); if (sock != -1) { ndprintf(stdout, "[NETWORK] Connecting to server...\n"); if(connect (sock, runp->ai_addr, runp->ai_addrlen) == 0) { ndprintf(stdout, "[NETWORK] connected to server\n"); serverfd = sock; result = get_resp_value(0); break; } close(sock); } runp = runp->ai_next; } freeaddrinfo(ai); return result; }
int speed() { int ret; ret = sockwrite(serverfd, SPEED, NULL); return get_resp_value(ret); }
int loc_y() { int ret; ret = sockwrite(serverfd, LOC_Y, NULL); return get_resp_value(ret); }
void drive (int degree,int speed) { int ret; ret = sockwrite(serverfd, DRIVE, "%d %d", degree, speed); get_resp_value(ret); }
int damage() { int ret; ret = sockwrite(serverfd, DAMAGE, NULL); return get_resp_value(ret); }
void set_name(char *name) { int ret; ret = sockwrite(serverfd, NAME, "%.14s", name); get_resp_value(ret); }
int cannon (int degree,int range) { int ret; ret = sockwrite(serverfd, CANNON, "%d %d", degree, range); return get_resp_value(ret); }
int elapsed(void) { int ret; ret = sockwrite(serverfd, ELAPSED, NULL); return get_resp_value(ret); }
int loc_x(void) { int ret; ret = sockwrite(serverfd, LOC_X, NULL); return get_resp_value(ret); }
void cycle(void) { int ret; ret = sockwrite(serverfd, CYCLE, NULL); get_resp_value(ret); }
int scan(int degree, int resolution) { int ret; ret = sockwrite(serverfd, SCAN, "%d %d", degree, resolution); return get_resp_value(ret); }
void start(void) { int ret; ret = sockwrite(serverfd, START, NULL); get_resp_value(ret); }
int scan (int degree,int resolution) { int ret; /* Send the command */ ret = sockwrite(serverfd, SCAN, "%d %d", degree, resolution); /* Retrieve response : TODO ... */ return get_resp_value(ret); }
int image(char *path) { FILE *f; void *data; long fsize; int ret = 0; f = fopen(path, "rb"); if (!f) { ndprintf(stderr, "[WARNING] Cannot find file %s\n", path); return 0; } fseek(f, 0, SEEK_END); fsize = ftell(f); if (fsize < 0 || fsize > MAX_IMAGE_BYTES) { ndprintf(stderr, "[WARNING] File %s longer than %d bytes\n", path, MAX_IMAGE_BYTES); goto out_close; } rewind(f); data = malloc(fsize); if (!data) { ndprintf(stderr, "[WARNING] Cannot allocate memory\n"); goto out_close; } if (fread(data, 1, fsize, f) != (size_t)fsize) { ndprintf(stderr, "[WARNING] Error reading from %s\n", path); goto out_free; } ret = sockwrite(serverfd, IMAGE, "%ld", fsize); get_resp_value(ret); ret = write(serverfd, data, fsize); get_resp_value(ret); out_free: free(data); out_close: fclose(f); return ret; }