Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hardcoded pvp #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .cargo/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ JAVA_COMPILER_IMAGE="ghcr.io/delta/codecharacter-java-compiler:latest"
JAVA_RUNNER_IMAGE="ghcr.io/delta/codecharacter-java-runner:latest"
PYTHON_RUNNER_IMAGE="ghcr.io/delta/codecharacter-python-runner:latest"

MAX_LOG_SIZE="200000"
COMPILATION_TIME_LIMIT="5"
RUNTIME_TIME_LIMIT="10"
COMPILATION_MEMORY_LIMIT="300m"
RUNTIME_MEMORY_LIMIT="100m"
EPOLL_WAIT_TIMEOUT="30000"

RABBITMQ_HOST="amqp://guest:guest@localhost"
REQUEST_QUEUE="gameRequestQueue"
RESPONSE_QUEUE="gameStatusUpdateQueue"

MAX_LOG_SIZE=200000
COMPILATION_TIME_LIMIT=5
RUNTIME_TIME_LIMIT=10
COMPILATION_MEMORY_LIMIT=300m
RUNTIME_MEMORY_LIMIT=100m
EPOLL_WAIT_TIMEOUT=30_000
MAP_SIZE="64"

RABBIT_MQ_HOST=amqp://guest:guest@localhost
NORMAL_GAME_REQUEST_QUEUE=gameRequestQueue
PVP_GAME_REQUEST_QUEUE=gamePvpRequestQueue
GAME_RESPONSE_QUEUE=gameStatusUpdateQueue
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Cargo.lock
driver.log
codecharacter-driver-2022
config.toml
.vscode
*.txt
4 changes: 4 additions & 0 deletions driver_clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rm -rf /tmp/20f70cb3-1c90-4118-b916-3e78f1422e3e/
docker stop 20f70cb3-1c90-4118-b916-3e78f1422e3e_player2_cpp_runner
docker stop 20f70cb3-1c90-4118-b916-3e78f1422e3e_player1_cpp_runner
cargo run
Binary file added run
Binary file not shown.
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub enum SimulatorError {
FifoCreationError(String),
EpollError(String),
TimeOutError(String),
RabbitMqError(String),
}

#[derive(Debug)]
Expand Down
21 changes: 19 additions & 2 deletions src/fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs::{remove_file, File, OpenOptions};
use std::os::unix::prelude::{AsRawFd, OpenOptionsExt};

use crate::error::SimulatorError;
use nix::fcntl::{self, FcntlArg, OFlag};
use nix::fcntl::{self, FcntlArg, FdFlag, OFlag};
use nix::libc::O_NONBLOCK;
use nix::sys::stat;
use nix::unistd::mkfifo;
Expand All @@ -22,13 +22,25 @@ impl Fifo {
Err(e) => Err(SimulatorError::FifoCreationError(format!("{e}"))),
}
}

fn make_blocking(fd: i32) -> Result<(), SimulatorError> {
let mut flags = OFlag::from_bits_truncate(fcntl::fcntl(fd, FcntlArg::F_GETFL).unwrap());
flags.remove(OFlag::O_NONBLOCK);
fcntl::fcntl(fd, FcntlArg::F_SETFL(flags))
.map_err(|e| SimulatorError::FifoCreationError(format!("{e}")))?;
Ok(())
}

fn remove_close(fd: i32) -> Result<(), SimulatorError> {
let mut flags = FdFlag::from_bits_truncate(fcntl::fcntl(fd, FcntlArg::F_GETFD).unwrap());

flags.remove(FdFlag::FD_CLOEXEC);
fcntl::fcntl(fd, FcntlArg::F_SETFD(flags))
.map_err(|e| SimulatorError::FifoCreationError(format!("{e}")))?;

Ok(())
}

fn setup_pipe(f: &str) -> Result<(File, File), SimulatorError> {
Fifo::open_fifo(f)?;
let stdin = OpenOptions::new()
Expand All @@ -41,9 +53,13 @@ impl Fifo {
.open(f)
.map_err(|e| SimulatorError::FifoCreationError(format!("{e}")))?;
let stdin_fd = stdin.as_raw_fd();
let stdout_fd = stdout.as_raw_fd();
Fifo::make_blocking(stdin_fd)?;
Fifo::remove_close(stdin_fd)?;
Fifo::remove_close(stdout_fd)?;
Ok((stdin, stdout))
}

pub fn new(filename: String) -> Result<Self, SimulatorError> {
let (fin, fout) = Fifo::setup_pipe(&filename)?;
Ok(Self {
Expand All @@ -52,6 +68,7 @@ impl Fifo {
_out: Some(fout),
})
}

pub fn get_ends(&mut self) -> Option<(File, File)> {
match (self._in.take(), self._out.take()) {
(Some(_in), Some(_out)) => Some((_in, _out)),
Expand Down Expand Up @@ -91,7 +108,7 @@ mod fifo_tests {
assert_eq!(s1, s2);
assert_eq!(string, "Hello World".to_owned());

println!("{string}");
log::info!("{string}");
}
#[test]
fn added_data_to_fifo_before_running_cmd_is_saved() {
Expand Down
4 changes: 4 additions & 0 deletions src/game_dir.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use log::info;

#[derive(Debug)]
pub struct GameDir {
full_path: String,
}

impl GameDir {
pub fn new(game_id: &str) -> Option<Self> {
std::fs::create_dir(format!("/tmp/{game_id}")).ok()?;
info!("Created dir /tmp/{game_id}");
Some(GameDir {
full_path: format!("/tmp/{game_id}"),
})
Expand Down
Loading
Loading