private void ScaleChannels(ChannelScaleAction action, int count = 1)
        {
            for (int index = 0; index < count && !this.cancelTokenSource.IsCancellationRequested; ++index)
            {
                int          num         = 0;
                bool?        nullable    = new bool?();
                IPushChannel pushChannel = (IPushChannel)null;
                lock (this.channelsLock)
                {
                    switch (action)
                    {
                    case ChannelScaleAction.Create:
                        pushChannel = this.PushChannelFactory.CreateChannel(this.ChannelSettings);
                        PushServiceBase.ChannelWorker channelWorker = new PushServiceBase.ChannelWorker(pushChannel, new Action <IPushChannel, CancellationTokenSource>(this.DoChannelWork));
                        channelWorker.WorkerTask.ContinueWith((Action <Task>)(t => Log.Error("Channel Worker Failed Task: " + t.Exception.ToString())), TaskContinuationOptions.OnlyOnFaulted);
                        this.channels.Add(channelWorker);
                        num      = this.channels.Count;
                        nullable = new bool?(false);
                        break;

                    case ChannelScaleAction.Destroy:
                        if (this.channels.Count > 0)
                        {
                            PushServiceBase.ChannelWorker channel = this.channels[0];
                            this.channels.RemoveAt(0);
                            channel.Dispose();
                            num      = this.channels.Count;
                            nullable = new bool?(true);
                            break;
                        }
                        break;
                    }
                }
                if (nullable.HasValue && !nullable.Value)
                {
                    ChannelCreatedDelegate onChannelCreated = this.OnChannelCreated;
                    if (onChannelCreated != null)
                    {
                        onChannelCreated((object)this, pushChannel);
                    }
                }
                else if (nullable.HasValue && nullable.Value)
                {
                    ChannelDestroyedDelegate channelDestroyed = this.OnChannelDestroyed;
                    if (channelDestroyed != null)
                    {
                        channelDestroyed((object)this);
                    }
                }
            }
        }
Example #2
0
        void ScaleChannels(ChannelScaleAction action, int count = 1)
        {
            for (int i = 0; i < count; i++)
            {
                var  newCount  = 0;
                bool?destroyed = null;

                lock (channels)
                {
                    if (action == ChannelScaleAction.Create)
                    {
                        var newChannel = this.CreateChannel(this.ChannelSettings);

                        newChannel.Events.RegisterProxyHandler(this.Events);

                        newChannel.OnQueueTimed += new Action <double>(newChannel_OnQueueTimed);

                        channels.Add(newChannel);

                        newCount  = channels.Count;
                        destroyed = false;
                    }
                    else if (action == ChannelScaleAction.Destroy && channels.Count > 1)
                    {
                        var channelOn = channels[0];
                        channels.RemoveAt(0);

                        //Now stop the channel but let it finish
                        channelOn.Stop(true);

                        channelOn.Events.UnRegisterProxyHandler(this.Events);

                        newCount  = channels.Count;
                        destroyed = true;
                    }
                }

                if (destroyed.HasValue && !destroyed.Value)
                {
                    this.Events.RaiseChannelCreated(this.Platform, newCount);
                }
                else if (destroyed.HasValue && destroyed.Value)
                {
                    this.Events.RaiseChannelDestroyed(this.Platform, newCount);
                }
            }
        }
Example #3
0
        private void ScaleChannels(ChannelScaleAction action, int count = 1)
        {
            //if (stopping)
            //	return;

            for (int i = 0; i < count; i++)
            {
                if (cancelTokenSource.IsCancellationRequested)
                    break;

                var newCount = 0;
                bool? destroyed = null;
                IPushChannel newChannel = default(IPushChannel);

                lock (channels)
                {
                    if (action == ChannelScaleAction.Create)
                    {
                        newChannel = this.PushChannelFactory.CreateChannel(this.ChannelSettings);

                        var chanWorker = new ChannelWorker(newChannel, DoChannelWork);
                        chanWorker.WorkerTask.ContinueWith(t =>
                        {
                            var ex = t.Exception;
                            Log.Error("Channel Worker Failed Task: " + ex.ToString());
                        }, TaskContinuationOptions.OnlyOnFaulted);

                        channels.Add(chanWorker);

                        newCount = channels.Count;
                        destroyed = false;
                    }
                    else if (action == ChannelScaleAction.Destroy && channels.Count > 1)
                    {
                        var channelOn = channels[0];
                        channels.RemoveAt(0);

                        //Stop the channel worker, which will dispose the channel too
                        channelOn.Dispose ();

                        newCount = channels.Count;
                        destroyed = true;
                    }
                }

                if (destroyed.HasValue && !destroyed.Value)
                {
                    var evt = OnChannelCreated;
                    if (evt != null)
                        evt(this, newChannel);
                }
                else if (destroyed.HasValue && destroyed.Value)
                {
                    var evt = this.OnChannelDestroyed;
                    if (evt != null)
                        evt(this);
                }
            }
        }
