Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
500Foods authored Jul 15, 2024
1 parent f47930a commit 1bf3a2b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 100 deletions.
3 changes: 1 addition & 2 deletions elements/001-hydrogen/hydrogen/src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ AppConfig* load_config(const char* config_path) {
config->upload_dir = strdup(json_string_value(json_object_get(root, "UploadDir")));
config->max_upload_size = json_integer_value(json_object_get(root, "MaxUploadSize"));
config->log_file_path = strdup(json_string_value(json_object_get(root, "LogFile")));
config->web_root = strdup(json_string_value(json_object_get(root, "WebRoot")));

config->executable_path = get_executable_path();

Expand All @@ -168,7 +169,6 @@ AppConfig* load_config(const char* config_path) {
config->mdns.services[i].type = strdup(json_string_value(json_object_get(service, "Type")));
config->mdns.services[i].port = json_integer_value(json_object_get(service, "Port"));

// Parse TXT records
const char* txt_records_str = json_string_value(json_object_get(service, "TxtRecords"));
char* txt_records_copy = strdup(txt_records_str);
char* saveptr;
Expand All @@ -177,7 +177,6 @@ AppConfig* load_config(const char* config_path) {
char** txt_records = NULL;

while (token != NULL) {
// Trim leading and trailing whitespace
while (*token == ' ') token++;
char* end = token + strlen(token) - 1;
while (end > token && *end == ' ') end--;
Expand Down
1 change: 1 addition & 0 deletions elements/001-hydrogen/hydrogen/src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct {
char *upload_path;
char *upload_dir;
size_t max_upload_size;
char *web_root;
mDNSConfig mdns;
} AppConfig;

Expand Down
70 changes: 30 additions & 40 deletions elements/001-hydrogen/hydrogen/src/hydrogen.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// System Libraries
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -47,7 +48,6 @@ void inthandler(int signum) {

printf("\n");
log_this("Shutdown", "Cleaning up and shutting down", 0, true, true, true);
usleep(10000);

pthread_mutex_lock(&terminate_mutex);
keep_running = 0;
Expand Down Expand Up @@ -99,49 +99,56 @@ void graceful_shutdown() {
// 5. Free other resources
log_this("Shutdown", "Freeing other resources", 0, true, true, true);

// 6. Shutdown Logging (do this last)
// 6. Shutdown Logging
log_this("Shutdown", "Initiating Logging shutdown", 0, true, true, true);
log_queue_shutdown = 1;
pthread_cond_broadcast(&terminate_cond);
pthread_join(log_thread, NULL);

// Switch to printf for logging after this point
printf("- Closing file logging\n");
log_this("Shutdown", "Closing File Logging", 0, true, true, true);
close_file_logging();
printf("- Logging shutdown complete\n");
log_this("Shutdown", "Logging shutdown complete", 0, true, true, true);

// 7. Final cleanup
printf("- Destroying queue system\n");
// 7. Shutdown queue system
log_this("Shutdown", "Initiating queue system shutdown", 0, true, true, true);
queue_system_destroy();
printf("- Destroying condition variable and mutex\n");
log_this("Shutdown", "Initiating queue system shutdown", 0, true, true, true);

// 8. Synchronization variables
log_this("Shutdown", "Releasing condition variable and mutex", 0, true, true, true);
pthread_cond_destroy(&terminate_cond);
pthread_mutex_destroy(&terminate_mutex);

// Free app_config
// 9. Free app_config
log_this("Shutdown", "Releasing configuration information", 0, true, true, true);
if (app_config) {
free(app_config->server_name);
free(app_config->executable_path);
free(app_config->log_file_path);
free(app_config->upload_path);
free(app_config->upload_dir);

free(app_config->web_root);

// Free mDNS config
free(app_config->mdns.device_id);
free(app_config->mdns.friendly_name);
free(app_config->mdns.model);
free(app_config->mdns.manufacturer);
free(app_config->mdns.version);
for (int i = 0; i < app_config->mdns.num_services; i++) {
free(app_config->mdns.services[i].name);
free(app_config->mdns.services[i].type);
for (int j = 0; j < app_config->mdns.services[i].num_txt_records; j++) {
free(app_config->mdns.services[i].txt_records[j]);
}
free(app_config->mdns.services[i].txt_records);
}
free(app_config->mdns.services);

free(app_config);
app_config = NULL;
}

printf("Graceful shutdown complete\n");
log_this("Shutdown", "Shutdown complete", 0, true, true, true);
shutting_down = 1;
}

Expand All @@ -156,9 +163,6 @@ int main(int argc, char *argv[]) {
act.sa_handler = inthandler;
sigaction(SIGINT, &act, NULL);

// Temp storage for output messages
char buffer[256];

// Initialize the queue system
queue_system_init();

Expand Down Expand Up @@ -194,40 +198,28 @@ int main(int argc, char *argv[]) {
}

// Output app information
snprintf(buffer, sizeof(buffer), LOG_LINE_BREAK);
log_this("Initialization", buffer, 0, true, true, true);

snprintf(buffer, sizeof(buffer), "Server Name: %s", app_config->server_name);
log_this("Initialization", buffer, 0, true, true, true);

snprintf(buffer, sizeof(buffer), "Executable: %s", app_config->executable_path);
log_this("Initialization", buffer, 0, true, false, true);

snprintf(buffer, sizeof(buffer), "Version: %s", VERSION);
log_this("Initialization", buffer, 0, true, false, true);
log_this("Initialization", "%s", 0, true, true, true, LOG_LINE_BREAK);
log_this("Initialization", "Server Name: %s", 0, true, true, true, app_config->server_name);
log_this("Initialization", "Executable: %s", 0, true, true, true, app_config->executable_path);
log_this("Initialization", "Version: %s", 0, true, true, true, VERSION);

long file_size = get_file_size(app_config->executable_path);
if (file_size >= 0) {
snprintf(buffer, sizeof(buffer), "Size: %ld bytes", file_size);
log_this("Initialization", buffer, 0, true, false, true);
log_this("Initialization", "Size: %ld", 0, true, true, true, file_size);
} else {
log_this("Initialization", "Error: Unable to get file size", 0, true, false, true);
}

char* mod_time = get_file_modification_time(app_config->executable_path);
if (mod_time) {
snprintf(buffer, sizeof(buffer), "Last modified: %s", mod_time);
log_this("Initialization", buffer, 0, true, false, true);
log_this("Initialization", "Last Modified: %s", 0, true, false, true, mod_time);
free(mod_time);
} else {
log_this("Initialization", "Error: Unable to get modification time", 0, true, false, true);
}

snprintf(buffer, sizeof(buffer), "Log File: %s", app_config->log_file_path ? app_config->log_file_path : "None");
log_this("Initialization", buffer, 0, true, true, true);

snprintf(buffer, sizeof(buffer), LOG_LINE_BREAK);
log_this("Initialization", buffer, 0, true, true, true);
log_this("Initialization", "Log File: %s", 0, true, true, true, app_config->log_file_path ? app_config->log_file_path : "None");
log_this("Initialization", "%s", 0, true, true, true, LOG_LINE_BREAK);

// Initialize print queue
init_print_queue();
Expand Down Expand Up @@ -296,20 +288,18 @@ int main(int argc, char *argv[]) {
// Ready to go
// Give threads a moment to launch first
usleep(10000);
snprintf(buffer, sizeof(buffer), LOG_LINE_BREAK);
log_this("Initialization", buffer, 0, true, true, true);
log_this("Initialization", "%s", 0, true, true, true, LOG_LINE_BREAK);
log_this("Initialization", "Application started", 0, true, true, true);
log_this("Initialization", "Press Ctrl+C to exit", 0, true, false, true);
log_this("Initialization", "%s", 0, true, true, true, LOG_LINE_BREAK);

////////////////////////////////////////////////////////////////////////////////
log_this("Initialization", "Entering main loop", 0, true, true, true);

while (keep_running) {
pthread_mutex_lock(&terminate_mutex);
pthread_cond_wait(&terminate_cond, &terminate_mutex);
pthread_mutex_unlock(&terminate_mutex);
}
log_this("Shutdown", "Exiting main loop", 0, true, true, true);

////////////////////////////////////////////////////////////////////////////////

Expand Down
14 changes: 10 additions & 4 deletions elements/001-hydrogen/hydrogen/src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>

// Project Libraries
#include "logging.h"
Expand All @@ -16,8 +17,11 @@
extern volatile sig_atomic_t log_queue_shutdown;
extern pthread_cond_t terminate_cond;
extern pthread_mutex_t terminate_mutex;
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;

void log_this(const char* subsystem, const char* format, int priority, bool LogConsole, bool LogDatabase, bool LogFile, ...) {
pthread_mutex_lock(&log_mutex);

char details[1024]; // Increased buffer size to accommodate formatted string
va_list args;
va_start(args, LogFile);
Expand All @@ -30,7 +34,7 @@ void log_this(const char* subsystem, const char* format, int priority, bool LogC
"{\"subsystem\":\"%s\",\"details\":\"%s\",\"priority\":%d,\"LogConsole\":%s,\"LogDatabase\":%s,\"LogFile\":%s}",
subsystem, details, priority, LogConsole ? "true" : "false", LogDatabase ? "true" : "false", LogFile ? "true" : "false");

// Enqueue the log message
// Enqueue the log message
Queue* log_queue = queue_find("SystemLog");
if (log_queue && !log_queue_shutdown) {
queue_enqueue(log_queue, json_message, strlen(json_message), priority);
Expand All @@ -39,11 +43,13 @@ void log_this(const char* subsystem, const char* format, int priority, bool LogC
pthread_cond_signal(&terminate_cond);

// If this is a shutdown-related log, sleep to ensure logging completes
if ((strcmp(subsystem, "Shutdown") == 0) || (strcasestr(format, "shutdown") != NULL)) {
usleep(100000); // Sleep for 100ms
}
//if ((strcmp(subsystem, "Shutdown") == 0) || (strcmp(subsystem,"Initialization") == 0) || (strcasestr(format, "shutdown") != NULL)) {
// usleep(100000); // Sleep for 100ms
// }
} else if (LogConsole) {
// If the log queue is shutting down or not available, print to console as a fallback
printf("%s: %s\n", subsystem, details);
}

pthread_mutex_unlock(&log_mutex);
}
7 changes: 0 additions & 7 deletions elements/001-hydrogen/hydrogen/src/mdns_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,18 +381,13 @@ mdns_t *mdns_init(const char *app_name,
mdns_service_t *services,
int num_services) {

log_this("mDNS", "Entering mdns_init function", 0, true, true, true);
usleep(10000);

mdns_t *mdns = malloc(sizeof(mdns_t));
if (!mdns) {
log_this("mDNS", "Out of memory", 4, true, true, true);
return NULL;
}

log_this("mDNS", "Getting network information", 0, true, true, true);
usleep(10000);

// Set up timeout for get_network_info
struct timeval start, end;
gettimeofday(&start, NULL);
Expand All @@ -410,8 +405,6 @@ mdns_t *mdns_init(const char *app_name,
return NULL;
}

log_this("mDNS", "Creating multicast sockets", 0, true, true, true);
usleep(10000);
mdns->sockfd_v4 = create_multicast_socket(AF_INET, MDNS_GROUP_V4);
mdns->sockfd_v6 = create_multicast_socket(AF_INET6, MDNS_GROUP_V6);

Expand Down
Loading

0 comments on commit 1bf3a2b

Please sign in to comment.