-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-thread-client.c
147 lines (124 loc) · 4.58 KB
/
multi-thread-client.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
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "irohnet.h"
int run_client(EndpointConfig_t *config, slice_ref_uint8_t alpn_slice,
char const *node_id_raw, char const *relay_url_raw,
char const **addrs_raw, int addrs_len) {
printf("Starting client...\n");
// create node addr
NodeAddr_t node_addr = node_addr_default();
// char *nodeAddrToUse = node_id;
int err = node_addr_from_string(strdup(node_id_raw), &node_addr);
if (err != 0) {
fprintf(stderr, "invalid node_addr '%s'\n", node_id_raw);
return -1;
}
printf("binding iroh endpoint\n");
Endpoint_t *ep = endpoint_default();
int bind_res = endpoint_bind(config, 0, 0, &ep);
if (bind_res != 0) {
fprintf(stderr, "failed to bind\n");
return -1;
}
// connect
printf("connecting to %s\n", alpn_slice.ptr);
Connection_t *conn = connection_default();
int ret = endpoint_connect(&ep, alpn_slice, node_addr, &conn);
if (ret != 0) {
printf("failed to connect to server\n");
return -1;
}
RecvStream_t *recv_stream = recv_stream_default();
SendStream_t *send_stream = send_stream_default();
printf("opening bi stream\n");
err = connection_open_bi(&conn, &send_stream, &recv_stream);
if (err != 0) {
printf("Failed to establish stream : %d\n", err);
return 1;
}
printf("sending message\n");
char *data2 = "hello world from client";
slice_ref_uint8_t buffer;
buffer.ptr = (uint8_t *)&data2[0];
buffer.len = strlen(data2);
err = send_stream_write(&send_stream, buffer);
if (err != 0) {
fprintf(stderr, "failed to write\n");
return -1;
}
send_stream_finish(send_stream);
printf("reading to end\n");
Vec_uint8_t recvBuffer = rust_buffer_alloc(0);
err = recv_stream_read_to_end_timeout(&recv_stream, &recvBuffer, 1024, 5000);
if (err == ENDPOINT_RESULT_TIMEOUT) {
printf("Response timed out\n");
return 1;
} else if (err != 0) {
printf("Failed to wait for response: %d\n", err);
return 1;
}
printf("Received message: %s\n", recvBuffer.ptr);
recv_stream_free(recv_stream);
connection_free(conn);
return 0;
}
// Define a structure to pass multiple parameters to the pthread functions
typedef struct {
EndpointConfig_t *config;
slice_ref_uint8_t alpn_slice;
Endpoint_t *ep;
bool json_output; // For server
const char *node_id; // For client
const char *relay_url; // For client
const char **addrs; // For client
int addrs_len; // For client
} ThreadParam;
// Wrapper function for the client
void *client_thread_func(void *arg) {
ThreadParam *params = (ThreadParam *)arg;
run_client(params->config, params->alpn_slice, params->node_id,
params->relay_url, params->addrs, params->addrs_len);
pthread_exit(NULL);
}
int main(int argc, char const *const argv[]) {
iroh_enable_tracing();
pthread_t client_threads[2];
ThreadParam client_params[2];
// Initialize parameters for each thread, including different ALPNs
char alpn1[] = "/cool/alpn/1";
char alpn2[] = "/cool/alpn/2";
// Assuming server and client specific parameters are initialized here...
// Server thread 1
client_params[0].alpn_slice.ptr = (uint8_t *)&alpn1[0];
client_params[0].alpn_slice.len = strlen(alpn1);
client_params[0].json_output = false; // Or true, based on your requirement
EndpointConfig_t config = endpoint_config_default();
endpoint_config_add_alpn(&config, client_params[0].alpn_slice);
client_params[0].config = &config;
// Server thread 2 with different ALPN
client_params[1].alpn_slice.ptr = (uint8_t *)&alpn2[0];
client_params[1].alpn_slice.len = strlen(alpn2);
client_params[1].json_output = false; // Or true
EndpointConfig_t config2 = endpoint_config_default();
endpoint_config_add_alpn(&config2, client_params[1].alpn_slice);
client_params[1].config = &config2;
if (argc < 1) {
fprintf(stderr,
"client must be supplied <node id> <relay-url> <addr1> .. <addrn>");
return -1;
}
char const *nodeAddressStr = argv[1];
client_params[0].node_id = strdup(nodeAddressStr);
client_params[1].node_id = strdup(nodeAddressStr);
pthread_create(&client_threads[0], NULL, client_thread_func,
(void *)&client_params[0]);
pthread_create(&client_threads[1], NULL, client_thread_func,
(void *)&client_params[1]);
pthread_join(client_threads[0], NULL);
pthread_join(client_threads[1], NULL);
return 0;
}