package com.sf.vertx.api.pojo;

import java.io.Serializable;

import com.alibaba.fastjson2.annotation.JSONField;

import lombok.Data;

/**
 * String ip：负载IP <br>
 * final Integer weight：权重，保存配置的权重 <br>
 * Integer effectiveWeight：有效权重，轮询的过程权重可能变化 <br>
 * Integer currentWeight：当前权重，比对该值大小获取节点<br>
 * 第一次加权轮询时：currentWeight = weight = effectiveWeight <br>
 * 后面每次加权轮询时：currentWeight 的值都会不断变化，其他权重不变 <br>
 */
public class Node implements Comparable<Node>, Serializable {
	private static final long serialVersionUID = -2846988871213226377L;
	private String ip;
    private Integer port;
	private Integer weight;
	private Integer effectiveWeight;
	private Integer currentWeight;
	private String protocol; // 协议

	public Node() {
	}
	
	public Node(String ip, Integer weight) {
		this.ip = ip;
		this.weight = weight;
		this.effectiveWeight = weight;
		this.currentWeight = weight;
	}

	public Node(String ip, Integer weight, Integer effectiveWeight, Integer currentWeight) {
		this.ip = ip;
		this.weight = weight;
		this.effectiveWeight = effectiveWeight;
		this.currentWeight = currentWeight;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public Integer getWeight() {
		return weight;
	}

	public void setWeight(Integer weight) {
		this.weight = weight;
	}

	public Integer getEffectiveWeight() {
		return effectiveWeight;
	}

	public void setEffectiveWeight(Integer effectiveWeight) {
		this.effectiveWeight = effectiveWeight;
	}

	public Integer getCurrentWeight() {
		return currentWeight;
	}

	public void setCurrentWeight(Integer currentWeight) {
		this.currentWeight = currentWeight;
	}

	@Override
	public int compareTo(Node node) {
		return currentWeight > node.currentWeight ? 1 : (currentWeight.equals(node.currentWeight) ? 0 : -1);
	}

	@Override
	public String toString() {
		return "{ip='" + ip + "', weight=" + weight + ", effectiveWeight=" + effectiveWeight + ", currentWeight="
				+ currentWeight + "}";
	}
}
