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

nstream.reflect.MetaEdge 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.MetaEdgeAgent;
import nstream.reflect.model.DataStats;
import nstream.reflect.model.LinkStats;
import nstream.reflect.model.LogEntry;
import nstream.reflect.model.ProcessStats;
import nstream.reflect.model.StoreStats;
import nstream.reflect.model.SystemStats;
import swim.api.Downlink;
import swim.system.EdgeBinding;
import swim.system.EdgeProxy;
import swim.system.LinkBinding;
import swim.system.MeshAddress;
import swim.system.MeshBinding;
import swim.system.NodeAddress;
import swim.system.NodeBinding;
import swim.system.WarpBinding;
import swim.uri.Uri;
import swim.uri.UriPath;

public class MetaEdge extends EdgeProxy implements MetaRouter {
  final MetaEdgeAgent agent;
  final MetaSystem metaSystem;

  public MetaEdge(EdgeBinding edgeBinding) {
    super(edgeBinding);
    this.agent = new MetaEdgeAgent(this);
    this.metaSystem = new MetaSystem(this);
  }

  @Override
  public MetaRouter metaParent() {
    return null;
  }

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

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

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

  @Override
  public StoreStats storeStats() {
    return this.agent.storeStats();
  }

  @Override
  public ProcessStats processStats() {
    return this.metaSystem.processStats();
  }

  @Override
  public SystemStats systemStats() {
    return this.metaSystem.systemStats();
  }

  public void cueSystemStats() {
    this.agent.cueSystemStats();
    for (MeshBinding mesh : meshes().values()) {
      final MetaMesh metaMesh = mesh.meshWrapper().unwrapMesh(MetaMesh.class);
      if (metaMesh != null) {
        metaMesh.cueSystemStats();
      }
    }
  }

  @Override
  public MeshBinding injectMesh(MeshAddress meshAddress, MeshBinding mesh) {
    return new MetaMesh(this, super.injectMesh(meshAddress, mesh));
  }

  @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 trace(Object message) {
    this.agent.didLogTrace(LogEntry.trace(message));
  }

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

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

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

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

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

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

  @Override
  public void didOpen() {
    super.didOpen();
    this.metaSystem.open();
  }

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

  @Override
  public NodeBinding createNode(NodeAddress nodeAddress) {
    final Uri nodeUri = nodeAddress.nodeUri();
    if ("swim".equals(nodeUri.schemeName())) {
      UriPath nodePath = nodeUri.path();
      if ("meta:edge".equals(nodePath.head())) {
        return this.agent;
      } else if ("meta:mesh".equals(nodePath.head())) {
        nodePath = nodePath.tail(); // drop meta:mesh
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop /
        }
        return resolveMetaMesh(nodePath);
      } else if ("meta:part".equals(nodePath.head())) {
        nodePath = nodePath.tail(); // drop meta:part
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop /
        }
        return resolveMetaPart(nodePath);
      } else if ("meta:host".equals(nodePath.head())) {
        nodePath = nodePath.tail(); // drop meta:host
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop /
        }
        return resolveMetaHost(nodePath);
      } else if ("meta:node".equals(nodePath.head())) {
        nodePath = nodePath.tail(); // drop meta:node
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop /
        }
        return resolveMetaNode(nodePath);
      }
    }
    return super.createNode(nodeAddress);
  }

  protected NodeBinding resolveMetaMesh(UriPath nodePath) {
    if (nodePath.isAbsolute()) {
      return null;
    }
    final Uri meshUri = nodePath.isEmpty() ? Uri.empty() : Uri.parse(nodePath.head());
    final MeshBinding meshBinding = getMesh(meshUri);
    if (meshBinding != null) {
      final MetaMesh metaMesh = meshBinding.meshWrapper().unwrapMesh(MetaMesh.class);
      if (metaMesh != null) {
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop meshUri
          if (!nodePath.isEmpty()) {
            nodePath = nodePath.tail(); // drop /
          }
        }
        return metaMesh.resolveMetaMesh(nodePath);
      }
    }
    return null;
  }

  protected NodeBinding resolveMetaPart(UriPath nodePath) {
    final MeshBinding meshBinding = network();
    if (meshBinding != null) {
      final MetaMesh metaMesh = meshBinding.meshWrapper().unwrapMesh(MetaMesh.class);
      if (metaMesh != null) {
        return metaMesh.resolveMetaPart(nodePath);
      }
    }
    return null;
  }

  protected NodeBinding resolveMetaHost(UriPath nodePath) {
    final MeshBinding meshBinding = network();
    if (meshBinding != null) {
      final MetaMesh metaMesh = meshBinding.meshWrapper().unwrapMesh(MetaMesh.class);
      if (metaMesh != null) {
        return metaMesh.resolveMetaHost(nodePath);
      }
    }
    return null;
  }

  protected NodeBinding resolveMetaNode(UriPath nodePath) {
    final MeshBinding meshBinding = network();
    if (meshBinding != null) {
      final MetaMesh metaMesh = meshBinding.meshWrapper().unwrapMesh(MetaMesh.class);
      if (metaMesh != null) {
        return metaMesh.resolveMetaNode(nodePath);
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy