-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulator.rs
102 lines (95 loc) · 3.09 KB
/
simulator.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::fs::File;
use std::os::linux::process::CommandExt;
use std::process::{Command, Stdio};
use std::env;
use crate::error::SimulatorError;
pub struct Simulator {
game_id: String,
}
impl Simulator {
pub fn new(game_id: String) -> Self {
Simulator { game_id }
}
pub fn run_pvp(
&self,
stdin: File,
stdout: File,
p1_r: String,
p1_w: String,
p2_r: String,
p2_w: String,
) -> Result<std::process::Child, SimulatorError> {
Command::new("docker")
.args([
"run",
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
&format!(
"--memory-swap={}",
env::var("RUNTIME_MEMORY_LIMIT").unwrap()
),
"--cpus=1",
"--ulimit",
&format!(
"cpu={}:{}",
env::var("RUNTIME_TIME_LIMIT").unwrap(),
env::var("RUNTIME_TIME_LIMIT").unwrap()
),
"--name",
&format!("{}_simulator", self.game_id),
"--rm",
"-i",
"-v",
&format!("/tmp/{}:/tmp/{}", self.game_id, self.game_id),
&env::var("SIMULATOR_IMAGE").unwrap(),
"--type=PvP",
&format!("p1_in={p1_r}"), //p1_in
&format!("p1_out={p1_w}"), // p3_in
&format!("p2_in={p2_r}"), // p2_in
&format!("p2_out={p2_w}"), // p4_in
])
.create_pidfd(true)
.stdin(stdin)
.stdout(stdout)
.stderr(Stdio::piped())
.spawn()
.map_err(|err| {
SimulatorError::UnidentifiedError(format!(
"Couldnt spawn the simulator process: {err}"
))
})
}
pub fn run(&self, stdin: File, stdout: File) -> Result<std::process::Child, SimulatorError> {
Command::new("docker")
.args([
"run",
&format!("--memory={}", env::var("RUNTIME_MEMORY_LIMIT").unwrap()),
&format!(
"--memory-swap={}",
env::var("RUNTIME_MEMORY_LIMIT").unwrap()
),
"--cpus=1",
"--ulimit",
&format!(
"cpu={}:{}",
env::var("RUNTIME_TIME_LIMIT").unwrap(),
env::var("RUNTIME_TIME_LIMIT").unwrap()
),
"--rm",
"--name",
&format!("{}_simulator", self.game_id),
"-i",
&env::var("SIMULATOR_IMAGE").unwrap(),
"--type=Normal",
])
.create_pidfd(true)
.stdin(stdin)
.stdout(stdout)
.stderr(Stdio::piped())
.spawn()
.map_err(|err| {
SimulatorError::UnidentifiedError(format!(
"Couldnt spawn the simulator process: {err}"
))
})
}
}