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

nstream.reflect.MetaLane Maven / Gradle / Ivy

The newest version!
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// 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 nstream.reflect;

import nstream.reflect.agent.MetaLaneAgent;
import nstream.reflect.model.DataStats;
import nstream.reflect.model.LinkStats;
import nstream.reflect.model.LogEntry;
import swim.api.Downlink;
import swim.recon.Recon;
import swim.structure.Value;
import swim.system.LaneBinding;
import swim.system.LaneContext;
import swim.system.LaneProxy;
import swim.system.LinkBinding;
import swim.system.LinkContext;
import swim.system.NodeBinding;
import swim.system.Push;
import swim.system.WarpBinding;
import swim.uri.UriPath;
import swim.warp.Envelope;

public class MetaLane extends LaneProxy implements MetaCell {
  final MetaNode parent;
  final MetaLaneAgent agent;

  public MetaLane(MetaNode parent, LaneBinding laneBinding) {
    super(laneBinding);
    this.parent = parent;
    this.agent = new MetaLaneAgent(this);
  }

  @Override
  public void setLaneContext(LaneContext laneContext) {
    super.setLaneContext(laneContext);
    this.agent.didSetLaneUri(laneContext.laneUri());
  }

  @Override
  public MetaNode metaParent() {
    return this.parent;
  }

  @Override
  public MetaLaneAgent metaAgent() {
    return this.agent;
  }

  @Override
  public LinkStats linkStats() {
    return this.agent.linkStats();
  }

  @Override
  public DataStats dataStats() {
    return this.agent.dataStats();
  }

  @Override
  public void openUplink(LinkBinding link) {
    if (link instanceof WarpBinding) {
      link = new MetaWarpUplink(this, (WarpBinding) link);
    }
    super.openUplink(link);
  }

  @Override
  public LinkBinding bindDownlink(Downlink downlink) {
    final LinkBinding link = super.bindDownlink(downlink);
    if (link instanceof WarpBinding) {
      final MetaWarpDownlink metaLink = link.linkWrapper().unwrapLink(MetaWarpDownlink.class);
      if (metaLink != null) {
        metaLink.parent = this;
      }
    }
    return link;
  }

  @Override
  public void openDownlink(LinkBinding link) {
    if (link instanceof WarpBinding) {
      if (link.linkWrapper().unwrapLink(MetaWarpDownlink.class) == null) {
        link = new MetaWarpDownlink(this, (WarpBinding) link);
      }
    }
    super.openDownlink(link);
  }

  @Override
  public void pushDown(Push push) {
    super.pushDown(push);
    final Object message = push.message();
    if (message instanceof Envelope) {
      this.agent.didPushDown((Envelope) message);
    }
  }

  @Override
  public void pushUp(Push push) {
    super.pushUp(push);
    final Object message = push.message();
    if (message instanceof Envelope) {
      this.agent.didPushUp((Envelope) message);
    }
  }

  @Override
  public void trace(Object message) {
    this.agent.didLogTrace(LogEntry.trace(nodeUri(), laneUri(), message));
  }

  @Override
  public void debug(Object message) {
    this.agent.didLogDebug(LogEntry.debug(nodeUri(), laneUri(), message));
  }

  @Override
  public void info(Object message) {
    this.agent.didLogInfo(LogEntry.info(nodeUri(), laneUri(), message));
  }

  @Override
  public void warn(Object message) {
    this.agent.didLogWarn(LogEntry.warn(nodeUri(), laneUri(), message));
  }

  @Override
  public void error(Object message) {
    this.agent.didLogError(LogEntry.error(nodeUri(), laneUri(), message));
  }

  @Override
  public void didFail(Throwable error) {
    try {
      this.agent.didLogFail(LogEntry.fail(nodeUri(), laneUri(), error));
    } finally {
      super.didFail(error);
    }
  }

  @Override
  public void didOpen() {
    this.agent.didOpenLane();
  }

  @Override
  public void willLoad() {
    super.willLoad();
    this.agent.load();
  }

  @Override
  public void didClose() {
    this.agent.didCloseLane();
    this.agent.close();
    super.didClose();
  }

  public NodeBinding resolveMetaLane(UriPath nodePath) {
    if (nodePath.isEmpty()) {
      return this.agent;
    } else if ("uplink".equals(nodePath.head())) {
      nodePath = nodePath.tail(); // drop uplink
      if (!nodePath.isEmpty()) {
        nodePath = nodePath.tail(); // drop /
      }
      return resolveMetaUplink(nodePath);
    }
    return null;
  }

  public NodeBinding resolveMetaUplink(UriPath nodePath) {
    if (nodePath.isAbsolute() || nodePath.isEmpty()) {
      return null;
    }
    final Value linkKey = Recon.parse(nodePath.head());
    final LinkContext uplinkBinding = getUplink(linkKey);
    final MetaWarpUplink metaUplink = uplinkBinding.linkWrapper().unwrapLink(MetaWarpUplink.class);
    if (metaUplink != null) {
      if (!nodePath.isEmpty()) {
        nodePath = nodePath.tail(); // drop linkKey
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop /
        }
      }
      return metaUplink.resolveMetaUplink(nodePath);
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy