void CommandLoaderOnCommandsUpdated(object sender, CommandUpdateEventArgs <ICommand> e) { var workingDict = m_CommandDict.Values.ToDictionary(c => c.Name, StringComparer.OrdinalIgnoreCase); var updatedCommands = 0; foreach (var c in e.Commands) { if (c == null) { continue; } var castedCommand = c.Command as ICommand <TAppSession, TRequestInfo>; if (castedCommand == null) { if (Logger.IsErrorEnabled) { Logger.Error("Invalid command has been found! Command name: " + c.Command.Name); } continue; } if (c.UpdateAction == CommandUpdateAction.Remove) { workingDict.Remove(castedCommand.Name); if (Logger.IsInfoEnabled) { Logger.InfoFormat("The command '{0}' has been removed from this server!", c.Command.Name); } } else if (c.UpdateAction == CommandUpdateAction.Add) { workingDict.Add(castedCommand.Name, castedCommand); if (Logger.IsInfoEnabled) { Logger.InfoFormat("The command '{0}' has been added into this server!", c.Command.Name); } } else { workingDict[c.Command.Name] = castedCommand; if (Logger.IsInfoEnabled) { Logger.InfoFormat("The command '{0}' has been updated!", c.Command.Name); } } updatedCommands++; } if (updatedCommands > 0) { var commandFilters = CommandFilterFactory.GenerateCommandFilterLibrary(this.GetType(), workingDict.Values.Cast <ICommand>()); Interlocked.Exchange(ref m_CommandDict, workingDict); Interlocked.Exchange(ref m_CommandFilterDict, commandFilters); } }
/// <summary> /// Setups the command into command dictionary /// </summary> /// <param name="commandDict">The target command dict.</param> /// <returns></returns> protected virtual bool SetupCommands(Dictionary <string, ICommand <TAppSession, TRequestInfo> > commandDict) { foreach (var loader in m_CommandLoaders) { loader.Error += new EventHandler <ErrorEventArgs>(CommandLoaderOnError); loader.Updated += new EventHandler <CommandUpdateEventArgs <ICommand> >(CommandLoaderOnCommandsUpdated); if (!loader.Initialize <ICommand <TAppSession, TRequestInfo> >(RootConfig, this)) { if (Logger.IsErrorEnabled) { Logger.ErrorFormat("Failed initialize the command loader {0}.", loader.ToString()); } return(false); } IEnumerable <ICommand> commands; if (!loader.TryLoadCommands(out commands)) { if (Logger.IsErrorEnabled) { Logger.ErrorFormat("Failed load commands from the command loader {0}.", loader.ToString()); } return(false); } if (commands != null && commands.Any()) { foreach (var c in commands) { if (commandDict.ContainsKey(c.Name)) { if (Logger.IsErrorEnabled) { Logger.Error("Duplicated name command has been found! Command name: " + c.Name); } return(false); } var castedCommand = c as ICommand <TAppSession, TRequestInfo>; if (castedCommand == null) { if (Logger.IsErrorEnabled) { Logger.Error("Invalid command has been found! Command name: " + c.Name); } return(false); } commandDict.Add(c.Name, castedCommand); } } } m_CommandFilterDict = CommandFilterFactory.GenerateCommandFilterLibrary(this.GetType(), commandDict.Values.Cast <ICommand>()); return(true); }
/// <summary> /// Setups the command filters. /// </summary> /// <param name="commands">The commands.</param> private void SetupCommandFilters(IEnumerable <ICommand <TAppSession, TRequestInfo> > commands) { m_CommandFilterDict = CommandFilterFactory.GenerateCommandFilterLibrary(this.GetType(), commands.Cast <ICommand>()); }