more wip ipc refactor

This commit is contained in:
Jacob Dufault 2017-03-03 00:12:11 -08:00
parent 94383d589b
commit b3a544e880
3 changed files with 250 additions and 84 deletions

View File

@ -4,6 +4,7 @@
#include "compilation_database_loader.h"
#include "indexer.h"
#include "ipc.h"
#include "query.h"
bool ParsePreferredSymbolLocation(const std::string& content, PreferredSymbolLocation* obj) {
@ -94,7 +95,13 @@ indexer.exe --index-file /work2/chrome/src/chrome/foo.cc
}
*/
#include "ipc.h"
struct IpcMessage_IsAlive : public IpcMessage<IpcMessage_IsAlive> {
static IpcMessageId id;
};
IpcMessageId IpcMessage_IsAlive::id = "IsAlive";
void IndexerServerMain() {
IpcServer ipc("language_server");
@ -104,15 +111,13 @@ void IndexerServerMain() {
std::cout << "Server has " << messages.size() << " messages" << std::endl;
for (auto& message : messages) {
switch (message->kind) {
case JsonMessage::Kind::IsAlive:
{
if (message->runtime_id == IpcMessage_IsAlive::id) {
IpcMessage_IsAlive response;
ipc.SendToClient(0, &response); // todo: make non-blocking
break;
}
default:
std::cerr << "Unhandled IPC message with kind " << static_cast<int>(message->kind) << std::endl;
else {
std::cerr << "Unhandled IPC message with kind " << message->runtime_id << " (hash " << message->hashed_runtime_id << ")" << std::endl;
exit(1);
break;
}
@ -123,6 +128,37 @@ void IndexerServerMain() {
}
}
void EmitReferences(IpcClient& ipc) {
}
// Separate thread whose only job is to read from stdin and
// dispatch read commands to the actual indexer program. This
// cannot be done on the main thread because reading from std::cin
// blocks.
void LanguageServerStdinToServerDispatcher(IpcClient& ipc) {
while (true) {
std::string input;
std::cin >> input;
}
}
// Main loop for the language server. |ipc| is connected to
// a server.
void LanguageServerLoop(IpcClient& ipc) {
while (true) {
std::string input;
std::cin >> input;
std::cout << "got input " << input << std::endl << std::endl;
if (input == "references") {
}
}
}
void LanguageServerMain() {
IpcClient ipc("language_server", 0);
@ -139,7 +175,7 @@ void LanguageServerMain() {
std::vector<std::unique_ptr<BaseIpcMessage>> messages = ipc.TakeMessages();
bool has_server = false;
for (auto& message : messages) {
if (message->kind == JsonMessage::Kind::IsAlive) {
if (message->runtime_id == IpcMessage_IsAlive::id) {
has_server = true;
break;
}
@ -152,10 +188,131 @@ void LanguageServerMain() {
}
std::cout << "Found indexer server" << std::endl;
LanguageServerLoop(ipc);
}
#if false
struct IpcMessage_IsAlive : public BaseIpcMessage {
IpcMessage_IsAlive();
// BaseIpcMessage:
void Serialize(Writer& writer) override;
void Deserialize(Reader& reader) override;
};
struct IpcMessage_ImportIndex : public BaseIpcMessage {
std::string path;
IpcMessage_ImportIndex();
// BaseMessage:
void Serialize(Writer& writer) override;
void Deserialize(Reader& reader) override;
};
struct IpcMessage_CreateIndex : public BaseIpcMessage {
std::string path;
std::vector<std::string> args;
IpcMessage_CreateIndex();
// BaseMessage:
void Serialize(Writer& writer) override;
void Deserialize(Reader& reader) override;
};
IpcMessage_IsAlive::IpcMessage_IsAlive() {
kind = JsonMessage::Kind::IsAlive;
}
void IpcMessage_IsAlive::Serialize(Writer& writer) {}
void IpcMessage_IsAlive::Deserialize(Reader& reader) {}
IpcMessage_ImportIndex::IpcMessage_ImportIndex() {
kind = JsonMessage::Kind::ImportIndex;
}
void IpcMessage_ImportIndex::Serialize(Writer& writer) {
writer.StartObject();
::Serialize(writer, "path", path);
writer.EndObject();
}
void IpcMessage_ImportIndex::Deserialize(Reader& reader) {
::Deserialize(reader, "path", path);
}
IpcMessage_CreateIndex::IpcMessage_CreateIndex() {
kind = JsonMessage::Kind::CreateIndex;
}
void IpcMessage_CreateIndex::Serialize(Writer& writer) {
writer.StartObject();
::Serialize(writer, "path", path);
::Serialize(writer, "args", args);
writer.EndObject();
}
void IpcMessage_CreateIndex::Deserialize(Reader& reader) {
::Deserialize(reader, "path", path);
::Deserialize(reader, "args", args);
}
// TODO: make it so we don't need an explicit list
// of available ipc message types. Maybe use string or
// a hash, not sure.
struct IpcMessage_DocumentSymbolsRequest : public BaseIpcMessage {
std::string document;
};
struct IpcMessage_DocumentSymbolsResponse : public BaseIpcMessage {
};
struct ListSymbols : public BaseType<ListSymbols> {
static IpcRegistry::Id id;
};
IpcRegistry::Id ListSymbols::id = "ListSymbols";
struct ListSymbol2s : public BaseType<ListSymbol2s> {
static IpcRegistry::Id id;
};
IpcRegistry::Id ListSymbol2s::id = "ListSymbols";
#endif
void main2() {
//ListSymbols l;
//auto& x = ListSymbols::register_;
//ListSymbol2s l2;
//auto& y = ListSymbol2s::register_;
std::cout << "main2" << std::endl;
std::cin.get();
}
int main(int argc, char** argv) {
if (argc == 1)
IpcMessage_IsAlive _;
//main2();
//return 0;
if (argc == 2)
LanguageServerMain();
else
IndexerServerMain();

56
ipc.cc
View File

@ -24,40 +24,19 @@ void JsonMessage::SetPayload(size_t payload_size, const char* payload) {
memcpy(payload_dest, payload, payload_size);
}
IpcMessage_IsAlive::IpcMessage_IsAlive() {
kind = JsonMessage::Kind::IsAlive;
}
BaseIpcMessage::BaseIpcMessage(BaseIpcMessage::DoNotDeriveDirectly) {}
void IpcMessage_IsAlive::Serialize(Writer& writer) {}
BaseIpcMessage::~BaseIpcMessage() {}
void IpcMessage_IsAlive::Deserialize(Reader& reader) {}
void BaseIpcMessage::Serialize(Writer& writer) {}
IpcMessage_ImportIndex::IpcMessage_ImportIndex() {
kind = JsonMessage::Kind::ImportIndex;
}
void BaseIpcMessage::Deserialize(Reader& reader) {}
void IpcMessage_ImportIndex::Serialize(Writer& writer) {
writer.StartObject();
::Serialize(writer, "path", path);
writer.EndObject();
}
void IpcMessage_ImportIndex::Deserialize(Reader& reader) {
::Deserialize(reader, "path", path);
}
IpcMessage_CreateIndex::IpcMessage_CreateIndex() {
kind = JsonMessage::Kind::CreateIndex;
}
IpcRegistry IpcRegistry::Instance;
void IpcMessage_CreateIndex::Serialize(Writer& writer) {
writer.StartObject();
::Serialize(writer, "path", path);
::Serialize(writer, "args", args);
writer.EndObject();
}
void IpcMessage_CreateIndex::Deserialize(Reader& reader) {
::Deserialize(reader, "path", path);
::Deserialize(reader, "args", args);
std::unique_ptr<BaseIpcMessage> IpcRegistry::Allocate(int id) {
return std::unique_ptr<BaseIpcMessage>((*allocators)[id]());
}
IpcDirectionalChannel::IpcDirectionalChannel(const std::string& name) {
@ -100,12 +79,12 @@ void IpcDirectionalChannel::PushMessage(BaseIpcMessage* message) {
if ((*shared->shared_bytes_used + sizeof(JsonMessage) + payload_size) >= shmem_size)
continue;
get_free_message()->kind = message->kind;
get_free_message()->message_id = message->hashed_runtime_id;
get_free_message()->SetPayload(payload_size, output.GetString());
*shared->shared_bytes_used += sizeof(JsonMessage) + get_free_message()->payload_size;
assert(*shared->shared_bytes_used < shmem_size);
get_free_message()->kind = JsonMessage::Kind::Invalid;
get_free_message()->message_id = -1;
break;
}
@ -122,27 +101,14 @@ std::vector<std::unique_ptr<BaseIpcMessage>> IpcDirectionalChannel::TakeMessages
memcpy(local_block, shared->shared_start, *shared->shared_bytes_used);
*shared->shared_bytes_used = 0;
get_free_message()->kind = JsonMessage::Kind::Invalid;
get_free_message()->message_id = -1;
}
std::vector<std::unique_ptr<BaseIpcMessage>> result;
char* message = local_block;
while (remaining_bytes > 0) {
std::unique_ptr<BaseIpcMessage> base_message;
switch (as_message(message)->kind) {
case JsonMessage::Kind::IsAlive:
base_message = std::make_unique<IpcMessage_IsAlive>();
break;
case JsonMessage::Kind::CreateIndex:
base_message = std::make_unique<IpcMessage_CreateIndex>();
break;
case JsonMessage::Kind::ImportIndex:
base_message = std::make_unique<IpcMessage_ImportIndex>();
break;
default:
assert(false);
}
std::unique_ptr<BaseIpcMessage> base_message = IpcRegistry::Instance.Allocate(as_message(message)->message_id);
rapidjson::Document document;
document.Parse(as_message(message)->payload(), as_message(message)->payload_size);

105
ipc.h
View File

@ -4,6 +4,7 @@
#include <chrono>
#include <string>
#include <thread>
#include <unordered_map>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
@ -21,56 +22,98 @@ using Reader = rapidjson::Document;
// completely different address.
struct JsonMessage {
enum class Kind {
Invalid,
IsAlive,
CreateIndex,
ImportIndex,
};
Kind kind;
int message_id;
size_t payload_size;
const char* payload();
void SetPayload(size_t payload_size, const char* payload);
};
using IpcMessageId = std::string;
struct BaseIpcMessage {
JsonMessage::Kind kind;
virtual ~BaseIpcMessage() {}
virtual ~BaseIpcMessage();
virtual void Serialize(Writer& writer) = 0;
virtual void Deserialize(Reader& reader) = 0;
virtual void Serialize(Writer& writer);
virtual void Deserialize(Reader& reader);
IpcMessageId runtime_id;
int hashed_runtime_id;
/*
private:
template<typename T>
friend struct IpcMessage;
*/
enum class DoNotDeriveDirectly {
DeriveFromIpcMessageInstead
};
BaseIpcMessage(DoNotDeriveDirectly);
};
struct IpcMessage_IsAlive : public BaseIpcMessage {
IpcMessage_IsAlive();
struct IpcRegistry {
using Allocator = std::function<BaseIpcMessage*()>;
// BaseIpcMessage:
void Serialize(Writer& writer) override;
void Deserialize(Reader& reader) override;
// Use unique_ptrs so we can initialize on first use
// (static init order might not be right).
std::unique_ptr<std::unordered_map<int, Allocator>> allocators;
std::unique_ptr<std::unordered_map<int, std::string>> hash_to_id;
template<typename T>
int RegisterAllocator();
std::unique_ptr<BaseIpcMessage> Allocate(int id);
static IpcRegistry* instance() {
// TODO: Remove static magic. Just call register explicitly.
return instance_;
}
static IpcRegistry* instance_;
};
struct IpcMessage_ImportIndex : public BaseIpcMessage {
std::string path;
template<typename T>
int IpcRegistry::RegisterAllocator() {
if (!allocators) {
allocators = std::make_unique<std::unordered_map<int, Allocator>>();
hash_to_id = std::make_unique<std::unordered_map<int, std::string>>();
}
IpcMessage_ImportIndex();
IpcMessageId id = T::id;
// BaseMessage:
void Serialize(Writer& writer) override;
void Deserialize(Reader& reader) override;
int hash = std::hash<IpcMessageId>()(id);
auto it = allocators->find(hash);
assert(allocators->find(hash) == allocators->end() && "There is already an IPC message with the given id");
(*hash_to_id)[hash] = id;
(*allocators)[hash] = []() {
return new T();
};
return hash;
}
template<typename TChild>
struct IpcMessage : public BaseIpcMessage {
IpcMessage();
static int hashed_id_;
};
struct IpcMessage_CreateIndex : public BaseIpcMessage {
std::string path;
std::vector<std::string> args;
template<typename TChild>
int IpcMessage<TChild>::hashed_id_ = IpcRegistry::Instance.RegisterAllocator<TChild>();
template<typename TChild>
IpcMessage<TChild>::IpcMessage()
: BaseIpcMessage(DoNotDeriveDirectly::DeriveFromIpcMessageInstead) {
runtime_id = TChild::id;
hashed_runtime_id = hashed_id_;
}
IpcMessage_CreateIndex();
// BaseMessage:
void Serialize(Writer& writer) override;
void Deserialize(Reader& reader) override;
};
struct IpcDirectionalChannel {
// NOTE: We keep all pointers in terms of char* so pointer arithmetic is