Exemplo n.º 1
0
        private HandlerBundle CreateBundle(IMessageHandler handler, ISubscription subs = null, IMessage message = null)
        {
            Logger.LogInfo("Activating handler, topic = " + (subs.IsNull() ? "Not known" : subs.Topic.Name) + ", channel = " + (subs.IsNull() ? "Not known" : subs.ChannelMonicker));
            handler.Initialize();
            HandlerBundle bundle = new HandlerBundle {
                Handler     = handler,
                HostingTask = Task.Run(async() => {
                    IMessageHandler processor = handler as IMessageHandler;
                    // Can be null if being hydrated
                    if (subs.IsNotNull())
                    {
                        processor.Accept(subs, message);
                    }
                    while (processor.Viable)
                    {
                        if (processor.IsWorking)
                        {
                            await Task.Delay(TaskDelay);
                        }
                        else
                        {
                            await processor.Work();
                        }
                    }
                })
            };

            return(bundle);
        }
Exemplo n.º 2
0
        private void AssignMessageToHandler(ISubscription subs, IMessage message)
        {
            HandlerBundle bundle = ActiveHandlers.FirstOrDefault(b => b.Handler.CanAccept(subs, message).Success);
            bool          stateAcceptable = bundle.IsNull() || bundle.HostingTask.Status == TaskStatus.WaitingForActivation, reanimated = false;

            // There is a handler that can accept the current subscription, but its task has ceased
            // Reuse it to ensure no messages are lost
            if (bundle.IsNotNull() && !stateAcceptable)
            {
                ActiveHandlers.Remove(bundle);
                bundle.HostingTask.Wait();
                // If the handler is blocked, we still allow this flow to proceed, as this places a message in the handler and the task immediately exits - but the message is preserved
                bundle = CreateBundle(bundle.Handler, subs, message);
                ActiveHandlers.Add(bundle);
                reanimated = true;
                Logger.LogInfo("Reanimated handler for (" + subs.Topic.Name + "," + subs.ChannelMonicker + ")");
            }
            bundle
            .IsNull()
            .IfTrue(() => CreateNewHandler(subs, message))
            .IfFalse(() => { if (!reanimated)
                             {
                                 bundle.Handler.Accept(subs, message);
                             }
                     });
        }
Exemplo n.º 3
0
 private HandlerBundle CreateBundle(IMessageHandler handler, ISubscription subs = null, IMessage message = null)
 {
     Logger.LogInfo("Activating handler, topic = " + (subs.IsNull() ? "Not known" : subs.Topic.Name) + ", channel = " + (subs.IsNull() ? "Not known" : subs.ChannelMonicker));
     handler.Initialize();
     HandlerBundle bundle = new HandlerBundle {
         Handler = handler,
         HostingTask = Task.Run(async () => {
             IMessageHandler processor = handler as IMessageHandler;
             // Can be null if being hydrated
             if (subs.IsNotNull())
                 processor.Accept(subs, message);
             while (processor.Viable) {
                 if (processor.IsWorking)
                     await Task.Delay(TaskDelay);
                 else
                     await processor.Work();
             }
         })
     };
     return bundle;
 }
Exemplo n.º 4
0
        public MainWindow()
        {
            using (LoadBotForm lbf = new LoadBotForm())
            {
                if (lbf.ShowDialog() != DialogResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }
                bot = new IRCBot(lbf.BotLogin);
            }
            InitializeComponent();

            DirectoryInfo PluginDirectory;

            try
            {
                PluginDirectory = new DirectoryInfo("Plugins");
                if (!PluginDirectory.Exists)
                {
                    PluginDirectory.Create();
                }
            }
            catch
            {
                MessageBox.Show("No access to \"Plugin\" Directory.");
                return;
            }
            foreach (FileInfo pluginFile in PluginDirectory.GetFiles("*.dll"))
            {
                try
                {
                    LoadedPlugin plugin = PluginHandler.LoadPlugin(pluginFile.FullName);
                    plugin.OnException += Plugin_OnException;
                    if (!PluginIDList.Add(plugin.PluginID))
                    {
                        throw new Exception("Duplicate id");
                    }

                    HandlerBundle handlers = new HandlerBundle();

                    handlers.Bot               = new BotHandler(plugin);
                    handlers.Bot.OnException  += OnException;
                    handlers.Bot.OnSayMessage += Bot_OnSayMessage;

                    handlers.UI           = new BotUIHandler(plugin);
                    handlers.UI.OnTabAdd += UI_OnTabAdd;

                    plugin.Initilze(handlers);


                    foreach (var command in plugin.Details.LoadedCommands)
                    {
                        var handler = new CommandHandler(plugin, command);
                        if (!HandlerList.ContainsKey(handler.ID))
                        {
                            HandlerList.Add(handler.ID, handler);
                        }
                    }


                    PluginDisplayControl display = new PluginDisplayControl(plugin);
                    display.Parent   = PluginDisplayPanel;
                    display.Width    = PluginDisplayPanel.Width;
                    display.Anchor   = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                    display.Location = new Point(0, PluginList.Count * display.Height);
                    PluginDisplayPanel.Controls.Add(display);

                    PluginList.Add(plugin);
                }
                catch
                {
                    Debug.WriteLine("Error on file {0}", pluginFile.Name);
                }

                LoadSettings();
            }
        }
Exemplo n.º 5
0
 public void Initilze(HandlerBundle _handlers)
 {
     Handlers = _handlers;
     PluginInstance.PluginLoad(Handlers.Bot, Handlers.UI, Permissions);
     Enabled = true;
 }