From e5da3e49e161851790d35bc8b7267424dea73973 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Fri, 5 Feb 2010 13:31:12 +0000 Subject: [PATCH] Convert GisWms to use GisHttp --- src/data/gis-http.c | 17 +++++++--- src/data/gis-http.h | 8 ++++- src/data/gis-wms.c | 78 +++++++++++++-------------------------------- src/data/gis-wms.h | 16 +++++----- src/plugins/elev.c | 2 +- src/plugins/map.c | 2 +- src/plugins/sat.c | 2 +- src/tile-test.c | 4 +-- 8 files changed, 55 insertions(+), 74 deletions(-) diff --git a/src/data/gis-http.c b/src/data/gis-http.c index ad21baf..0b29c4d 100644 --- a/src/data/gis-http.c +++ b/src/data/gis-http.c @@ -24,11 +24,18 @@ GisHttp *gis_http_new(const gchar *prefix) { - GisHttp *http = g_new0(GisHttp, 1); - http->prefix = g_strdup(prefix); - http->soup = soup_session_sync_new(); - g_object_set(http->soup, "user-agent", PACKAGE_STRING, NULL); - return http; + GisHttp *self = g_new0(GisHttp, 1); + self->soup = soup_session_sync_new(); + self->prefix = g_strdup(prefix); + g_object_set(self->soup, "user-agent", PACKAGE_STRING, NULL); + return self; +} + +void gis_http_free(GisHttp *self) +{ + g_object_unref(self->soup); + g_free(self->prefix); + g_free(self); } /* For passing data to the chunck callback */ diff --git a/src/data/gis-http.h b/src/data/gis-http.h index 051df0b..c042a42 100644 --- a/src/data/gis-http.h +++ b/src/data/gis-http.h @@ -24,8 +24,8 @@ #include "gis-data.h" typedef struct _GisHttp { - gchar *prefix; SoupSession *soup; + gchar *prefix; } GisHttp; /** @@ -34,6 +34,12 @@ typedef struct _GisHttp { */ GisHttp *gis_http_new(const gchar *prefix); +/** + * Free data associated with the GisHttp + * @param http the GisHttp to free + */ +void gis_http_free(GisHttp *http); + /** * @param http GisHttp connection to use * @param uri The uri to fetch diff --git a/src/data/gis-wms.c b/src/data/gis-wms.c index defedb3..23bc3f7 100644 --- a/src/data/gis-wms.c +++ b/src/data/gis-wms.c @@ -63,6 +63,7 @@ #include #include "gis-wms.h" +#include "gis-http.h" static gchar *_make_uri(GisWms *wms, GisTile *tile) { @@ -89,75 +90,42 @@ static gchar *_make_uri(GisWms *wms, GisTile *tile) tile->edge.n); } -static void _soup_chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _file) +gchar *gis_wms_fetch(GisWms *self, GisTile *tile, GisCacheType mode, + GisChunkCallback callback, gpointer user_data) { - FILE *file = _file; - if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) { - g_warning("GisWms: soup_chunk_cb - soup failed with %d", message->status_code); - return; - } - goffset total = soup_message_headers_get_content_length(message->response_headers); - if (fwrite(chunk->data, chunk->length, 1, file) != 1) - g_warning("GisWms: soup_chunk_cb - eror writing data"); -} - -char *gis_wms_make_local(GisWms *self, GisTile *tile) -{ - /* Get file path */ - gchar *tile_path = gis_tile_get_path(tile); - gchar *path = g_strdup_printf("%s/%s/%s%s%s", - g_get_user_cache_dir(), PACKAGE, - self->cache_prefix, tile_path, self->cache_ext); - g_free(tile_path); - - /* Return if it already exists */ - if (g_file_test(path, G_FILE_TEST_EXISTS)) - return path; - - /* Open temp file for writing */ - gchar *tmp_path = g_strconcat(path, ".part", NULL); - gchar *dirname = g_path_get_dirname(tmp_path); - g_mkdir_with_parents(dirname, 0755); - g_free(dirname); - FILE *file = fopen(tmp_path, "a"); - - /* Download file */ - gchar *uri = _make_uri(self, tile); - g_debug("GisWms: make_local - fetching %s", uri); - SoupMessage *message = soup_message_new("GET", uri); - g_signal_connect(message, "got-chunk", G_CALLBACK(_soup_chunk_cb), file); - soup_message_headers_set_range(message->request_headers, ftell(file), -1); - int status = soup_session_send_message(self->soup, message); - if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) - g_warning("GisWms: make_local - soup failed with %d", message->status_code); + gchar *uri = _make_uri(self, tile); + gchar *tilep = gis_tile_get_path(tile); + gchar *local = g_strdup_printf("%s%s", tilep, self->extension); + gchar *path = gis_http_fetch(self->http, uri, local, + mode, callback, user_data); g_free(uri); - - /* Clean up */ - fclose(file); - rename(tmp_path, path); - g_free(tmp_path); + g_free(tilep); + g_free(local); return path; } GisWms *gis_wms_new( - gchar *uri_prefix, gchar *uri_layer, gchar *uri_format, - gchar *cache_prefix, gchar *cache_ext, - gint width, gint height) + const gchar *uri_prefix, const gchar *uri_layer, + const gchar *uri_format, const gchar *prefix, + const gchar *extension, gint width, gint height) { GisWms *self = g_new0(GisWms, 1); - self->uri_prefix = uri_prefix; - self->uri_layer = uri_layer; - self->uri_format = uri_format; - self->cache_prefix = cache_prefix; - self->cache_ext = cache_ext; + self->http = gis_http_new(prefix); + self->uri_prefix = g_strdup(uri_prefix); + self->uri_layer = g_strdup(uri_layer); + self->uri_format = g_strdup(uri_format); + self->extension = g_strdup(extension); self->width = width; self->height = height; - self->soup = soup_session_sync_new(); return self; } void gis_wms_free(GisWms *self) { - g_object_unref(self->soup); + gis_http_free(self->http); + g_free(self->uri_prefix); + g_free(self->uri_layer); + g_free(self->uri_format); + g_free(self->extension); g_free(self); } diff --git a/src/data/gis-wms.h b/src/data/gis-wms.h index 07547e4..1057341 100644 --- a/src/data/gis-wms.h +++ b/src/data/gis-wms.h @@ -19,28 +19,28 @@ #define __GIS_WMS_H__ #include -#include +#include "data/gis-http.h" #include "objects/gis-tile.h" typedef struct _GisWms { + GisHttp *http; gchar *uri_prefix; gchar *uri_layer; gchar *uri_format; - gchar *cache_prefix; - gchar *cache_ext; + gchar *extension; gint width; gint height; - SoupSession *soup; } GisWms; GisWms *gis_wms_new( - gchar *uri_prefix, gchar *uri_layer, gchar *uri_format, - gchar *cache_prefix, gchar *cache_ext, - gint width, gint height); + const gchar *uri_prefix, const gchar *uri_layer, + const gchar *uri_format, const gchar *prefix, + const gchar *extension, gint width, gint height); -char *gis_wms_make_local(GisWms *wms, GisTile *tile); +gchar *gis_wms_fetch(GisWms *wms, GisTile *tile, GisCacheType mode, + GisChunkCallback callback, gpointer user_data); void gis_wms_free(GisWms *self); diff --git a/src/plugins/elev.c b/src/plugins/elev.c index 65f3b82..2a61cde 100644 --- a/src/plugins/elev.c +++ b/src/plugins/elev.c @@ -195,7 +195,7 @@ static void _load_tile(GisTile *tile, gpointer _self) GisPluginElev *self = _self; struct _LoadTileData *load = g_new0(struct _LoadTileData, 1); - load->path = gis_wms_make_local(self->wms, tile); + load->path = gis_wms_fetch(self->wms, tile, GIS_ONCE, NULL, NULL); g_debug("GisPluginElev: _load_tile: %s", load->path); load->self = self; load->tile = tile; diff --git a/src/plugins/map.c b/src/plugins/map.c index 6c0b57d..27261f1 100644 --- a/src/plugins/map.c +++ b/src/plugins/map.c @@ -96,7 +96,7 @@ static void _load_tile(GisTile *tile, gpointer _self) { GisPluginMap *self = _self; g_debug("GisPluginMap: _load_tile start %p", g_thread_self()); - char *path = gis_wms_make_local(self->wms, tile); + char *path = gis_wms_fetch(self->wms, tile, GIS_ONCE, NULL, NULL); struct _LoadTileData *data = g_new0(struct _LoadTileData, 1); data->self = self; data->tile = tile; diff --git a/src/plugins/sat.c b/src/plugins/sat.c index 9ce831b..5a5208b 100644 --- a/src/plugins/sat.c +++ b/src/plugins/sat.c @@ -73,7 +73,7 @@ static void _load_tile(GisTile *tile, gpointer _self) { GisPluginSat *self = _self; g_debug("GisPluginSat: _load_tile start %p", g_thread_self()); - char *path = gis_wms_make_local(self->wms, tile); + char *path = gis_wms_fetch(self->wms, tile, GIS_ONCE, NULL, NULL); struct _LoadTileData *data = g_new0(struct _LoadTileData, 1); data->self = self; data->tile = tile; diff --git a/src/tile-test.c b/src/tile-test.c index 2151f24..66e105d 100644 --- a/src/tile-test.c +++ b/src/tile-test.c @@ -58,7 +58,7 @@ gpointer do_bmng_cache(gpointer _image) GisWms *bmng_wms = gis_wms_new( "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg", "bmng_test/", "jpg", 512, 256); - const char *path = gis_wms_make_local(bmng_wms, tile); + const char *path = gis_wms_fetch(bmng_wms, tile, GIS_ONCE, NULL, NULL); g_message("Loading bmng image: [%s]", path); GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL); @@ -84,7 +84,7 @@ gpointer do_osm_cache(gpointer _image) GisWms *osm_wms = gis_wms_new( "http://labs.metacarta.com/wms/vmap0", "basic", "image/png", "osm_test/", "png", 512, 256); - const char *path = gis_wms_make_local(osm_wms, tile); + const char *path = gis_wms_fetch(osm_wms, tile, GIS_ONCE, NULL, NULL); g_message("Loading osm image: [%s]", path); GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL); -- 2.43.2