Added logging
This commit is contained in:
parent
1137aaed90
commit
dac5fe1615
12
ws2012/P2P/uebungen/4/logging.properties
Normal file
12
ws2012/P2P/uebungen/4/logging.properties
Normal file
@ -0,0 +1,12 @@
|
||||
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
|
||||
|
||||
tutego.level = ALL
|
||||
|
||||
java.util.logging.SimpleFormatter.format=[%1$tF %1$tr] %3$s %4$s: %5$s %n
|
||||
|
||||
java.util.logging.ConsoleHandler.level=ALL
|
||||
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
|
||||
|
||||
java.util.logging.FileHandler.level=SEVERE
|
||||
java.util.logging.FileHandler.pattern=network.log
|
||||
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
|
||||
@ -1,25 +1,41 @@
|
||||
package network;
|
||||
|
||||
import java.util.logging.LogManager;
|
||||
|
||||
import peer.Peer;
|
||||
|
||||
public class Network {
|
||||
|
||||
|
||||
//grow a network: Starting with one peer, repeatedly let peers spawn or leave at random
|
||||
/**
|
||||
* grow a network: Starting with one peer, repeatedly let peers spawn or
|
||||
* leave at random
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
System.setProperty("java.util.logging.config.file",
|
||||
"logging.properties");
|
||||
|
||||
try {
|
||||
LogManager.getLogManager().readConfiguration();
|
||||
}
|
||||
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Peer a = new Peer("A");
|
||||
System.out.println("Spawn B:");
|
||||
|
||||
// System.out.println("Spawn B:");
|
||||
a.spawn("B");
|
||||
System.out.println("Spawn C:");
|
||||
|
||||
// System.out.println("Spawn C:");
|
||||
a.spawn("C");
|
||||
System.out.println("Spawn D:");
|
||||
|
||||
// System.out.println("Spawn D:");
|
||||
a.spawn("D");
|
||||
|
||||
System.out.println("Leave A:");
|
||||
|
||||
// System.out.println("Leave A:");
|
||||
a.leave();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -3,40 +3,40 @@ package peer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Node {
|
||||
|
||||
|
||||
|
||||
private final static Logger LOGGER = Logger.getLogger(Node.class.getName());
|
||||
|
||||
// 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 String name;
|
||||
|
||||
|
||||
private List<Node> neighbours = new ArrayList<Node>();
|
||||
|
||||
|
||||
public Node(int id, String name){
|
||||
public Node(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Add one single neighbour
|
||||
*
|
||||
* @param neighbour
|
||||
*/
|
||||
public void addSingleNeighbour(Node neighbour){
|
||||
|
||||
public void addSingleNeighbour(Node neighbour) {
|
||||
|
||||
Iterator<Node> it = this.neighbours.iterator();
|
||||
|
||||
if(!it.hasNext()){ //list of neighbours is empty
|
||||
|
||||
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()){
|
||||
} else {
|
||||
// check if neighbour is already in list
|
||||
while (it.hasNext()) {
|
||||
if (it.next().getId() == neighbour.getId()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -44,34 +44,37 @@ public class Node {
|
||||
}
|
||||
printInfos();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a list of new neighbours
|
||||
*
|
||||
* @param neighbours
|
||||
*/
|
||||
public void addMultiNeighbours(List<Node> neighbours){
|
||||
public void addMultiNeighbours(List<Node> neighbours) {
|
||||
Iterator<Node> it = neighbours.iterator();
|
||||
while(it.hasNext()){
|
||||
while (it.hasNext()) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if actual node has neighbour with given id -> remove it
|
||||
*
|
||||
* @param node
|
||||
*/
|
||||
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(){
|
||||
printInfos();
|
||||
}
|
||||
|
||||
public boolean hasNeighbours() {
|
||||
return (this.neighbours.size() > 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* checks if actual node has neighbour with given id
|
||||
@ -79,35 +82,36 @@ public class Node {
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public boolean hasNeighbour(int id){
|
||||
for(int i = 0; i < neighbours.size(); i++){
|
||||
public boolean hasNeighbour(int id) {
|
||||
for (int i = 0; i < neighbours.size(); i++) {
|
||||
Node tmp = neighbours.get(i);
|
||||
if(tmp.id == id){
|
||||
if (tmp.id == id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public List<Node> getNeighbours(){
|
||||
|
||||
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 + ", ");
|
||||
|
||||
}
|
||||
System.out.println();
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void printInfos() {
|
||||
StringBuilder result = new StringBuilder(128);
|
||||
result.append("Name: " + this.name + ", Neighbours: ");
|
||||
for (int i = 0; i < this.neighbours.size(); i++) {
|
||||
result.append(this.neighbours.get(i).name + ", ");
|
||||
|
||||
}
|
||||
LOGGER.info(result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -2,50 +2,51 @@ package peer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Peer {
|
||||
|
||||
private final static Logger LOGGER = Logger.getLogger(Node.class.getName());
|
||||
|
||||
private Node node;
|
||||
|
||||
|
||||
public Peer(String name){
|
||||
public Peer(String name) {
|
||||
this.node = new Node(new Random().nextInt(), name);
|
||||
}
|
||||
|
||||
public Node getNode(){
|
||||
|
||||
public Node getNode() {
|
||||
return this.node;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create another peer, mutually link creator and spawn.
|
||||
*/
|
||||
public void spawn(String name){
|
||||
//create a new node
|
||||
public void spawn(String name) {
|
||||
LOGGER.info("Spawning " + name);
|
||||
|
||||
// create a new node
|
||||
Node spawnNode = new Node(new Random().nextInt(), name);
|
||||
spawnNode.addSingleNeighbour(this.node);
|
||||
|
||||
//link new node to actual 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(){
|
||||
public void leave() {
|
||||
LOGGER.info("Leaving..." + node.getName());
|
||||
|
||||
List<Node> neighbours = this.node.getNeighbours();
|
||||
|
||||
for(int i = 0; i < neighbours.size(); i++){
|
||||
//add all neighbours
|
||||
|
||||
for (int i = 0; i < neighbours.size(); i++) {
|
||||
// add all neighbours
|
||||
neighbours.get(i).addMultiNeighbours(neighbours);
|
||||
//remove itself
|
||||
neighbours.get(i).removeNeighbour(this.node);
|
||||
// remove itself
|
||||
neighbours.get(i).removeNeighbour(this.node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user