some improvements
This commit is contained in:
parent
e567ffc0b0
commit
c06d2c7cf0
@ -1,11 +1,23 @@
|
||||
package network;
|
||||
|
||||
import peer.Peer;
|
||||
|
||||
public class Network {
|
||||
|
||||
|
||||
//grow a network: Starting with one peer, repeatedly let peers spawn or leave at random
|
||||
public static void main(String[] args) {
|
||||
|
||||
Peer a = new Peer("A");
|
||||
System.out.println("Spawn B:");
|
||||
a.spawn("B");
|
||||
System.out.println("Spawn C:");
|
||||
a.spawn("C");
|
||||
System.out.println("Spawn D:");
|
||||
a.spawn("D");
|
||||
|
||||
System.out.println("Leave A:");
|
||||
a.leave();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,19 +1,28 @@
|
||||
package peer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class Node {
|
||||
|
||||
<<<<<<< HEAD
|
||||
// node id must be unique! id = IP of peer ?!?
|
||||
// -> Should be unique if we don't consider peers behind a NAT.
|
||||
private int id;
|
||||
|
||||
private List<Node> neighbours;
|
||||
=======
|
||||
private int id; // node id must be unique! id = IP of peer ?!?
|
||||
//-> Should be unique if we don't consider peers behind a NAT.
|
||||
private String name;
|
||||
|
||||
public Node(int id, List<Node> neighbours){
|
||||
this.id = id;
|
||||
this.neighbours = neighbours;
|
||||
private List<Node> neighbours = new ArrayList<Node>();
|
||||
>>>>>>> some improvements
|
||||
|
||||
public Node(int id, String name){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
@ -23,7 +32,21 @@ public class Node {
|
||||
* @param neighbour
|
||||
*/
|
||||
public void addSingleNeighbour(Node neighbour){
|
||||
this.neighbours.add(neighbour);
|
||||
|
||||
Iterator<Node> it = this.neighbours.iterator();
|
||||
|
||||
if(!it.hasNext()){ //list of neighbours is empty
|
||||
this.neighbours.add(neighbour);
|
||||
}else{
|
||||
//check if neighbour is already in list
|
||||
while(it.hasNext()){
|
||||
if(it.next().getId() == neighbour.getId()){
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.neighbours.add(neighbour);
|
||||
}
|
||||
printInfos();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,14 +57,26 @@ public class Node {
|
||||
public void addMultiNeighbours(List<Node> neighbours){
|
||||
Iterator<Node> it = neighbours.iterator();
|
||||
while(it.hasNext()){
|
||||
this.neighbours.add(it.next());
|
||||
addSingleNeighbour(it.next());
|
||||
}
|
||||
}
|
||||
|
||||
//checks if actual node has neighbour with given id -> remove it
|
||||
public void removeNeighbour(Node node){
|
||||
for(int i = 0; i < this.neighbours.size(); i++){
|
||||
Node tmp = this.neighbours.get(i);
|
||||
if(tmp.id == node.id){
|
||||
this.neighbours.remove(i);
|
||||
}
|
||||
}
|
||||
printInfos();
|
||||
}
|
||||
|
||||
public boolean hasNeighbours(){
|
||||
return (this.neighbours.size() > 0);
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
/**
|
||||
* checks if actual node has neighbour with given id
|
||||
*
|
||||
@ -54,7 +89,33 @@ public class Node {
|
||||
if(tmp.id == id){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
=======
|
||||
public int getId(){
|
||||
return this.id;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Node> getNeighbours(){
|
||||
return this.neighbours;
|
||||
}
|
||||
|
||||
|
||||
//for testing
|
||||
public void printInfos(){
|
||||
System.out.print("Name: " + this.name + ", Neighbours: ");
|
||||
for(int i = 0; i < this.neighbours.size(); i++){
|
||||
System.out.print(this.neighbours.get(i).name + ", ");
|
||||
>>>>>>> some improvements
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
}
|
||||
=======
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
>>>>>>> some improvements
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
package peer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class Peer {
|
||||
|
||||
private Node node;
|
||||
<<<<<<< HEAD
|
||||
|
||||
/**
|
||||
* Create another peer, mutually link creator and spawn.
|
||||
@ -18,6 +22,41 @@ public class Peer {
|
||||
*/
|
||||
public void leave() {
|
||||
|
||||
=======
|
||||
|
||||
|
||||
public Peer(String name){
|
||||
this.node = new Node(new Random().nextInt(), name);
|
||||
}
|
||||
|
||||
public Node getNode(){
|
||||
return this.node;
|
||||
}
|
||||
|
||||
|
||||
//create another peer, mutually link creator and spawn
|
||||
public void spawn(String name){
|
||||
//create a new node
|
||||
Node spawnNode = new Node(new Random().nextInt(), name);
|
||||
spawnNode.addSingleNeighbour(this.node);
|
||||
|
||||
//link new node to actual node
|
||||
this.node.addSingleNeighbour(spawnNode);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//circularly link all neighbours, remove itself form all neighbours and exit
|
||||
public void leave(){
|
||||
List<Node> neighbours = this.node.getNeighbours();
|
||||
|
||||
for(int i = 0; i < neighbours.size(); i++){
|
||||
//add all neighbours
|
||||
neighbours.get(i).addMultiNeighbours(neighbours);
|
||||
//remove itself
|
||||
neighbours.get(i).removeNeighbour(this.node);
|
||||
}
|
||||
>>>>>>> some improvements
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user