Files
integration/luprex/core/cpp/main.cpp

132 lines
3.8 KiB
C++
Raw Normal View History

2020-11-12 14:24:33 -05:00
2021-01-22 19:10:47 -05:00
#include "textgame.hpp"
#include "driver.hpp"
#include "drivenengine.hpp"
2021-10-12 13:54:08 -04:00
#include <iostream>
#include <iomanip>
2021-10-12 13:54:08 -04:00
void write_closed_message(Channel *ch, StreamBuffer *out) {
std::ostringstream oss;
oss << "Chan " << ch->chid() << " closed [" << ch->error() << "]\n";
out->write_bytes(oss.str());
}
void dump_lines(StreamBuffer *in, StreamBuffer *out, int chid) {
while (true) {
std::string l = in->readline();
if (l == "") break;
std::ostringstream oss;
oss << "Chan " << chid << ": " << l;
out->write_bytes(oss.str());
}
}
// This test allows input on stdin or on port 8085.
// You can type lines and see them echoed.
class TNTest1 : public DrivenEngine {
public:
2021-10-08 21:03:52 -04:00
std::vector<UniqueChannel> channels_;
virtual void event_init() {
2021-10-08 21:03:52 -04:00
listen_port(8085);
}
2021-10-08 21:03:52 -04:00
virtual void event_update() {
2021-10-08 21:03:52 -04:00
while (true) {
UniqueChannel ch = new_incoming_channel();
if (ch == nullptr) break;
ch->set_readline(true);
2021-10-08 21:03:52 -04:00
channels_.emplace_back(std::move(ch));
}
2021-10-08 21:03:52 -04:00
Channel *stdioch = get_stdio_channel();
dump_lines(stdioch->in(), stdioch->out(), 0);
std::vector<UniqueChannel> keep;
for (UniqueChannel &ch : channels_) {
dump_lines(ch->in(), stdioch->out(), ch->chid());
if (ch->closed()) {
2021-10-12 13:54:08 -04:00
write_closed_message(ch.get(), stdioch->out());
} else {
keep.emplace_back(std::move(ch));
}
}
channels_ = std::move(keep);
}
};
// This test connects to a public webserver and prints
// the output from the server.
class TNTest2 : public DrivenEngine {
public:
std::vector<UniqueChannel> channels_;
virtual void event_init() {
UniqueChannel ch = new_outgoing_channel("stanford.edu:80");
ch->out()->write_bytes("GET http://stanford.edu/index.html HTTP/1.1\n\n");
channels_.emplace_back(std::move(ch));
}
virtual void event_update() {
Channel *stdioch = get_stdio_channel();
dump_lines(stdioch->in(), stdioch->out(), 0);
std::vector<UniqueChannel> keep;
for (UniqueChannel &ch : channels_) {
dump_lines(ch->in(), stdioch->out(), ch->chid());
if (ch->closed()) {
write_closed_message(ch.get(), stdioch->out());
} else {
keep.emplace_back(std::move(ch));
}
}
channels_ = std::move(keep);
}
};
// This test produces a DNS resolution failure.
class TNTest3 : public DrivenEngine {
public:
std::vector<UniqueChannel> channels_;
virtual void event_init() {
UniqueChannel ch = new_outgoing_channel("akjsdkajshdakjshd.alk:80");
ch->out()->write_bytes("GET http://stanford.edu/index.html HTTP/1.1\n\n");
channels_.emplace_back(std::move(ch));
}
virtual void event_update() {
Channel *stdioch = get_stdio_channel();
dump_lines(stdioch->in(), stdioch->out(), 0);
std::vector<UniqueChannel> keep;
for (UniqueChannel &ch : channels_) {
dump_lines(ch->in(), stdioch->out(), ch->chid());
if (ch->closed()) {
write_closed_message(ch.get(), stdioch->out());
} else {
2021-10-08 21:03:52 -04:00
keep.emplace_back(std::move(ch));
}
}
2021-10-08 21:03:52 -04:00
channels_ = std::move(keep);
}
};
2020-11-12 14:24:33 -05:00
2021-10-12 13:54:08 -04:00
// This test just prints the time.
class TNTest4 : public DrivenEngine {
public:
int count;
virtual void event_init() {
count = 0;
}
virtual void event_update() {
std::cerr << std::fixed << std::setprecision(2) << get_clock() << " ";
count++;
if (count == 10) {
std::cerr << std::endl;
count = 0;
}
}
};
2020-11-12 14:24:33 -05:00
int main(int argc, char **argv)
{
2021-10-13 19:41:59 -04:00
TextGame tg;
driver_drive(&tg);
2020-11-12 14:24:33 -05:00
}