Skip to content

Commit 5298db3

Browse files
shubham-1806bhoopesh369
authored andcommitted
feat:pvp
1 parent e9bfe5d commit 5298db3

File tree

5 files changed

+43
-153
lines changed

5 files changed

+43
-153
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ serde = { version = "1.0", features = ["derive"] }
1818
serde_json = "1.0"
1919
crossbeam-channel = "0.5.9"
2020
fs_extra = "1.3.0"
21-
walkdir = "2.3.2"

src/handlers.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, os::fd::AsRawFd};
1+
use std::env;
22

33
use log::info;
44
use nix::sys::epoll::EpollFlags;
@@ -48,10 +48,7 @@ fn handle_event(
4848
match entry {
4949
EpollEntryType::StdErr(_) => unreachable!(),
5050
EpollEntryType::Process(mut p) => {
51-
info!("For process {}", p.get_process().id());
5251
let exit_status = p.wait()?;
53-
println!("died: {}", p.get_process().id());
54-
5552
if exit_status.success() {
5653
res.push(None);
5754
} else {
@@ -271,6 +268,8 @@ impl Handler for PvPGameRequest {
271268
game_dir_handle.create_sub_dir(player1_dir);
272269
game_dir_handle.create_sub_dir(player2_dir);
273270

271+
//print all files in game_dir_handle
272+
274273
if let Some(resp) = copy_files(
275274
&self.game_id,
276275
&self.player1,
@@ -291,24 +290,25 @@ impl Handler for PvPGameRequest {
291290
return resp;
292291
}
293292

294-
info!("Copied files successfully");
295-
296293
let p1_in = format!("{}/p1_in", game_dir_handle.get_path());
297294
let p2_in = format!("{}/p2_in", game_dir_handle.get_path());
298295
let p3_in = format!("{}/p3_in", game_dir_handle.get_path());
299296
let p4_in = format!("{}/p4_in", game_dir_handle.get_path());
300297
let p5_in = format!("{}/p5_in", game_dir_handle.get_path());
301298

302-
let pipe1 = Fifo::new(p1_in);
303-
let pipe2 = Fifo::new(p2_in);
304-
let pipe3 = Fifo::new(p3_in);
305-
let pipe4 = Fifo::new(p4_in);
306-
let pipe5 = Fifo::new(p5_in);
299+
300+
301+
let pipe1 = Fifo::new(p1_in.to_owned());
302+
let pipe2 = Fifo::new(p2_in.to_owned());
303+
let pipe3 = Fifo::new(p3_in.to_owned());
304+
let pipe4 = Fifo::new(p4_in.to_owned());
305+
let pipe5 = Fifo::new(p5_in.to_owned());
306+
307307

308308
match (pipe1, pipe2, pipe3, pipe4, pipe5) {
309309
(Ok(mut p1), Ok(mut p2), Ok(mut p3), Ok(mut p4), Ok(mut p5)) => {
310-
let (sim_p1_r, p1_w) = p1.get_ends().unwrap();
311-
let (sim_p2_r, p2_w) = p2.get_ends().unwrap();
310+
let (_sim_p1_r, p1_w) = p1.get_ends().unwrap();
311+
let (_sim_p2_r, p2_w) = p2.get_ends().unwrap();
312312
let (p1_r, sim_p1_w) = p3.get_ends().unwrap();
313313
let (p2_r, sim_p2_w) = p4.get_ends().unwrap();
314314
let (sim_r, sim_w) = p5.get_ends().unwrap();
@@ -327,30 +327,19 @@ impl Handler for PvPGameRequest {
327327
&game_dir_handle,
328328
&player2_dir.to_string(),
329329
);
330-
info!("Got runners");
331330
let initialize = || -> Result<_, SimulatorError> {
332331
let mut player1_process = runner1.run(p1_r, p1_w, GameType::PvPGame)?;
333332
let mut player2_process = runner2.run(p2_r, p2_w, GameType::PvPGame)?;
334333
let simulator = simulator::Simulator::new(self.game_id.to_string());
335-
336334
let mut sim_process = simulator.run_pvp(
337335
sim_r,
338336
sim_w,
339-
sim_p1_r.as_raw_fd(),
340-
sim_p1_w.as_raw_fd(),
341-
sim_p2_r.as_raw_fd(),
342-
sim_p2_w.as_raw_fd(),
337+
p1_in,
338+
p3_in,
339+
p2_in,
340+
p4_in,
343341
)?;
344342

345-
println!("player1_process={}", player1_process.id());
346-
println!("player2_process={}", player2_process.id());
347-
println!("sim_process={}", sim_process.id());
348-
349-
println!("sim_p1_r={}", sim_p1_r.as_raw_fd());
350-
println!("sim_p1_w={}", sim_p1_w.as_raw_fd());
351-
println!("sim_p2_r={}", sim_p2_r.as_raw_fd());
352-
println!("sim_p2_w={}", sim_p2_w.as_raw_fd());
353-
354343
let player1_stderr = player1_process.stderr.take().unwrap();
355344
let player2_stderr = player2_process.stderr.take().unwrap();
356345

@@ -402,8 +391,6 @@ impl Handler for PvPGameRequest {
402391
Err(err) => return create_error_response(self.game_id.to_owned(), err),
403392
};
404393

405-
info!("Crossed first possib");
406-
407394
let mut outputs: Vec<ProcessOutput> = vec![];
408395

409396
while !event_handler.is_empty() {
@@ -416,7 +403,6 @@ impl Handler for PvPGameRequest {
416403
}
417404
}
418405

419-
info!("Crossed second possib");
420406

421407
let process1 = outputs.remove(0);
422408
let process2 = outputs.remove(0);

src/runner/cpp.rs

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::{
55
process::{Child, Command, Stdio},
66
};
77

8-
use log::info;
9-
108
use crate::error::SimulatorError;
119

1210
use super::{GameType, Runnable};
@@ -27,34 +25,8 @@ impl Runner {
2725
}
2826
}
2927

30-
// impl Runnable for Runner {
31-
// fn run(&self, stdin: File, stdout: File, game_type: GameType) -> Result<Child, SimulatorError> {
32-
// info!("Running the C++ runner process");
33-
34-
// info!("{}", self.player_dir);
35-
// info!("{}", self.current_dir);
36-
37-
// Command::new("/home/shubham/Desktop/Projects/Codecharacter/codecharacter-driver-2023/player_code/cpp/run")
38-
// .args([
39-
// &game_type.to_string(),
40-
// ])
41-
// .current_dir(&self.current_dir)
42-
// .create_pidfd(true)
43-
// .stdin(stdin)
44-
// .stdout(stdout)
45-
// .stderr(Stdio::piped())
46-
// .spawn()
47-
// .map_err(|err| {
48-
// SimulatorError::UnidentifiedError(format!(
49-
// "Couldnt spawn the C++ runner process: {err}"
50-
// ))
51-
// })
52-
// }
53-
// }
54-
55-
5628
impl Runnable for Runner {
57-
fn run(&self, stdin: File, stdout: File, game_type: GameType) -> Result<Child, SimulatorError> {
29+
fn run(&self, stdin: File, stdout: File, _game_type: GameType) -> Result<Child, SimulatorError> {
5830
let compile = Command::new("docker")
5931
.args([
6032
"run",
@@ -98,19 +70,7 @@ impl Runnable for Runner {
9870
return Err(SimulatorError::CompilationError(stderr));
9971
}
10072

101-
info!("Compiled succesfully");
102-
103-
info!("The dirs are {} {}", self.player_dir, self.current_dir);
104-
105-
//list files in a directory
106-
107-
let files = std::fs::read_dir(format!("{}/{}", self.current_dir, self.player_dir)).unwrap();
108-
for file in files {
109-
println!("Name: {}", file.unwrap().path().display());
110-
}
111-
112-
113-
let gg = Command::new("docker")
73+
Command::new("docker")
11474
.args([
11575
"run",
11676
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
@@ -143,8 +103,6 @@ impl Runnable for Runner {
143103
SimulatorError::UnidentifiedError(format!(
144104
"Couldnt spawn the C++ runner process: {err}"
145105
))
146-
});
147-
info!("Started the C++ runner process");
148-
gg
106+
})
149107
}
150108
}

src/runner/simulator.rs

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use std::fs::File;
22

3-
use std::os::fd::RawFd;
43
use std::os::linux::process::CommandExt;
54
use std::process::{Command, Stdio};
65

76
use std::env;
87

9-
use log::info;
10-
118
use crate::error::SimulatorError;
129

1310
pub struct Simulator {
@@ -23,36 +20,31 @@ impl Simulator {
2320
&self,
2421
stdin: File,
2522
stdout: File,
26-
p1_r: RawFd,
27-
p1_w: RawFd,
28-
p2_r: RawFd,
29-
p2_w: RawFd,
23+
p1_r: String,
24+
p1_w: String,
25+
p2_r: String,
26+
p2_w: String,
3027
) -> Result<std::process::Child, SimulatorError> {
31-
Command::new("/home/shubham/Desktop/Projects/Codecharacter/codecharacter-simulator-2023/build/bin/main")
28+
Command::new("docker")
3229
.args([
33-
// "run",
34-
// &format!("--memory={}", "100m"),
35-
// &format!(
36-
// "--memory-swap={}",
37-
// "100m"
38-
// ),
39-
// "--cpus=1",
40-
// "--ulimit",
41-
// &format!(
42-
// "cpu={}:{}",
43-
// "100",
44-
// "100"
45-
// ),
46-
// "--rm",
47-
// "--name",
48-
// &format!("{}_simulator", self.game_id),
49-
// "-i",
50-
// "pvp_sim",
30+
"run",
31+
&format!("--memory={}",env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
32+
&format!("--memory-swap={}",env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
33+
"--cpus=1",
34+
"--ulimit",
35+
&format!("cpu={}:{}",env::var("RUNTIME_TIME_LIMIT").unwrap(), env::var("RUNTIME_TIME_LIMIT").unwrap()),
36+
"--name",
37+
&format!("{}_simulator", self.game_id),
38+
"--rm",
39+
"-i",
40+
"-v",
41+
&format!("/tmp/{}:/tmp/{}",self.game_id,self.game_id),
42+
&env::var("SIMULATOR_IMAGE").unwrap(),
5143
"--type=PvP",
52-
&format!("p1_in={p1_r}"),
53-
&format!("p1_out={p1_w}"),
54-
&format!("p2_in={p2_r}"),
55-
&format!("p2_out={p2_w}"),
44+
&format!("p1_in={p1_r}"), //p1_in
45+
&format!("p1_out={p1_w}"), // p3_in
46+
&format!("p2_in={p2_r}"), // p2_in
47+
&format!("p2_out={p2_w}"), // p4_in
5648
])
5749
.create_pidfd(true)
5850
.stdin(stdin)
@@ -66,49 +58,6 @@ impl Simulator {
6658
})
6759
}
6860

69-
// pub fn run_pvp(
70-
// &self,
71-
// stdin: File,
72-
// stdout: File,
73-
// p1_r: RawFd,
74-
// p1_w: RawFd,
75-
// p2_r: RawFd,
76-
// p2_w: RawFd,
77-
// ) -> Result<std::process::Child, SimulatorError> {
78-
// info!("fds are {} {} {} {} ", p1_r, p1_w, p2_r, p2_w);
79-
// let rand = Command::new("docker")
80-
// .args([
81-
// "run",
82-
// &format!("--memory={}",env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
83-
// &format!("--memory-swap={}",env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
84-
// "--cpus=1",
85-
// "--ulimit",
86-
// &format!("cpu={}:{}",env::var("RUNTIME_TIME_LIMIT").unwrap(), env::var("RUNTIME_TIME_LIMIT").unwrap()),
87-
// "--rm",
88-
// "--name",
89-
// &format!("{}_simulator", self.game_id),
90-
// "-i",
91-
// &env::var("SIMULATOR_IMAGE").unwrap(),
92-
// "--type=PvP",
93-
// &format!("p1_in={p1_r}"),
94-
// &format!("p1_out={p1_w}"),
95-
// &format!("p2_in={p2_r}"),
96-
// &format!("p2_out={p2_w}"),
97-
// ])
98-
// .create_pidfd(true)
99-
// .stdin(stdin)
100-
// .stdout(stdout)
101-
// .stderr(Stdio::piped())
102-
// .spawn()
103-
// .map_err(|err| {
104-
// SimulatorError::UnidentifiedError(format!(
105-
// "Couldnt spawn the simulator process: {err}"
106-
// ))
107-
// });
108-
// info!("Simulator started");
109-
// rand
110-
// }
111-
11261
pub fn run(&self, stdin: File, stdout: File) -> Result<std::process::Child, SimulatorError> {
11362
Command::new("docker")
11463
.args([
@@ -122,7 +71,7 @@ impl Simulator {
12271
"--name",
12372
&format!("{}_simulator", self.game_id),
12473
"-i",
125-
"ghcr.io/delta/codecharacter-simulator:latest",
74+
&env::var("SIMULATOR_IMAGE").unwrap(),
12675
"--type=Normal",
12776
])
12877
.create_pidfd(true)

src/utils.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::{
55

66

77
use fs_extra::dir::CopyOptions;
8-
use log::info;
98

109
use crate::{
1110
create_error_response, error,
@@ -18,7 +17,6 @@ pub fn copy_dir_all(
1817
dst: impl AsRef<std::path::Path>,
1918
) -> std::io::Result<()> {
2019
let opt = CopyOptions::new();
21-
info!("Copying dir from {:?} to {:?}", src.as_ref(), dst.as_ref());
2220
for entry in std::fs::read_dir(src).unwrap() {
2321
let entry = entry?;
2422
let ty = entry.file_type()?;

0 commit comments

Comments
 (0)