Skip to content
Browse files

*** empty log message ***

  • Loading branch information...
1 parent d0606ee commit 45ff9eef1dfc44e2dfb644f0c9c74c50a154023d codebox_rob committed Mar 13, 2011
View
8 bmdb/src/upgrade.c
@@ -195,8 +195,10 @@ int convertAddrValues(){
sqlite3_finalize(stmtSelect);
struct BinaryAddress* curr = addrList;
+ struct BinaryAddress* next;
// Go through each of the BinaryAddress structs we populated earlier and run some SQL to convert the ad values to hex strings
while(curr != NULL){
+ next = curr->next;
sqlite3_bind_text(stmtUpdate, 1, curr->txtData, -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(stmtUpdate, 2, curr->binData, curr->binDataLength, SQLITE_TRANSIENT);
@@ -207,7 +209,10 @@ int convertAddrValues(){
}
sqlite3_reset(stmtUpdate);
- curr = curr->next;
+ free(curr->binData);
+ free(curr->txtData);
+ free(curr);
+ curr = next;
}
sqlite3_finalize(stmtUpdate);
@@ -362,3 +367,4 @@ static int upgrade7(){
return SUCCESS;
}
+
View
8 capture/src/sql.c
@@ -82,6 +82,8 @@ void setupDb(){
int updateDb(int dr, struct Data* diffList){
int status = SUCCESS;
+ beginTrans(FALSE);
+
// Insert all the Data structs into the d/b, stopping if there are any failures
while (diffList != NULL) {
status = insertDataPartial(dr, diffList);
@@ -90,6 +92,12 @@ int updateDb(int dr, struct Data* diffList){
}
diffList = diffList->next;
}
+
+ if (status == SUCCESS){
+ commitTrans();
+ } else {
+ rollbackTrans();
+ }
return status;
}
View
19 shared/src/alert.c
@@ -39,14 +39,14 @@ struct Alert* allocAlert(){
// Create an Alert struct on the heap
struct Alert* alert = (struct Alert*) malloc( sizeof( struct Alert ) );
- alert->id = 0;
- alert->name = NULL;
- alert->active = 0;
- alert->bound = NULL;
- alert->periods = NULL;
+ alert->id = 0;
+ alert->name = NULL;
+ alert->active = 0;
+ alert->bound = NULL;
+ alert->periods = NULL;
alert->direction = 0;
- alert->amount = 0;
- alert->next = NULL;
+ alert->amount = 0;
+ alert->next = NULL;
return alert;
}
@@ -187,6 +187,7 @@ struct DateCriteriaPart* makeDateCriteriaPart(char* txt){
return result;
} else {
+ free(result);
logMsg(LOG_ERR, "makeDateCriteriaPart argument was invalid relative part: %s", txt);
return NULL;
}
@@ -214,7 +215,11 @@ struct DateCriteriaPart* makeDateCriteriaPart(char* txt){
}
if (thisPart->val1 == BAD_NUM || thisPart->val2 == BAD_NUM || thisPart->val1 > thisPart->val2){
+ if (firstResult != NULL){
+ freeDateCriteriaPart(firstResult);
+ }
firstResult = NULL;
+ freeDateCriteriaPart(thisPart);
break;
}
View
5 shared/src/common.h
@@ -42,7 +42,7 @@
#define EOL "\n"
#endif
-#define VERSION "0.7.2"
+#define VERSION "0.7.3"
#define DB_VERSION 7
#ifdef _WIN32
@@ -223,8 +223,11 @@ time_t getCurrentYearForTs(time_t ts);
time_t getCurrentMonthForTs(time_t ts);
time_t getCurrentDayForTs(time_t ts);
time_t getNextYearForTs(time_t ts);
+time_t getNextLocalYearForTs(time_t ts);
time_t getNextMonthForTs(time_t ts);
+time_t getNextLocalMonthForTs(time_t ts);
time_t getNextDayForTs(time_t ts);
+time_t getNextLocalDayForTs(time_t ts);
time_t getNextHourForTs(time_t ts);
time_t getNextMinForTs(time_t ts);
time_t addToDate(time_t ts, char unit, int num);
View
105 shared/src/db.c
@@ -32,10 +32,10 @@
#include "common.h"
#define BUSY_WAIT_INTERVAL 30000
-#define SQL_SELECT_CONFIG "SELECT value FROM config WHERE key=?"
-#define SQL_INSERT_CONFIG "INSERT INTO config (key, value) VALUES (?, ?)"
-#define SQL_UPDATE_CONFIG "UPDATE config SET key=?, value=? WHERE key=?"
-#define SQL_DELETE_CONFIG "DELETE FROM config WHERE key=?"
+#define SQL_SELECT_CONFIG "SELECT value FROM config WHERE key=?"
+#define SQL_INSERT_CONFIG "INSERT INTO config (key, value) VALUES (?, ?)"
+#define SQL_UPDATE_CONFIG "UPDATE config SET key=?, value=? WHERE key=?"
+#define SQL_DELETE_CONFIG "DELETE FROM config WHERE key=?"
/*
Contains common database-handling routines.
@@ -55,6 +55,15 @@ void prepareSql(sqlite3_stmt **stmt, const char *sql){
}
}
+#ifndef MULTI_THREADED_CLIENT
+ struct StmtList{
+ char* sql;
+ sqlite3_stmt* stmt;
+ struct StmtList* next;
+ };
+ struct StmtList* stmtList = NULL;
+#endif
+
sqlite3* openDb(){
// Open the database file, exit if there is a problem
assert(!dbOpen);
@@ -88,7 +97,11 @@ sqlite3* openDb(){
dbOpen = TRUE;
setBusyWait(BUSY_WAIT_INTERVAL);
-
+
+ #ifndef MULTI_THREADED_CLIENT
+ stmtList = NULL;
+ #endif
+
return db;
}
@@ -130,56 +143,49 @@ void dbVersionCheck(){
#endif
#ifndef MULTI_THREADED_CLIENT
- struct StmtList{
- char* sql;
- sqlite3_stmt* stmt;
- struct StmtList* next;
- };
- struct StmtList* stmtList = NULL;
-
sqlite3_stmt *getStmt(char* sql){
- struct StmtList* list = stmtList;
- struct StmtList* match = NULL;
-
- // Check if we have a prepared stmt for this SQL already...
- while(list != NULL){
- if (strcmp(list->sql, sql) == 0){
- // Found one we made earlier
- match = list;
- break;
- } else {
- list = list->next;
- }
+ struct StmtList* list = stmtList;
+ struct StmtList* match = NULL;
+
+ // Check if we have a prepared stmt for this SQL already...
+ while(list != NULL){
+ if (strcmp(list->sql, sql) == 0){
+ // Found one we made earlier
+ match = list;
+ break;
+ } else {
+ list = list->next;
}
+ }
- if (match == NULL){
- // No ready-made stmt was found, so make one and store it for next time
- match = malloc(sizeof(struct StmtList));
- match->sql = strdup(sql);
-
- sqlite3_stmt* stmt;
- prepareSql(&stmt, sql);
- match->stmt = stmt;
- match->next = NULL;
-
- // Attach the new StmtList to the module-level variable
- if (stmtList == NULL){
- // First item in the list
- stmtList = match;
- } else {
- // Move to the end of the list...
- list = stmtList;
- while(list->next != NULL){
- list = list->next;
- }
- // ...and then add the new struct
- list->next = match;
+ if (match == NULL){
+ // No ready-made stmt was found, so make one and store it for next time
+ match = malloc(sizeof(struct StmtList));
+ match->sql = strdup(sql);
+
+ sqlite3_stmt* stmt;
+ prepareSql(&stmt, sql);
+ match->stmt = stmt;
+ match->next = NULL;
+
+ // Attach the new StmtList to the module-level variable
+ if (stmtList == NULL){
+ // First item in the list
+ stmtList = match;
+ } else {
+ // Move to the end of the list...
+ list = stmtList;
+ while(list->next != NULL){
+ list = list->next;
}
+ // ...and then add the new struct
+ list->next = match;
}
-
- return match->stmt;
}
+ return match->stmt;
+}
+
void finishedStmt(sqlite3_stmt* stmt){
sqlite3_reset(stmt);
}
@@ -396,6 +402,7 @@ int setConfigIntValue(char* key, int value){
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 2, value);
} else {
+ free(currentValue);
// Update the existing config row
stmt = getStmt(SQL_UPDATE_CONFIG);
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_TRANSIENT);
@@ -420,6 +427,7 @@ int setConfigTextValue(char* key, char* value){
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, value, -1, SQLITE_TRANSIENT);
} else {
+ free(currentValue);
// Update the existing config row
stmt = getStmt(SQL_UPDATE_CONFIG);
sqlite3_bind_text(stmt, 1, key, -1, SQLITE_TRANSIENT);
@@ -472,3 +480,4 @@ void closeDb(){
sqlite3_close(db);
dbOpen = FALSE;
}
+
View
43 shared/src/time.c
@@ -74,7 +74,7 @@ time_t getCurrentDayForTs(time_t ts){
}
time_t getNextYearForTs(time_t ts){
- // Returns a timestamp value representing the start of the year following the one in which 'ts' occurs
+ // Returns a timestamp value representing the start of the year following the one in which 'ts' occurs, calculated for the GMT timezone
struct tm *t = gmtime(&ts);
t->tm_sec = 0;
@@ -87,8 +87,21 @@ time_t getNextYearForTs(time_t ts){
return timegm(t);
}
+time_t getNextLocalYearForTs(time_t ts){
+ // Returns a timestamp value representing the start of the year following the one in which 'ts' occurs, calculated for the local timezone
+ struct tm *t = localtime(&ts);
+
+ t->tm_sec = 0;
+ t->tm_min = 0;
+ t->tm_hour = 0;
+ t->tm_mday = 1;
+ t->tm_mon = 0;
+ t->tm_year += 1;
+
+ return mktime(t);
+}
time_t getNextMonthForTs(time_t ts){
- // Returns a timestamp value representing the start of the month following the one in which 'ts' occurs
+ // Returns a timestamp value representing the start of the month following the one in which 'ts' occurs, calculated for the GMT timezone
struct tm *t = gmtime(&ts);
t->tm_sec = 0;
@@ -100,8 +113,20 @@ time_t getNextMonthForTs(time_t ts){
return timegm(t);
}
+time_t getNextLocalMonthForTs(time_t ts){
+ // Returns a timestamp value representing the start of the month following the one in which 'ts' occurs, calculated for the local timezone
+ struct tm *t = localtime(&ts);
+
+ t->tm_sec = 0;
+ t->tm_min = 0;
+ t->tm_hour = 0;
+ t->tm_mday = 1;
+ t->tm_mon += 1;
+
+ return mktime(t);
+}
time_t getNextDayForTs(time_t ts){
- // Returns a timestamp value representing the start of the day following the one in which 'ts' occurs
+ // Returns a timestamp value representing the start of the day following the one in which 'ts' occurs, calculated for the GMT timezone
struct tm *t = gmtime(&ts);
t->tm_sec = 0;
@@ -112,6 +137,18 @@ time_t getNextDayForTs(time_t ts){
return timegm(t);
}
+time_t getNextLocalDayForTs(time_t ts){
+ // Returns a timestamp value representing the start of the day following the one in which 'ts' occurs, calculated for the local timezone
+ struct tm *t = localtime(&ts);
+
+ t->tm_sec = 0;
+ t->tm_min = 0;
+ t->tm_hour = 0;
+ t->tm_mday += 1;
+
+ return mktime(t);
+}
+
time_t getNextHourForTs(time_t ts){
// Returns a timestamp value representing the start of the hour following the one in which 'ts' occurs
struct tm *t = gmtime(&ts);
View
12 shared_client/src/clientAlert.c
@@ -465,13 +465,13 @@ static struct DateCriteria* getIntervalForId(sqlite3_stmt *stmtSelectInterval, i
if (rc == SQLITE_ROW) {
// We found the requested interval, read out the values
- yearTxt = strdup(sqlite3_column_text(stmtSelectInterval, 0));
- monthTxt = strdup(sqlite3_column_text(stmtSelectInterval, 1));
- dayTxt = strdup(sqlite3_column_text(stmtSelectInterval, 2));
- weekdayTxt = strdup(sqlite3_column_text(stmtSelectInterval, 3));
- hourTxt = strdup(sqlite3_column_text(stmtSelectInterval, 4));
+ yearTxt = sqlite3_column_text(stmtSelectInterval, 0);
+ monthTxt = sqlite3_column_text(stmtSelectInterval, 1);
+ dayTxt = sqlite3_column_text(stmtSelectInterval, 2);
+ weekdayTxt = sqlite3_column_text(stmtSelectInterval, 3);
+ hourTxt = sqlite3_column_text(stmtSelectInterval, 4);
- result = makeDateCriteria(yearTxt, monthTxt, dayTxt, weekdayTxt, hourTxt);
+ result = makeDateCriteria(yearTxt, monthTxt, dayTxt, weekdayTxt, hourTxt);
} else if (rc != SQLITE_DONE) {
logMsg(LOG_ERR, "stmtSelectInterval failed: %d", rc);
View
7 shared_client/src/clientQuery.c
@@ -102,15 +102,15 @@ struct Data* getQueryValues(time_t tsFrom, time_t tsTo, int group, char* hs, cha
break;
case QUERY_GROUP_DAYS:
- result = doQuery(stmt, minFrom, maxTo, &getNextDayForTs, &addToDateD, hs, ad, bindQueryParams);
+ result = doQuery(stmt, minFrom, maxTo, &getNextLocalDayForTs, &addToDateD, hs, ad, bindQueryParams);
break;
case QUERY_GROUP_MONTHS:
- result = doQuery(stmt, minFrom, maxTo, &getNextMonthForTs, &addToDateM, hs, ad, bindQueryParams);
+ result = doQuery(stmt, minFrom, maxTo, &getNextLocalMonthForTs, &addToDateM, hs, ad, bindQueryParams);
break;
case QUERY_GROUP_YEARS:
- result = doQuery(stmt, minFrom, maxTo, &getNextYearForTs, &addToDateY, hs, ad, bindQueryParams);
+ result = doQuery(stmt, minFrom, maxTo, &getNextLocalYearForTs, &addToDateY, hs, ad, bindQueryParams);
break;
case QUERY_GROUP_TOTAL:
@@ -145,6 +145,7 @@ static struct Data* doQuery(sqlite3_stmt *stmt, time_t minFrom, time_t maxTo, ti
struct Data* current;
while(TRUE){
+
current = doQueryForInterval(stmt, from, to, hs, ad, bindQueryParams);
if (current->dl > 0 || current->ul > 0){
// Only return the struct if it contains some data
View
11 shared_client/src/clientSummary.c
@@ -108,10 +108,13 @@ void freeSummary(struct Summary* summary){
freeData(summary->year);
freeData(summary->total);
- int i;
- for(i=0; i<summary->hostCount; i++){
- free(summary->hostNames[i]);
- }
+ if (summary->hostNames != NULL){
+ int i;
+ for(i=0; i<summary->hostCount; i++){
+ free(summary->hostNames[i]);
+ }
+ free(summary->hostNames);
+ }
}
View
159 webserver/src/handleRss.c
@@ -44,13 +44,13 @@ struct NameValuePair* makeRssRequestValues();
#define DEFAULT_RSS_ITEM_COUNT 10
#define FEED_TITLE "BitMeter OS Events"
#define HTML_LINE_BREAK "&lt;br&gt;"
-
+
void processRssRequest(SOCKET fd, struct Request* req){
- /* The skeleton XML returned by the RSS feed is contained in the 'rss.xml' file in
+ /* The skeleton XML returned by the RSS feed is contained in the 'rss.xml' file in
the root of the web folder, the placeholder comments of the form <!--[something]-->
are replaced by values that we calculate below. */
struct NameValuePair* values = makeRssRequestValues();
-
+
processFileRequest(fd, req, values);
freeNameValuePairs(values);
}
@@ -63,36 +63,45 @@ struct NameValuePair* makeRssRequestValues(){
title = malloc(strlen(FEED_TITLE) + 3 + strlen(serverName) + 1);
sprintf(title, "%s - %s", FEED_TITLE, serverName);
} else {
- title = strdup(FEED_TITLE);
+ title = strdup(FEED_TITLE);
+ }
+ if (serverName != NULL){
+ free(serverName);
}
/* The feed needs a site url, if the url must work when accessed remotely then the hostname
must be specfied in the config table - check if it's there... */
char* rssHost = getConfigText(CONFIG_WEB_RSS_HOST, TRUE);
if ((rssHost == NULL) || (strlen(rssHost) == 0)){
- rssHost = "localhost"; // use 'localhost' as the default host name for the feed
+ if (rssHost != NULL){
+ free(rssHost);
+ }
+ rssHost = strdup("localhost"); // use 'localhost' as the default host name for the feed
}
int rssPort = getConfigInt(CONFIG_WEB_PORT, TRUE);
- if (rssPort < MIN_PORT || rssPort > MAX_PORT){
+ if (rssPort < MIN_PORT || rssPort > MAX_PORT){
rssPort = DEFAULT_PORT; // No custom port was specified
}
-
+
// Build a url for the site using the host and port
char rssUrl[7 + strlen(rssHost) + 1 + 5 + 1];
sprintf(rssUrl, "http://%s:%d", rssHost, rssPort);
-
+ if (rssHost != NULL){
+ free(rssHost);
+ }
+
// How often we publish (hourly or daily)
int rssFreq = getConfigInt(CONFIG_WEB_RSS_FREQ, TRUE);
if ((rssFreq != RSS_FREQ_DAILY) && (rssFreq != RSS_FREQ_HOURLY)){
rssFreq = RSS_FREQ_DAILY;
}
-
+
// How many items appear in the feed
int rssItemCount = getConfigInt(CONFIG_WEB_RSS_ITEMS, TRUE);
if (rssItemCount < 1){
rssItemCount = DEFAULT_RSS_ITEM_COUNT;
}
-
+
// Fill this array with the XML <item> elements that we will include in the feed
char* items[rssItemCount];
if (rssFreq == RSS_FREQ_HOURLY){
@@ -115,56 +124,57 @@ struct NameValuePair* makeRssRequestValues(){
itemTxt = items[j];
strcpy(itemsTxt + offset, itemTxt);
offset += strlen(itemTxt);
+ free(itemTxt);
}
itemsTxt[size] = 0;
-
+
// Publication date for the feed, in the correct format
char* pubDate = getPubDate(rssFreq);
-
+
// Refresh interval for the feed in minutes
char* ttlTxt = (rssFreq == RSS_FREQ_HOURLY) ? "60" : "1440";
-
+
// Textual frequency of updates
char* freqTxt = (rssFreq == RSS_FREQ_HOURLY) ? "hourly" : "daily";
-
+
// These 6 values get substituted into the XML from the static file
struct NameValuePair* pair = makeNameValuePair("title", title);
appendNameValuePair(&pair, makeNameValuePair("link", rssUrl));
appendNameValuePair(&pair, makeNameValuePair("pubdate", pubDate));
appendNameValuePair(&pair, makeNameValuePair("items", itemsTxt));
appendNameValuePair(&pair, makeNameValuePair("ttl", ttlTxt));
appendNameValuePair(&pair, makeNameValuePair("freq", freqTxt));
-
+
// We can free these because the values were copied in makeNameValuePair calls above
- free(title);
+ free(title);
free(pubDate);
free(itemsTxt);
-
+
return pair;
}
#ifdef _WIN32
// Timezone not formatted correctly on Windows using '%z' so we have to roll our own
static void getRfc822Time(struct tm* time, char* timeTxt){
char part1[48];
strftime(part1, 47, "%a, %d %b %Y %H:%M:%S", time);
-
+
TIME_ZONE_INFORMATION info;
int ret = GetTimeZoneInformation(&info);
-
+
int bias;
if (ret == TIME_ZONE_ID_STANDARD || ret == TIME_ZONE_ID_UNKNOWN){
bias = -info.StandardBias;
} else if (ret == TIME_ZONE_ID_DAYLIGHT){
bias = -info.DaylightBias;
} else {
bias = 0;
- //logMsg(LOG_ERR, "Unable to retrieve timezone information, rc=%d", ret);
+ //logMsg(LOG_ERR, "Unable to retrieve timezone information, rc=%d", ret);
}
int hh = bias / 60;
int mm = bias % 60;
int plus = hh >= 0;
-
- sprintf(timeTxt, "%s %c%02d%02d", part1, (plus ? '+' : '-'), abs(hh), mm);
+
+ sprintf(timeTxt, "%s %c%02d%02d", part1, (plus ? '+' : '-'), abs(hh), mm);
}
#endif
#ifndef _WIN32
@@ -173,62 +183,63 @@ struct NameValuePair* makeRssRequestValues(){
}
#endif
-static char* makeItem(char* title, char* amountDescription, char* alertDescription, struct tm* pubTime,
+static char* makeItem(char* title, char* amountDescription, char* alertDescription, struct tm* pubTime,
char* guid){
// Build the <item>...</item> XML for a single item in the feed
-
+
// Publication time for the item
char pubTimeTxt[48];
getRfc822Time(pubTime, pubTimeTxt);
-
+
if (alertDescription == NULL){
alertDescription = "";
}
-
+
// XML gets written into here
- char* itemTxt = malloc(6 + 7 + strlen(title) + 8 + 13 + strlen(amountDescription) +
- strlen(HTML_LINE_BREAK) + strlen(alertDescription) + 14 + 9 + strlen(pubTimeTxt) +
+ char* itemTxt = malloc(6 + 7 + strlen(title) + 8 + 13 + strlen(amountDescription) +
+ strlen(HTML_LINE_BREAK) + strlen(alertDescription) + 14 + 9 + strlen(pubTimeTxt) +
10 + 27 + strlen(guid) + 7 + 7 + 1);
- sprintf(itemTxt,
+ sprintf(itemTxt,
"<item>"
"<title>%s</title>"
"<description>%s%s%s</description>"
"<pubDate>%s</pubDate>"
"<guid isPermaLink=\"false\">%s</guid>"
- "</item>",
- title, amountDescription,
- strlen(alertDescription) > 0 ? HTML_LINE_BREAK : "",
+ "</item>",
+ title, amountDescription,
+ strlen(alertDescription) > 0 ? HTML_LINE_BREAK : "",
alertDescription, pubTimeTxt, guid);
+
return itemTxt;
}
static void getDailyItems(char** itemsTxt, int rssItemCount, char* rssUrl){
- /* Populate the itemsTxt array with the various XML strings, each string contains an
+ /* Populate the itemsTxt array with the various XML strings, each string contains an
item with information about upload/download volumes for a single day. */
struct tm toStruct = getLocalTime(getTime());
toStruct.tm_sec = 0;
toStruct.tm_min = 0;
toStruct.tm_hour = 0;
-
+
int count=0;
time_t from, to;
char itemTitle[24]; // The title of the item, which will contain just a date
char dlTxt[24]; // Download amount, formatted
char ulTxt[24]; // Upload amount, formatted
char descAmt[264]; // Descriptive text detailing the UL/DL amounts, for the body of the item
char descDate[64]; // Publication date for the item, in the correct format
-
+
while (count<rssItemCount){
// Calculate the upper/lower bounds that correspond to the day we are working on
to = mktime(&toStruct);
toStruct.tm_mday -= 1;
from = mktime(&toStruct);
-
+
// The title of the item will be the day from the start of the query range
strftime(itemTitle, 23, "%A %d %B", &toStruct);
-
+
// Run the query
struct Data* totals = getQueryValues(from, to, QUERY_GROUP_TOTAL, NULL, NULL);
if (totals == NULL){
@@ -237,19 +248,19 @@ static void getDailyItems(char** itemsTxt, int rssItemCount, char* rssUrl){
formatAmount(totals->dl, TRUE, TRUE, dlTxt);
formatAmount(totals->ul, TRUE, TRUE, ulTxt);
freeData(totals);
-
+
// The descriptive text also contains the date, in a more verbose format
strftime(descDate, 63, "%A %d %B %Y", &toStruct);
-
- /* Construct the descriptive text, its HTML but we must escape the entities so it
+
+ /* Construct the descriptive text, its HTML but we must escape the entities so it
doesn't break the XML structure */
sprintf(descAmt, "On %s totals were as follows:%sDownload: %s%sUpload: %s",
descDate, HTML_LINE_BREAK, dlTxt, HTML_LINE_BREAK, ulTxt);
-
+
// If there are any Alerts defined then get some text describing their status
char* descAlerts = getAlertsTxt(to);
-
- /* Build a unique ID (guid) to identify this item, this will be the same for a given
+
+ /* Build a unique ID (guid) to identify this item, this will be the same for a given
day every time the feed is requested, to ensure that aggregators don't display
duplicates */
char guid[strlen(rssUrl) + 32];
@@ -258,19 +269,24 @@ static void getDailyItems(char** itemsTxt, int rssItemCount, char* rssUrl){
toStruct.tm_mday += 1;
normaliseTm(&toStruct);
char* itemTxt = makeItem(itemTitle, descAmt, descAlerts, &toStruct, guid);
+
toStruct.tm_mday -= 1;
itemsTxt[count++] = itemTxt;
+
+ if (descAlerts != NULL){
+ free(descAlerts);
+ }
}
}
static void getHourlyItems(char** itemsTxt, int rssItemCount, char* rssUrl){
- /* Populate the itemsTxt array with the various XML strings, each string contains an
+ /* Populate the itemsTxt array with the various XML strings, each string contains an
item with information about upload/download volumes for a single hour. */
- struct tm toStruct = getLocalTime(getTime());
-
+ struct tm toStruct = getLocalTime(getTime());
+
toStruct.tm_sec = 0;
toStruct.tm_min = 0;
-
+
int count=0;
time_t from, to;
char itemTitle[64]; // The title of the item, which will contain a date, and a time range
@@ -279,45 +295,48 @@ static void getHourlyItems(char** itemsTxt, int rssItemCount, char* rssUrl){
char ulTxt[24]; // Upload amount, formatted
char descAmt[264]; // Descriptive text detailing the UL/DL amounts, for the body of the item
char descDate[64]; // Publication date for the item, in the correct format
-
+
while (count<rssItemCount){
- // Calculate the upper/lower bounds that correspond to the hour we are working on
+ // Calculate the upper/lower bounds that correspond to the hour we are working on
to = mktime(&toStruct);
toStruct.tm_hour -= 1;
from = mktime(&toStruct);
-
+
// The title of the item will be the day, followed by the time range coveed by the item
strftime(titleDate, 23, "%A, %d %b", &toStruct);
sprintf(itemTitle, "%s %d:00-%d:00", titleDate, toStruct.tm_hour, toStruct.tm_hour+1);
-
+
// Run the query
struct Data* totals = getQueryValues(from, to, QUERY_GROUP_TOTAL, NULL, NULL);
formatAmount(totals->dl, TRUE, TRUE, dlTxt);
formatAmount(totals->ul, TRUE, TRUE, ulTxt);
freeData(totals);
-
+
// The descriptive text also contains the date, in a more verbose format
strftime(descDate, 63, "%A %d %B %Y", &toStruct);
-
- /* Construct the descriptive text, its HTML but we must escape the entities so it
+
+ /* Construct the descriptive text, its HTML but we must escape the entities so it
doesn't break the XML structure */
sprintf(descAmt, "On %s between %d:00 and %d:00 totals were as follows:%sDownload: %s%sUpload: %s",
descDate, toStruct.tm_hour, toStruct.tm_hour+1, HTML_LINE_BREAK, dlTxt, HTML_LINE_BREAK, ulTxt);
-
+
// If there are any Alerts defined then get some text describing their status
char* descAlerts = getAlertsTxt(to);
-
- /* Build a unique ID (guid) to identify this item, this will be the same for a given
+
+ /* Build a unique ID (guid) to identify this item, this will be the same for a given
time/day every time the feed is requested, to ensure that aggregators don't display
duplicates */
char guid[strlen(rssUrl) + 32];
sprintf(guid, "%s/#hourly.%d.%d", rssUrl, (int)from, (int)to);
-
+
toStruct.tm_hour += 1;
char* itemTxt = makeItem(itemTitle, descAmt, descAlerts, &toStruct, guid);
toStruct.tm_hour -= 1;
-
+
itemsTxt[count++] = itemTxt;
+ if (descAlerts != NULL){
+ free(descAlerts);
+ }
}
}
@@ -329,17 +348,17 @@ static char* getPubDate(int rssFreq){
if (rssFreq == RSS_FREQ_DAILY){
now.tm_hour = 0;
}
-
+
char pubDateTxt[48];
getRfc822Time(&now, pubDateTxt);
-
+
return strdup(pubDateTxt);
}
static char* getAlertsTxt(time_t now){
// Builds text describing the status of any alerts that have been defined, as they were at specified date/time
struct Data* totals;
-
+
struct Alert* firstAlert = getAlerts();
struct Alert* alert = firstAlert;
@@ -360,14 +379,14 @@ static char* getAlertsTxt(time_t now){
sprintf(alertTextPart1, "The Alert '%s'", alert->name);
// The second part of the text contains the status of the Alert
- double progress = (double)100 * total / alert->amount;
+ double progress = (double)100 * total / alert->amount;
char alertTextPart2[64];
if (total > alert->amount){
sprintf(alertTextPart2, "has exceeded its limit, total is now %1.2f%% of maximum", progress);
} else {
sprintf(alertTextPart2, "has reached %1.2f%% of maximum", progress);
}
-
+
// The third part of the text contains the current/maximum amounts
char limitAmt[16];
formatAmount(alert->amount, TRUE, TRUE, limitAmt);
@@ -376,15 +395,15 @@ static char* getAlertsTxt(time_t now){
char alertTextPart3[64];
sprintf(alertTextPart3, "[current: %s, limit: %s].", currentAmt, limitAmt);
- // Join the parts together into a single string
+ // Join the parts together into a single string
int alertTextLen = strlen(alertTextPart1) + 1 + strlen(alertTextPart2) + 1 + strlen(alertTextPart3);
char alertText[alertTextLen + 1];
sprintf(alertText, "%s %s %s", alertTextPart1, alertTextPart2, alertTextPart3);
-
+
// Add the new string into the 'alertsText' value that will be returned
if (alertsText==NULL){
alertsText = strdup(alertText); // This is the first Alert, so alertsText is NULL
-
+
} else {
// This is not the first Alert, so append the new text onto the existing text
char* newAlertsTxt = malloc(strlen(alertsText) + strlen(HTML_LINE_BREAK) + alertTextLen + 1);
@@ -393,10 +412,10 @@ static char* getAlertsTxt(time_t now){
strcat(newAlertsTxt, alertText);
free(alertsText);
- alertsText = newAlertsTxt;
+ alertsText = newAlertsTxt;
}
-
- alert = alert->next;
+
+ alert = alert->next;
}
freeAlert(firstAlert);
View
2 webserver/web/m/about.xml
@@ -14,7 +14,7 @@
full interface <a href="/">click here</a>.
</p>
<p>
- Copyright &copy; 2010 Rob Dawson
+ Copyright 2011 Rob Dawson
</p>
<p>
Licensed under the <a href="http://www.gnu.org/licenses/gpl.txt">GNU General Public License</a>
View
2 webserver/web/m/summary.xml
@@ -10,7 +10,7 @@
<h1></h1>
<table>
<tr>
- <th>&nbsp;</th><th>Download</th><th>Upload</th><th>Combined</th>
+ <th> </th><th>Download</th><th>Upload</th><th>Combined</th>
</tr>
<tr>
<td class="name">Today</td><td class="value"><!--[dlDay]--></td><td class="value"><!--[ulDay]--></td><td class="value"><!--[cmDay]--></td>

0 comments on commit 45ff9ee

Please sign in to comment.
Something went wrong with that request. Please try again.