master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24/* <DESC>
25 * A multi-threaded application that uses a progress bar to show
26 * status. It uses Gtk+ to make a smooth pulse.
27 * </DESC>
28 */
29/*
30 * Written by Jud Bishop after studying the other examples provided with
31 * libcurl.
32 *
33 * To compile (on a single line):
34 * gcc -ggdb `pkg-config --cflags --libs gtk+-2.0` -lcurl -lssl -lcrypto
35 * -lgthread-2.0 -dl smooth-gtk-thread.c -o smooth-gtk-thread
36 */
37#include <stdio.h>
38#include <gtk/gtk.h>
39#include <glib.h>
40#include <unistd.h>
41#include <pthread.h>
43#include <curl/curl.h>
45#define NUMT 4
47static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
48static int j = 0;
49static gint num_urls = 9; /* make sure this is less than urls[] */
50static const char * const urls[] = {
51 "90022",
52 "90023",
53 "90024",
54 "90025",
55 "90026",
56 "90027",
57 "90028",
58 "90029",
59 "90030"
60};
62static size_t write_cb(void *ptr, size_t size, size_t nmemb, FILE *stream)
63{
64 return fwrite(ptr, size, nmemb, stream);
65}
67static void run_one(const gchar *http, int j)
68{
69 CURL *curl;
71 curl = curl_easy_init();
72 if(curl) {
73 printf("j = %d\n", j);
75 FILE *outfile = fopen(urls[j], "wb");
76 if(outfile) {
77 /* Set the URL and transfer type */
78 curl_easy_setopt(curl, CURLOPT_URL, http);
80 /* Write to the file */
81 curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
82 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
83 (void)curl_easy_perform(curl);
85 fclose(outfile);
86 }
87 curl_easy_cleanup(curl);
88 }
89}
91static void *pull_one_url(void *NaN)
92{
93 /* protect the reading and increasing of 'j' with a mutex */
94 pthread_mutex_lock(&lock);
95 while(j < num_urls) {
96 gchar *http;
97 int i = j;
98 j++;
99 pthread_mutex_unlock(&lock);
100 http = g_strdup_printf("https://example.com/%s", urls[i]);
101 if(http) {
102 run_one(http, i);
103 g_free(http);
104 }
105 pthread_mutex_lock(&lock);
106 }
107 pthread_mutex_unlock(&lock);
108 return NULL;
109}
111static gboolean pulse_bar(gpointer data)
112{
113 gdk_threads_enter();
114 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(data));
115 gdk_threads_leave();
117 /* Return true so the function is called again; returning false removes this
118 * timeout function.
119 */
120 return TRUE;
121}
123static void *create_thread(void *progress_bar)
124{
125 pthread_t tid[NUMT];
126 int i;
128 /* Make sure I do not create more threads than URLs. */
129 for(i = 0; i < NUMT && i < num_urls; i++) {
130 int error = pthread_create(&tid[i],
131 NULL, /* default attributes please */
132 pull_one_url,
133 NULL);
134 if(error)
135 fprintf(stderr, "Could not run thread number %d, errno %d\n", i, error);
136 else
137 fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
138 }
140 /* Wait for all threads to terminate. */
141 for(i = 0; i < NUMT && i < num_urls; i++) {
142 pthread_join(tid[i], NULL);
143 fprintf(stderr, "Thread %d terminated\n", i);
144 }
146 /* This stops the pulsing if you have it turned on in the progress bar
147 section */
148 g_source_remove(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(progress_bar),
149 "pulse_id")));
151 /* This destroys the progress bar */
152 gtk_widget_destroy(progress_bar);
154 /* [Un]Comment this out to kill the program rather than pushing close. */
155#if 0
156 gtk_main_quit();
157#endif
159 return NULL;
160}
162static gboolean cb_delete(GtkWidget *window, gpointer data)
163{
164 gtk_main_quit();
165 return FALSE;
166}
168int main(int argc, const char *argv[])
169{
170 GtkWidget *top_window, *outside_frame, *inside_frame, *progress_bar;
172 /* Must initialize libcurl before any threads are started */
173 CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
174 if(result != CURLE_OK)
175 return (int)result;
177 /* Init thread */
178 g_thread_init(NULL);
179 gdk_threads_init();
180 gdk_threads_enter();
182 gtk_init(&argc, &argv);
184 /* Base window */
185 top_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
187 /* Frame */
188 outside_frame = gtk_frame_new(NULL);
189 gtk_frame_set_shadow_type(GTK_FRAME(outside_frame), GTK_SHADOW_OUT);
190 gtk_container_add(GTK_CONTAINER(top_window), outside_frame);
192 /* Frame */
193 inside_frame = gtk_frame_new(NULL);
194 gtk_frame_set_shadow_type(GTK_FRAME(inside_frame), GTK_SHADOW_IN);
195 gtk_container_set_border_width(GTK_CONTAINER(inside_frame), 5);
196 gtk_container_add(GTK_CONTAINER(outside_frame), inside_frame);
198 /* Progress bar */
199 progress_bar = gtk_progress_bar_new();
200 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(progress_bar));
201 /* Make uniform pulsing */
202 gint pulse_ref = g_timeout_add(300, pulse_bar, progress_bar);
203 g_object_set_data(G_OBJECT(progress_bar), "pulse_id",
204 GINT_TO_POINTER(pulse_ref));
205 gtk_container_add(GTK_CONTAINER(inside_frame), progress_bar);
207 gtk_widget_show_all(top_window);
208 printf("gtk_widget_show_all\n");
210 g_signal_connect(G_OBJECT(top_window), "delete-event",
211 G_CALLBACK(cb_delete), NULL);
213 if(!g_thread_create(&create_thread, progress_bar, FALSE, NULL))
214 g_warning("cannot create the thread");
216 gtk_main();
217 gdk_threads_leave();
218 printf("gdk_threads_leave\n");
220 curl_global_cleanup();
222 return 0;
223}
Diagnostics4 git calls · 30 ms
CallStatusSizeTime030 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit5 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.6 KiB · hit21 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=21 git.response.bytes=2617 git.response.cache=HIT git.response.duration_ms=21 git.fetch.wants=cca3be738bc90ac4121921d7cd5830036252ee84 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2617 git.pkt.payload.max=2596 git.sideband.band1.bytes=2595 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e1a388cb8e5e44
- Request
- 90528c94…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format