Files
integration/luprex/core/cpp/lpxclient.cpp

76 lines
2.3 KiB
C++
Raw Normal View History

#include "lpxclient.hpp"
#include "drivenengine.hpp"
#include "world.hpp"
#include "luaconsole.hpp"
#include "invocation.hpp"
#include "util.hpp"
#include <memory>
class LpxClient : public DrivenEngine {
public:
using StringVec = LuaConsole::StringVec;
UniqueWorld world_;
int64_t actor_id_;
InvocationQueue unack_;
UniqueChannel channel_;
LuaConsole console_;
Gui gui_;
int64_t gui_place_;
public:
virtual void event_init(int argc, char *argv[]) {
// Create the world model.
world_.reset(new World(util::WORLD_TYPE_C_SYNC));
// This is a temporary actor that will be used only until the server sends
// us the first difference transmission. We do this only to establish
// the invariant that there's always an actor. When the first difference
// transmission arrives, this actor may be deleted, or it may just be
// ignored, at the server's discretion.
actor_id_ = world_->create_login_actor();
// Establish a connection to the server.
channel_ = new_outgoing_channel("localhost:8085");
2021-11-04 14:49:25 -04:00
// Set the console prompt
get_stdio_channel()->set_prompt(console_.get_prompt());
}
void do_command(const util::StringVec &words) {
2021-11-04 14:49:25 -04:00
if (words.empty()) return;
stdostream() << "Command: ";
for (const std::string &word : words) {
stdostream() << word << " ";
}
stdostream() << std::endl;
}
virtual void event_update() {
// Check for keyboard input on stdin.
while (true) {
std::string line = get_stdio_channel()->in()->readline();
if (line == "") break;
console_.add(line);
2021-11-04 14:49:25 -04:00
get_stdio_channel()->set_prompt(console_.get_prompt());
do_command(console_.get_command());
}
// Check for communication from server..
if (channel_ != nullptr) {
if (channel_->closed()) {
stdostream() << "Server closed connection " << channel_->error() << std::endl;
channel_.reset();
// stop_driver();
} else {
// Implement reception of messages.
}
}
}
};
UniqueDrivenEngine make_LpxClient() {
return UniqueDrivenEngine(new LpxClient);
}