summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfcolin2007-02-01 10:08:30 +0000
committerfcolin2007-02-01 10:08:30 +0000
commitaf97284f0d3fc154d0aefca85fa78b25e481c256 (patch)
treee7da4cefef98cf0d985d26204ab3f35e13a132ab
parenteb9264d9ecbc98b7a31c8668c5ee920d393402af (diff)
downloadivy-csharp-af97284f0d3fc154d0aefca85fa78b25e481c256.zip
ivy-csharp-af97284f0d3fc154d0aefca85fa78b25e481c256.tar.gz
ivy-csharp-af97284f0d3fc154d0aefca85fa78b25e481c256.tar.bz2
ivy-csharp-af97284f0d3fc154d0aefca85fa78b25e481c256.tar.xz
Utilisateur : Fcolin Date : 3/07/06 Heure : 15:40 Archivé dans $/CSharp/Ivy/IvyProbeConsole Commentaire: revision de l'analyde de commande ! (vss 16)
-rw-r--r--CSharp/Ivy/IvyProbeConsole/IvyProbeConsole.cs184
1 files changed, 99 insertions, 85 deletions
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 =
+ @"^\.(?<cmd>direct) (?<target>[^ ]*) (?<id>[0-9]+) (?<message>.*)$|" +
+ @"^\.(?<cmd>die) (?<target>.*)$|" +
+ @"^\.(?<cmd>unbind) (?<target>.*)$|" +
+ @"^\.(?<cmd>bind) (?<target>.*)$|" +
+ @"^\.(?<cmd>ping) (?<target>.*)$|" +
+ @"^\.(?<cmd>quit)$|" +
+ @"^\.(?<cmd>bye)$|" +
+ @"^\.(?<cmd>list)$|" +
+ @"^\.(?<cmd>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<IvyClient> 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<IvyClient> 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")]