2020-08-19 14:59:31 -06:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-19 14:59:31 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "UCIEndpoint.h"
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2021-01-24 15:28:26 +01:00
|
|
|
#include <AK/Debug.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-08-19 14:59:31 -06:00
|
|
|
#include <LibCore/EventLoop.h>
|
|
|
|
|
|
|
|
|
|
namespace Chess::UCI {
|
|
|
|
|
|
|
|
|
|
Endpoint::Endpoint(NonnullRefPtr<Core::IODevice> in, NonnullRefPtr<Core::IODevice> out)
|
|
|
|
|
: m_in(in)
|
|
|
|
|
, m_out(out)
|
|
|
|
|
, m_in_notifier(Core::Notifier::construct(in->fd(), Core::Notifier::Read))
|
|
|
|
|
{
|
|
|
|
|
set_in_notifier();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void Endpoint::send_command(Command const& command)
|
2020-08-19 14:59:31 -06:00
|
|
|
{
|
2022-12-06 01:12:49 +00:00
|
|
|
dbgln_if(UCI_DEBUG, "{} Sent UCI Command: {}", class_name(), DeprecatedString(command.to_deprecated_string().characters(), Chomp));
|
|
|
|
|
m_out->write(command.to_deprecated_string());
|
2020-08-19 14:59:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Endpoint::event(Core::Event& event)
|
|
|
|
|
{
|
2022-03-18 11:29:14 -06:00
|
|
|
switch (static_cast<Command::Type>(event.type())) {
|
2020-08-19 14:59:31 -06:00
|
|
|
case Command::Type::UCI:
|
|
|
|
|
return handle_uci();
|
|
|
|
|
case Command::Type::Debug:
|
2022-04-01 20:58:27 +03:00
|
|
|
return handle_debug(static_cast<DebugCommand const&>(event));
|
2020-08-19 14:59:31 -06:00
|
|
|
case Command::Type::IsReady:
|
|
|
|
|
return handle_uci();
|
|
|
|
|
case Command::Type::SetOption:
|
2022-04-01 20:58:27 +03:00
|
|
|
return handle_setoption(static_cast<SetOptionCommand const&>(event));
|
2020-08-19 14:59:31 -06:00
|
|
|
case Command::Type::Position:
|
2022-04-01 20:58:27 +03:00
|
|
|
return handle_position(static_cast<PositionCommand const&>(event));
|
2020-08-19 14:59:31 -06:00
|
|
|
case Command::Type::Go:
|
2022-04-01 20:58:27 +03:00
|
|
|
return handle_go(static_cast<GoCommand const&>(event));
|
2020-08-19 14:59:31 -06:00
|
|
|
case Command::Type::Stop:
|
|
|
|
|
return handle_stop();
|
|
|
|
|
case Command::Type::Id:
|
2022-04-01 20:58:27 +03:00
|
|
|
return handle_id(static_cast<IdCommand const&>(event));
|
2020-08-19 14:59:31 -06:00
|
|
|
case Command::Type::UCIOk:
|
|
|
|
|
return handle_uciok();
|
|
|
|
|
case Command::Type::ReadyOk:
|
|
|
|
|
return handle_readyok();
|
|
|
|
|
case Command::Type::BestMove:
|
2022-04-01 20:58:27 +03:00
|
|
|
return handle_bestmove(static_cast<BestMoveCommand const&>(event));
|
2020-08-19 14:59:31 -06:00
|
|
|
case Command::Type::Info:
|
2022-04-01 20:58:27 +03:00
|
|
|
return handle_info(static_cast<InfoCommand const&>(event));
|
2020-08-19 14:59:31 -06:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Endpoint::set_in_notifier()
|
|
|
|
|
{
|
|
|
|
|
m_in_notifier = Core::Notifier::construct(m_in->fd(), Core::Notifier::Read);
|
|
|
|
|
m_in_notifier->on_ready_to_read = [this] {
|
|
|
|
|
while (m_in->can_read_line())
|
|
|
|
|
Core::EventLoop::current().post_event(*this, read_command());
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NonnullOwnPtr<Command> Endpoint::read_command()
|
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString line(ReadonlyBytes(m_in->read_line(4096).bytes()), Chomp);
|
2020-08-19 14:59:31 -06:00
|
|
|
|
2021-05-01 21:10:08 +02:00
|
|
|
dbgln_if(UCI_DEBUG, "{} Received UCI Command: {}", class_name(), line);
|
2020-08-19 14:59:31 -06:00
|
|
|
|
|
|
|
|
if (line == "uci") {
|
|
|
|
|
return make<UCICommand>(UCICommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("debug"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<DebugCommand>(DebugCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("isready"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<IsReadyCommand>(IsReadyCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("setoption"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<SetOptionCommand>(SetOptionCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("position"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<PositionCommand>(PositionCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("go"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<GoCommand>(GoCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("stop"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<StopCommand>(StopCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("id"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<IdCommand>(IdCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("uciok"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<UCIOkCommand>(UCIOkCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("readyok"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<ReadyOkCommand>(ReadyOkCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("bestmove"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<BestMoveCommand>(BestMoveCommand::from_string(line));
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (line.starts_with("info"sv)) {
|
2020-08-19 14:59:31 -06:00
|
|
|
return make<InfoCommand>(InfoCommand::from_string(line));
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 13:12:07 +02:00
|
|
|
dbgln("command line: {}", line);
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-08-19 14:59:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|