Example #4
0
        private void ScaleChannels(ChannelScaleAction action, int count = 1)
        {
            //if (stopping)
            //	return;

            for (int i = 0; i < count; i++)
            {
                if (cancelTokenSource.IsCancellationRequested)
                {
                    break;
                }

                var          newCount   = 0;
                bool?        destroyed  = null;
                IPushChannel newChannel = default(IPushChannel);

                lock (channelsLock)
                {
                    if (action == ChannelScaleAction.Create)
                    {
                        newChannel = this.PushChannelFactory.CreateChannel(this.ChannelSettings);

                        var chanWorker = new ChannelWorker(newChannel, DoChannelWork);
                        chanWorker.WorkerTask.ContinueWith(t =>
                        {
                            var ex = t.Exception;
                            Log.Error("Channel Worker Failed Task: " + ex.ToString());
                        }, TaskContinuationOptions.OnlyOnFaulted);

                        channels.Add(chanWorker);

                        newCount  = channels.Count;
                        destroyed = false;
                    }
                    else if (action == ChannelScaleAction.Destroy && channels.Count > 0)
                    {
                        var channelOn = channels[0];
                        channels.RemoveAt(0);

                        //Stop the channel worker, which will dispose the channel too
                        channelOn.Dispose();

                        newCount  = channels.Count;
                        destroyed = true;
                    }
                }

                if (destroyed.HasValue && !destroyed.Value)
                {
                    var evt = OnChannelCreated;
                    if (evt != null)
                    {
                        evt(this, newChannel);
                    }
                }
                else if (destroyed.HasValue && destroyed.Value)
                {
                    var evt = this.OnChannelDestroyed;
                    if (evt != null)
                    {
                        evt(this);
                    }
                }
            }
        }
Example #5
0
 private void ScaleChannels(ChannelScaleAction action, int count = 1)
 {
     if (action == ChannelScaleAction.Create)
     {
         this.ChannelLoadBalancer.AddChannels(count);
     }
     else if (action == ChannelScaleAction.Destroy)
     {
         this.ChannelLoadBalancer.RemoveChannels(count);
     }
 }
Example #6
0
        private void ScaleChannels(ChannelScaleAction action, int count = 1)
        {
            for (int i = 0; i < count; i++)
            {
                var newCount = 0;
                bool? destroyed = null;
                IPushChannel newChannel = default(IPushChannel);

                lock (channels)
                {
                    if (action == ChannelScaleAction.Create)
                    {
                        newChannel = this.PushChannelFactory.CreateChannel(this.ChannelSettings);

                        var chanWorker = new ChannelWorker(newChannel, DoChannelWork);
                        chanWorker.WorkerTask.ContinueWith(t =>
                        {
                            var ex = t.Exception;
                            Log.Error("Channel Worker Failed Task: " + ex.ToString());
                        }, TaskContinuationOptions.OnlyOnFaulted);

                        channels.Add(chanWorker);

                        newCount = channels.Count;
                        destroyed = false;
                    }
                    else if (action == ChannelScaleAction.Destroy && channels.Count > 1)
                    {
                        var channelOn = channels[0];
                        channels.RemoveAt(0);

                        //Now stop the channel but let it finish
                        channelOn.Channel.Dispose();

                        newCount = channels.Count;
                        destroyed = true;
                    }
                }

                if (destroyed.HasValue && !destroyed.Value)
                {
                    var evt = OnChannelCreated;
                    if (evt != null)
                        evt(this, newChannel);
                }
                else if (destroyed.HasValue && destroyed.Value)
                {
                    var evt = this.OnChannelDestroyed;
                    if (evt != null)
                        evt(this);
                }
            }
        }