-/* Copyright (c) 2006-2014, 2016. The SimGrid Team.
+/* Copyright (c) 2006-2018. The SimGrid Team.
* All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
protected char[][] bitfieldBlocks = new char[Common.FILE_PIECES][Common.PIECES_BLOCKS];
protected short[] piecesCount = new short[Common.FILE_PIECES];
protected int piecesRequested = 0;
- protected ArrayList<Integer> currentPieces = new ArrayList<Integer>();
+ protected ArrayList<Integer> currentPieces = new ArrayList<>();
protected int currentPiece = -1;
- protected HashMap<Integer, Connection> activePeers = new HashMap<Integer, Connection>();
- protected HashMap<Integer, Connection> peers = new HashMap<Integer, Connection>();
+ protected HashMap<Integer, Connection> activePeers = new HashMap<>();
+ protected HashMap<Integer, Connection> peers = new HashMap<>();
protected Comm commReceived = null;
public Peer(Host host, String name, String[]args) {
Msg.info("Wrong number of arguments");
}
if (args.length == 3) {
- init(Integer.valueOf(args[0]),true);
+ init(Integer.parseInt(args[0]),true);
} else {
- init(Integer.valueOf(args[0]),false);
+ init(Integer.parseInt(args[0]),false);
}
//Retrieve the deadline
- deadline = Double.valueOf(args[1]);
+ deadline = Double.parseDouble(args[1]);
if (deadline < 0) {
Msg.info("Wrong deadline supplied");
return;
}
}
catch (MsgException e) {
+ e.printStackTrace();
commReceived = null;
}
}
}
private boolean getPeersData() {
- boolean success = false, sendSuccess = false;
+ boolean success = false;
+ boolean sendSuccess = false;
double timeout = Msg.getClock() + Common.GET_PEERS_TIMEOUT;
//Build the task to send to the tracker
TrackerTask taskSend = new TrackerTask(hostname, mailboxTracker, id);
sendSuccess = true;
}
catch (MsgException e) {
+ e.printStackTrace();
}
}
while (!success && Msg.getClock() < timeout) {
success = true;
}
}
- catch (MsgException e) {}
+ catch (MsgException e) {
+ e.printStackTrace();
+ }
commReceived = null;
}
commReceived = null;
return success;
}
- void handleMessage(Task task) {
+ private void handleMessage(Task task) {
MessageTask message = (MessageTask)task;
Connection remotePeer = peers.get(message.peerId);
switch (message.type) {
}
}
break;
+ default:
+ Msg.error("Unexpected message type: " + message.type);
+ break;
}
if (remotePeer != null) {
remotePeer.addSpeedValue(1 / (Msg.getClock() - beginReceiveTime));
beginReceiveTime = Msg.getClock();
}
- void waitForPieces() {
+ private void waitForPieces() {
boolean finished = false;
while (Msg.getClock() < deadline && !finished) {
if (commReceived == null) {
* @brief Updates the list of who has a piece from a bitfield
* @param bitfield bitfield
*/
- private void updatePiecesCountFromBitfield(char bitfield[]) {
+ private void updatePiecesCountFromBitfield(char[] bitfield) {
for (int i = 0; i < Common.FILE_PIECES; i++) {
if (bitfield[i] == '1') {
piecesCount[i]++;
* If the peer has more than pieces, he downloads the pieces that are the less
* replicated
*/
- void updateCurrentPiece() {
+ private void updateCurrentPiece() {
if (currentPieces.size() >= (Common.FILE_PIECES - pieces)) {
return;
}
- if (true || pieces < 3) {
- int peerPiece;
- do {
- currentPiece = stream.randInt(0,Common.FILE_PIECES - 1);
- } while (!(bitfield[currentPiece] == '0' && !currentPieces.contains(currentPiece)));
- }
- else {
- //trivial min algorithm.
- //TODO
- }
+
+ //TODO: trivial min algorithm when pieces >= 3
+ do {
+ currentPiece = stream.randInt(0,Common.FILE_PIECES - 1);
+ } while (!(bitfield[currentPiece] == '0' && !currentPieces.contains(currentPiece)));
+
currentPieces.add(currentPiece);
Msg.debug("New interested piece: " + currentPiece);
assert currentPiece >= 0 && currentPiece < Common.FILE_PIECES;
} else {
//Random optimistic unchoking
if (round == 0) {
- int j = 0, i;
+ int j = 0;
do {
- i = 0;
+ int i = 0;
int idChosen = stream.randInt(0,peers.size() - 1);
for (Connection connection : peers.values()) {
if (i == idChosen) {
}
i++;
} //TODO: Not really the best way ever
- if (!peerChoosed.interested) {
+ if (peerChoosed != null && !peerChoosed.interested) {
peerChoosed = null;
}
j++;
return;
}
for (Integer piece : currentPieces) {
- //Getting the block to send.
- int blockIndex = -1, blockLength = 0;
- blockIndex = getFirstBlock(piece);
- blockLength = Common.PIECES_BLOCKS - blockIndex ;
+ //Getting the block to send.
+ int blockIndex = getFirstBlock(piece);
+ int blockLength = Common.PIECES_BLOCKS - blockIndex ;
blockLength = blockLength > Common.BLOCKS_REQUESTED ? Common.BLOCKS_REQUESTED : blockLength;
if (remotePeer.bitfield[piece] == '1') {
sendRequest(remotePeer.mailbox, piece, blockIndex, blockLength);
}
private String getStatus() {
- String s = "";
- for (int i = 0; i < Common.FILE_PIECES; i++) {
- s = s + bitfield[i];
- }
- return s;
+ StringBuilder s = new StringBuilder("");
+ for (int i = 0; i < Common.FILE_PIECES; i++)
+ s.append(bitfield[i]);
+ return s.toString();
}
}