Skip to content

Commit

Permalink
Update Persistence.c
Browse files Browse the repository at this point in the history
Signed-off-by: Josef Edwards <joed6834@colorado.edu>
  • Loading branch information
bearycool11 authored Nov 12, 2024
1 parent a32408f commit 1f52ecd
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions Persistence.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,99 @@ void io_socket_cleanup(io_socket_t *io_socket) {
io_socket_close(io_socket);
}

// Function to save the state of the PML logic loop
void save_pml_logic_loop(const char* filename, pml_logic_loop_t* pml_logic_loop) {
FILE* file = fopen(filename, "wb");
if (file == NULL) {
perror("Failed to open file for saving PML Logic Loop");
return;
}
fwrite(pml_logic_loop, sizeof(pml_logic_loop_t), 1, file);
fclose(file);
}

// Function to load the state of the PML logic loop
pml_logic_loop_t* load_pml_logic_loop(const char* filename) {
FILE* file = fopen(filename, "rb");
if (file == NULL) {
perror("Failed to open file for loading PML Logic Loop");
return NULL;
}

pml_logic_loop_t* pml_logic_loop = malloc(sizeof(pml_logic_loop_t));
if (pml_logic_loop == NULL) {
perror("Memory allocation failed for PML Logic Loop");
fclose(file);
return NULL;
}
fread(pml_logic_loop, sizeof(pml_logic_loop_t), 1, file);
fclose(file);

return pml_logic_loop;
}

// Function to save the state of the memory silo
void save_memory_silo(const char* filename, memory_silo_t* memory_silo) {
FILE* file = fopen(filename, "wb");
if (file == NULL) {
perror("Failed to open file for saving Memory Silo");
return;
}
fwrite(memory_silo, sizeof(memory_silo_t), 1, file);
fclose(file);
}

// Function to load the state of the memory silo
memory_silo_t* load_memory_silo(const char* filename) {
FILE* file = fopen(filename, "rb");
if (file == NULL) {
perror("Failed to open file for loading Memory Silo");
return NULL;
}

memory_silo_t* memory_silo = malloc(sizeof(memory_silo_t));
if (memory_silo == NULL) {
perror("Memory allocation failed for Memory Silo");
fclose(file);
return NULL;
}
fread(memory_silo, sizeof(memory_silo_t), 1, file);
fclose(file);

return memory_silo;
}

// Function to save the state of the IO socket
void save_io_socket(const char* filename, io_socket_t* io_socket) {
FILE* file = fopen(filename, "wb");
if (file == NULL) {
perror("Failed to open file for saving IO Socket");
return;
}
fwrite(io_socket, sizeof(io_socket_t), 1, file);
fclose(file);
}

// Function to load the state of the IO socket
io_socket_t* load_io_socket(const char* filename) {
FILE* file = fopen(filename, "rb");
if (file == NULL) {
perror("Failed to open file for loading IO Socket");
return NULL;
}

io_socket_t* io_socket = malloc(sizeof(io_socket_t));
if (io_socket == NULL) {
perror("Memory allocation failed for IO Socket");
fclose(file);
return NULL;
}
fread(io_socket, sizeof(io_socket_t), 1, file);
fclose(file);

return io_socket;
}

// Main function to demonstrate persistence with socket and PML logic loop
int main() {
// Initialize the IO socket
Expand All @@ -74,6 +167,11 @@ int main() {
int memory_silo_id = 1; // Example silo ID
pml_logic_loop_init(&io_socket, memory_silo_id);

// Load previous state from files if needed
pml_logic_loop_t* loaded_pml_logic_loop = load_pml_logic_loop("pml_logic_loop_state.dat");
memory_silo_t* loaded_memory_silo = load_memory_silo("memory_silo_state.dat");
io_socket_t* loaded_io_socket = load_io_socket("io_socket_state.dat");

// Main loop for processing
while (1) {
pml_logic_loop_process(&io_socket);
Expand All @@ -85,6 +183,11 @@ int main() {
}
}

// Save state before exiting
save_pml_logic_loop("pml_logic_loop_state.dat", loaded_pml_logic_loop);
save_memory_silo("memory_silo_state.dat", loaded_memory_silo);
save_io_socket("io_socket_state.dat", loaded_io_socket);

// Clean up and close the socket
io_socket_cleanup(&io_socket);

Expand Down

0 comments on commit 1f52ecd

Please sign in to comment.