Some parsing using nom
authorunc0rr
Sun, 08 Jan 2017 23:57:45 +0300
changeset 12138 81df2e1f9ae9
parent 12137 1525923cd7e3
child 12139 109e6765b1fc
Some parsing using nom
gameServer2/Cargo.toml
gameServer2/src/main.rs
gameServer2/src/protocol/messages.rs
gameServer2/src/protocol/mod.rs
gameServer2/src/protocol/parser.rs
--- a/gameServer2/Cargo.toml	Sat Jan 07 21:34:00 2017 +0300
+++ b/gameServer2/Cargo.toml	Sun Jan 08 23:57:45 2017 +0300
@@ -8,4 +8,4 @@
 mio = "0.6"
 slab = "0.3"
 netbuf = "0.3.8"
-nom = "^2.0"
+nom = "2.0"
--- a/gameServer2/src/main.rs	Sat Jan 07 21:34:00 2017 +0300
+++ b/gameServer2/src/main.rs	Sun Jan 08 23:57:45 2017 +0300
@@ -2,6 +2,8 @@
 extern crate mio;
 extern crate slab;
 extern crate netbuf;
+#[macro_use]
+extern crate nom;
 
 //use std::io::*;
 //use rand::Rng;
--- a/gameServer2/src/protocol/messages.rs	Sat Jan 07 21:34:00 2017 +0300
+++ b/gameServer2/src/protocol/messages.rs	Sun Jan 08 23:57:45 2017 +0300
@@ -4,67 +4,67 @@
 use std::convert::From;
 
 #[derive(PartialEq, Debug)]
-pub enum HWProtocolMessage {
+pub enum HWProtocolMessage<'a> {
     // core
     Ping,
     Pong,
-    Quit(Option<String>),
-    //Cmd(String, Vec<String>),
-    Global(String),
-    Watch(String),
+    Quit(Option<&'a str>),
+    //Cmd(&'a str, Vec<&'a str>),
+    Global(&'a str),
+    Watch(&'a str),
     ToggleServerRegisteredOnly,
     SuperPower,
-    Info(String),
+    Info(&'a str),
     // not entered state
-    Nick(String),
+    Nick(&'a str),
     Proto(u32),
-    Password(String, String),
-    Checker(String),
+    Password(&'a str, &'a str),
+    Checker(&'a str),
     // lobby
     List,
-    Chat(String),
-    CreateRoom(String, Option<String>),
-    Join(String, Option<String>),
-    Follow(String),
-    Rnd(Vec<String>),
-    Kick(String),
-    Ban(String, String, u32),
-    BanIP(String, String, u32),
-    BanNick(String, String, u32),
+    Chat(&'a str),
+    CreateRoom(&'a str, Option<&'a str>),
+    Join(&'a str, Option<&'a str>),
+    Follow(&'a str),
+    Rnd(Vec<&'a str>),
+    Kick(&'a str),
+    Ban(&'a str, &'a str, u32),
+    BanIP(&'a str, &'a str, u32),
+    BanNick(&'a str, &'a str, u32),
     BanList,
-    Unban(String),
+    Unban(&'a str),
     SetServerVar(ServerVar),
     GetServerVar,
     RestartServer,
     Stats,
     // in room
-    Part(Option<String>),
+    Part(Option<&'a str>),
     Cfg(GameCfg),
     AddTeam(TeamInfo),
-    RemoveTeam(String),
-    SetHedgehogsNumber(String, u8),
-    SetTeamColor(String, u8),
+    RemoveTeam(&'a str),
+    SetHedgehogsNumber(&'a str, u8),
+    SetTeamColor(&'a str, u8),
     ToggleReady,
     StartGame,
-    EngineMessage,
+    EngineMessage(&'a str),
     RoundFinished,
     ToggleRestrictJoin,
     ToggleRestrictTeams,
     ToggleRegisteredOnly,
-    RoomName(String),
-    Delegate(String),
-    TeamChat(String),
+    RoomName(&'a str),
+    Delegate(&'a str),
+    TeamChat(&'a str),
     MaxTeams(u8),
     Fix,
     Unfix,
-    Greeting(String),
-    CallVote(Option<(String, Option<String>)>),
-    Vote(String),
-    ForceVote(String),
-    Save(String, String),
-    Delete(String, String),
-    SaveRoom(String),
-    LoadRoom(String),
+    Greeting(&'a str),
+    CallVote(Option<(&'a str, Option<&'a str>)>),
+    Vote(&'a str),
+    ForceVote(&'a str),
+    Save(&'a str, &'a str),
+    Delete(&'a str, &'a str),
+    SaveRoom(&'a str),
+    LoadRoom(&'a str),
 }
 
 pub fn number<T: From<u8>
--- a/gameServer2/src/protocol/mod.rs	Sat Jan 07 21:34:00 2017 +0300
+++ b/gameServer2/src/protocol/mod.rs	Sun Jan 08 23:57:45 2017 +0300
@@ -3,6 +3,7 @@
 use std::io::Result;
 
 mod messages;
+mod parser;
 
 pub struct FrameDecoder {
     buf: netbuf::Buf,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/protocol/parser.rs	Sun Jan 08 23:57:45 2017 +0300
@@ -0,0 +1,44 @@
+use nom::*;
+
+use std::str;
+use std::str::FromStr;
+use super::messages::HWProtocolMessage;
+use super::messages::HWProtocolMessage::*;
+
+named!(end_of_message, tag!("\n\n"));
+named!(a_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8));
+//fn number_line<T>(input: &[u8]) -> IResult<&[u8], T> {
+//}
+named!(basic_message<&[u8], HWProtocolMessage>, alt!(
+    do_parse!(tag!("PING") >> (Ping))
+    | do_parse!(tag!("PONG") >> (Pong))
+    | do_parse!(tag!("LIST") >> (List))
+    | do_parse!(tag!("BANLIST") >> (BanList))
+    | do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar))
+    | do_parse!(tag!("TOGGLE_READY") >> (ToggleReady))
+    | do_parse!(tag!("START_GAME") >> (StartGame))
+    | do_parse!(tag!("ROUNDFINISHED") >> (RoundFinished))
+    | do_parse!(tag!("TOGGLE_RESTRICT_JOINS") >> (ToggleRestrictJoin))
+    | do_parse!(tag!("TOGGLE_RESTRICT_TEAMS") >> (ToggleRestrictTeams))
+    | do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly))
+));
+
+named!(one_param_message<&[u8], HWProtocolMessage>, alt!(
+    do_parse!(tag!("NICK") >> eol >> n: a_line >> (Nick(n)))
+    | do_parse!(tag!("PROTO") >> eol >> d: map_res!(a_line, FromStr::from_str) >> (Proto(d)))
+));
+
+named!(message<&[u8],HWProtocolMessage>, terminated!(alt!(
+    basic_message
+    | one_param_message
+), end_of_message));
+
+named!(messages<&[u8], Vec<HWProtocolMessage> >, many0!(message));
+
+#[test]
+fn parse_test() {
+    assert_eq!(message(b"PING\n\n"), IResult::Done(&b""[..], Ping));
+    assert_eq!(message(b"START_GAME\n\n"), IResult::Done(&b""[..], StartGame));
+    assert_eq!(message(b"NICK\nit's me\n\n"), IResult::Done(&b""[..], Nick("it's me")));
+    assert_eq!(message(b"PROTO\n51\n\n"), IResult::Done(&b""[..], Proto(51)));
+}