From c06d2c7cf074d774fe7781ffb666fe2454bb6e53 Mon Sep 17 00:00:00 2001 From: "M.Scholz" Date: Tue, 13 Nov 2012 17:58:53 +0100 Subject: [PATCH] some improvements --- .../P2P/uebungen/4/src/network/Network.java | 12 +++ ws2012/P2P/uebungen/4/src/peer/Node.java | 77 +++++++++++++++++-- ws2012/P2P/uebungen/4/src/peer/Peer.java | 39 ++++++++++ 3 files changed, 120 insertions(+), 8 deletions(-) diff --git a/ws2012/P2P/uebungen/4/src/network/Network.java b/ws2012/P2P/uebungen/4/src/network/Network.java index 1bf6a823..cdabade6 100644 --- a/ws2012/P2P/uebungen/4/src/network/Network.java +++ b/ws2012/P2P/uebungen/4/src/network/Network.java @@ -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(); } diff --git a/ws2012/P2P/uebungen/4/src/peer/Node.java b/ws2012/P2P/uebungen/4/src/peer/Node.java index da7c4885..93465992 100644 --- a/ws2012/P2P/uebungen/4/src/peer/Node.java +++ b/ws2012/P2P/uebungen/4/src/peer/Node.java @@ -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 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 neighbours){ - this.id = id; - this.neighbours = neighbours; + private List neighbours = new ArrayList(); +>>>>>>> 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 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 neighbours){ Iterator 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; } -} \ No newline at end of file + + public List 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 diff --git a/ws2012/P2P/uebungen/4/src/peer/Peer.java b/ws2012/P2P/uebungen/4/src/peer/Peer.java index 69fca905..0812cc71 100644 --- a/ws2012/P2P/uebungen/4/src/peer/Peer.java +++ b/ws2012/P2P/uebungen/4/src/peer/Peer.java @@ -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 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 }