-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmural_server.cpp
159 lines (126 loc) · 3.37 KB
/
mural_server.cpp
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
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <vector>
#include <sstream>
#ifndef __APPLE__
#include <sys/epoll.h>
#include <sys/inotify.h>
#endif
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "tags.h"
#include "search.h"
using namespace std;
int enforce(int a, const char* blame) {
if (a == -1) {
perror(blame);
exit(1);
}
return a;
}
class Timer {
struct timespec start;
public:
Timer() {
clock_gettime(CLOCK_MONOTONIC, &start);
}
long elapsedMS() {
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
return 1000*(end.tv_sec - start.tv_sec) +
(long)((end.tv_nsec - start.tv_nsec)/(1000*1000));
}
};
void handle_one_query(const vector<TagInfo>& tags) {
string query;
getline(cin, query);
if (!query.length()) {
cout << "DONE" << endl;
return;
}
Timer t;
vector<TagInfo> matches = find_best_fuzzy_matches(tags, query, 32);
for (size_t i = 0; i < matches.size(); ++i) {
const TagInfo& match = matches[i];
cout << "MATCH\t"
<< match.symbol << "\t"
<< match.file << "\t"
<< match.row << endl;;
}
cout << "DONE " << t.elapsedMS() << "ms "
<< "#match: " << matches.size() << endl;
}
#ifndef __APPLE__
void mural_epoll_add(const int epoll_fd, const int fd) {
struct epoll_event evt;
evt.data.fd = fd;
evt.events = EPOLLIN;
enforce(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &evt), "mural_add_epoll");
}
void read_inotify_events(const int inotify_fd,
const string& tag_file,
vector<TagInfo>& tags) {
const int EVENT_SIZE = sizeof(struct inotify_event);
const int BUF_SIZE = 1024 * (EVENT_SIZE + 16);
char buf[BUF_SIZE];
// The examples I've read check for EINTR, but its not clear when
// that would happen, or what I should do about it. So just fail
// hard. They also include a check for len == 0, same thing.
int len = enforce(read(inotify_fd, buf, BUF_SIZE), "read");
int i = 0;
while (i < len) {
struct inotify_event *event = (struct inotify_event*) &buf[i];
if (event->mask & IN_CLOSE_WRITE) {
// it can only be one thing...
read_tags_file(tag_file, tags);
}
i += EVENT_SIZE + event->len;
}
}
#endif
int main(int argc, char** argv) {
if (argc != 2) {
cerr << "usage: " << argv[0] << " tagfile" << endl;
return 1;
}
string tags_file(argv[1]);
vector<TagInfo> tags;
read_tags_file(tags_file, tags);
#ifndef __APPLE__
int inotify_fd = enforce(inotify_init(), "inotify_init");
int efd = enforce(epoll_create(2), "epoll_create");
mural_epoll_add(efd, 0);
mural_epoll_add(efd, inotify_fd);
enforce(
inotify_add_watch(inotify_fd, tags_file.c_str(), IN_CLOSE_WRITE),
"inotify_add_watch");
const int MAXEVENTS = 64;
struct epoll_event* events = new struct epoll_event[MAXEVENTS];
while (1) {
int n = epoll_wait(efd, events, MAXEVENTS, -1);
for (int i = 0; i < n; ++i) {
int active_fd = events[i].data.fd;
if (active_fd == 0) {
handle_one_query(tags);
} else if (active_fd == inotify_fd) {
read_inotify_events(inotify_fd, tags_file, tags);
} else {
assert(false);
}
}
if (cin.eof()) {
break;
}
}
#else
while (!cin.eof()) {
handle_one_query(tags);
}
#endif
return 0;
}