-/* Copyright (c) 2006-2019. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
+import java.util.Random;
import org.simgrid.msg.Msg;
import org.simgrid.msg.Comm;
import org.simgrid.msg.Host;
import org.simgrid.msg.Task;
import org.simgrid.msg.Process;
-import org.simgrid.msg.RngStream;
import org.simgrid.msg.MsgException;
public class Peer extends Process {
+ Random rand = new Random();
protected int round = 0;
protected double beginReceiveTime;
protected double deadline;
- protected static RngStream stream = new RngStream();
protected int id;
protected String mailbox;
protected String mailboxTracker;
//TODO: trivial min algorithm when pieces >= 3
do {
- currentPiece = stream.randInt(0,Common.FILE_PIECES - 1);
+ currentPiece = rand.nextInt(Common.FILE_PIECES);
} while (!(bitfield[currentPiece] == '0' && !currentPieces.contains(currentPiece)));
currentPieces.add(currentPiece);
sendChoked(peerChoked.mailbox);
activePeers.remove(e.getKey());
}
- Connection peerChoosed = null;
+ Connection peerChosen = null;
//Separate the case from when the peer is seeding.
if (pieces == Common.FILE_PIECES) {
//Find the last unchoked peer.
double unchokeTime = deadline + 1;
for (Connection connection : peers.values()) {
if (connection.lastUnchoke < unchokeTime && connection.interested) {
- peerChoosed = connection;
+ peerChosen = connection;
unchokeTime = connection.lastUnchoke;
}
}
int j = 0;
do {
int i = 0;
- int idChosen = stream.randInt(0,peers.size() - 1);
+ int idChosen = rand.nextInt(peers.size());
for (Connection connection : peers.values()) {
if (i == idChosen) {
- peerChoosed = connection;
+ peerChosen = connection;
break;
}
i++;
} //TODO: Not really the best way ever
- if (peerChoosed != null && !peerChoosed.interested) {
- peerChoosed = null;
+ if (peerChosen != null && !peerChosen.interested) {
+ peerChosen = null;
}
j++;
- } while (peerChoosed == null && j < Common.MAXIMUM_PEERS);
+ } while (peerChosen == null && j < Common.MAXIMUM_PEERS);
} else {
Connection fastest = null;
double fastestSpeed = 0;
fastestSpeed = c.peerSpeed;
}
}
- peerChoosed = fastest;
+ peerChosen = fastest;
}
}
- if (peerChoosed != null) {
- activePeers.put(peerChoosed.id,peerChoosed);
- peerChoosed.chokedUpload = false;
- peerChoosed.lastUnchoke = Msg.getClock();
- sendUnchoked(peerChoosed.mailbox);
+ if (peerChosen != null) {
+ activePeers.put(peerChosen.id,peerChosen);
+ peerChosen.chokedUpload = false;
+ peerChosen.lastUnchoke = Msg.getClock();
+ sendUnchoked(peerChosen.mailbox);
}
}