Add tests for Server (#34)
This commit is contained in:
@@ -1,70 +1,8 @@
|
||||
#include "Server/Logger.h"
|
||||
#include "Server/Server.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
|
||||
namespace clice {
|
||||
|
||||
namespace cl {
|
||||
|
||||
llvm::cl::opt<std::string> config("config",
|
||||
llvm::cl::desc("The path of the config file"),
|
||||
llvm::cl::value_desc("path"));
|
||||
|
||||
} // namespace cl
|
||||
|
||||
int Server::run(int argc, const char** argv) {
|
||||
llvm::cl::SetVersionPrinter([](llvm::raw_ostream& os) { os << "clice version: 0.0.1\n"; });
|
||||
llvm::cl::ParseCommandLineOptions(argc, argv, "clice language server");
|
||||
|
||||
if(cl::config.empty()) {
|
||||
log::warn("No config file specified; using default configuration.");
|
||||
} else {
|
||||
/// config::load(argv[0], cl::config.getValue());
|
||||
log::info("Successfully loaded configuration file from {0}.", cl::config.getValue());
|
||||
}
|
||||
|
||||
/// Get the resource directory.
|
||||
if(auto error = fs::init_resource_dir(argv[0])) {
|
||||
log::fatal("Failed to get resource directory, because {0}", error);
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto dispatch = [this](json::Value value) -> async::Task<> {
|
||||
assert(value.kind() == json::Value::Object);
|
||||
auto object = value.getAsObject();
|
||||
assert(object && "value is not an object");
|
||||
if(auto method = object->get("method")) {
|
||||
auto name = *method->getAsString();
|
||||
auto params = object->get("params");
|
||||
if(auto id = object->get("id")) {
|
||||
if(auto iter = requests.find(name); iter != requests.end()) {
|
||||
/// auto tracer = Tracer();
|
||||
log::info("Receive request: {0}", name);
|
||||
co_await iter->second(std::move(*id),
|
||||
params ? std::move(*params) : json::Value(nullptr));
|
||||
log::info("Request {0} is done, elapsed {1}", name, 0);
|
||||
|
||||
} else {
|
||||
log::warn("Unknown request: {0}", name);
|
||||
}
|
||||
} else {
|
||||
if(auto iter = notifications.find(name); iter != notifications.end()) {
|
||||
log::info("Notification: {0}", name);
|
||||
co_await iter->second(params ? std::move(*params) : json::Value(nullptr));
|
||||
} else {
|
||||
log::warn("Unknown notification: {0}", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
co_return;
|
||||
};
|
||||
|
||||
async::listen(dispatch, "127.0.0.1", 50051);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Server::Server() {
|
||||
addMethod("initialize", &Server::onInitialize);
|
||||
addMethod("initialized", &Server::onInitialized);
|
||||
@@ -108,4 +46,73 @@ Server::Server() {
|
||||
addMethod("context/all", &Server::onContextAll);
|
||||
}
|
||||
|
||||
async::Task<> Server::onReceive(json::Value value) {
|
||||
assert(value.kind() == json::Value::Object);
|
||||
auto object = value.getAsObject();
|
||||
assert(object && "value is not an object");
|
||||
if(auto method = object->get("method")) {
|
||||
auto name = *method->getAsString();
|
||||
auto params = object->get("params");
|
||||
if(auto id = object->get("id")) {
|
||||
if(auto iter = requests.find(name); iter != requests.end()) {
|
||||
/// auto tracer = Tracer();
|
||||
log::info("Receive request: {0}", name);
|
||||
co_await iter->second(std::move(*id),
|
||||
params ? std::move(*params) : json::Value(nullptr));
|
||||
log::info("Request {0} is done, elapsed {1}", name, 0);
|
||||
|
||||
} else {
|
||||
log::warn("Unknown request: {0}", name);
|
||||
}
|
||||
} else {
|
||||
if(auto iter = notifications.find(name); iter != notifications.end()) {
|
||||
log::info("Notification: {0}", name);
|
||||
co_await iter->second(params ? std::move(*params) : json::Value(nullptr));
|
||||
} else {
|
||||
log::warn("Unknown notification: {0}", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
co_return;
|
||||
}
|
||||
|
||||
async::Task<> Server::request(llvm::StringRef method, json::Value params) {
|
||||
co_await async::write(json::Object{
|
||||
{"jsonrpc", "2.0" },
|
||||
{"id", id += 1 },
|
||||
{"method", method },
|
||||
{"params", std::move(params)},
|
||||
});
|
||||
}
|
||||
|
||||
async::Task<> Server::notify(llvm::StringRef method, json::Value params) {
|
||||
co_await async::write(json::Object{
|
||||
{"jsonrpc", "2.0" },
|
||||
{"method", method },
|
||||
{"params", std::move(params)},
|
||||
});
|
||||
}
|
||||
|
||||
async::Task<> Server::response(json::Value id, json::Value result) {
|
||||
co_await async::write(json::Object{
|
||||
{"jsonrpc", "2.0" },
|
||||
{"id", id },
|
||||
{"result", std::move(result)},
|
||||
});
|
||||
}
|
||||
|
||||
async::Task<> Server::registerCapacity(llvm::StringRef id,
|
||||
llvm::StringRef method,
|
||||
json::Value registerOptions) {
|
||||
co_await request("client/registerCapability",
|
||||
json::Object{
|
||||
{"registrations",
|
||||
json::Array{json::Object{
|
||||
{"id", id},
|
||||
{"method", method},
|
||||
{"registerOptions", std::move(registerOptions)},
|
||||
}}},
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace clice
|
||||
|
||||
Reference in New Issue
Block a user