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

io.firebus.information.NodeInformation Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package io.firebus.information;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;

import io.firebus.Address;

public class NodeInformation 
{
	protected int nodeId;
	protected ArrayList
addresses; protected ArrayList repeaters; protected HashMap functions; protected long lastSentDiscovery; protected long lastUpdated; protected int rating; protected boolean unconnectable; protected boolean unresponsive; public NodeInformation(int ni) { nodeId = ni; initialise(); } protected void initialise() { addresses = new ArrayList
(); repeaters = new ArrayList(); functions = new HashMap(); unconnectable = false; unresponsive = false; rating = 0; } public void setLastDiscoverySentTime(long t) { lastSentDiscovery = t; } public void setLastUpdatedTime(long t) { lastUpdated = t; } public void setUnconnectable() { unconnectable = true; } public void setUnresponsive() { unresponsive = true; } public void addAddress(Address a) { if(a != null && !containsAddress(a)) addresses.add(a); } public void removeAddress(Address a) { if(containsAddress(a)) addresses.remove(a); } public void addRepeater(int id) { if(!repeaters.contains(id)) repeaters.add(id); } public void addFunctionInformation(String fn, FunctionInformation si) { if(!functions.containsKey(fn)) functions.put(fn, si); } public int getFunctionCount() { return functions.size(); } public void removeFunctionInformation(String fn) { if(functions.containsKey(fn)) functions.remove(fn); } public int getNodeId() { return nodeId; } public int getAddressCount() { return addresses.size(); } public Address getAddress(int i) { return addresses.get(i); } public boolean containsAddress(Address a) { for(int i = 0; i < addresses.size(); i++) if(addresses.get(i).equals(a)) return true; return false; } public long getLastDiscoverySentTime() { return lastSentDiscovery; } public long getLastAdvertisedTime() { return lastUpdated; } public int getRandomRepeater() { Random r = new Random(); if(repeaters.size() > 0) return repeaters.get(r.nextInt(repeaters.size())); return 0; } public FunctionInformation getFunctionInformation(String sn) { return functions.get(sn); } public boolean isUnconnectable() { return unconnectable; } public boolean isUnresponsive() { return unresponsive; } public void reduceRating(int r) { rating -= r; } public void resetRating() { rating = 0; } public int getRating() { return rating; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Id : " + nodeId + " [" + getRating() + "]\r\n"); for(int i = 0; i < addresses.size(); i++) sb.append("Address : " + addresses.get(i) + "\r\n"); for(int i = 0; i < repeaters.size(); i++) sb.append("Repeater : " + repeaters.get(i) + "\r\n"); Iterator it = functions.keySet().iterator(); while(it.hasNext()) { String fn = it.next(); FunctionInformation fi = functions.get(fn); sb.append("Function : " + fi + "\r\n"); } return sb.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy