-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAPI_Llama.c
187 lines (149 loc) · 6.08 KB
/
API_Llama.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define API_URL "localhost"
#define API_PORT 8080
#define CROSS_TALK_URL "localhost"
#define CROSS_TALK_PORT 8081
#define PMLL_URL "localhost"
#define PMLL_PORT 8082
#define GITHUB_API_URL "api.github.com"
#define GITHUB_API_PORT 443
// Function to create a new socket
int create_socket() {
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket");
exit(1);
}
return sockfd;
}
// Function to set up the server address
void set_up_server_address(struct sockaddr_in* server_addr, char* url, int port) {
server_addr->sin_family = AF_INET;
server_addr->sin_port = htons(port);
inet_pton(AF_INET, url, &server_addr->sin_addr);
}
// Function to connect to the API server
void connect_to_api_server(int sockfd, struct sockaddr_in* server_addr) {
if (connect(sockfd, (struct sockaddr*)server_addr, sizeof(*server_addr)) < 0) {
perror("connect");
exit(1);
}
}
// Function to connect to the cross-talk server
void connect_to_cross_talk_server(int sockfd, struct sockaddr_in* server_addr) {
if (connect(sockfd, (struct sockaddr*)server_addr, sizeof(*server_addr)) < 0) {
perror("connect");
exit(1);
}
}
// Function to connect to the PMLL server
void connect_to_pmll_server(int sockfd, struct sockaddr_in* server_addr) {
if (connect(sockfd, (struct sockaddr*)server_addr, sizeof(*server_addr)) < 0) {
perror("connect");
exit(1);
}
}
// Function to connect to the GitHub API server
void connect_to_github_api_server(int sockfd, struct sockaddr_in* server_addr) {
if (connect(sockfd, (struct sockaddr*)server_addr, sizeof(*server_addr)) < 0) {
perror("connect");
exit(1);
}
}
// Function to send a request to the API server
void send_request_to_api_server(int sockfd, char* request) {
send(sockfd, request, strlen(request), 0);
}
// Function to receive a response from the API server
void receive_response_from_api_server(int sockfd, char* response) {
recv(sockfd, response, 1024, 0);
}
// Function to send a request to the cross-talk server
void send_request_to_cross_talk_server(int sockfd, char* request) {
send(sockfd, request, strlen(request), 0);
}
// Function to receive a response from the cross-talk server
void receive_response_from_cross_talk_server(int sockfd, char* response) {
recv(sockfd, response, 1024, 0);
}
// Function to send a request to the PMLL server
void send_request_to_pmll_server(int sockfd, char* request) {
send(sockfd, request, strlen(request), 0);
}
// Function to receive a response from the PMLL server
void receive_response_from_pmll_server(int sockfd, char* response) {
recv(sockfd, response, 1024, 0);
}
// Function to send a request to the GitHub API server
void send_request_to_github_api_server(int sockfd, char* request) {
send(sockfd, request, strlen(request), 0);
}
// Function to receive a response from the GitHub API server
void receive_response_from_github_api_server(int sockfd, char* response) {
recv(sockfd, response, 1024, 0);
}
int main() {
int api_sockfd, cross_talk_sockfd, pmll_sockfd, github_api_sockfd;
struct sockaddr_in api_server_addr, cross_talk_server_addr, pmll_server_addr, github_api_server_addr;
// Create a new socket for the API server
api_sockfd = create_socket();
// Set up the server address for the API server
set_up_server_address(&api_server_addr, API_URL, API_PORT);
// Connect to the API server
connect_to_api_server(api_sockfd, &api_server_addr);
// Create a new socket for the cross-talk server
cross_talk_sockfd = create_socket();
// Set up the server address for the cross-talk server
set_up_server_address(&cross_talk_server_addr, CROSS_TALK_URL, CROSS_TALK_PORT);
// Connect to the cross-talk server
connect_to_cross_talk_server(cross_talk_sockfd, &cross_talk_server_addr);
// Create a new socket for the PMLL server
pmll_sockfd = create_socket();
// Set up the server address for the PMLL server
set_up_server_address(&pmll_server_addr, PMLL_URL, PMLL_PORT);
// Connect to the PMLL server
connect_to_pmll_server(pmll_sockfd, &pmll_server_addr);
// Create a new socket for the GitHub API server
github_api_sockfd = create_socket();
// Set up the server address for the GitHub API server
set_up_server_address(&github_api_server_addr, GITHUB_API_URL, GITHUB_API_PORT);
// Connect to the GitHub API server
connect_to_github_api_server(github_api_sockfd, &github_api_server_addr);
// Send a request to the API server
char* request = "GET /api/endpoint HTTP/1.1\r\nHost: localhost:8080\r\n\r\n";
send_request_to_api_server(api_sockfd, request);
// Receive a response from the API server
char response[1024];
receive_response_from_api_server(api_sockfd, response);
printf("%s", response);
// Send a request to the cross-talk server
request = "GET /cross-talk/endpoint HTTP/1.1\r\nHost: localhost:8081\r\n\r\n";
send_request_to_cross_talk_server(cross_talk_sockfd, request);
// Receive a response from the cross-talk server
receive_response_from_cross_talk_server(cross_talk_sockfd, response);
printf("%s", response);
// Send a request to the PMLL server
request = "GET /pmll/endpoint HTTP/1.1\r\nHost: localhost:8082\r\n\r\n";
send_request_to_pmll_server(pmll_sockfd, request);
// Receive a response from the PMLL server
receive_response_from_pmll_server(pmll_sockfd, response);
printf("%s", response);
// Send a request to the GitHub API server
request = "GET /repos/bearycool11/pmll HTTP/1.1\r\nHost: api.github.com\r\n\r\n";
send_request_to_github_api_server(github_api_sockfd, request);
// Receive a response from the GitHub API server
receive_response_from_github_api_server(github_api_sockfd, response);
printf("%s", response);
// Close the sockets
close(api_sockfd);
close(cross_talk_sockfd);
close(pmll_sockfd);
close(github_api_sockfd);
return 0;
}