-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
API.c
193 lines (161 loc) · 5.47 KB
/
API.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "vector_Matrix.h" // Include the vector_Matrix header
#define CLIENT_ID "Ov23li5l71lKLE8kYTIc"
#define CLIENT_SECRET "7b4626c63b6b939b9b14680b32ab01ec231cf2e2"
#define REPO_OWNER "OpenAI, Josef Edwards, Andrew Ng, Fei Fei Li"
#define REPO_NAME "pmll"
// Default server configuration
#define DEFAULT_API_URL "127.0.0.1"
#define DEFAULT_API_PORT 8080
// Dynamic API endpoints and ports
const char* endpoints[] = {
"VECTOR_MATRIX",
"MEMORY_SILO",
"IO_SOCKET",
"LOGIC_LOOP",
"PML_LOGIC_LOOP",
"ARLL",
"EFLL"
};
const int ports[] = {
8080, // VECTOR_MATRIX
8081, // MEMORY_SILO
8082, // IO_SOCKET
8083, // LOGIC_LOOP
8084, // PML_LOGIC_LOOP
8085, // ARLL
8086 // EFLL
};
#define NUM_ENDPOINTS (sizeof(endpoints) / sizeof(endpoints[0]))
// Function to create a new socket
int create_socket() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
return sockfd;
}
// Function to set up the server address
void set_up_server_address(struct sockaddr_in* server_addr, const char* url, int port) {
memset(server_addr, 0, sizeof(*server_addr));
server_addr->sin_family = AF_INET;
server_addr->sin_port = htons(port);
if (inet_pton(AF_INET, url, &server_addr->sin_addr) <= 0) {
fprintf(stderr, "Invalid address: %s\n", url);
exit(EXIT_FAILURE);
}
}
// Function to connect to a server
void connect_to_server(int sockfd, struct sockaddr_in* server_addr) {
if (connect(sockfd, (struct sockaddr*)server_addr, sizeof(*server_addr)) < 0) {
perror("Failed to connect to server");
close(sockfd);
exit(EXIT_FAILURE);
}
}
// Function to handle dynamic API connections
void handle_dynamic_api_connections() {
struct sockaddr_in server_addr;
int sockfd;
for (int i = 0; i < NUM_ENDPOINTS; i++) {
printf("Connecting to %s on port %d...\n", endpoints[i], ports[i]);
sockfd = create_socket();
set_up_server_address(&server_addr, DEFAULT_API_URL, ports[i]);
connect_to_server(sockfd, &server_addr);
// Send a basic request
const char* request = "API_CONNECTION_TEST\n";
if (send(sockfd, request, strlen(request), 0) < 0) {
perror("Failed to send request");
} else {
printf("Request sent to %s successfully.\n", endpoints[i]);
}
// Receive a response
char response[1024];
if (recv(sockfd, response, sizeof(response), 0) < 0) {
perror("Failed to receive response");
} else {
printf("Response from %s: %s\n", endpoints[i], response);
}
// Close the socket
close(sockfd);
}
}
// Function to perform vector matrix operations
void handle_vector_matrix_operations() {
int rows = 3, cols = 3;
int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
vector_matrix_t* matrix = init_vector_matrix(rows, cols);
if (!matrix) {
fprintf(stderr, "Failed to initialize vector matrix\n");
exit(EXIT_FAILURE);
}
if (populate_vector_matrix(matrix, values) != 0) {
fprintf(stderr, "Failed to populate vector matrix\n");
free_vector_matrix(matrix);
exit(EXIT_FAILURE);
}
printf("Input Matrix:\n");
for (int i = 0; i < matrix->rows; i++) {
for (int j = 0; j < matrix->cols; j++) {
printf("%d ", matrix->data[i][j]);
}
printf("\n");
}
if (pmll_vector_matrix_process(matrix) != 0) {
fprintf(stderr, "Vector matrix processing failed\n");
free_vector_matrix(matrix);
exit(EXIT_FAILURE);
}
free_vector_matrix(matrix);
}
// Function to make a GET request
void make_get_request(const char* url) {
CURL* curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "Failed to initialize cURL\n");
exit(EXIT_FAILURE);
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "cURL GET request failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
// Function to make a POST request
void make_post_request(const char* url, const char* data) {
CURL* curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "Failed to initialize cURL\n");
exit(EXIT_FAILURE);
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "cURL POST request failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
int main() {
printf("Performing vector matrix operations...\n");
handle_vector_matrix_operations();
printf("Handling dynamic API connections...\n");
handle_dynamic_api_connections();
printf("Making a GET request to the GitHub API...\n");
const char* get_url = "https://api.github.com/repos/OpenAI/pmll";
make_get_request(get_url);
printf("Making a POST request to create a new issue...\n");
const char* post_data = "{\"title\":\"New issue\",\"body\":\"This is a new issue\"}";
const char* post_url = "https://api.github.com/repos/OpenAI/pmll/issues";
make_post_request(post_url, post_data);
return 0;
}