Builder is now using the FSM I wrote. Still not as clean as I like but big improvement.

master
Zed A. Shaw 2 months ago
parent dcf1a4020d
commit a7c5de6ac3
  1. 138
      builder.cpp
  2. 54
      builder.hpp
  3. 11
      fsm.hpp
  4. 14
      fsmtest.cpp

@ -1,12 +1,6 @@
#include "builder.hpp"
#include "dbc.hpp"
#include "watcher.hpp"
#include "game_engine.hpp"
#include <chrono> // for milliseconds
#include <efsw/efsw.hpp>
#include <fmt/chrono.h>
#include <fmt/color.h>
#include <fmt/core.h>
#include <fmt/core.h>
#include <fstream>
#include <git2.h>
@ -82,17 +76,46 @@ MatchResult Builder::parse_line(const string &line) {
}
}
enum BuildState {
WAITING, BUILDING, DONE, STARTING, READING
};
void Builder::run() {
int rc = gui.main_loop(game, [&] {
event(GO);
return 0;
});
if(rc != 0) println("ERROR IN GUI");
void Builder::run() {
git_repository* repo = nullptr;
event(QUIT);
}
void Builder::building(BuildEvent ev) {
println(">>> BUILDING");
// check if there's output
if(build_done) {
bool good = end_build(build_out, gui, build_cmd);
if(good) {
gui.build_works();
} else {
// BUG: make it not play two sounds
gui.build_failed(true, build_cmd);
}
build_out = NULL;
state(DONE);
} else {
auto m = parse_line(line);
if(m.match) {
gui.output(format("HIT WITH {} @ {}:{}:{} {}", m.type, m.file_name, m.lnumber, m.col, m.message));
game.hit(m.type);
}
state(READING);
}
}
try {
void Builder::start(BuildEvent ev) {
gui.output(format("Using build command: {}", build_cmd));
efsw::FileWatcher* fileWatcher = new efsw::FileWatcher();
fileWatcher = new efsw::FileWatcher();
dbc::check(fileWatcher != nullptr, "Failed to create filewatcher.");
git_libgit2_init();
@ -100,23 +123,17 @@ void Builder::run() {
int err = git_repository_open(&repo, git_path.c_str());
dbc::check(err == 0, git_error_last()->message);
UpdateListener* listener = new UpdateListener(repo);
listener = new UpdateListener(repo);
dbc::check(listener != nullptr, "Failed to create listener.");
gui.output(format("Watching directory {} for changes...", git_path));
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
FILE *build_out = NULL;
bool build_done = false;
string line = "";
BuildState state = WAITING;
std::future<FILE *> build_fut;
wid = fileWatcher->addWatch(git_path, listener, true);
fileWatcher->watch();
int rc = gui.main_loop(game, [&] {
switch(state) {
case WAITING:
state(WAITING);
}
void Builder::waiting(BuildEvent ev) {
if(listener->changes) {
game.start_round();
gui.building();
@ -124,56 +141,27 @@ void Builder::run() {
build_fut = std::async([&]() {
return start_command(build_cmd);
});
state = STARTING;
state(STARTING);
}
break;
}
case STARTING: {
println(">>> STARTING");
void Builder::starting(BuildEvent ev) {
std::future_status status = build_fut.wait_for(0ms);
if(status == std::future_status::ready) {
build_out = build_fut.get();
state = READING;
state(READING);
} else {
state = STARTING;
state(STARTING);
}
}
break;
}
case READING: {
void Builder::reading(BuildEvent ev) {
line = read_line(build_out, build_done);
state = BUILDING;
}
break;
case BUILDING: {
println(">>> BUILDING");
// check if there's output
if(build_done) {
bool good = end_build(build_out, gui, build_cmd);
if(good) {
gui.build_works();
} else {
// BUG: make it not play two sounds
gui.build_failed(true, build_cmd);
}
build_out = NULL;
state = DONE;
} else {
auto m = parse_line(line);
if(m.match) {
gui.output(format("HIT WITH {} @ {}:{}:{} {}", m.type, m.file_name, m.lnumber, m.col, m.message));
game.hit(m.type);
}
state = READING;
}
}
break;
state(BUILDING);
}
case DONE: {
void Builder::done(BuildEvent ev) {
game.end_round();
if(game.is_dead()) {
@ -183,21 +171,21 @@ void Builder::run() {
listener->reset_state();
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
state = WAITING;
}
break;
}
return 0;
});
dbc::check(rc == 0, "Invalid return from main_loop.");
state(WAITING);
}
void Builder::exit(BuildEvent ev) {
if(ev == QUIT) {
fileWatcher->removeWatch(wid);
git_libgit2_shutdown();
} catch(dbc::Error &err) {
state(EXIT);
}
}
void Builder::error(BuildEvent ev) {
// how to avoid doing this more than once?
if(ev == CRASH) {
if(repo != nullptr) git_repository_free(repo);
git_libgit2_shutdown();
throw err;
}
}

@ -2,6 +2,11 @@
#include "gui.hpp"
#include "game_engine.hpp"
#include <stdio.h>
#include "fsm.hpp"
#include <efsw/efsw.hpp>
#include <future>
#include <stdio.h>
#include "watcher.hpp"
using std::string;
@ -14,11 +19,28 @@ struct MatchResult {
string message = "";
};
class Builder {
enum BuildState {
START, WAITING, BUILDING, DONE, STARTING, READING,
EXIT, ERROR
};
enum BuildEvent {
GO, QUIT, CRASH
};
class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
GUI gui;
GameEngine game;
string git_path = "NOT SET";
string build_cmd = "NOT SET";
efsw::FileWatcher* fileWatcher = NULL;
UpdateListener* listener = NULL;
efsw::WatchID wid;
FILE *build_out = NULL;
bool build_done = false;
string line = "";
std::future<FILE *> build_fut;
git_repository* repo = nullptr;
public:
@ -27,4 +49,34 @@ class Builder {
MatchResult parse_line(const string &line);
void run();
void event(BuildEvent ev) override {
try {
if(ev == QUIT) {
exit(ev);
}
switch(_state) {
FSM_STATE(BUILDING, building, ev);
FSM_STATE(START, start, ev);
FSM_STATE(WAITING, waiting, ev);
FSM_STATE(DONE, done, ev);
FSM_STATE(STARTING, starting, ev);
FSM_STATE(READING, reading, ev);
FSM_STATE(EXIT, exit, ev);
FSM_STATE(ERROR, exit, ev);
}
} catch(...) {
error(ev);
}
}
void building(BuildEvent ev);
void start(BuildEvent ev);
void waiting(BuildEvent ev);
void done(BuildEvent ev);
void starting(BuildEvent ev);
void reading(BuildEvent ev);
void error(BuildEvent ev);
void exit(BuildEvent ev);
};

@ -1,5 +1,10 @@
#pragma once
#include <fmt/core.h>
#define FSM_EV(S, F) case S: F(); break
#define FSM_STATE(S, F, E) case S: fmt::println(">>> " #S ":" #F ":{}", int(E)); F(E); break
template<typename S, typename E>
class DeadSimpleFSM {
protected:
@ -11,6 +16,8 @@ public:
void state(S next_state) {
_state = next_state;
}
};
#define FSM_T(S, F) case S: F(); break
bool in_state(S state) {
return _state == state;
}
};

@ -14,24 +14,24 @@ enum MyEvent {
class MyFSM : DeadSimpleFSM<MyState, MyEvent> {
public:
void event(MyEvent ev) override {
switch(ev) {
FSM_T(STARTED, start);
FSM_T(PUSH, push);
FSM_T(QUIT, quit);
switch(_state) {
FSM_STATE(START, start, ev);
FSM_STATE(RUNNING, push, ev);
FSM_STATE(END, quit, ev);
}
}
void start() {
void start(MyEvent ev) {
println("<<< START");
state(RUNNING);
}
void push() {
void push(MyEvent ev) {
println("<<< RUN");
state(RUNNING);
}
void quit() {
void quit(MyEvent ev) {
println("<<< STOP");
state(END);
}

Loading…
Cancel
Save