1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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 http://curl.haxx.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 ***************************************************************************/
22/* This is a multi threaded application that uses a progress bar to show
23 * status.  It uses Gtk+ to make a smooth pulse.
24 *
25 * Written by Jud Bishop after studying the other examples provided with
26 * libcurl.
27 *
28 * To compile (on a single line):
29 * gcc -ggdb `pkg-config --cflags  --libs gtk+-2.0` -lcurl -lssl -lcrypto
30 *   -lgthread-2.0 -dl  smooth-gtk-thread.c -o smooth-gtk-thread
31 */
32
33#include <stdio.h>
34#include <gtk/gtk.h>
35#include <glib.h>
36#include <unistd.h>
37#include <pthread.h>
38
39#include <curl/curl.h>
40#include <curl/types.h> /* new for v7 */
41#include <curl/easy.h> /* new for v7 */
42
43#define NUMT 4
44
45pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
46int j = 0;
47gint num_urls = 9; /* Just make sure this is less than urls[]*/
48const char * const urls[]= {
49  "90022",
50  "90023",
51  "90024",
52  "90025",
53  "90026",
54  "90027",
55  "90028",
56  "90029",
57  "90030"
58};
59
60size_t write_file(void *ptr, size_t size, size_t nmemb, FILE *stream)
61{
62  /* printf("write_file\n"); */
63  return fwrite(ptr, size, nmemb, stream);
64}
65
66/* http://xoap.weather.com/weather/local/46214?cc=*&dayf=5&unit=i */
67void *pull_one_url(void *NaN)
68{
69  CURL *curl;
70  CURLcode res;
71  gchar *http;
72  FILE *outfile;
73
74  /* Stop threads from entering unless j is incremented */
75  pthread_mutex_lock(&lock);
76  while ( j < num_urls )
77  {
78    printf("j = %d\n", j);
79
80    http =
81      g_strdup_printf("xoap.weather.com/weather/local/%s?cc=*&dayf=5&unit=i\n",
82                      urls[j]);
83
84    printf( "http %s", http );
85
86    curl = curl_easy_init();
87    if(curl)
88    {
89
90      outfile = fopen(urls[j], "w");
91      /* printf("fopen\n"); */
92
93      /* Set the URL and transfer type */
94      curl_easy_setopt(curl, CURLOPT_URL, http);
95
96      /* Write to the file */
97      curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
98      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file);
99
100      j++;  /* critical line */
101      pthread_mutex_unlock(&lock);
102
103      res = curl_easy_perform(curl);
104
105      fclose(outfile);
106      printf("fclose\n");
107
108      curl_easy_cleanup(curl);
109    }
110    g_free (http);
111
112    /* Adds more latency, testing the mutex.*/
113    sleep(1);
114
115  } /* end while */
116  return NULL;
117}
118
119
120gboolean pulse_bar(gpointer data)
121{
122  gdk_threads_enter();
123  gtk_progress_bar_pulse (GTK_PROGRESS_BAR (data));
124  gdk_threads_leave();
125
126  /* Return true so the function will be called again;
127   * returning false removes this timeout function.
128   */
129  return TRUE;
130}
131
132void *create_thread(void *progress_bar)
133{
134  pthread_t tid[NUMT];
135  int i;
136  int error;
137
138  /* Make sure I don't create more threads than urls. */
139  for(i=0; i < NUMT && i < num_urls ; i++) {
140    error = pthread_create(&tid[i],
141                           NULL, /* default attributes please */
142                           pull_one_url,
143                           NULL);
144    if(0 != error)
145      fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
146    else
147      fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);
148  }
149
150  /* Wait for all threads to terminate. */
151  for(i=0; i < NUMT && i < num_urls; i++) {
152    error = pthread_join(tid[i], NULL);
153    fprintf(stderr, "Thread %d terminated\n", i);
154  }
155
156  /* This stops the pulsing if you have it turned on in the progress bar
157     section */
158  g_source_remove(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(progress_bar),
159                                                    "pulse_id")));
160
161  /* This destroys the progress bar */
162  gtk_widget_destroy(progress_bar);
163
164  /* [Un]Comment this out to kill the program rather than pushing close. */
165  /* gtk_main_quit(); */
166
167
168  return NULL;
169
170}
171
172static gboolean cb_delete(GtkWidget *window, gpointer data)
173{
174  gtk_main_quit();
175  return FALSE;
176}
177
178int main(int argc, char **argv)
179{
180  GtkWidget *top_window, *outside_frame, *inside_frame, *progress_bar;
181
182  /* Must initialize libcurl before any threads are started */
183  curl_global_init(CURL_GLOBAL_ALL);
184
185  /* Init thread */
186  g_thread_init(NULL);
187  gdk_threads_init ();
188  gdk_threads_enter ();
189
190  gtk_init(&argc, &argv);
191
192  /* Base window */
193  top_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
194
195  /* Frame */
196  outside_frame = gtk_frame_new(NULL);
197  gtk_frame_set_shadow_type(GTK_FRAME(outside_frame), GTK_SHADOW_OUT);
198  gtk_container_add(GTK_CONTAINER(top_window), outside_frame);
199
200  /* Frame */
201  inside_frame = gtk_frame_new(NULL);
202  gtk_frame_set_shadow_type(GTK_FRAME(inside_frame), GTK_SHADOW_IN);
203  gtk_container_set_border_width(GTK_CONTAINER(inside_frame), 5);
204  gtk_container_add(GTK_CONTAINER(outside_frame), inside_frame);
205
206  /* Progress bar */
207  progress_bar = gtk_progress_bar_new();
208  gtk_progress_bar_pulse (GTK_PROGRESS_BAR (progress_bar));
209  /* Make uniform pulsing */
210  gint pulse_ref = g_timeout_add (300, pulse_bar, progress_bar);
211  g_object_set_data(G_OBJECT(progress_bar), "pulse_id",
212                    GINT_TO_POINTER(pulse_ref));
213  gtk_container_add(GTK_CONTAINER(inside_frame), progress_bar);
214
215  gtk_widget_show_all(top_window);
216  printf("gtk_widget_show_all\n");
217
218  g_signal_connect(G_OBJECT (top_window), "delete-event",
219                   G_CALLBACK(cb_delete), NULL);
220
221  if (!g_thread_create(&create_thread, progress_bar, FALSE, NULL) != 0)
222    g_warning("can't create the thread");
223
224  gtk_main();
225  gdk_threads_leave();
226  printf("gdk_threads_leave\n");
227
228  return 0;
229}
230
231