diff --git a/src/client.cpp b/src/client.cpp index 1d2278f..21d4c55 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,4 +1,5 @@ #include "data.hpp" +#include "resourceloc.hpp" #include #include #include @@ -7,16 +8,16 @@ #include namespace client { -void sendMsg(int msqid, data::RequestMsg *msg) { - if (msgsnd(msqid, msg, sizeof(data::RequestMsg) - sizeof(long), 0) < 0) { +void sendMsg(int msqid, data::RequestMapMsg *msg) { + if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) { BOOST_LOG_TRIVIAL(fatal) << "send error " << strerror(errno); exit(1); } } std::string recvMsg(int msqid) { - data::ResponseMsg msg; - if (msgrcv(msqid, &msg, sizeof(data::ResponseMsg) - sizeof(long), + data::ResponseMapMsg msg; + if (msgrcv(msqid, &msg, sizeof(data::ResponseMapMsg) - sizeof(long), data::clientbound_msg, 0) < 0) { BOOST_LOG_TRIVIAL(fatal) << "receive error " << strerror(errno); exit(1); @@ -36,9 +37,8 @@ void map(key_t key) { exit(0); } - data::RequestMsg req; + data::RequestMapMsg req; req.msgtype = data::serverbound_msg; - req.type = data::map; req.datalen = inp.size(); std::copy(inp.begin(), inp.end(), req.data); @@ -50,20 +50,6 @@ void resolveResourceLoc(key_t key) { std::string inp; std::cin >> inp; - auto msqid = msgget(key, 0664); - - if (msqid < 0) { - BOOST_LOG_TRIVIAL(fatal) << "msgget fail"; - exit(0); - } - - data::RequestMsg req; - req.msgtype = data::serverbound_msg; - req.type = data::resolve_resource_loc; - req.datalen = inp.size(); - std::copy(inp.begin(), inp.end(), req.data); - - sendMsg(msqid, &req); - std::cout << recvMsg(msqid) << std::endl; + std::cout << resourceloc::resolve(inp) << std::endl; } } // namespace client diff --git a/src/data.hpp b/src/data.hpp index 6c099d7..602f407 100644 --- a/src/data.hpp +++ b/src/data.hpp @@ -6,21 +6,14 @@ const int clientbound_msg = 2; key_t getIpcKeyFromExeName(char *argv0); -enum RequestType { - map, - resolve_resource_loc, -}; - -struct RequestMsg { +struct RequestMapMsg { long msgtype; - RequestType type; - unsigned int datalen; char data[128]; }; -struct ResponseMsg { +struct ResponseMapMsg { long msgtype; unsigned int datalen; diff --git a/src/server.cpp b/src/server.cpp index 137b2e3..fbbd259 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -12,30 +12,16 @@ namespace server { void sendToClient(int msqid, std::string msg) { - data::ResponseMsg pkt; + data::ResponseMapMsg pkt; pkt.msgtype = data::clientbound_msg; pkt.datalen = msg.size(); std::copy(msg.begin(), msg.end(), pkt.data); - if (msgsnd(msqid, &pkt, sizeof(data::ResponseMsg) - sizeof(long), 0) < 0) { + if (msgsnd(msqid, &pkt, sizeof(data::ResponseMapMsg) - sizeof(long), 0) < 0) { BOOST_LOG_TRIVIAL(error) << "failed to send response " << strerror(errno); } } -void handleResolveResourceLoc(int msqid, data::RequestMsg *msg) { - std::string s(&msg->data[0], msg->datalen); - BOOST_LOG_TRIVIAL(info) << "got request to resolve resourceloc '" << s << "'"; - auto res = resourceloc::resolve(s); - - sendToClient(msqid, res); -} - -void handleMap(int msqid, mappings::Mappings *mappings, data::RequestMsg *msg) { - std::string s(&msg->data[0], msg->datalen); - BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'"; - sendToClient(msqid, mappings->map(s)); -} - void run(key_t key) { BOOST_LOG_TRIVIAL(info) << "starting server"; @@ -50,22 +36,17 @@ void run(key_t key) { auto mappings = mappings::Mappings::load(); - data::RequestMsg msg; + data::RequestMapMsg msg; for (;;) { - if (msgrcv(msqid, &msg, sizeof(data::RequestMsg) - sizeof(long), + if (msgrcv(msqid, &msg, sizeof(data::RequestMapMsg) - sizeof(long), data::serverbound_msg, 0) < 0) { BOOST_LOG_TRIVIAL(error) << "receive error " << strerror(errno); continue; } - switch (msg.type) { - case data::map: - handleMap(msqid, &mappings, &msg); - break; - case data::resolve_resource_loc: - handleResolveResourceLoc(msqid, &msg); - break; - } + std::string s(msg.data, msg.datalen); + BOOST_LOG_TRIVIAL(info) << "got request to map '" << s << "'"; + sendToClient(msqid, mappings.map(s)); } } } // namespace server