From 15cf9ffa946c88b2fe901a0e31856c279ee20277 Mon Sep 17 00:00:00 2001 From: DcruBro Date: Tue, 2 Jun 2026 09:43:01 +0200 Subject: [PATCH] stuff --- .gitignore | 1 + include/server/io/dump.h | 20 +++++++++++++++++ src/client/main.c | 5 ++++- src/server/io/dump.c | 32 ++++++++++++++++++++++++++++ src/server/main.c | 4 ++++ test_computation.c | 46 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 include/server/io/dump.h create mode 100644 src/server/io/dump.c create mode 100644 test_computation.c diff --git a/.gitignore b/.gitignore index 493ec6c..6f39660 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ main CMakeFiles/ .vscode/ build/ +.DS_Store diff --git a/include/server/io/dump.h b/include/server/io/dump.h new file mode 100644 index 0000000..821e301 --- /dev/null +++ b/include/server/io/dump.h @@ -0,0 +1,20 @@ +#ifndef DUMP_H +#define DUMP_H + +#include +#include +#include +#include + +typedef struct { + char loc[256]; + char* dataPtr; + size_t dataLen; +} dump_t; + +bool Generic_FileExists(char* file); + +// Pass dump_t +void* Dump_DumpRoutine(void* dumpData); + +#endif \ No newline at end of file diff --git a/src/client/main.c b/src/client/main.c index 5ebd835..b987e08 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -114,7 +114,10 @@ void on_data(ssize_t dataBufLen) { pid_t pid = fork(); if (pid < 0) { perror("Fork failed"); - // TODO; Send TASK_REJECT packet to server to inform that task could not be processed + // Send TASK_REJECT packet to server to inform that task could not be processed + char errPacket[1]; + errPacket[0] = PACKET_TYPE_TASK_REJECT; + Generic_SendSocket(sock, errPacket, 1); return; } diff --git a/src/server/io/dump.c b/src/server/io/dump.c new file mode 100644 index 0000000..4f89080 --- /dev/null +++ b/src/server/io/dump.c @@ -0,0 +1,32 @@ +#include +#include + +bool Generic_FileExists(char* file) { + FILE* f = fopen(file, "r"); + if (f) { + fclose(f); + return true; + } + return false; +} + +void* Dump_DumpRoutine(void* dumpData) { + if (!dumpData) { + perror("Dump - dumpData ptr null!\n"); + return NULL; + } + + dump_t localDump = *(dump_t*)dumpData; // Assume + + FILE* fout = fopen(localDump.loc, "wb"); + if (!fout) { + perror("Dump - failed to open file\n"); + return NULL; + } + + fwrite(localDump.dataPtr, localDump.dataLen, 1, fout); + + fclose(fout); + + return NULL; +} \ No newline at end of file diff --git a/src/server/main.c b/src/server/main.c index 2666131..786b331 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -8,6 +8,7 @@ #include #include #include +#include volatile int running = 1; task_queue_t taskQueue; @@ -221,6 +222,9 @@ void on_data(TcpClient* client) { memcpy(&resultDataSize, &client->dataBuf[1], sizeof(uint32_t)); printf("Task %u result data size: %u bytes\n", task->taskId, resultDataSize); + dump_t dumpDefinition; + dumpDefinition.loc = "./dump.bin"; + // Store results task->assigned_to = 0; task->assigned_at = 0; diff --git a/test_computation.c b/test_computation.c new file mode 100644 index 0000000..0fca52d --- /dev/null +++ b/test_computation.c @@ -0,0 +1,46 @@ +#include +#include + +int main(void) { + FILE *file1 = fopen("input.bin", "rb"); + if (!file1) { + perror("Error opening input.bin"); + return 1; + } + + int n; + if (fread(&n, sizeof(n), 1, file1) != 1) { + perror("Error reading input.bin"); + fclose(file1); + return 1; + } + fclose(file1); + + printf("Got: %d\n", n); + + // Primes or smth +int count = 0; + + for (int i = 2; i < 500000; i++) { + int prime = 1; + for (int d = 2; d * d <= n; d++) { + if (n % d == 0) { + prime = 0; + break; + } + } + count += prime; + n += 2; + } + + FILE *file2 = fopen("output.bin", "wb"); + if (!file2) { + perror("Error opening output.bin"); + return 1; + } + + fwrite(&count, sizeof(count), 1, file2); + fclose(file2); + + return 0; +}