From af97284f0d3fc154d0aefca85fa78b25e481c256 Mon Sep 17 00:00:00 2001 From: fcolin Date: Thu, 1 Feb 2007 10:08:30 +0000 Subject: Utilisateur : Fcolin Date : 3/07/06 Heure : 15:40 Archivé dans $/CSharp/Ivy/IvyProbeConsole Commentaire: revision de l'analyde de commande ! (vss 16) --- CSharp/Ivy/IvyProbeConsole/IvyProbeConsole.cs | 184 ++++++++++++++------------ 1 file changed, 99 insertions(+), 85 deletions(-) (limited to 'CSharp') diff --git a/CSharp/Ivy/IvyProbeConsole/IvyProbeConsole.cs b/CSharp/Ivy/IvyProbeConsole/IvyProbeConsole.cs index 691e544..516d702 100644 --- a/CSharp/Ivy/IvyProbeConsole/IvyProbeConsole.cs +++ b/CSharp/Ivy/IvyProbeConsole/IvyProbeConsole.cs @@ -71,7 +71,27 @@ namespace IvyProbeConsole private volatile Thread looperThread; private Ivy bus; private bool timestamp, quiet, debug, exitOnDie = false; - private Regex directMsgRE; + enum Command { + direct, + die, + unbind, + bind, + ping, + quit, + bye, + list, + help}; + const string reg_parse = + @"^\.(?direct) (?[^ ]*) (?[0-9]+) (?.*)$|" + + @"^\.(?die) (?.*)$|" + + @"^\.(?unbind) (?.*)$|" + + @"^\.(?bind) (?.*)$|" + + @"^\.(?ping) (?.*)$|" + + @"^\.(?quit)$|" + + @"^\.(?bye)$|" + + @"^\.(?list)$|" + + @"^\.(?help)$|"; + private Regex cmd_regexp; [STAThread] public static void Main(String[] args) @@ -161,7 +181,7 @@ namespace IvyProbeConsole this.debug = debug; try { - directMsgRE = new Regex("^\\.direct ([^ ]*) ([0-9]+) (.*)"); + cmd_regexp = new Regex(reg_parse, RegexOptions.IgnoreCase); } catch (ArgumentException ree) { @@ -217,94 +237,92 @@ namespace IvyProbeConsole s = in_Renamed.ReadLine(); if (s == null) break; - parseCommand(s); + if (s.Length == 0) continue; + if ( s.StartsWith( ".") ) + parseCommand(s); + else + { + println("-> Sent to " + bus.SendMsg(s) + " peers"); + } } println("End of input. Good bye !"); bus.Stop(); traceDebug("Probe Thread stopped"); } - internal void parseCommand(String s) + internal void parseCommand(string s) { Match result; traceDebug("parsing the [" + s + "] (length " + s.Length + ") string"); - // crude parsing of the ".xyz" commands - // TODO use regexps instends of String.lastIndexOf(String). Example - // provided with .direct ! - if (s.Length == 0) - { - println("-> Sent to " + bus.SendMsg(s) + " peers"); - } - else if ((result = directMsgRE.Match(s)).Success) - { - String target = result.Groups[1].ToString(); - ushort id = ushort.Parse(result.Groups[2].ToString()); - String message = result.Groups[3].ToString(); - List v = bus.GetClientsByName(target); - if (v.Count == 0) - println("no Ivy client with the name \"" + target + "\""); - for (int i = 0; i < v.Count; i++) - v[i].SendDirectMsg(id, message); - return ; - } - else if (s.LastIndexOf(".die ") >= 0) - { - String target = s.Substring(5); - if (bus.Die(target, ".die command") == 0) - println("no Ivy client with the name \"" + target + "\""); - } - else if (s.LastIndexOf(".unbind ") >= 0) - { - String regexp = s.Substring(8); - if (bus.UnbindMsg(regexp)) - { - println("you want to unsubscribe to " + regexp); - } - else - { - println("you can't unsubscribe to " + regexp + ", your're not subscribed to it"); - } - } - else if (s.LastIndexOf(".bind ") >= 0) - { - String regexp = s.Substring(6); - println("you want to subscribe to " + regexp); - bus.BindMsg(regexp, receive); - } - else if (s.LastIndexOf(".ping ") >= 0) - { - String target = s.Substring(6); - if (bus.Ping( target, "test") == 0) - { - println("no Ivy client with the name \"" + target + "\""); - } - } - else if ((s.LastIndexOf(".quit") >= 0) || (s.LastIndexOf(".bye") >= 0)) - { - bus.Stop(); - System.Environment.Exit(0); - } - else if (s.LastIndexOf(".list") >= 0) - { - println(bus.IvyClients.Count + " clients on the bus"); - foreach (IvyClient client in bus.IvyClients ) - { - println("-> " + client.ApplicationName); - } - } - else if (s.LastIndexOf(".help") >= 0) - { - println(helpCommands); - } - else if (s[0] == '.') - { - println("this command is not recognized"); - println(helpCommands); - } - else + + result = cmd_regexp.Match(s); + + + if (result.Success) { - println("-> Sent to " + bus.SendMsg(s) + " peers"); - } + string cmd = result.Groups["cmd"].Value; + string target; + try + { + Command TheCommand = (Command)Enum.Parse(typeof(Command), cmd); + + switch ( TheCommand ) + { + case Command.direct: + { + target = result.Groups["target"].Value; + ushort id = ushort.Parse(result.Groups["id"].Value); + string message = result.Groups["message"].Value; + List v = bus.GetClientsByName(target); + if (v.Count == 0) + println("no Ivy client with the name \"" + target + "\""); + for (int i = 0; i < v.Count; i++) + v[i].SendDirectMsg(id, message); + } + break; + case Command.die: + target = result.Groups["target"].Value; + if (bus.Die(target, ".die command") == 0) + println("no Ivy client with the name \"" + target + "\""); + break; + case Command.unbind: + target = result.Groups["target"].Value; + if (bus.UnbindMsg(target)) + println("you want to unsubscribe to " + target); + else + println("you can't unsubscribe to " + target + ", your're not subscribed to it"); + break; + case Command.bind: + target = result.Groups["target"].Value; + println("you want to subscribe to " + target); + bus.BindMsg(target, receive); + break; + case Command.ping: + target = result.Groups["target"].Value; + if (bus.Ping(target, "test") == 0) + println("no Ivy client with the name \"" + target + "\""); + break; + case Command.quit: + case Command.bye: + bus.Stop(); + System.Environment.Exit(0); + break; + case Command.list: + println(bus.IvyClients.Count + " clients on the bus"); + foreach (IvyClient client in bus.IvyClients ) + println("-> " + client.ApplicationName); + break; + case Command.help: + println(helpCommands); + break; + } + } + catch (ArgumentException) + { + Console.WriteLine("manque de la commande dans l'enum"); + } + } + } // parseCommand @@ -334,11 +352,7 @@ namespace IvyProbeConsole private void receive(object sender, IvyMessageEventArgs e) { String s = e.Client.ApplicationName + " sent "; - // Copy the collection to a new array starting at index 0. - String[] args = new String[e.Arguments.Count]; - e.Arguments.CopyTo(args, 0); - - s += string.Join(",", args); + s += string.Join(",", e.Arguments); println(s); } [Conditional("DEBUG")] -- cgit v1.1