-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
199 lines (164 loc) · 5.21 KB
/
Copy pathclient.cpp
File metadata and controls
199 lines (164 loc) · 5.21 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
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
194
195
196
197
198
199
/*
Original author of the starter code
Tanzir Ahmed
Department of Computer Science & Engineering
Texas A&M University
Date: 2/8/20
Please include your Name, UIN, and the date below
Name: Arkeldi Bylyku
UIN: 830004713
Date: 09/19/2022
*/
#include "FIFORequestChannel.h"
#include "common.h"
using namespace std;
#define MIN(A, B) ((A) < (B) ? (A) : (B))
void close_channel(FIFORequestChannel *_channel) {
MESSAGE_TYPE q = QUIT_MSG;
_channel->cwrite(&q, sizeof(MESSAGE_TYPE));
}
void launch_server(unsigned int buf_size) {
pid_t pid = fork();
if (pid == 0) {
char buf[11];
sprintf(buf, "%d", buf_size);
execl("./server", "server", "-m", buf, NULL);
}
}
int main(int argc, char *argv[]) {
int opt;
int p = 1;
double t = 0.0;
int e = 1;
bool entire_file, single_item;
entire_file = false;
single_item = false;
bool new_channel = false;
string filename = "";
unsigned int buf_size = MAX_MESSAGE;
while ((opt = getopt(argc, argv, "p:t:e:f:m:c")) != -1) {
switch (opt) {
case 'p':
p = atoi(optarg);
break;
case 't':
t = atof(optarg);
single_item = true;
break;
case 'e':
e = atoi(optarg);
single_item = true;
break;
case 'f':
filename = optarg;
entire_file = true;
break;
case 'm':
buf_size = atoi(optarg);
break;
case 'c':
new_channel = true;
break;
}
}
launch_server(buf_size);
FIFORequestChannel main("control", FIFORequestChannel::CLIENT_SIDE);
FIFORequestChannel *chan = &main;
char *buf = new char[buf_size];
if (new_channel) {
MESSAGE_TYPE m = NEWCHANNEL_MSG;
chan->cwrite(&m, sizeof(m));
chan->cread(buf, buf_size);
printf("New channel name: %s\n", buf);
chan = new FIFORequestChannel(buf, FIFORequestChannel::CLIENT_SIDE);
}
if (entire_file) {
// Create "received" directory if not exists
struct stat st;
if (stat("received", &st) == -1) {
if (mkdir("received", 0700) == -1) {
EXITONERROR("mkdir failed");
}
}
// Create file
string local_filename = "received/" + filename;
FILE *fp = fopen(local_filename.c_str(), "w");
if (fp == NULL) {
EXITONERROR("fopen failed");
}
// Get total file size
filemsg msg(0, 0);
// write to buf
memcpy(buf, &msg, sizeof(msg));
memcpy(buf + sizeof(msg), filename.c_str(), filename.size() + 1);
chan->cwrite(buf, sizeof(msg) + filename.size() + 1);
__int64_t file_size;
chan->cread(&file_size, sizeof(file_size));
// Read file in chunks of bufsize
for (__int64_t i = 0; i < file_size; i += buf_size) {
unsigned int size_to_read = MIN(buf_size, file_size - i);
filemsg msg(i, size_to_read);
// write to buf
memcpy(buf, &msg, sizeof(msg));
memcpy(buf + sizeof(msg), filename.c_str(), filename.size() + 1);
if (chan->cwrite(buf, sizeof(msg) + filename.size() + 1) < 0) {
EXITONERROR("cwrite failed");
}
// Read data
unsigned int read_size = 0;
// The OS can partition the read into multiple reads
while (read_size < size_to_read) {
int n = chan->cread(buf + read_size, size_to_read - read_size);
if (n < 0) {
EXITONERROR("cread failed");
}
read_size += n;
}
// Write to file
if (fwrite(buf, 1, size_to_read, fp) != size_to_read) {
EXITONERROR("fwrite failed");
}
}
fclose(fp);
} else if (single_item) {
// Send a single data point request
datamsg msg(p, t, e);
memcpy(buf, &msg, sizeof(datamsg));
chan->cwrite(buf, sizeof(datamsg));
double reply;
chan->cread(&reply, sizeof(double));
printf("%0.3f,%0.3f\n", t, reply);
} else {
// Request first 1000 entries
// Save them to received/x1.csv
FILE *fp = fopen("received/x1.csv", "w");
if (fp == NULL) {
EXITONERROR("fopen failed");
}
for (int i = 0; i < 1000; i++) {
t = 0.004 * i;
if (fprintf(fp, "%g", t) < 0) {
EXITONERROR("fprintf failed");
}
for (e = 1; e <= 2; e++) {
datamsg msg(p, t, e);
memcpy(buf, &msg, sizeof(datamsg));
chan->cwrite(buf, sizeof(datamsg));
double reply;
chan->cread(&reply, sizeof(double));
if (fprintf(fp, ",%g", reply) < 0) {
EXITONERROR("fprintf failed");
}
}
if (fprintf(fp, "\n") < 0) {
EXITONERROR("fprintf failed");
}
}
}
close_channel(chan);
if (new_channel) {
close_channel(&main);
delete chan;
}
delete[] buf;
}