Приоритизация входных каналов в Retlang - PullRequest
0 голосов
/ 26 июня 2009

Как можно обрабатывать ввод каналов по приоритетам? Есть ли что-нибудь эквивалентное в конструкцию "reactWithin(0) { ... case TIMEOUT }" Скалы?

1 Ответ

0 голосов
/ 10 августа 2009

Я написал класс подписки, который доставляет приоритетные сообщения через заданный интервал. Это не идеальный способ использования приоритетных сообщений, но я опубликую его для будущих поколений. Я думаю, что пользовательский RequestReplyChannel будет лучшим вариантом для некоторых других случаев. Реализация PriorityQueue оставлена ​​читателю в качестве упражнения.

class PrioritySubscriber<T> : BaseSubscription<T>
{
    private readonly PriorityQueue<T> queue;
    private readonly IScheduler scheduler;
    private readonly Action<T> receive;
    private readonly int interval;

    private readonly object sync = new object();
    private ITimerControl next = null;

    public PrioritySubscriber(IComparer<T> comparer, IScheduler scheduler,
        Action<T> receive, int interval)
    {
        this.queue = new PriorityQueue<T>(comparer);
        this.scheduler = scheduler;
        this.receive = receive;
        this.interval = interval;
    }

    protected override void OnMessageOnProducerThread(T msg)
    {
        lock (this.sync)
        {
            this.queue.Enqueue(msg);

            if (this.next == null)
            {
                this.next =
                    this.scheduler.Schedule(this.Receive, this.interval);
            }
        }
    }

    private void Receive()
    {
        T msg;

        lock (this.sync)
        {
            msg = this.queue.Dequeue();

            if (this.queue.Count > 0)
            {
                this.next =
                    this.scheduler.Schedule(this.Receive, this.interval);
            }
        }

        this.receive(msg);
    }
}
...