+ protect the verbose flag with mutex

pull/9/head
kelson42 14 years ago
parent a6b177a8f8
commit 9d81b35ebd

@ -77,6 +77,7 @@ static pthread_mutex_t welcomeLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t searcherLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t searcherLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t compressorLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t compressorLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t resourceLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t resourceLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t verboseFlagLock = PTHREAD_MUTEX_INITIALIZER;
// Urlencode // Urlencode
//based on javascript encodeURIComponent() //based on javascript encodeURIComponent()
@ -129,6 +130,15 @@ void introduceTaskbar(string &content, const string &humanReadableBookId) {
pthread_mutex_unlock(&resourceLock); pthread_mutex_unlock(&resourceLock);
} }
/* Should display debug information? */
bool isVerbose() {
bool value;
pthread_mutex_lock(&verboseFlagLock);
value = verboseFlag;
pthread_mutex_unlock(&verboseFlagLock);
return value;
}
/* For compression */ /* For compression */
#define COMPRESSOR_BUFFER_SIZE 5000000 #define COMPRESSOR_BUFFER_SIZE 5000000
static Bytef *compr = (Bytef *)malloc(COMPRESSOR_BUFFER_SIZE); static Bytef *compr = (Bytef *)malloc(COMPRESSOR_BUFFER_SIZE);
@ -142,6 +152,12 @@ static int accessHandlerCallback(void *cls,
const char * upload_data, const char * upload_data,
size_t * upload_data_size, size_t * upload_data_size,
void ** ptr) { void ** ptr) {
/* Debug */
if (isVerbose()) {
std::cout << "Requesting " << url << std::endl;
}
/* Unexpected method */ /* Unexpected method */
if (0 != strcmp(method, "GET")) if (0 != strcmp(method, "GET"))
return MHD_NO; return MHD_NO;
@ -228,7 +244,7 @@ static int accessHandlerCallback(void *cls,
pthread_mutex_lock(&searcherLock); pthread_mutex_lock(&searcherLock);
try { try {
std::string patternString = string(pattern); std::string patternString = string(pattern);
searcher->search(patternString, startNumber, endNumber, verboseFlag); searcher->search(patternString, startNumber, endNumber, isVerbose());
content = searcher->getHtml(); content = searcher->getHtml();
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
@ -246,13 +262,13 @@ static int accessHandlerCallback(void *cls,
found = reader->getContentByUrl(urlStr, content, contentLength, mimeType); found = reader->getContentByUrl(urlStr, content, contentLength, mimeType);
if (found) { if (found) {
if (verboseFlag) { if (isVerbose()) {
cout << "Found " << urlStr << endl; cout << "Found " << urlStr << endl;
cout << "content size: " << contentLength << endl; cout << "content size: " << contentLength << endl;
cout << "mimeType: " << mimeType << endl; cout << "mimeType: " << mimeType << endl;
} }
} else { } else {
if (verboseFlag) if (isVerbose())
cout << "Failed to find " << urlStr << endl; cout << "Failed to find " << urlStr << endl;
content = "<html><head><title>Content not found</title></head><body><h1>Not Found</h1><p>The requested URL " + urlStr + " was not found on this server.</p></body></html>"; content = "<html><head><title>Content not found</title></head><body><h1>Not Found</h1><p>The requested URL " + urlStr + " was not found on this server.</p></body></html>";
@ -568,6 +584,7 @@ int main(int argc, char **argv) {
pthread_mutex_init(&searcherLock, NULL); pthread_mutex_init(&searcherLock, NULL);
pthread_mutex_init(&compressorLock, NULL); pthread_mutex_init(&compressorLock, NULL);
pthread_mutex_init(&resourceLock, NULL); pthread_mutex_init(&resourceLock, NULL);
pthread_mutex_init(&verboseFlagLock, NULL);
/* Start the HTTP daemon */ /* Start the HTTP daemon */
void *page = NULL; void *page = NULL;
@ -631,5 +648,6 @@ int main(int argc, char **argv) {
pthread_mutex_destroy(&resourceLock); pthread_mutex_destroy(&resourceLock);
pthread_mutex_destroy(&mapLock); pthread_mutex_destroy(&mapLock);
pthread_mutex_destroy(&welcomeLock); pthread_mutex_destroy(&welcomeLock);
pthread_mutex_destroy(&verboseFlagLock);
exit(0); exit(0);
} }

Loading…
Cancel
Save