public void TestPublishSubscribeChannel()
        {
            IApplicationContext ctx     = TestUtils.GetContext(@"Channel\Config\ChannelParserTests.xml");
            IMessageChannel     channel = (IMessageChannel)ctx.GetObject("publishSubscribeChannel");

            Assert.That(channel.GetType(), Is.EqualTo(typeof(PublishSubscribeChannel)));
        }
		public virtual void Configure(IMessageDispatcherSettings settings)
		{
			if (settings.InputChannel.Value == null)
			{
				throw new NoInputChannelConfiguredException("You must specify an input channel for a message dispatcher");
			}

			if (settings.InvalidChannel.Value == null)
			{
				throw new NoInputChannelConfiguredException("You must specify an invalid message channel for a message dispatcher");
			}

			if (settings.MessageProcessorTypes.Value == null || settings.MessageProcessorTypes.Value.Count == 0)
			{
				throw new NoMessageProcessorsConfiguredException(
					"You must specify one or more message processors for a message dispatcher");
			}

			if (settings.DurationOfDispatchingSlice.Value.TotalMilliseconds == 0)
			{
				throw new NoDispatchingSliceDurationConfiguredException(
					"You must specify a duration for the dispatcher to dispatch messages during.");
			}

			if (settings.NumberOfMessagesToDispatchPerSlice.Value == 0)
			{
				throw new NoNumberOfMessagesPerSliceConfiguredException(
					"You must specify the maximum number of messages to be dispatched during a slice of time.");
			}

			CurrentSettings = settings;

			InputChannel = settings.InputChannel.Value;
			InvalidChannel = settings.InvalidChannel.Value;

			MessageProcessors = new List<IMessageProcessor>();

			foreach (var type in CurrentSettings.MessageProcessorTypes.Value)
			{
				var processor = Container.GetInstance(type) as IMessageProcessor;

				if (processor == null)
				{
					continue;
				}

				this.WriteInfoMessage(string.Format("Adding processor {0} to dispatcher.", processor.GetType().Name));

				MessageProcessors.Add(processor);
			}

			this.WriteInfoMessage(
				string.Format(
					"Dispatcher configured with input channel {0}({1}) and {2} message processors.",
					InputChannel.GetType().Name,
					InputChannel.ChannelName,
					MessageProcessors.Count()));

			Configured = true;
		}
        public void TestDirectChannelByDefault()
        {
            IApplicationContext ctx     = TestUtils.GetContext(@"Channel\Config\ChannelParserTests.xml");
            IMessageChannel     channel = (IMessageChannel)ctx.GetObject("defaultChannel");

            Assert.That(channel.GetType(), Is.EqualTo(typeof(DirectChannel)));
        }
        public void TestChannelType()
        {
            IApplicationContext ctx     = TestUtils.GetContext(@"Channel\Config\threadLocalChannelParserTests.xml");
            IMessageChannel     channel = (IMessageChannel)ctx.GetObject("channel");

            Assert.That(channel.GetType(), Is.EqualTo(typeof(ThreadLocalChannel)));
        }
