我需要在与具有构造函数的类不同的类中实现静态计数器

时间:2014-08-10 16:49:41

标签: java

我需要为一个P2P网络中的节点ID创建静态计数器。目前节点成为ID,但我需要更改它,因为我需要每个节点的确切ID。节点1必须具有ID = 1,依此类推。我的问题是计数器必须不在类中,节点的构造函数是。

所以我的计数器必须在PastryNodeFactory类

public class PastryNodeFactory implements ComponentFactory {
@Override
public Component createComponent(Host host) {
    PastryNode node = new PastryNode(host.getTransLayer());
    return node;
}
}

这是我的PastryNode类:

public class PastryNode extends AbstractOverlayNode<PastryID, PastryContact>
    implements DHTNode<PastryID, PastryContact, PastryKey> {

private static Logger log = SimLogger.getLogger(PastryNode.class);

/**
 * The transport layer of the node
 */
private TransLayer transLayer;

/**
 * The contact of the node
 */
private final PastryContact localContact;

/**
 * The message handler of this node
 */
protected PastryMessageHandler msgHandler;

/**
 * The leaf set of the node
 */
protected LeafSet leafSet;

/**
 * The neighborhood set of the node
 */
protected NeighborhoodSet neighborhoodSet;

/**
 * Tells whether this node is in a state where it should be present in the
 * overlay. This means, it either is present or absent due to churn. This
 * flag is needed to decide if a node should initiate a automatic join after
 * a churn event. This way, absent nodes stay absent after a churn event.
 */
private boolean wantsToBePresent = false;

/**
 * Holds pending operations
 */
protected final Map<Integer, AbstractPastryOperation<?>> registeredOperations = new LinkedHashMap<Integer, AbstractPastryOperation<?>>();

/**
 * A map that contains all stored DHTObjects of this node, wrapped in an
 * instance of DHTListener. This allows for later addition of other
 * DHT-Related services.
 */
private DHTListener<PastryKey> dht = new SimpleDHTService<PastryKey>();

private KBRLookupProvider<PastryID, PastryContact, PastryKey> kbrProvider;

private KBRListener<PastryID, PastryContact, PastryKey> kbrListener;

// private final Map<PastryKey, DHTObject> storedObjects;

protected PastryNode(TransLayer translayer) {
    super(new PastryID(
            translayer.getLocalTransInfo(PastryConstants.PASTRY_PORT)),
            PastryConstants.PASTRY_PORT);

    this.transLayer = translayer;

    localContact = new PastryContact(getOverlayID(), getTransLayer()
            .getLocalTransInfo(PastryConstants.PASTRY_PORT));

你能帮我解决这个问题吗?

0 个答案:

没有答案