gameServer2/src/protocol/parser.rs
changeset 12140 23ee939ba66a
parent 12139 109e6765b1fc
child 12141 e25a82ce2374
equal deleted inserted replaced
12139:109e6765b1fc 12140:23ee939ba66a
     5 use super::messages::HWProtocolMessage;
     5 use super::messages::HWProtocolMessage;
     6 use super::messages::HWProtocolMessage::*;
     6 use super::messages::HWProtocolMessage::*;
     7 
     7 
     8 named!(end_of_message, tag!("\n\n"));
     8 named!(end_of_message, tag!("\n\n"));
     9 named!(a_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8));
     9 named!(a_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8));
       
    10 named!( u8_line<&[u8],  u8>, map_res!(a_line, FromStr::from_str));
       
    11 named!(u32_line<&[u8], u32>, map_res!(a_line, FromStr::from_str));
    10 named!(opt_param<&[u8], Option<&str> >, opt!(flat_map!(preceded!(eol, a_line), non_empty)));
    12 named!(opt_param<&[u8], Option<&str> >, opt!(flat_map!(preceded!(eol, a_line), non_empty)));
    11 
    13 
    12 named!(basic_message<&[u8], HWProtocolMessage>, alt!(
    14 named!(basic_message<&[u8], HWProtocolMessage>, alt!(
    13     do_parse!(tag!("PING") >> (Ping))
    15       do_parse!(tag!("PING") >> (Ping))
    14     | do_parse!(tag!("PONG") >> (Pong))
    16     | do_parse!(tag!("PONG") >> (Pong))
    15     | do_parse!(tag!("LIST") >> (List))
    17     | do_parse!(tag!("LIST") >> (List))
    16     | do_parse!(tag!("BANLIST") >> (BanList))
    18     | do_parse!(tag!("BANLIST")        >> (BanList))
    17     | do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar))
    19     | do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar))
    18     | do_parse!(tag!("TOGGLE_READY") >> (ToggleReady))
    20     | do_parse!(tag!("TOGGLE_READY")   >> (ToggleReady))
    19     | do_parse!(tag!("START_GAME") >> (StartGame))
    21     | do_parse!(tag!("START_GAME")     >> (StartGame))
    20     | do_parse!(tag!("ROUNDFINISHED") >> (RoundFinished))
    22     | do_parse!(tag!("ROUNDFINISHED")  >> (RoundFinished))
    21     | do_parse!(tag!("TOGGLE_RESTRICT_JOINS") >> (ToggleRestrictJoin))
    23     | do_parse!(tag!("TOGGLE_RESTRICT_JOINS")  >> (ToggleRestrictJoin))
    22     | do_parse!(tag!("TOGGLE_RESTRICT_TEAMS") >> (ToggleRestrictTeams))
    24     | do_parse!(tag!("TOGGLE_RESTRICT_TEAMS")  >> (ToggleRestrictTeams))
    23     | do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly))
    25     | do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly))
    24 ));
    26 ));
    25 
    27 
    26 named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
    28 named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
    27     do_parse!(tag!("NICK") >> eol >> n: a_line >> (Nick(n)))
    29       do_parse!(tag!("NICK")    >> eol >> n: a_line >> (Nick(n)))
    28     | do_parse!(tag!("PROTO") >> eol >> d: map_res!(a_line, FromStr::from_str) >> (Proto(d)))
    30     | do_parse!(tag!("INFO")    >> eol >> n: a_line >> (Info(n)))
    29     | do_parse!(tag!("QUIT") >> msg: opt_param >> (Quit(msg)))
    31     | do_parse!(tag!("CHAT")    >> eol >> m: a_line >> (Chat(m)))
       
    32     | do_parse!(tag!("FOLLOW")  >> eol >> n: a_line >> (Follow(n)))
       
    33     | do_parse!(tag!("KICK")    >> eol >> n: a_line >> (Kick(n)))
       
    34     | do_parse!(tag!("UNBAN")   >> eol >> n: a_line >> (Unban(n)))
       
    35     | do_parse!(tag!("EM")      >> eol >> m: a_line >> (EngineMessage(m)))
       
    36     | do_parse!(tag!("TEAMCHAT")    >> eol >> m: a_line >> (TeamChat(m)))
       
    37     | do_parse!(tag!("ROOM_NAME")   >> eol >> n: a_line >> (RoomName(n)))
       
    38     | do_parse!(tag!("REMOVE_TEAM") >> eol >> n: a_line >> (RemoveTeam(n)))
       
    39 
       
    40     | do_parse!(tag!("PROTO")   >> eol >> d: u32_line >> (Proto(d)))
       
    41 
       
    42     | do_parse!(tag!("QUIT")   >> msg: opt_param >> (Quit(msg)))
       
    43 ));
       
    44 
       
    45 named!(cmd_message<&[u8], HWProtocolMessage>, preceded!(tag!("CMD\n"), alt!(
       
    46       do_parse!(tag_no_case!("STATS") >> (Stats))
       
    47     | do_parse!(tag_no_case!("FIX")   >> (Fix))
       
    48     | do_parse!(tag_no_case!("UNFIX") >> (Unfix))
       
    49     | do_parse!(tag_no_case!("RESTART_SERVER") >> eol >> tag!("YES") >> (RestartServer))
       
    50     | do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly))
       
    51     | do_parse!(tag_no_case!("SUPER_POWER")     >> (SuperPower))
       
    52     | do_parse!(tag_no_case!("PART")     >> eol >> m: opt_param >> (Quit(m)))
       
    53     | do_parse!(tag_no_case!("QUIT")     >> eol >> m: opt_param >> (Part(m)))
       
    54     | do_parse!(tag_no_case!("DELEGATE") >> eol >> n: a_line  >> (Delegate(n)))
       
    55     | do_parse!(tag_no_case!("SAVEROOM") >> eol >> r: a_line  >> (SaveRoom(r)))
       
    56     | do_parse!(tag_no_case!("LOADROOM") >> eol >> r: a_line  >> (LoadRoom(r)))
       
    57     | do_parse!(tag_no_case!("DELETE")   >> eol >> r: a_line  >> (Delete(r)))
       
    58     | do_parse!(tag_no_case!("GLOBAL")   >> eol >> m: a_line  >> (Global(m)))
       
    59     | do_parse!(tag_no_case!("WATCH")    >> eol >> i: a_line  >> (Watch(i)))
       
    60     | do_parse!(tag_no_case!("GREETING") >> eol >> m: a_line  >> (Greeting(m)))
       
    61     | do_parse!(tag_no_case!("VOTE")     >> eol >> m: a_line  >> (Vote(m)))
       
    62     | do_parse!(tag_no_case!("FORCE")    >> eol >> m: a_line  >> (ForceVote(m)))
       
    63     | do_parse!(tag_no_case!("INFO")     >> eol >> n: a_line  >> (Info(n)))
       
    64     | do_parse!(tag_no_case!("MAXTEAMS") >> eol >> n: u8_line >> (MaxTeams(n)))
       
    65 )));
       
    66 
       
    67 named!(complex_message<&[u8], HWProtocolMessage>, alt!(
       
    68       do_parse!(tag!("PASSWORD")  >> eol >>
       
    69                     p: a_line     >> eol >>
       
    70                     s: a_line     >>
       
    71                     (Password(p, s)))
       
    72     | do_parse!(tag!("CHECKER")   >> eol >>
       
    73                     i: u32_line   >> eol >>
       
    74                     n: a_line     >> eol >>
       
    75                     p: a_line     >>
       
    76                     (Checker(i, n, p)))
       
    77     | do_parse!(tag!("CREATE_ROOM") >> eol >>
       
    78                     n: a_line       >>
       
    79                     p: opt_param    >>
       
    80                     (CreateRoom(n, p)))
       
    81     | do_parse!(tag!("JOIN")        >> eol >>
       
    82                     n: a_line       >>
       
    83                     p: opt_param    >>
       
    84                     (Join(n, p)))
       
    85     | do_parse!(tag!("BAN")    >> eol >>
       
    86                     n: a_line     >> eol >>
       
    87                     r: a_line     >> eol >>
       
    88                     t: u32_line   >>
       
    89                     (Ban(n, r, t)))
       
    90     | do_parse!(tag!("BAN_IP")    >> eol >>
       
    91                     n: a_line     >> eol >>
       
    92                     r: a_line     >> eol >>
       
    93                     t: u32_line   >>
       
    94                     (BanIP(n, r, t)))
       
    95     | do_parse!(tag!("BAN_NICK")    >> eol >>
       
    96                     n: a_line     >> eol >>
       
    97                     r: a_line     >> eol >>
       
    98                     t: u32_line   >>
       
    99                     (BanNick(n, r, t)))
    30 ));
   100 ));
    31 
   101 
    32 named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
   102 named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
    33     basic_message
   103       basic_message
    34     | one_param_message
   104     | one_param_message
    35 ), end_of_message));
   105     | cmd_message
       
   106     | complex_message
       
   107     ), end_of_message
       
   108 ));
    36 
   109 
    37 
   110 
    38 #[test]
   111 #[test]
    39 fn parse_test() {
   112 fn parse_test() {
    40     assert_eq!(message(b"PING\n\n"),          IResult::Done(&b""[..], Ping));
   113     assert_eq!(message(b"PING\n\n"),          IResult::Done(&b""[..], Ping));
    41     assert_eq!(message(b"START_GAME\n\n"),    IResult::Done(&b""[..], StartGame));
   114     assert_eq!(message(b"START_GAME\n\n"),    IResult::Done(&b""[..], StartGame));
    42     assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me")));
   115     assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me")));
    43     assert_eq!(message(b"PROTO\n51\n\n"),     IResult::Done(&b""[..], Proto(51)));
   116     assert_eq!(message(b"PROTO\n51\n\n"),     IResult::Done(&b""[..], Proto(51)));
    44     assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye"))));
   117     assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye"))));
    45     assert_eq!(message(b"QUIT\n\n"),          IResult::Done(&b""[..], Quit(None)));
   118     assert_eq!(message(b"QUIT\n\n"),          IResult::Done(&b""[..], Quit(None)));
       
   119     assert_eq!(message(b"CMD\nwatch\ndemo\n\n"), IResult::Done(&b""[..], Watch("demo")));
       
   120     assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), IResult::Done(&b""[..], Ban("me", "bad", 77)));
    46 }
   121 }