generated from bearycool11/AI_memory_Loops
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_engine.cpp
157 lines (129 loc) · 4.22 KB
/
benchmark_engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <jansson.h> // JSON parsing library
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>
#define BUFFER_SIZE 1024
#define CHUNK_SIZE 256
#define RETRY_LIMIT 5
// Define the buffer and chunk
char buffer[BUFFER_SIZE];
char chunk[CHUNK_SIZE];
// Define the JSON object and error variable
json_t* json;
json_error_t json_error;
// Function prototypes
int setup_server_socket();
int handle_client_connection(int client_sock);
void write_to_knowledge_graph(json_t* json);
void pmll_arll_efll_logic(int client_sock);
void log_message(const char* level, const char* message);
void cleanup();
void pmll_arll_efll_logic(int client_sock) {
int retry_count = 0;
int success = 0;
// PMLL: Persistent Memory Logic Loop
while (retry_count < RETRY_LIMIT) {
memset(buffer, 0, sizeof(buffer)); // Clear the buffer for each retry
printf("\n[PMLL] Listening for incoming data (attempt %d)...\n", retry_count + 1);
int bytes_received;
while ((bytes_received = recv(client_sock, chunk, CHUNK_SIZE, 0)) > 0) {
chunk[bytes_received] = '\0';
strcat(buffer, chunk);
// Attempt to parse JSON
json = json_loads(buffer, 0, &json_error);
if (!json) {
log_message("ERROR", json_error.text);
continue; // Wait for more data
}
// ARLL: Adaptive Redundancy Logic Loop
printf("\n[ARLL] Valid JSON received. Writing to knowledge graph...\n");
write_to_knowledge_graph(json);
success = 1;
break; // Exit the retry loop if successful
}
if (success) {
printf("[PMLL] Data processed successfully.\n");
break; // Exit the retry loop
}
log_message("WARNING", "[EFLL] Error in processing. Retrying...");
retry_count++;
usleep(100000); // Sleep before retrying
}
if (!success) {
log_message("FATAL", "[EFLL] Maximum retries reached. Exiting...");
}
}
int setup_server_socket() {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
if (bind(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
close(sock);
exit(EXIT_FAILURE);
}
if (listen(sock, 1) < 0) {
perror("Listen failed");
close(sock);
exit(EXIT_FAILURE);
}
printf("[INFO] Server listening on port 8080...\n");
return sock;
}
int handle_client_connection(int client_sock) {
pmll_arll_efll_logic(client_sock);
return 0;
}
void write_to_knowledge_graph(json_t* json) {
if (!json) {
log_message("ERROR", "Invalid JSON data for knowledge graph.");
return;
}
printf("[ARLL] Writing JSON data to knowledge graph...\n");
// Simulate writing JSON to a knowledge graph
json_dumpf(json, stdout, JSON_INDENT(2));
printf("\n[ARLL] Data successfully written to knowledge graph.\n");
}
void log_message(const char* level, const char* message) {
time_t now = time(NULL);
char timestamp[64];
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", localtime(&now));
printf("[%s] [%s] %s\n", level, timestamp, message);
}
void cleanup() {
if (json) {
json_decref(json);
json = NULL;
}
memset(buffer, 0, sizeof(buffer));
}
int main() {
int server_sock = setup_server_socket();
while (1) {
printf("[INFO] Waiting for a client to connect...\n");
int client_sock = accept(server_sock, NULL, NULL);
if (client_sock < 0) {
perror("Accept failed");
continue;
}
printf("[INFO] Client connected.\n");
// Handle client connection
handle_client_connection(client_sock);
// Cleanup after each client
cleanup();
close(client_sock);
printf("[INFO] Client connection closed.\n");
}
close(server_sock);
return 0;
}