forked from yuanrongxi/libpaxos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_main.c
More file actions
70 lines (53 loc) · 1.38 KB
/
Copy pathclient_main.c
File metadata and controls
70 lines (53 loc) · 1.38 KB
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
#include "evpaxos.h"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
static void event_callback(struct bufferevent* bev, short events, void* arg)
{
if(events & BEV_EVENT_CONNECTED){
printf("Connected\n");
}
else if(events & BEV_EVENT_ERROR){
printf("%s\n", evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
}
}
/*clientÏòpaxos proposer·¢ÆðÒ»¸öÁ¬½Ó*/
static struct bufferevent* connect_to_proposer((struct event_base* b, struct sockaddr* addr)
{
struct bufferevent* bev;
bev = bufferevent_socket_new(b, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, NULL, NULL, event_callback, NULL);
if (bufferevent_socket_connect(bev, addr, sizeof(struct sockaddr)) < 0){
bufferevent_free(bev);
return NULL;
}
event_base_dispatch(b);
return bev;
}
int main (int argc, char const *argv[])
{
int rate;
struct event_base* base;
struct bufferevent* bev;
struct sockaddr address;
int address_len = sizeof(struct sockaddr);
if (argc != 3)
usage(argv[0]);
if (evutil_parse_sockaddr_port(argv[1], &address, &address_len) == -1)
usage(argv[0]);
rate = atoi(argv[2]);
base = event_base_new();
bev = connect_to_proposer(base, &address);
char value[] = "hello!";
int len = strlen(value) + 1;
while(1) {
int i;
for (i = 0; i < rate; ++i) {
paxos_submit(bev, value, len);
event_base_dispatch(base);
}
sleep(1);
}
return 0;
}