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

nstream.reflect.MetaHost 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.MetaHostAgent;
import nstream.reflect.model.DataStats;
import nstream.reflect.model.HostStats;
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.HostBinding;
import swim.system.HostProxy;
import swim.system.LinkBinding;
import swim.system.NodeAddress;
import swim.system.NodeBinding;
import swim.system.WarpBinding;
import swim.uri.Uri;
import swim.uri.UriPath;

public class MetaHost extends HostProxy implements MetaRouter {
  final MetaPart parent;
  final MetaHostAgent agent;

  public MetaHost(MetaPart parent, HostBinding hostBinding) {
    super(hostBinding);
    this.parent = parent;
    this.agent = new MetaHostAgent(this);
  }

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

  @Override
  public MetaHostAgent 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.parent.processStats();
  }

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

  public HostStats hostStats() {
    return new HostStats(hostUri(), isRemote(), isConnected(), isPrimary(), isReplica(),
                         isMaster(), isSlave(), linkStats(), processStats());
  }

  public void cueSystemStats() {
    this.agent.cueSystemStats();
  }

  @Override
  public NodeBinding injectNode(NodeAddress nodeAddress, NodeBinding node) {
    return new MetaNode(this, super.injectNode(nodeAddress, node));
  }

  @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 didBecomeMaster() {
    super.didBecomeMaster();
    this.agent.bubbleStats();
    this.agent.cueStats();
  }

  @Override
  public void didBecomeSlave() {
    super.didBecomeSlave();
    this.agent.bubbleStats();
    this.agent.cueStats();
  }

  @Override
  public void didConnect() {
    super.didConnect();
    if (isPrimary() || isReplica()) {
      this.agent.bubbleStats();
      this.agent.cueStats();
    }
  }

  @Override
  public void didDisconnect() {
    super.didDisconnect();
    if (isPrimary() || isReplica()) {
      this.agent.bubbleStats();
      this.agent.cueStats();
    }
  }

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

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

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

  public NodeBinding resolveMetaNode(UriPath nodePath) {
    if (nodePath.isAbsolute()) {
      return null;
    }
    final Uri nodeUri = nodePath.isEmpty() ? Uri.empty() : Uri.parse(nodePath.head());
    final NodeBinding nodeBinding = getNode(nodeUri);
    if (nodeBinding != null) {
      final MetaNode metaNode = nodeBinding.nodeWrapper().unwrapNode(MetaNode.class);
      if (metaNode != null) {
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop nodeUri
          if (!nodePath.isEmpty()) {
            nodePath = nodePath.tail(); // drop /
          }
        }
        return metaNode.resolveMetaNode(nodePath);
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy