portingtools/src/client.cpp

62 lines
1.5 KiB
C++

#include "data.hpp"
#include "resourceloc.hpp"
#include <boost/log/trivial.hpp>
#include <cerrno>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <string>
#include <sys/msg.h>
namespace client {
void sendMsg(int msqid, data::RequestMapMsg* msg) {
if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) {
throw std::runtime_error(std::string("send error ") + strerror(errno));
}
}
std::string recvMsg(int msqid) {
data::ResponseMapMsg msg;
if (msgrcv(
msqid,
&msg,
sizeof(data::ResponseMapMsg) - sizeof(long),
data::clientbound_msg,
0
)
< 0) {
throw std::runtime_error(std::string("receive error ") + strerror(errno));
}
return std::string(&msg.data[0], msg.datalen);
}
void map(key_t key) {
std::string inp;
std::cin >> inp;
auto msqid = msgget(key, 0664);
if (msqid < 0) {
throw std::runtime_error("msgget fail. is the server running?");
}
data::RequestMapMsg req;
req.msgtype = data::serverbound_msg;
req.datalen = inp.size();
std::copy(inp.begin(), inp.end(), req.data);
sendMsg(msqid, &req);
std::cout << recvMsg(msqid) << std::endl;
}
void resolveResourceLoc(key_t key) {
std::string inp;
std::cin >> inp;
std::cout << resourceloc::resolve(inp) << std::endl;
}
}