#include <string.h>
#include "ftp.h"
#include <math.h>
+#include <time.h>
#include <pthread.h>
#include "metarinfo.h"
void ftpData(void *userData, const char *data, int len)
{
-
+ if(userData) {} // do nothing to make stupid warning go away
if (len <= 0)
return;
}
}
+extern unsigned int sleep(unsigned int);
void *fetch_ftp( ) {
// try three times if it fails
/* this happens too often, so i'll leave it commented fprintf(stderr, "METAR update failed for some reason, retry %i\n", tries); */
sleep(15); /* give it some time in case the server is busy or down */
}
+
tries++;
if ( strlen(metar_station) != 8 ){
- fprintf(stderr,"You didn't supply a valid station code\n");
- return;
+ ERR("You didn't supply a valid METAR station code\n");
+ return NULL;
}
if (metar_server == NULL)
metar_server = strdup("weather.noaa.gov");
initFtp();
res = connectFtp(metar_server, 0);
if (res < 0) {
- fprintf(stderr, "Couldn't connect to %s\n", metar_server);
- return;
+ ERR("Couldn't connect to %s\n", metar_server);
+ return NULL;
}
res = changeFtpDirectory(metar_path);
if (res < 0) {
- fprintf(stderr, "Metar update failed (couldn't CWD to %s)\n", metar_path);
+ ERR("Metar update failed (couldn't CWD to %s)\n", metar_path);
disconnectFtp();
- return;
+ return NULL;
}
if (res == 0) {
- fprintf(stderr,
- "Metar update failed\n");
- return;
+ ERR("Metar update failed\n");
+ return NULL;
}
if (getFtp(ftpData, NULL, metar_station) < 0) {
- fprintf(stderr, "Failed to get file %s\n", metar_station);
+ ERR("Failed to get file %s\n", metar_station);
}
disconnectFtp();
}
else {
ftp_ok = 0;
- }
+ }
} while (ftp_ok == 0 && tries < 3);
status = 1;
+ return NULL;
}
static pthread_t thread1;
void update_metar() {
int iret1;
if (!status) {
- status = 2;
+ status = 2;
iret1 = pthread_create( &thread1, NULL, fetch_ftp, NULL);
}
else if (status == 2) { /* thread is still running. what else can we do? */