public static final int MAX_UNCHOKED_PEERS = 4; /* Number of peers that can be unchocked at a given time */
public static final int UPDATE_CHOKED_INTERVAL = 30; /* Interval between each update of the choked peers */
public static final int MAX_PIECES = 1; /* Number of pieces the peer asks for simultaneously */
+ private Common() {
+ throw new IllegalAccessError("Utility class");
+ }
}
import java.util.Arrays;
public class Connection {
- public int id;
- public char bitfield[];
- public String mailbox;
+ protected int id;
+ protected char bitfield[];
+ protected String mailbox;
// Indicates if we are interested in something this peer has
- public boolean amInterested = false;
+ protected boolean amInterested = false;
// Indicates if the peer is interested in one of our pieces
- public boolean interested = false;
+ protected boolean interested = false;
// Indicates if the peer is choked for the current peer
- public boolean chokedUpload = true;
+ protected boolean chokedUpload = true;
// Indicates if the peer has choked the current peer
- public boolean chokedDownload = true;
+ protected boolean chokedDownload = true;
// Number of messages we have received from the peer
- public int messagesCount = 0;
- public double peerSpeed = 0;
- public double lastUnchoke = 0;
+ protected int messagesCount = 0;
+ protected double peerSpeed = 0;
+ protected double lastUnchoke = 0;
public Connection(int id) {
this.id = id;
BITFIELD,
REQUEST,
PIECE
- };
+ }
- public Type type;
- public String issuerHostname;
- public String mailbox;
- public int peerId;
- public char bitfield[];
- public int index;
- public int blockIndex;
- public int blockLength;
- public boolean stalled;
+ protected Type type;
+ protected String issuerHostname;
+ protected String mailbox;
+ protected int peerId;
+ protected char bitfield[];
+ protected int index;
+ protected int blockIndex;
+ protected int blockLength;
+ protected boolean stalled;
public MessageTask(Type type, String issuerHostname, String mailbox, int peerId) {
this(type,issuerHostname,mailbox,peerId,-1,false,-1,-1);
//Retrieve the end time
deadline = Double.valueOf(args[0]);
//Building peers array
- peersList = new ArrayList<Integer>();
+ peersList = new ArrayList<>();
- Msg.info("Tracker launched.");
+ Msg.info("Tracker launched.");
while (Msg.getClock() < deadline) {
if (commReceived == null) {
commReceived = Task.irecv(Common.TRACKER_MAILBOX);
public enum Type {
REQUEST,
ANSWER
- };
+ }
- public Type type;
- public String hostname;
- public String mailbox;
- public int peerId;
- public int uploaded;
- public int downloaded;
- public int left;
- public double interval;
- public ArrayList<Integer> peers;
+ protected Type type;
+ protected String hostname;
+ protected String mailbox;
+ protected int peerId;
+ protected int uploaded;
+ protected int downloaded;
+ protected int left;
+ protected double interval;
+ protected ArrayList<Integer> peers;
public TrackerTask(String hostname, String mailbox, int peerId) {
this(hostname, mailbox, peerId, 0, 0, Common.FILE_SIZE);
this.uploaded = uploaded;
this.downloaded = downloaded;
this.left = left;
- this.peers = new ArrayList<Integer>();
+ this.peers = new ArrayList<>();
}
}
public final static int PERIODIC_FIX_FINGERS_DELAY = 120;
public final static int PERIODIC_CHECK_PREDECESSOR_DELAY = 120;
public final static int PERIODIC_LOOKUP_DELAY = 10;
+ private Common() {
+ throw new IllegalAccessError("Utility class");
+ }
}
import org.simgrid.msg.NativeException;
import org.simgrid.msg.HostNotFoundException;
-public class Main {
- public static Mutex mutex;
- public static Process processToMigrate = null;
+class Main {
+ protected static Mutex mutex;
+ protected static Process processToMigrate = null;
public static void main(String[] args) throws NativeException {
Msg.init(args);
Emigrant emigrant = new Emigrant("Jacquelin","emigrant");
emigrant.start();
} catch (HostNotFoundException e){
- System.out.println("Create processes failed!");
+ Msg.error("Create processes failed!");
+ e.printStackTrace();
}
/* execute the simulation. */
for (i = 0; i < number_of_tasks; i++) {
char mailbox[256];
- sprintf(mailbox, "worker-%ld", i % workers_count);
+ snprintf(mailbox, 255, "worker-%ld", i % workers_count);
msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1));
*((double *) task->data) = MSG_get_clock();
XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
for (i = 0; i < workers_count; i++) {
char mailbox[256];
- sprintf(mailbox, "worker-%ld", i % workers_count);
+ snprintf(mailbox, 255, "worker-%ld", i % workers_count);
msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
int a = MSG_task_send_with_timeout(task,mailbox,1.0);
- if (a == MSG_OK)
- continue;
+ if (a != MSG_OK){
+ MSG_task_destroy(task);
+ }
if (a == MSG_HOST_FAILURE) {
XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
- MSG_task_destroy(task);
return 0;
} else if (a == MSG_TRANSFER_FAILURE) {
XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
- MSG_task_destroy(task);
} else if (a == MSG_TIMEOUT) {
XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
- MSG_task_destroy(task);
- } else {
+ } else if (a != MSG_OK){
XBT_INFO("Hey ?! What's up ? ");
xbt_die("Unexpected behavior with '%s': %d", mailbox, a);
}
long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
- sprintf(mailbox, "worker-%ld", id);
+ snprintf(mailbox, 79,"worker-%ld", id);
while (1) {
- int a;
- double time1, time2;
-
- time1 = MSG_get_clock();
- a = MSG_task_receive( &(task), mailbox);
- time2 = MSG_get_clock();
+ double time1 = MSG_get_clock();
+ int a = MSG_task_receive( &(task), mailbox);
+ double time2 = MSG_get_clock();
if (a == MSG_OK) {
XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
if (MSG_task_get_data(task) == FINALIZE) {