author | alfadur |
Mon, 18 Jun 2018 09:22:53 -0400 | |
changeset 13421 | cdf69667593b |
parent 13124 | 1e39b8749072 |
child 13424 | 81e0ed105f5d |
permissions | -rw-r--r-- |
12138 | 1 |
use nom::*; |
2 |
||
13421 | 3 |
use std::{ |
4 |
str, str::FromStr, |
|
5 |
ops::Range |
|
6 |
}; |
|
7 |
use super::{ |
|
8 |
messages::{HWProtocolMessage, HWProtocolMessage::*}, |
|
9 |
test::gen_proto_msg |
|
10 |
}; |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
11 |
|
12138 | 12 |
named!(end_of_message, tag!("\n\n")); |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12145
diff
changeset
|
13 |
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:
12145
diff
changeset
|
14 |
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:
12145
diff
changeset
|
15 |
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:
12145
diff
changeset
|
16 |
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:
12145
diff
changeset
|
17 |
named!(opt_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(eol, str_line), non_empty), String::from))); |
12139 | 18 |
|
12138 | 19 |
named!(basic_message<&[u8], HWProtocolMessage>, alt!( |
12140 | 20 |
do_parse!(tag!("PING") >> (Ping)) |
12138 | 21 |
| do_parse!(tag!("PONG") >> (Pong)) |
22 |
| do_parse!(tag!("LIST") >> (List)) |
|
12140 | 23 |
| do_parse!(tag!("BANLIST") >> (BanList)) |
12138 | 24 |
| do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar)) |
12140 | 25 |
| do_parse!(tag!("TOGGLE_READY") >> (ToggleReady)) |
26 |
| do_parse!(tag!("START_GAME") >> (StartGame)) |
|
27 |
| do_parse!(tag!("ROUNDFINISHED") >> (RoundFinished)) |
|
28 |
| do_parse!(tag!("TOGGLE_RESTRICT_JOINS") >> (ToggleRestrictJoin)) |
|
29 |
| do_parse!(tag!("TOGGLE_RESTRICT_TEAMS") >> (ToggleRestrictTeams)) |
|
12138 | 30 |
| do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly)) |
31 |
)); |
|
32 |
||
33 |
named!(one_param_message<&[u8], HWProtocolMessage>, alt!( |
|
12140 | 34 |
do_parse!(tag!("NICK") >> eol >> n: a_line >> (Nick(n))) |
35 |
| do_parse!(tag!("INFO") >> eol >> n: a_line >> (Info(n))) |
|
36 |
| do_parse!(tag!("CHAT") >> eol >> m: a_line >> (Chat(m))) |
|
13421 | 37 |
| do_parse!(tag!("PART") >> msg: opt_param >> (Part(msg))) |
12140 | 38 |
| do_parse!(tag!("FOLLOW") >> eol >> n: a_line >> (Follow(n))) |
39 |
| do_parse!(tag!("KICK") >> eol >> n: a_line >> (Kick(n))) |
|
40 |
| do_parse!(tag!("UNBAN") >> eol >> n: a_line >> (Unban(n))) |
|
41 |
| do_parse!(tag!("EM") >> eol >> m: a_line >> (EngineMessage(m))) |
|
42 |
| do_parse!(tag!("TEAMCHAT") >> eol >> m: a_line >> (TeamChat(m))) |
|
43 |
| do_parse!(tag!("ROOM_NAME") >> eol >> n: a_line >> (RoomName(n))) |
|
44 |
| do_parse!(tag!("REMOVE_TEAM") >> eol >> n: a_line >> (RemoveTeam(n))) |
|
45 |
||
46 |
| do_parse!(tag!("PROTO") >> eol >> d: u32_line >> (Proto(d))) |
|
47 |
||
48 |
| do_parse!(tag!("QUIT") >> msg: opt_param >> (Quit(msg))) |
|
49 |
)); |
|
50 |
||
51 |
named!(cmd_message<&[u8], HWProtocolMessage>, preceded!(tag!("CMD\n"), alt!( |
|
52 |
do_parse!(tag_no_case!("STATS") >> (Stats)) |
|
53 |
| do_parse!(tag_no_case!("FIX") >> (Fix)) |
|
54 |
| do_parse!(tag_no_case!("UNFIX") >> (Unfix)) |
|
55 |
| do_parse!(tag_no_case!("RESTART_SERVER") >> eol >> tag!("YES") >> (RestartServer)) |
|
56 |
| do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly)) |
|
57 |
| do_parse!(tag_no_case!("SUPER_POWER") >> (SuperPower)) |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
58 |
| do_parse!(tag_no_case!("PART") >> m: opt_param >> (Part(m))) |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
59 |
| do_parse!(tag_no_case!("QUIT") >> m: opt_param >> (Quit(m))) |
12140 | 60 |
| do_parse!(tag_no_case!("DELEGATE") >> eol >> n: a_line >> (Delegate(n))) |
61 |
| do_parse!(tag_no_case!("SAVEROOM") >> eol >> r: a_line >> (SaveRoom(r))) |
|
62 |
| do_parse!(tag_no_case!("LOADROOM") >> eol >> r: a_line >> (LoadRoom(r))) |
|
63 |
| do_parse!(tag_no_case!("DELETE") >> eol >> r: a_line >> (Delete(r))) |
|
64 |
| do_parse!(tag_no_case!("GLOBAL") >> eol >> m: a_line >> (Global(m))) |
|
65 |
| do_parse!(tag_no_case!("WATCH") >> eol >> i: a_line >> (Watch(i))) |
|
66 |
| do_parse!(tag_no_case!("GREETING") >> eol >> m: a_line >> (Greeting(m))) |
|
67 |
| do_parse!(tag_no_case!("VOTE") >> eol >> m: a_line >> (Vote(m))) |
|
68 |
| do_parse!(tag_no_case!("FORCE") >> eol >> m: a_line >> (ForceVote(m))) |
|
69 |
| do_parse!(tag_no_case!("INFO") >> eol >> n: a_line >> (Info(n))) |
|
70 |
| do_parse!(tag_no_case!("MAXTEAMS") >> eol >> n: u8_line >> (MaxTeams(n))) |
|
71 |
))); |
|
72 |
||
73 |
named!(complex_message<&[u8], HWProtocolMessage>, alt!( |
|
74 |
do_parse!(tag!("PASSWORD") >> eol >> |
|
75 |
p: a_line >> eol >> |
|
76 |
s: a_line >> |
|
77 |
(Password(p, s))) |
|
78 |
| do_parse!(tag!("CHECKER") >> eol >> |
|
79 |
i: u32_line >> eol >> |
|
80 |
n: a_line >> eol >> |
|
81 |
p: a_line >> |
|
82 |
(Checker(i, n, p))) |
|
83 |
| do_parse!(tag!("CREATE_ROOM") >> eol >> |
|
84 |
n: a_line >> |
|
85 |
p: opt_param >> |
|
86 |
(CreateRoom(n, p))) |
|
13421 | 87 |
| do_parse!(tag!("JOIN_ROOM") >> eol >> |
12140 | 88 |
n: a_line >> |
89 |
p: opt_param >> |
|
13421 | 90 |
(JoinRoom(n, p))) |
12140 | 91 |
| do_parse!(tag!("BAN") >> eol >> |
92 |
n: a_line >> eol >> |
|
93 |
r: a_line >> eol >> |
|
94 |
t: u32_line >> |
|
95 |
(Ban(n, r, t))) |
|
96 |
| do_parse!(tag!("BAN_IP") >> eol >> |
|
97 |
n: a_line >> eol >> |
|
98 |
r: a_line >> eol >> |
|
99 |
t: u32_line >> |
|
100 |
(BanIP(n, r, t))) |
|
101 |
| do_parse!(tag!("BAN_NICK") >> eol >> |
|
102 |
n: a_line >> eol >> |
|
103 |
r: a_line >> eol >> |
|
104 |
t: u32_line >> |
|
105 |
(BanNick(n, r, t))) |
|
12138 | 106 |
)); |
107 |
||
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
108 |
named!(malformed_message<&[u8], HWProtocolMessage>, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
109 |
do_parse!(separated_list!(eol, a_line) >> (Malformed))); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
110 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
111 |
named!(empty_message<&[u8], HWProtocolMessage>, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
112 |
do_parse!(alt!(end_of_message | eol) >> (Empty))); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
113 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
114 |
named!(message<&[u8], HWProtocolMessage>, alt!(terminated!( |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
115 |
alt!( |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
116 |
basic_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
117 |
| one_param_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
118 |
| cmd_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
119 |
| complex_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
120 |
), end_of_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
121 |
) |
12145 | 122 |
| terminated!(malformed_message, end_of_message) |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
123 |
| empty_message |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
124 |
) |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
125 |
); |
12138 | 126 |
|
12141 | 127 |
named!(pub extract_messages<&[u8], Vec<HWProtocolMessage> >, many0!(complete!(message))); |
12138 | 128 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
129 |
proptest! { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
130 |
#[test] |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
131 |
fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
132 |
println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes()); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
133 |
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:
12147
diff
changeset
|
134 |
} |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
135 |
} |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
136 |
|
12138 | 137 |
#[test] |
138 |
fn parse_test() { |
|
12139 | 139 |
assert_eq!(message(b"PING\n\n"), IResult::Done(&b""[..], Ping)); |
140 |
assert_eq!(message(b"START_GAME\n\n"), IResult::Done(&b""[..], StartGame)); |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12145
diff
changeset
|
141 |
assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me".to_string()))); |
12139 | 142 |
assert_eq!(message(b"PROTO\n51\n\n"), IResult::Done(&b""[..], Proto(51))); |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12145
diff
changeset
|
143 |
assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye".to_string())))); |
12139 | 144 |
assert_eq!(message(b"QUIT\n\n"), IResult::Done(&b""[..], Quit(None))); |
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12145
diff
changeset
|
145 |
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:
12145
diff
changeset
|
146 |
assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), IResult::Done(&b""[..], Ban("me".to_string(), "bad".to_string(), 77))); |
12141 | 147 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
148 |
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:
12147
diff
changeset
|
149 |
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:
12147
diff
changeset
|
150 |
|
12145 | 151 |
assert_eq!(extract_messages(b"QUIT\n1\n2\n\n"), IResult::Done(&b""[..], vec![Malformed])); |
152 |
||
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
153 |
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:
12141
diff
changeset
|
154 |
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:
12141
diff
changeset
|
155 |
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:
12141
diff
changeset
|
156 |
assert_eq!(extract_messages(b"\n\n\nPING\n\n"), IResult::Done(&b""[..], vec![Empty, Empty, Ping])); |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
157 |
} |