author | alfadur |
Tue, 26 Jun 2018 23:22:38 +0300 | |
changeset 13422 | 5fb27f94fc3b |
parent 13419 | 81e0ed105f5d |
child 13423 | 87a6cad20c90 |
permissions | -rw-r--r-- |
12133 | 1 |
use nom::*; |
2 |
||
13416 | 3 |
use std::{ |
4 |
str, str::FromStr, |
|
5 |
ops::Range |
|
6 |
}; |
|
7 |
use super::{ |
|
8 |
messages::{HWProtocolMessage, HWProtocolMessage::*}, |
|
9 |
test::gen_proto_msg |
|
10 |
}; |
|
13419 | 11 |
use server::coretypes::{ |
13422 | 12 |
HedgehogInfo, TeamInfo, GameCfg |
13419 | 13 |
}; |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
14 |
|
12133 | 15 |
named!(end_of_message, tag!("\n\n")); |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
16 |
named!(str_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8)); |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
17 |
named!( a_line<&[u8], String>, map!(str_line, String::from)); |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
18 |
named!( u8_line<&[u8], u8>, map_res!(str_line, FromStr::from_str)); |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
19 |
named!(u32_line<&[u8], u32>, map_res!(str_line, FromStr::from_str)); |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
20 |
named!(opt_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(eol, str_line), non_empty), String::from))); |
13419 | 21 |
named!(hog_line<&[u8], HedgehogInfo>, |
22 |
do_parse!(name: str_line >> eol >> hat: str_line >> |
|
23 |
(HedgehogInfo{name: name.to_string(), hat: hat.to_string()}))); |
|
24 |
named!(_8_hogs<&[u8], [HedgehogInfo; 8]>, |
|
25 |
do_parse!(h1: hog_line >> eol >> h2: hog_line >> eol >> |
|
26 |
h3: hog_line >> eol >> h4: hog_line >> eol >> |
|
27 |
h5: hog_line >> eol >> h6: hog_line >> eol >> |
|
28 |
h7: hog_line >> eol >> h8: hog_line >> |
|
29 |
([h1, h2, h3, h4, h5, h6, h7, h8]))); |
|
12134 | 30 |
|
12133 | 31 |
named!(basic_message<&[u8], HWProtocolMessage>, alt!( |
12135 | 32 |
do_parse!(tag!("PING") >> (Ping)) |
12133 | 33 |
| do_parse!(tag!("PONG") >> (Pong)) |
34 |
| do_parse!(tag!("LIST") >> (List)) |
|
12135 | 35 |
| do_parse!(tag!("BANLIST") >> (BanList)) |
12133 | 36 |
| do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar)) |
12135 | 37 |
| do_parse!(tag!("TOGGLE_READY") >> (ToggleReady)) |
38 |
| do_parse!(tag!("START_GAME") >> (StartGame)) |
|
39 |
| do_parse!(tag!("ROUNDFINISHED") >> (RoundFinished)) |
|
40 |
| do_parse!(tag!("TOGGLE_RESTRICT_JOINS") >> (ToggleRestrictJoin)) |
|
41 |
| do_parse!(tag!("TOGGLE_RESTRICT_TEAMS") >> (ToggleRestrictTeams)) |
|
12133 | 42 |
| do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly)) |
43 |
)); |
|
44 |
||
45 |
named!(one_param_message<&[u8], HWProtocolMessage>, alt!( |
|
12135 | 46 |
do_parse!(tag!("NICK") >> eol >> n: a_line >> (Nick(n))) |
47 |
| do_parse!(tag!("INFO") >> eol >> n: a_line >> (Info(n))) |
|
48 |
| do_parse!(tag!("CHAT") >> eol >> m: a_line >> (Chat(m))) |
|
13416 | 49 |
| do_parse!(tag!("PART") >> msg: opt_param >> (Part(msg))) |
12135 | 50 |
| do_parse!(tag!("FOLLOW") >> eol >> n: a_line >> (Follow(n))) |
51 |
| do_parse!(tag!("KICK") >> eol >> n: a_line >> (Kick(n))) |
|
52 |
| do_parse!(tag!("UNBAN") >> eol >> n: a_line >> (Unban(n))) |
|
53 |
| do_parse!(tag!("EM") >> eol >> m: a_line >> (EngineMessage(m))) |
|
54 |
| do_parse!(tag!("TEAMCHAT") >> eol >> m: a_line >> (TeamChat(m))) |
|
55 |
| do_parse!(tag!("ROOM_NAME") >> eol >> n: a_line >> (RoomName(n))) |
|
56 |
| do_parse!(tag!("REMOVE_TEAM") >> eol >> n: a_line >> (RemoveTeam(n))) |
|
57 |
||
58 |
| do_parse!(tag!("PROTO") >> eol >> d: u32_line >> (Proto(d))) |
|
59 |
||
60 |
| do_parse!(tag!("QUIT") >> msg: opt_param >> (Quit(msg))) |
|
61 |
)); |
|
62 |
||
63 |
named!(cmd_message<&[u8], HWProtocolMessage>, preceded!(tag!("CMD\n"), alt!( |
|
64 |
do_parse!(tag_no_case!("STATS") >> (Stats)) |
|
65 |
| do_parse!(tag_no_case!("FIX") >> (Fix)) |
|
66 |
| do_parse!(tag_no_case!("UNFIX") >> (Unfix)) |
|
67 |
| do_parse!(tag_no_case!("RESTART_SERVER") >> eol >> tag!("YES") >> (RestartServer)) |
|
68 |
| do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly)) |
|
69 |
| do_parse!(tag_no_case!("SUPER_POWER") >> (SuperPower)) |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
70 |
| do_parse!(tag_no_case!("PART") >> m: opt_param >> (Part(m))) |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
71 |
| do_parse!(tag_no_case!("QUIT") >> m: opt_param >> (Quit(m))) |
12135 | 72 |
| do_parse!(tag_no_case!("DELEGATE") >> eol >> n: a_line >> (Delegate(n))) |
73 |
| do_parse!(tag_no_case!("SAVEROOM") >> eol >> r: a_line >> (SaveRoom(r))) |
|
74 |
| do_parse!(tag_no_case!("LOADROOM") >> eol >> r: a_line >> (LoadRoom(r))) |
|
75 |
| do_parse!(tag_no_case!("DELETE") >> eol >> r: a_line >> (Delete(r))) |
|
76 |
| do_parse!(tag_no_case!("GLOBAL") >> eol >> m: a_line >> (Global(m))) |
|
77 |
| do_parse!(tag_no_case!("WATCH") >> eol >> i: a_line >> (Watch(i))) |
|
78 |
| do_parse!(tag_no_case!("GREETING") >> eol >> m: a_line >> (Greeting(m))) |
|
79 |
| do_parse!(tag_no_case!("VOTE") >> eol >> m: a_line >> (Vote(m))) |
|
80 |
| do_parse!(tag_no_case!("FORCE") >> eol >> m: a_line >> (ForceVote(m))) |
|
81 |
| do_parse!(tag_no_case!("INFO") >> eol >> n: a_line >> (Info(n))) |
|
82 |
| do_parse!(tag_no_case!("MAXTEAMS") >> eol >> n: u8_line >> (MaxTeams(n))) |
|
83 |
))); |
|
84 |
||
85 |
named!(complex_message<&[u8], HWProtocolMessage>, alt!( |
|
86 |
do_parse!(tag!("PASSWORD") >> eol >> |
|
87 |
p: a_line >> eol >> |
|
88 |
s: a_line >> |
|
89 |
(Password(p, s))) |
|
90 |
| do_parse!(tag!("CHECKER") >> eol >> |
|
91 |
i: u32_line >> eol >> |
|
92 |
n: a_line >> eol >> |
|
93 |
p: a_line >> |
|
94 |
(Checker(i, n, p))) |
|
95 |
| do_parse!(tag!("CREATE_ROOM") >> eol >> |
|
96 |
n: a_line >> |
|
97 |
p: opt_param >> |
|
98 |
(CreateRoom(n, p))) |
|
13416 | 99 |
| do_parse!(tag!("JOIN_ROOM") >> eol >> |
12135 | 100 |
n: a_line >> |
101 |
p: opt_param >> |
|
13416 | 102 |
(JoinRoom(n, p))) |
13419 | 103 |
| do_parse!(tag!("ADD_TEAM") >> eol >> |
104 |
name: a_line >> eol >> |
|
105 |
color: u8_line >> eol >> |
|
106 |
grave: a_line >> eol >> |
|
107 |
fort: a_line >> eol >> |
|
108 |
voice_pack: a_line >> eol >> |
|
109 |
flag: a_line >> eol >> |
|
110 |
difficulty: u8_line >> eol >> |
|
111 |
hedgehogs: _8_hogs >> |
|
112 |
(AddTeam(TeamInfo{ |
|
113 |
name, color, grave, fort, |
|
114 |
voice_pack, flag, difficulty, |
|
115 |
hedgehogs, hedgehogs_number: 0 |
|
116 |
}))) |
|
117 |
| do_parse!(tag!("HH_NUM") >> eol >> |
|
118 |
n: a_line >> eol >> |
|
119 |
c: u8_line >> |
|
120 |
(SetHedgehogsNumber(n, c))) |
|
121 |
| do_parse!(tag!("TEAM_COLOR") >> eol >> |
|
122 |
n: a_line >> eol >> |
|
123 |
c: u8_line >> |
|
124 |
(SetTeamColor(n, c))) |
|
12135 | 125 |
| do_parse!(tag!("BAN") >> eol >> |
126 |
n: a_line >> eol >> |
|
127 |
r: a_line >> eol >> |
|
128 |
t: u32_line >> |
|
129 |
(Ban(n, r, t))) |
|
130 |
| do_parse!(tag!("BAN_IP") >> eol >> |
|
131 |
n: a_line >> eol >> |
|
132 |
r: a_line >> eol >> |
|
133 |
t: u32_line >> |
|
134 |
(BanIP(n, r, t))) |
|
135 |
| do_parse!(tag!("BAN_NICK") >> eol >> |
|
136 |
n: a_line >> eol >> |
|
137 |
r: a_line >> eol >> |
|
138 |
t: u32_line >> |
|
139 |
(BanNick(n, r, t))) |
|
12133 | 140 |
)); |
141 |
||
13422 | 142 |
named!(cfg_message<&[u8], HWProtocolMessage>, preceded!(tag!("CFG\n"), map!(alt!( |
143 |
do_parse!(tag!("THEME") >> eol >> |
|
144 |
name: a_line >> |
|
145 |
(GameCfg::Theme(name))) |
|
146 |
| do_parse!(tag!("SCRIPT") >> eol >> |
|
147 |
name: a_line >> |
|
148 |
(GameCfg::Script(name))) |
|
149 |
| do_parse!(tag!("AMMO") >> eol >> |
|
150 |
name: a_line >> |
|
151 |
value: opt_param >> |
|
152 |
(GameCfg::Ammo(name, value))) |
|
153 |
| do_parse!(tag!("SCHEME") >> eol >> |
|
154 |
name: a_line >> eol >> |
|
155 |
values: separated_list!(eol, a_line) >> |
|
156 |
(GameCfg::Scheme(name, |
|
157 |
if values.is_empty() {None} else {Some(values)}))) |
|
158 |
| do_parse!(tag!("FEATURE_SIZE") >> eol >> |
|
159 |
value: u32_line >> |
|
160 |
(GameCfg::FeatureSize(value))) |
|
161 |
| do_parse!(tag!("MAP") >> eol >> |
|
162 |
value: a_line >> |
|
163 |
(GameCfg::MapType(value))) |
|
164 |
| do_parse!(tag!("MAPGEN") >> eol >> |
|
165 |
value: u32_line >> |
|
166 |
(GameCfg::MapGenerator(value))) |
|
167 |
| do_parse!(tag!("MAZE_SIZE") >> eol >> |
|
168 |
value: u32_line >> |
|
169 |
(GameCfg::MazeSize(value))) |
|
170 |
| do_parse!(tag!("SEED") >> eol >> |
|
171 |
value: a_line >> |
|
172 |
(GameCfg::Seed(value))) |
|
173 |
| do_parse!(tag!("TEMPLATE") >> eol >> |
|
174 |
value: u32_line >> |
|
175 |
(GameCfg::Template(value))) |
|
176 |
| do_parse!(tag!("DRAWNMAP") >> eol >> |
|
177 |
value: a_line >> |
|
178 |
(GameCfg::DrawnMap(value))) |
|
179 |
), Cfg))); |
|
180 |
||
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
181 |
named!(malformed_message<&[u8], HWProtocolMessage>, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
182 |
do_parse!(separated_list!(eol, a_line) >> (Malformed))); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
183 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
184 |
named!(empty_message<&[u8], HWProtocolMessage>, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
185 |
do_parse!(alt!(end_of_message | eol) >> (Empty))); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
186 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
187 |
named!(message<&[u8], HWProtocolMessage>, alt!(terminated!( |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
188 |
alt!( |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
189 |
basic_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
190 |
| one_param_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
191 |
| cmd_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
192 |
| complex_message |
13422 | 193 |
| cfg_message |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
194 |
), end_of_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
195 |
) |
12140 | 196 |
| terminated!(malformed_message, end_of_message) |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
197 |
| empty_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
198 |
) |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
199 |
); |
12133 | 200 |
|
12136 | 201 |
named!(pub extract_messages<&[u8], Vec<HWProtocolMessage> >, many0!(complete!(message))); |
12133 | 202 |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
203 |
proptest! { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
204 |
#[test] |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
205 |
fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
206 |
println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes()); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
207 |
assert_eq!(message(msg.to_raw_protocol().as_bytes()), IResult::Done(&b""[..], msg.clone())) |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
208 |
} |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
209 |
} |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
210 |
|
12133 | 211 |
#[test] |
212 |
fn parse_test() { |
|
12134 | 213 |
assert_eq!(message(b"PING\n\n"), IResult::Done(&b""[..], Ping)); |
214 |
assert_eq!(message(b"START_GAME\n\n"), IResult::Done(&b""[..], StartGame)); |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
215 |
assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me".to_string()))); |
12134 | 216 |
assert_eq!(message(b"PROTO\n51\n\n"), IResult::Done(&b""[..], Proto(51))); |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
217 |
assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye".to_string())))); |
12134 | 218 |
assert_eq!(message(b"QUIT\n\n"), IResult::Done(&b""[..], Quit(None))); |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
219 |
assert_eq!(message(b"CMD\nwatch\ndemo\n\n"), IResult::Done(&b""[..], Watch("demo".to_string()))); |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12140
diff
changeset
|
220 |
assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), IResult::Done(&b""[..], Ban("me".to_string(), "bad".to_string(), 77))); |
12136 | 221 |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
222 |
assert_eq!(message(b"CMD\nPART\n\n"), IResult::Done(&b""[..], Part(None))); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
223 |
assert_eq!(message(b"CMD\nPART\n_msg_\n\n"), IResult::Done(&b""[..], Part(Some("_msg_".to_string())))); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
224 |
|
12140 | 225 |
assert_eq!(extract_messages(b"QUIT\n1\n2\n\n"), IResult::Done(&b""[..], vec![Malformed])); |
226 |
||
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
227 |
assert_eq!(extract_messages(b"PING\n\nPING\n\nP"), IResult::Done(&b"P"[..], vec![Ping, Ping])); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
228 |
assert_eq!(extract_messages(b"SING\n\nPING\n\n"), IResult::Done(&b""[..], vec![Malformed, Ping])); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
229 |
assert_eq!(extract_messages(b"\n\n\n\nPING\n\n"), IResult::Done(&b""[..], vec![Empty, Empty, Ping])); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
230 |
assert_eq!(extract_messages(b"\n\n\nPING\n\n"), IResult::Done(&b""[..], vec![Empty, Empty, Ping])); |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12142
diff
changeset
|
231 |
} |