-
Notifications
You must be signed in to change notification settings - Fork 121
/
Server.cpp
226 lines (180 loc) · 7.23 KB
/
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600 // at least windows vista required for grpc
#endif
#include "Server.hpp"
#include "eyaml/eyaml.h"
#include "server.grpc.pb.h"
#include <grpc/grpc.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <filesystem>
#include <memory>
#include <future>
#include <vector>
namespace fs = std::filesystem;
using namespace grpc;
using namespace buffers;
std::promise<void> exit_requested;
class CompilerServiceImpl final : public Compiler::Service {
public:
explicit CompilerServiceImpl(EnigmaPlugin& plugin, OptionsParser& options, CallBack &ecb):
plugin(plugin), options(options), ecb(ecb) {}
Status CompileBuffer(ServerContext* /*context*/, const CompileRequest* request, ServerWriter<CompileReply>* writer) override {
// use lambda capture to contain compile logic
auto fnc = [&] {
plugin.BuildGame(request->game(), emode_run, request->name().c_str());
};
// asynchronously launch the compile request
std::future<void> future = std::async(fnc);
// provide compile feedback to the client
while (future.wait_for(std::chrono::seconds(0)) != std::future_status::ready) {
CompileReply reply;
ecb.ProcessOutput();
reply.mutable_progress()->CopyFrom(ecb.GetProgress());
bool end = false;
const LogMessage msg = ecb.GetFirstLogMessage(end);
if (end) continue;
reply.add_message()->CopyFrom(msg);
while (true) {
const LogMessage nxt = ecb.GetNextLogMessage(end);
if (end) { ecb.ClearLogMessages(); break; }
reply.add_message()->CopyFrom(nxt);
}
writer->Write(reply);
}
return Status::OK;
}
Status GetResources(ServerContext* /*context*/, const Empty* /*request*/, ServerWriter<Resource>* writer) override {
const char* raw = plugin.FirstResource();
while (!plugin.ResourcesAtEnd()) {
Resource resource;
resource.set_name(raw);
resource.set_is_function(plugin.ResourceIsFunction());
if (resource.is_function()) {
resource.set_arg_count_min(plugin.ResourceArgCountMin());
resource.set_arg_count_max(plugin.ResourceArgCountMax());
resource.set_overload_count(plugin.ResourceOverloadCount());
}
resource.set_is_type_name(plugin.ResourceIsTypeName());
resource.set_is_global(plugin.ResourceIsGlobal());
for (int i = 0; i < resource.overload_count(); ++i) {
resource.add_parameters(plugin.ResourceParameters(i));
}
if (!plugin.ResourcesAtEnd()) {
resource.set_is_end(false);
raw = plugin.NextResource();
} else {
resource.set_is_end(true);
}
writer->Write(resource);
}
return Status::OK;
}
Status GetSystems(ServerContext* /*context*/, const Empty* /*request*/, ServerWriter<SystemType>* writer) override {
auto _api = this->options.GetAPI();
for (auto systems : _api) {
SystemType system;
system.set_name(systems.first);
for (auto&& subsystem : systems.second) {
SystemInfo* subInfo = system.add_subsystems();
std::ifstream ifabout(subsystem, std::ios_base::in);
if (!ifabout.is_open()) continue;
ey_data about = parse_eyaml(ifabout, subsystem);
std::string name = about.get("name");
std::string id = about.get("identifier");
std::string desc = about.get("description");
std::string author = about.get("author");
std::string target = about.get("target-platform");
if (id.empty())
id = about.get("id"); // allow alias
if (id.empty()) {
// compilers use filename minus ext as id
fs::path ey(subsystem);
id = ey.stem().string();
}
// allow author alias used by compiler descriptors
if (author.empty())
author = about.get("maintainer");
eyit represents = about.values.find("represents");
if (represents != about.values.end()) {
std::string repsStr = (represents->second)->data().get("build-platforms");
std::stringstream ss(repsStr);
std::string token;
while (ss >> token) {
if (token.back() == ',') token.pop_back();
subInfo->add_represents(token);
}
}
eyit depends = about.values.find("depends");
if (depends != about.values.end()) {
std::string depsStr = (depends->second)->data().get("build-platforms");
std::stringstream ss(depsStr);
std::string token;
while (ss >> token) {
if (token.back() == ',') token.pop_back();
subInfo->add_depends(token);
}
}
subInfo->set_name(name);
subInfo->set_id(id);
subInfo->set_description(desc);
subInfo->set_author(author);
subInfo->set_target(target);
}
writer->Write(system);
}
return Status::OK;
}
SyntaxError GetSyntaxError(syntax_error* err) {
SyntaxError error;
error.set_message(err->err_str);
error.set_line(err->line);
error.set_position(err->position);
error.set_absolute_index(err->absolute_index);
return error;
}
Status SetDefinitions(ServerContext* /*context*/, const SetDefinitionsRequest* request, SyntaxError* reply) override {
syntax_error* err = plugin.SetDefinitions(request->code().c_str(), request->yaml().c_str());
reply->CopyFrom(GetSyntaxError(err));
return Status::OK;
}
Status SetCurrentConfig(ServerContext* /*context*/, const SetCurrentConfigRequest* request, Empty* /*reply*/) override {
std::string yaml = this->options.APIyaml(&request->settings());
/*syntax_error* err = */plugin.SetDefinitions("", yaml.c_str());
return Status::OK;
}
Status SyntaxCheck(ServerContext* /*context*/, const SyntaxCheckRequest* request, SyntaxError* reply) override {
std::vector<const char*> script_names;
script_names.reserve(request->script_names().size());
for (const std::string &str : request->script_names()) script_names.push_back(str.c_str());
syntax_error* err = plugin.SyntaxCheck(request->script_count(), script_names.data(), request->code().c_str());
reply->CopyFrom(GetSyntaxError(err));
return Status::OK;
}
Status Teardown(ServerContext*, const ::buffers::Empty*, ::buffers::Empty*) override {
exit_requested.set_value();
return Status::OK;
}
private:
EnigmaPlugin& plugin;
OptionsParser& options;
CallBack &ecb;
};
int RunServer(const std::string& address, EnigmaPlugin& plugin, OptionsParser &options, CallBack &ecb) {
CompilerServiceImpl service(plugin, options, ecb);
ServerBuilder builder;
builder.AddListeningPort(address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << address << std::endl;
auto serveFn = [&]() {
server->Wait();
};
std::thread serving_thread(serveFn);
auto f = exit_requested.get_future();
f.wait();
server->Shutdown();
serving_thread.join();
return 0;
}