diff --git a/src/client/libdistren.c b/src/client/libdistren.c --- a/src/client/libdistren.c +++ b/src/client/libdistren.c @@ -23,7 +23,9 @@ #include "common/config.h" #include "common/options.h" +#include "common/protocol.h" #include "common/remoteio.h" +#include "common/request.h" #include "libdistren.h" @@ -31,10 +33,18 @@ #include #include +/** + * Handle common cleanup actions for distren_init(). + */ +static void distren_init_cleanup(distren_t distren); + int distren_init(distren_t *handle) { int tmp; + struct distren_request *req; + void *data; + if(!handle) return 1; @@ -48,7 +58,7 @@ int distren_init(distren_t *handle) if(_distren_getoptions(*handle)) { fprintf(stderr, "error getting configuration\n"); - distren_free(*handle); + distren_init_cleanup(*handle); return 1; } @@ -62,18 +72,43 @@ int distren_init(distren_t *handle) { fprintf(stderr, "error: unable to connect to server\n"); - (*handle)->rem = NULL; - distren_free(*handle); + distren_init_cleanup(*handle); return 1; } - multiio_poll((*handle)->multiio, 2000); - multiio_poll((*handle)->multiio, 2000); - multiio_poll((*handle)->multiio, 2000); - + /* send off a DISTREN_REQUEST_VERSION to the server */ + tmp = distren_request_version(&req, &data, DISTREN_SERVERTYPE_CLIENT, PACKAGE_STRING); + if(tmp) + { + fprintf(stderr, "error: unable to allocate request"); + + distren_init_cleanup(*handle); + return 1; + } + distren_request_send((*handle)->rem, req, data); + distren_request_free_with_data(req, data); + + while((*handle)->rem + && (*handle)->state == DISTREN_STATE_VERSION) + multiio_poll((*handle)->multiio, 500); + + if(!(*handle)->rem) + { + distren_init_cleanup(*handle); + return 1; + } + return 0; } +static void distren_init_cleanup(distren_t distren) +{ + if(distren->rem) + remoteio_close(distren->rem); + distren->rem = NULL; + distren_free(distren); +} + /** * @todo Stub */