-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.cpp
More file actions
117 lines (105 loc) · 2.65 KB
/
client.cpp
File metadata and controls
117 lines (105 loc) · 2.65 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
// This code is licensed under MIT license (see LICENSE for details)
#include <iostream>
#include <fcntl.h>
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
char *feed_ch;
char *fifo_query;
string fifo_feedback;
string ffs, wd;
void send(string msg) {
cout << "Waiting on server" << endl;
int fdq = open(fifo_query, O_WRONLY);
if (fdq < 0) {
cout << "Invalid named pipe for writing to server" << endl;
return;
}
size_t sz = msg.length();
if (write(fdq, (char*) &sz, sizeof(sz)) != (int) sizeof(sz)) {
cout << "Unsuccessful write to server" << endl;
return;
}
if (write(fdq, msg.c_str(), sz) != (int) sz) {
cout << "Unsuccessful write to server" << endl;
return;
}
int fdf = open(fifo_feedback.c_str(), O_RDONLY);
if (fdf < 0) {
cout << "Invalid named pipe for reading from server" << endl;
return;
}
string feed;
while (true) {
if (!read(fdf, (char*) &sz, sizeof(sz))) continue;
feed.resize(sz);
if (!read(fdf, &feed[0], sz)) {
cout << "Failed to read server message" << endl;
continue;
}
if (feed[0] == '\n') {
cout << feed << endl;
string line; getline(cin, line);
string query = ffs + " " + wd + " " + line;
sz = query.length();
if (write(fdq, (char*) &sz, sizeof(sz)) != (int) sizeof(sz)) {
cout << "Unsuccessful write to server" << endl;
break;
}
if (write(fdq, query.c_str(), sz) != (int) sz) {
cout << "Unsuccessful write to server" << endl;
break;
}
} else if (feed[0] == '\t') {
cout << feed.substr(1, feed.length() - 1) << endl;
} else {
cout << feed << endl;
break;
}
}
}
int main(int argc, char **argv) {
if (argc < 2) {
cout << "The first parameter should be the named pipe for writing to the server." << endl;
return 1;
}
fifo_query = argv[1];
int ct = 0;
while (true) {
fifo_feedback = "fifo-" + to_string(ct++);
if (access(fifo_feedback.c_str(), F_OK) == -1) break;
}
unlink(fifo_feedback.c_str());
umask(0);
if (mkfifo(fifo_feedback.c_str(), 0666) == -1) {
cout << "Unable to create named pipe " << fifo_feedback << endl;
return 0;
}
char *wdc = getcwd(NULL, 0);
wd = string(wdc) + "/";
free(wdc);
string fqs;
if (fifo_query[0] != '/') {
fqs = wd + string(fifo_query);
fifo_query = &fqs[0];
}
ffs = wd + string(fifo_feedback);
fifo_feedback = &ffs[0];
if (argc == 2) {
send(ffs);
} else {
ifstream query_list(argv[2]);
if (query_list.fail()) {
cout << "File of queries does not exist or cannot be read." << endl;
return 0;
}
string line;
while (getline(query_list, line)) {
cout << '\n' << line << endl;
send(ffs + " " + wd + " " + line);
}
}
unlink(fifo_feedback.c_str());
return 0;
}