Esempio n. 5
0
        public async Task <bool> IsBotPrivateChannel(IMessageChannel contextChannel)
        {
            try
            {
                return(contextChannel.GetType() == typeof(SocketDMChannel) && await contextChannel.GetUserAsync(Client.CurrentUser.Id) != null);
            }
            catch (Exception ex)
            {
                await LogHelper.LogEx(nameof(IsBotPrivateChannel), ex, LogCat.Discord);

                return(false);
            }
        }
        private MethodInfo GetBroadcastMethod()
        {
            if (_broadcastMethod != null)
            {
                return(_broadcastMethod);
            }

            // We have to look this up via the concrete type of the channel because Mono doesn't
            // support calling ldftn with interface methods.
            _broadcastMethod = _channel.GetType().GetMethod("Broadcast", new[] { typeof(object), typeof(object) });

            return(_broadcastMethod);
        }
        private void InitializeEndpoint()
        {
            lock (_initializationMonitor) {
                if (_initialized)
                {
                    return;
                }

                AssertUtils.ArgumentHasText(_inputChannelName, "inputChannelName is required");

                AssertUtils.IsTrue(_objectFactory.ContainsObject(_inputChannelName), "no such input channel '" + _inputChannelName + "' for endpoint '" + _objectName + "'");

                IMessageChannel channel = (IMessageChannel)_objectFactory.GetObject(_inputChannelName, typeof(IMessageChannel));
                if (channel is ISubscribableChannel)
                {
                    if (_pollerMetadata != null)
                    {
                        throw new ArgumentException("A poller should not be specified for endpoint '" + _objectName
                                                    + "', since '" + _inputChannelName + "' is a SubscribableChannel (not pollable).");
                    }
                    _endpoint = new EventDrivenConsumer((ISubscribableChannel)channel, _handler);
                }
                else if (channel is IPollableChannel)
                {
                    PollingConsumer pollingConsumer = new PollingConsumer((IPollableChannel)channel, _handler);
                    if (_pollerMetadata == null)
                    {
                        _pollerMetadata = IntegrationContextUtils.GetDefaultPollerMetadata(_objectFactory);
                        AssertUtils.ArgumentNotNull(_pollerMetadata, "No poller has been defined for endpoint '" + _objectName + "', and no default poller is available within the context.");
                    }
                    pollingConsumer.Trigger               = _pollerMetadata.Trigger;
                    pollingConsumer.MaxMessagesPerPoll    = _pollerMetadata.MaxMessagesPerPoll;
                    pollingConsumer.ReceiveTimeout        = _pollerMetadata.ReceiveTimeout;
                    pollingConsumer.TaskExecutor          = _pollerMetadata.TaskExecutor;
                    pollingConsumer.TransactionManager    = _pollerMetadata.TransactionManager;
                    pollingConsumer.TransactionDefinition = _pollerMetadata.TransactionDefinition;
                    pollingConsumer.AdviceChain           = _pollerMetadata.AdviceChain;
                    _endpoint = pollingConsumer;
                }
                else
                {
                    throw new ArgumentException("unsupported channel type: [" + channel.GetType() + "]");
                }
                _endpoint.AutoStartup   = _autoStartup;
                _endpoint.ObjectName    = _objectName;
                _endpoint.ObjectFactory = _objectFactory;
                _endpoint.AfterPropertiesSet();
                _initialized = true;
            }
        }
            public void RegisterChannel(string channelName, IMessageChannel channel)
            {
                if (channel.Name != null)
                {
                    if (channelName == null)
                    {
                        AssertUtils.ArgumentNotNull(channel.Name, "channel.Name", "channel name must not be null");
                        channelName = channel.Name;
                    }
                    else
                    {
                        AssertUtils.IsTrue(channel.Name.Equals(channelName),
                                           "channel name has already been set with a conflicting value");
                    }
                }
                RootObjectDefinition rod = new RootObjectDefinition(channel.GetType());

                RegisterObjectDefinition(channelName, rod); //, this);
            }
        public void TestPublishSubscribeChannelWithTaskExecutorReference()
        {
            IApplicationContext ctx     = TestUtils.GetContext(@"Channel\Config\ChannelParserTests.xml");
            IMessageChannel     channel = (IMessageChannel)ctx.GetObject("publishSubscribeChannelWithTaskExecutorRef");

            Assert.That(channel.GetType(), Is.EqualTo(typeof(PublishSubscribeChannel)));

            IMessageDispatcher dispatcher = TestUtils.GetFieldValue(channel, "_dispatcher") as IMessageDispatcher;

            Assert.IsNotNull(dispatcher);

            IExecutor dispatcherTaskExecutor = TestUtils.GetFieldValue(dispatcher, "_taskExecutor") as IExecutor;

            Assert.IsNotNull(dispatcherTaskExecutor);

            Assert.That(dispatcherTaskExecutor.GetType(), Is.EqualTo(typeof(ErrorHandlingTaskExecutor)));

            IExecutor innerExecutor      = TestUtils.GetFieldValue(dispatcherTaskExecutor, "_taskExecutor") as IExecutor;
            object    taskExecutorObject = ctx.GetObject("taskExecutor");

            Assert.That(taskExecutorObject, Is.EqualTo(innerExecutor));
        }
