-/* Copyright (c) 2006-2019. The SimGrid Team.
- * All rights reserved. */
+/* Copyright (c) 2006-2023. 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;
if (args.length != 3 && args.length != 2) {
Msg.info("Wrong number of arguments");
}
- if (args.length == 3) {
- init(Integer.parseInt(args[0]),true);
- } else {
- init(Integer.parseInt(args[0]),false);
- }
+ init(Integer.parseInt(args[0]), (args.length == 3));
+
//Retrieve the deadline
deadline = Double.parseDouble(args[1]);
if (deadline < 0) {
if (getPeersData()) {
Msg.debug("Got " + peers.size() + " peers from the tracker");
Msg.debug("Here is my current status: " + getStatus());
- beginReceiveTime = Msg.getClock();
+ beginReceiveTime = Msg.getClock();
if (hasFinished()) {
pieces = Common.FILE_PIECES;
sendHandshakeAll();
assert message.index >= 0 && message.index < Common.FILE_PIECES;
assert remotePeer.bitfield != null;
remotePeer.bitfield[message.index] = '1';
- piecesCount[message.index]++;
+ piecesCount[message.index]++;
//Send interested message to the peer if he has what we want
if (!remotePeer.amInterested && currentPieces.contains(message.index) ) {
remotePeer.amInterested = true;
sendInterested(remotePeer.mailbox);
}
-
+
if (currentPieces.contains(message.index)) {
- int blockIndex = getFirstBlock(message.index);
+ int blockIndex = getFirstBlock(message.index);
int blockLength = Common.PIECES_BLOCKS - blockIndex ;
- blockLength = blockLength > Common.BLOCKS_REQUESTED ? Common.BLOCKS_REQUESTED : blockLength;
+ blockLength = blockLength > Common.BLOCKS_REQUESTED ? Common.BLOCKS_REQUESTED : blockLength;
sendRequest(message.mailbox,message.index,blockIndex,blockLength);
}
break;
case REQUEST:
assert message.index >= 0 && message.index < Common.FILE_PIECES;
if (!remotePeer.chokedUpload) {
- Msg.debug("Received a REQUEST from " + message.peerId + "(" + message.issuerHostname + ") for "
+ Msg.debug("Received a REQUEST from " + message.peerId + "(" + message.issuerHostname + ") for "
+ message.peerId);
if (bitfield[message.index] == '1') {
- sendPiece(message.mailbox,message.index,false,message.blockIndex,message.blockLength);
+ sendPiece(message.mailbox,message.index,false,message.blockIndex,message.blockLength);
} else {
- Msg.debug("Received a REQUEST from " + message.peerId + " (" + message.issuerHostname
+ Msg.debug("Received a REQUEST from " + message.peerId + " (" + message.issuerHostname
+ ") but he is choked" );
}
}
break;
case PIECE:
if (message.stalled) {
- Msg.debug("The received piece " + message.index + " from " + message.peerId + " (" + message.issuerHostname
+ Msg.debug("The received piece " + message.index + " from " + message.peerId + " (" + message.issuerHostname
+ ") is stalled");
} else {
- Msg.debug("Received piece " + message.index + " from " + message.peerId + " ("
+ Msg.debug("Received piece " + message.index + " from " + message.peerId + " ("
+ message.issuerHostname + ")");
if (bitfield[message.index] == '0') {
updateBitfieldBlocks(message.index,message.blockIndex,message.blockLength);
//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);
// Update the list of current choked and unchoked peers, using the choke algorithm
private void updateChokedPeers() {
round = (round + 1) % 3;
- if (peers.size() == 0) {
+ if (peers.isEmpty()) {
return;
}
//remove a peer from the list
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);
}
}
return true;
}
- // Returns the first block of a piece that we don't have.
+ // Returns the first block of a piece that we don't have.
private int getFirstBlock(int piece) {
int blockIndex = -1;
for (int i = 0; i < Common.PIECES_BLOCKS; i++) {
//Getting the block to send.
int blockIndex = getFirstBlock(piece);
int blockLength = Common.PIECES_BLOCKS - blockIndex ;
- blockLength = blockLength > Common.BLOCKS_REQUESTED ? Common.BLOCKS_REQUESTED : blockLength;
+ blockLength = blockLength > Common.BLOCKS_REQUESTED ? Common.BLOCKS_REQUESTED : blockLength;
if (remotePeer.bitfield[piece] == '1') {
sendRequest(remotePeer.mailbox, piece, blockIndex, blockLength);
}
}
for (Connection connection : peers.values()) {
if (connection.bitfield != null && connection.bitfield[currentPiece] == '1' && !connection.amInterested) {
- connection.amInterested = true;
+ connection.amInterested = true;
MessageTask task = new MessageTask(MessageTask.Type.INTERESTED, hostname, this.mailbox, this.id);
- task.dsend(connection.mailbox);
+ task.dsend(connection.mailbox);
}
}
currentPiece = -1;
private void sendRequest(String mailbox, int piece, int blockIndex, int blockLength) {
Msg.debug("Sending a REQUEST to " + mailbox + " for piece " + piece + " and blocks " + blockIndex + ","
+ (blockIndex + blockLength));
- MessageTask task = new MessageTask(MessageTask.Type.REQUEST, hostname, this.mailbox, this.id, piece, blockIndex,
+ MessageTask task = new MessageTask(MessageTask.Type.REQUEST, hostname, this.mailbox, this.id, piece, blockIndex,
blockLength);
task.dsend(mailbox);
}