All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.runtime.downlink.PartialDownlinkModem Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package swim.runtime.downlink;

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import swim.collections.HashTrieSet;
import swim.structure.Value;
import swim.uri.Uri;

public abstract class PartialDownlinkModem extends DownlinkModel {
  final ConcurrentLinkedQueue upQueue;

  volatile HashTrieSet keyQueue;

  volatile Value lastKey;

  public PartialDownlinkModem(Uri meshUri, Uri hostUri, Uri nodeUri, Uri laneUri,
                              float prio, float rate, Value body) {
    super(meshUri, hostUri, nodeUri, laneUri, prio, rate, body);
    this.upQueue = new ConcurrentLinkedQueue();
    this.keyQueue = HashTrieSet.empty();
  }

  @Override
  protected boolean upQueueIsEmpty() {
    return this.upQueue.isEmpty();
  }

  @Override
  protected void queueUp(Value body) {
    this.upQueue.add(body);
  }

  protected void cueUpKey(Value key) {
    HashTrieSet oldKeyQueue;
    HashTrieSet newKeyQueue;
    do {
      oldKeyQueue = this.keyQueue;
      if (!oldKeyQueue.contains(key)) {
        newKeyQueue = oldKeyQueue.added(key);
      } else {
        newKeyQueue = oldKeyQueue;
        break;
      }
    } while (!KEY_QUEUE.compareAndSet(this, oldKeyQueue, newKeyQueue));
    if (oldKeyQueue != newKeyQueue) {
      cueUp();
    }
  }

  protected void cueUpKeys(Collection keys) {
    if (!keys.isEmpty()) {
      HashTrieSet oldKeyQueue;
      HashTrieSet newKeyQueue;
      do {
        oldKeyQueue = this.keyQueue;
        newKeyQueue = oldKeyQueue.added(keys);
      } while (!KEY_QUEUE.compareAndSet(this, oldKeyQueue, newKeyQueue));
      cueUp();
    }
  }

  protected abstract Value nextUpKey(Value key);

  @Override
  protected Value nextUpQueue() {
    return this.upQueue.poll();
  }

  @Override
  protected Value nextUpCue() {
    HashTrieSet oldKeyQueue;
    HashTrieSet newKeyQueue;
    Value key;
    do {
      oldKeyQueue = this.keyQueue;
      key = oldKeyQueue.next(this.lastKey);
      newKeyQueue = oldKeyQueue.removed(key);
    } while (oldKeyQueue != newKeyQueue && !KEY_QUEUE.compareAndSet(this, oldKeyQueue, newKeyQueue));
    if (key != null) {
      this.lastKey = key;
      return nextUpKey(key);
    } else {
      return null;
    }
  }

  @Override
  protected void feedUp() {
    if (!this.keyQueue.isEmpty()) {
      cueUp();
    }
    super.feedUp();
  }

  @SuppressWarnings("unchecked")
  static final AtomicReferenceFieldUpdater, HashTrieSet> KEY_QUEUE =
      AtomicReferenceFieldUpdater.newUpdater((Class>) (Class) PartialDownlinkModem.class, (Class>) (Class) HashTrieSet.class, "keyQueue");
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy