Initial implementation of lpxclient
This commit is contained in:
@@ -19,16 +19,57 @@ public:
|
||||
int64_t gui_place_;
|
||||
|
||||
public:
|
||||
virtual void event_init(int argc, char *argv[]) {
|
||||
void set_initial_state() {
|
||||
// Create the world model.
|
||||
world_.reset(new World(util::WORLD_TYPE_C_SYNC));
|
||||
|
||||
// Snapshot the initial state.
|
||||
world_->snapshot();
|
||||
|
||||
// Clear the unack command queue.
|
||||
unack_.clear();
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
// When the world is in synchronous mode, there's no
|
||||
// snapshot, and the commands in the unack queue are not
|
||||
// reflected in the world. In asynchronous mode, there is
|
||||
// a snapshot that allows return to the synchronous mode,
|
||||
// and the unack commands are in the world model.
|
||||
|
||||
void world_to_synchronous() {
|
||||
if (!world_->snapshot_empty()) {
|
||||
world_->rollback();
|
||||
}
|
||||
}
|
||||
|
||||
void world_to_asynchronous() {
|
||||
if (world_->snapshot_empty()) {
|
||||
world_->snapshot();
|
||||
for (const Invocation &inv : unack_) {
|
||||
world_->invoke(inv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void abandon_server() {
|
||||
// Put the world model back into a known-good state.
|
||||
set_initial_state();
|
||||
|
||||
// Disconnect from the server.
|
||||
channel_.reset();
|
||||
}
|
||||
|
||||
virtual void event_init(int argc, char *argv[]) {
|
||||
// Put the world into the starting state.
|
||||
set_initial_state();
|
||||
|
||||
// Establish a connection to the server.
|
||||
channel_ = new_outgoing_channel("localhost:8085");
|
||||
@@ -37,15 +78,164 @@ public:
|
||||
get_stdio_channel()->set_prompt(console_.get_prompt());
|
||||
}
|
||||
|
||||
void do_command(const util::StringVec &words) {
|
||||
if (words.empty()) return;
|
||||
stdostream() << "Command: ";
|
||||
for (const std::string &word : words) {
|
||||
stdostream() << word << " ";
|
||||
void send_invocation(const Invocation &inv) {
|
||||
if (channel_ == nullptr) {
|
||||
stdostream() << "Cannot invoke any actions, not connected." << std::endl;
|
||||
return;
|
||||
}
|
||||
world_to_asynchronous();
|
||||
world_->invoke(inv);
|
||||
unack_.push_back(inv);
|
||||
StreamBuffer *sb = channel_->out();
|
||||
sb->write_uint8(util::MSG_INVOKE);
|
||||
inv.serialize(sb);
|
||||
}
|
||||
|
||||
void do_lua_command(const StringVec &words) {
|
||||
if (words.size() != 2) {
|
||||
stdostream() << "lua command (lua) takes a single string" << std::endl;
|
||||
return;
|
||||
}
|
||||
const std::string &exp = words[1];
|
||||
InvocationData dummyresult;
|
||||
Invocation inv(Invocation::KIND_LUA, actor_id_, actor_id_, exp, dummyresult);
|
||||
send_invocation(inv);
|
||||
}
|
||||
|
||||
void do_syntax_command(const StringVec &words) {
|
||||
stdostream() << "Syntax Error: ";
|
||||
for (int i = 1; i < int(words.size()); i++) {
|
||||
stdostream() << words[i] << " ";
|
||||
}
|
||||
stdostream() << std::endl;
|
||||
}
|
||||
|
||||
void do_view_command(const StringVec &cmd) {
|
||||
if (cmd.size() != 1) {
|
||||
stdostream() << "view command takes no arguments" << std::endl;
|
||||
return;
|
||||
}
|
||||
for (int64_t id : world_->get_near(actor_id_, 100, true)) {
|
||||
const Tangible *tan = world_->tangible_get(id);
|
||||
const AnimStep &aqback = tan->anim_queue_.back();
|
||||
stdostream() << id << ": " << aqback.graphic() << " " << aqback.plane() << " " << aqback.xyz().debug_string() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void do_menu_command(const StringVec &cmd) {
|
||||
int64_t id;
|
||||
if (cmd.size() == 1) {
|
||||
id = actor_id_;
|
||||
} else if (cmd.size() == 2) {
|
||||
id = util::strtoint(cmd[1], -1);
|
||||
} else {
|
||||
stdostream() << "menu command expects a tangible ID or defaults to actor_id" << std::endl;
|
||||
return;
|
||||
}
|
||||
world_to_asynchronous();
|
||||
gui_place_ = id;
|
||||
world_->update_gui(actor_id_, id, &gui_);
|
||||
int index = 0;
|
||||
for (const GuiElt &elt : gui_.elts()) {
|
||||
stdostream() << index << " " << elt.label() << std::endl;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void do_choose_command(const StringVec &cmd) {
|
||||
int64_t index;
|
||||
if (cmd.size() == 1) {
|
||||
index = util::strtoint(cmd[0], -1);
|
||||
} else {
|
||||
stdostream() << "choose command consists of a single menu line number" << std::endl;
|
||||
return;
|
||||
}
|
||||
const Gui::EltVec &elts = gui_.elts();
|
||||
if ((index < 0) || (index >= int(elts.size()))) {
|
||||
stdostream() << "No menu item #" << index << std::endl;
|
||||
return;
|
||||
}
|
||||
std::string action = elts[index].action();
|
||||
stdostream() << "Invoking plan: " << action << std::endl;
|
||||
InvocationData dummyresult;
|
||||
dummyresult["flavor"] = "chocolate";
|
||||
dummyresult["color"] = "blue";
|
||||
Invocation inv(Invocation::KIND_PLAN, actor_id_, gui_place_, action, dummyresult);
|
||||
send_invocation(inv);
|
||||
}
|
||||
|
||||
void do_quit_command(const util::StringVec &words) {
|
||||
if (words.size() != 1) {
|
||||
stdostream() << "quit command takes no arguments" << std::endl;
|
||||
return;
|
||||
}
|
||||
abandon_server();
|
||||
stop_driver();
|
||||
}
|
||||
|
||||
void do_command(const util::StringVec &words) {
|
||||
if (words.empty()) return;
|
||||
else if (words[0] == "lua") do_lua_command(words);
|
||||
else if (words[0] == "syntax") do_syntax_command(words);
|
||||
else if (words[0] == "view") do_view_command(words);
|
||||
else if (words[0] == "menu") do_menu_command(words);
|
||||
else if (words[0] == "quit") do_quit_command(words);
|
||||
else if (util::validinteger(words[0])) do_choose_command(words);
|
||||
else {
|
||||
stdostream() << "Unknown command: " << words[0] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void receive_ack_from_server(StreamBuffer *sb) {
|
||||
// An ack is just a single byte, so there's nothing left to read.
|
||||
if (unack_.empty()) {
|
||||
// Invalid acknowledgement when theres' nothing in the unack queue.
|
||||
abandon_server();
|
||||
return;
|
||||
}
|
||||
world_to_synchronous();
|
||||
world_->invoke(unack_.front());
|
||||
unack_.pop_front();
|
||||
}
|
||||
|
||||
void receive_diff_from_server(StreamBuffer *sb) {
|
||||
world_to_synchronous();
|
||||
try {
|
||||
actor_id_ = world_->patch_everything(sb);
|
||||
} catch (const StreamEof &seof) {
|
||||
abandon_server();
|
||||
return;
|
||||
} catch (const StreamCorruption &scorr) {
|
||||
abandon_server();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool receive_message_from_server(StreamBuffer *sb) {
|
||||
if (sb->fill() < 5) return false;
|
||||
int64_t tr_before = sb->total_reads();
|
||||
uint8_t message_type = sb->read_uint8();
|
||||
uint32_t message_body_len = sb->read_uint32();
|
||||
if (sb->fill() < message_body_len) {
|
||||
sb->unread_to(tr_before);
|
||||
return false;
|
||||
}
|
||||
StreamBuffer body(sb->read_bytes(message_body_len), message_body_len);
|
||||
if (message_type == util::MSG_ACK) {
|
||||
receive_ack_from_server(&body);
|
||||
} else if (message_type == util::MSG_DIFF) {
|
||||
receive_diff_from_server(&body);
|
||||
} else {
|
||||
abandon_server();
|
||||
return false;
|
||||
}
|
||||
if (!body.empty()) {
|
||||
abandon_server();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void event_update() {
|
||||
// Check for keyboard input on stdin.
|
||||
while (true) {
|
||||
@@ -60,10 +250,10 @@ public:
|
||||
if (channel_ != nullptr) {
|
||||
if (channel_->closed()) {
|
||||
stdostream() << "Server closed connection " << channel_->error() << std::endl;
|
||||
channel_.reset();
|
||||
// stop_driver();
|
||||
abandon_server();
|
||||
} else {
|
||||
// Implement reception of messages.
|
||||
while (receive_message_from_server(channel_->in()));
|
||||
world_to_asynchronous();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user