#include "server.hpp" #include "data.hpp" #include "mappings.hpp" #include "resourceloc.hpp" #include #include #include #include #include #include #include namespace server { void sendToClient(int msqid, std::string msg) { data::ResponseMsg 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) { 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"; auto msqid = msgget(key, 0664 | IPC_CREAT); if (msqid < 0) { BOOST_LOG_TRIVIAL(fatal) << "msgget fail"; exit(1); } BOOST_LOG_TRIVIAL(info) << "msqid " << msqid; auto mappings = mappings::Mappings::load(); data::RequestMsg msg; for (;;) { if (msgrcv(msqid, &msg, sizeof(data::RequestMsg) - 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; } } } } // namespace server