This repository was archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathclient.c
More file actions
164 lines (137 loc) · 3.94 KB
/
client.c
File metadata and controls
164 lines (137 loc) · 3.94 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
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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/types.h>
struct Server {
char ip[255];
int port;
};
uint64_t MultModulo(uint64_t a, uint64_t b, uint64_t mod) {
uint64_t result = 0;
a = a % mod;
while (b > 0) {
if (b % 2 == 1)
result = (result + a) % mod;
a = (a * 2) % mod;
b /= 2;
}
return result % mod;
}
bool ConvertStringToUI64(const char *str, uint64_t *val) {
char *end = NULL;
unsigned long long i = strtoull(str, &end, 10);
if (errno == ERANGE) {
fprintf(stderr, "Out of uint64_t range: %s\n", str);
return false;
}
if (errno != 0)
return false;
*val = i;
return true;
}
int main(int argc, char **argv) {
uint64_t k = -1;
uint64_t mod = -1;
char servers[255] = {'\0'}; // TODO: explain why 255
while (true) {
int current_optind = optind ? optind : 1;
static struct option options[] = {{"k", required_argument, 0, 0},
{"mod", required_argument, 0, 0},
{"servers", required_argument, 0, 0},
{0, 0, 0, 0}};
int option_index = 0;
int c = getopt_long(argc, argv, "", options, &option_index);
if (c == -1)
break;
switch (c) {
case 0: {
switch (option_index) {
case 0:
ConvertStringToUI64(optarg, &k);
// TODO: your code here
break;
case 1:
ConvertStringToUI64(optarg, &mod);
// TODO: your code here
break;
case 2:
// TODO: your code here
memcpy(servers, optarg, strlen(optarg));
break;
default:
printf("Index %d is out of options\n", option_index);
}
} break;
case '?':
printf("Arguments error\n");
break;
default:
fprintf(stderr, "getopt returned character code 0%o?\n", c);
}
}
if (k == -1 || mod == -1 || !strlen(servers)) {
fprintf(stderr, "Using: %s --k 1000 --mod 5 --servers /path/to/file\n",
argv[0]);
return 1;
}
// TODO: for one server here, rewrite with servers from file
unsigned int servers_num = 1;
struct Server *to = malloc(sizeof(struct Server) * servers_num);
// TODO: delete this and parallel work between servers
to[0].port = 20001;
memcpy(to[0].ip, "127.0.0.1", sizeof("127.0.0.1"));
// TODO: work continiously, rewrite to make parallel
for (int i = 0; i < servers_num; i++) {
struct hostent *hostname = gethostbyname(to[i].ip);
if (hostname == NULL) {
fprintf(stderr, "gethostbyname failed with %s\n", to[i].ip);
exit(1);
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(to[i].port);
server.sin_addr.s_addr = *((unsigned long *)hostname->h_addr);
int sck = socket(AF_INET, SOCK_STREAM, 0);
if (sck < 0) {
fprintf(stderr, "Socket creation failed!\n");
exit(1);
}
if (connect(sck, (struct sockaddr *)&server, sizeof(server)) < 0) {
fprintf(stderr, "Connection failed\n");
exit(1);
}
// TODO: for one server
// parallel between servers
uint64_t begin = 1;
uint64_t end = k;
char task[sizeof(uint64_t) * 3];
memcpy(task, &begin, sizeof(uint64_t));
memcpy(task + sizeof(uint64_t), &end, sizeof(uint64_t));
memcpy(task + 2 * sizeof(uint64_t), &mod, sizeof(uint64_t));
if (send(sck, task, sizeof(task), 0) < 0) {
fprintf(stderr, "Send failed\n");
exit(1);
}
char response[sizeof(uint64_t)];
if (recv(sck, response, sizeof(response), 0) < 0) {
fprintf(stderr, "Recieve failed\n");
exit(1);
}
// TODO: from one server
// unite results
uint64_t answer = 0;
memcpy(&answer, response, sizeof(uint64_t));
printf("answer: %llu\n", answer);
close(sck);
}
free(to);
return 0;
}