/** * Initializes the node <tt>n</tt> associating it with the BitTorrent protocol and setting the * reference to the tracker, the status of the file and the bandwidth. * * @param n The node to initialize */ public void initialize(Node n) { Node tracker = Network.get(0); BitTorrent p; p = (BitTorrent) n.getProtocol(pid); p.setTracker(tracker); p.setThisNodeID(n.getID()); setFileStatus(p); setBandwidth(p); }
/** * Sets the completed pieces for the given protocol <tt>p</tt>. * * @parm percentage The percentage of the downloaded pieces, according to {@link * #getProbability()} * @param p the BitTorrent protocol */ private void choosePieces(int percentage, BitTorrent p) { double temp = ((double) p.nPieces / 100.0) * percentage; // We use a double to avoid the loss of precision // during the division operation int completed = (int) temp; // integer number of piece to set as completed // 0 if the peer is a newer p.setCompleted(completed); if (percentage == 100) p.setPeerStatus(1); int tmp; while (completed != 0) { tmp = CommonState.r.nextInt(p.nPieces); if (p.getStatus(tmp) != 16) { p.setStatus(tmp, 16); completed--; } } }
/** * Set the maximum bandwidth for the node, choosing uniformly at random among 4 values. * * <p>The allowed bandwidth speed are 640 Kbps, 1 Mbps, 2 Mbps and 4 Mbps. * * @param p The BitTorrent protocol */ private void setBandwidth(BitTorrent p) { // int value = CommonState.r.nextInt(4); // switch(value){ // case 0: p.setBandwidth(640);break; //640Kbps // case 1: p.setBandwidth(1024);break;// 1Mbps // case 2: p.setBandwidth(2048);break;// 2Mbps // case 3: p.setBandwidth(4096);break; //4Mbps // } p.setBandwidth(1024); }
/** * Sets the status of the shared file according to the probability value given by {@link * #getProbability()}. * * @param p The BitTorrent protocol */ private void setFileStatus(BitTorrent p) { int percentage = p.getThisNodeID() == 1 ? 100 : 0; choosePieces(percentage, p); }