/* 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. */
/* 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.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
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.Msg;
import org.simgrid.msg.Comm;
import org.simgrid.msg.Host;
import org.simgrid.msg.Task;
import org.simgrid.msg.Process;
- 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));
+
if (getPeersData()) {
Msg.debug("Got " + peers.size() + " peers from the tracker");
Msg.debug("Here is my current status: " + getStatus());
if (getPeersData()) {
Msg.debug("Got " + peers.size() + " peers from the tracker");
Msg.debug("Here is my current status: " + getStatus());
assert message.index >= 0 && message.index < Common.FILE_PIECES;
assert remotePeer.bitfield != null;
remotePeer.bitfield[message.index] = '1';
assert message.index >= 0 && message.index < Common.FILE_PIECES;
assert remotePeer.bitfield != null;
remotePeer.bitfield[message.index] = '1';
//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);
}
//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);
}
sendRequest(message.mailbox,message.index,blockIndex,blockLength);
}
break;
case REQUEST:
assert message.index >= 0 && message.index < Common.FILE_PIECES;
if (!remotePeer.chokedUpload) {
sendRequest(message.mailbox,message.index,blockIndex,blockLength);
}
break;
case REQUEST:
assert message.index >= 0 && message.index < Common.FILE_PIECES;
if (!remotePeer.chokedUpload) {
- sendPiece(message.mailbox,message.index,false,message.blockIndex,message.blockLength);
+ sendPiece(message.mailbox,message.index,false,message.blockIndex,message.blockLength);
+ message.issuerHostname + ")");
if (bitfield[message.index] == '0') {
updateBitfieldBlocks(message.index,message.blockIndex,message.blockLength);
+ message.issuerHostname + ")");
if (bitfield[message.index] == '0') {
updateBitfieldBlocks(message.index,message.blockIndex,message.blockLength);
} while (!(bitfield[currentPiece] == '0' && !currentPieces.contains(currentPiece)));
currentPieces.add(currentPiece);
} 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;
// Update the list of current choked and unchoked peers, using the choke algorithm
private void updateChokedPeers() {
round = (round + 1) % 3;
//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) {
//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) {
- 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);
//Getting the block to send.
int blockIndex = getFirstBlock(piece);
int blockLength = Common.PIECES_BLOCKS - blockIndex ;
//Getting the block to send.
int blockIndex = getFirstBlock(piece);
int blockLength = Common.PIECES_BLOCKS - blockIndex ;
if (remotePeer.bitfield[piece] == '1') {
sendRequest(remotePeer.mailbox, piece, blockIndex, 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) {
}
for (Connection connection : peers.values()) {
if (connection.bitfield != null && connection.bitfield[currentPiece] == '1' && !connection.amInterested) {
MessageTask task = new MessageTask(MessageTask.Type.INTERESTED, hostname, this.mailbox, this.id);
MessageTask task = new MessageTask(MessageTask.Type.INTERESTED, hostname, this.mailbox, this.id);
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));
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,