Esempio n. 10
0
        async public Task <Discord.Rest.RestUserMessage> safeSendMessage(IMessageChannel c, string m, bool splitMessage = false, Embed embed = null)
        {
            if (c == null)
            {
                Console.WriteLine("tried to send to no channel at all: " + m);
                return(null);
            }

            /*
             * if(c.Server == null)
             * {
             *  if(!c.IsPrivate)
             *  {
             *      Console.WriteLine("tried to send a non-private message to no server at all: " + c + " - " + m);
             *      return;
             *  }
             * }
             */
            if (server == null)
            {
                Console.WriteLine("Virtualserver's server was null at the time of sending! " + m);
                return(null);
            }
            SocketTextChannel stc = null;

            if (c.GetType() == typeof(SocketTextChannel))
            {
                stc = (SocketTextChannel)c;
            }
            if (stc != null && stc.Guild.Id != server.Id)
            {
                Console.WriteLine("something tried to send an illegal message: " + m);
                return(null);
            }
            while (splitMessage && m.Length >= 2000)
            {
                await safeSendMessage(c, m.Substring(0, 1999), false);

                m = m.Substring(1999);
            }
            try
            {
                if (stc != null)
                {
                    return(await stc.SendMessageAsync(m, false, embed));
                }
                else if (c.GetType() == typeof(SocketDMChannel))
                {
                    return(await((SocketDMChannel)c).SendMessageAsync(m, false, embed));
                }
                else if (c is RestDMChannel)
                {
                    return(await((RestDMChannel)c).SendMessageAsync(m, false, embed));
                }
                else
                {
                    Console.WriteLine("safeSendMessage was unable to deal with passed in msg of type: " + c.GetType());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(null);
        }
Esempio n. 11
0
		public static void TestThroughputSynchronously(
			IMessageChannel channel, int howManyMessages, int? maxMessagesToReceive)
		{
			var start = DateTime.Now;

			channel.Open();

			Console.WriteLine("Sending {0} messages through the {1} channel", howManyMessages, channel.GetType().FullName);

			SendMessages(channel, howManyMessages);

			Console.WriteLine("Sent {0} messages in {1} seconds", howManyMessages, DateTime.Now.Subtract(start).TotalSeconds);

			start = DateTime.Now;

			var receivedMessageCount = 0;

			var numberTimesToLoop = 1;
			if (maxMessagesToReceive.HasValue)
			{
				numberTimesToLoop = howManyMessages / maxMessagesToReceive.Value + 1;
			}
			else
			{
				maxMessagesToReceive = howManyMessages;
			}

			for (var i = 0; i < numberTimesToLoop; i++)
			{
				foreach (var message in channel.ReceiveMany(maxMessagesToReceive.Value, TimeSpan.MaxValue))
				{
					receivedMessageCount++;
				}

				if (howManyMessages - receivedMessageCount < maxMessagesToReceive)
				{
					maxMessagesToReceive = howManyMessages - receivedMessageCount;
				}
			}

			channel.Close();

			Console.WriteLine("Received {0} messages in {1}", receivedMessageCount, DateTime.Now.Subtract(start).TotalSeconds);
		}
Esempio n. 12
0
		public static void TestThroughputAsynchronously(
			IMessageChannel channel, int howManyMessages, int howManyThreads, int? maxMessagesToReceive = null)
		{
			channel.Open();

			var start = DateTime.Now;

			var numberTimesToLoop = 1;
			if (maxMessagesToReceive.HasValue)
			{
				var numberMessagesPerThread = howManyMessages / howManyThreads + 2;

				do
				{
					numberMessagesPerThread--;
					numberTimesToLoop = howManyMessages / (numberMessagesPerThread * howManyThreads) + 1;
				}
				while (numberMessagesPerThread > maxMessagesToReceive);

				Assert.LessOrEqual(numberMessagesPerThread, maxMessagesToReceive);
				maxMessagesToReceive = numberMessagesPerThread;
			}
			else
			{
				maxMessagesToReceive = howManyMessages / howManyThreads + 1;
			}

			Console.WriteLine(
				"Sending {0} messages through the {1} channel across {2} threads in batches of {3}",
				maxMessagesToReceive * howManyThreads * numberTimesToLoop,
				channel.GetType().FullName,
				howManyThreads,
				maxMessagesToReceive);

			for (var i = 0; i < numberTimesToLoop; i++)
			{
				var results = Parallel.For(
					0,
					howManyThreads,
					x =>
						{
							SendMessages(channel, maxMessagesToReceive.Value);
							channel.ReceiveMany(maxMessagesToReceive.Value, TimeSpan.MaxValue);
						});

				Assert.True(results.IsCompleted);
			}

			Console.WriteLine(
				"Received {0} messages in {1} seconds",
				maxMessagesToReceive * howManyThreads * numberTimesToLoop,
				DateTime.Now.Subtract(start).TotalSeconds);

			channel.Close();
		}
Esempio n. 13
0
 public async Task <bool> IsBotPrivateChannel(IMessageChannel contextChannel)
 {
     return(contextChannel.GetType() == typeof(SocketDMChannel) && await contextChannel.GetUserAsync(Client.CurrentUser.Id) != null);
 }