Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d459482de0e124e3450ec459795784babe76e170
[simgrid.git] / contrib / psg / src / example / bittorrent / BitfieldMsg.java
1 /*
2  * Copyright (c) 2007-2008 Fabrizio Frioli, Michele Pedrolli
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * --
18  *
19  * Please send your questions/suggestions to:
20  * {fabrizio.frioli, michele.pedrolli} at studenti dot unitn dot it
21  *
22  */
23
24 package example.bittorrent;
25
26 import peersim.core.*;
27
28 /**
29  *      This class is a {@link SimpleMsg} and represents the <tt>bitfield</tt>
30  *      message.
31  */
32 public class BitfieldMsg extends SimpleMsg{
33         /**
34          *      The status of the file to transmit to neighbors nodes
35          */
36         int[] array;
37         
38         /**
39          *      Defines the type of the Bitfield message. If <tt>isRequest</tt> is true, then
40          *      the message is a request of subscription; otherwise the message is a response.
41          */
42         boolean isRequest;
43         
44         /**
45          *      <p>The ACK value used to implement <i>ack</i> and <i>nack</i> messages.</p>
46          *      <p>It has value <tt>true</tt> if the message is a reponse and the sender has inserted
47          *      the receiver in its own cache of neighbors.<br/>
48          *      If for some reason (for instance the cache had already 80 peer inside at the moment of the
49          *      request) it was not possible to insert the peer, the value is <tt>false</tt>.<br/>
50          *      It has value <tt>false</tt> also if the message is a request and is sent when occours
51          *      an unespected message.
52          *      </p>
53          *      @see "The documentation to understand the 4 different types of Bitfield messages"
54          */
55         boolean ack;
56         
57         /**
58          *      The basic constructor of the Bitfield message.
59          *      @param type The type of the message, according to {@link SimpleMsg}
60          *      @param isRequest Defines if the message is a request or not
61          *      @param ack Defines if the message type is an <i>ack</i> or a <i>nack</i>
62          *      @param sender The sender node
63          *      @param source The array containing the status of the file
64          *      @param size The size of the array
65          */
66         public BitfieldMsg(int type, boolean isRequest, boolean ack, Node sender, int source[], int size){
67                 super.type = type;
68                 super.sender = sender;
69                 this.isRequest = isRequest;
70                 this.ack = ack;
71                 this.array = new int[size];
72                 for(int i=0; i<size;i++){ // it sends a copy
73                         if(source[i]==16)
74                                 this.array[i] = 1;
75                         else
76                                 this.array[i] = 0;
77                 }
78         }
79         
80         /**
81          *      Gets the array containing the status of the file.
82          *      @return The status of the file
83          */
84         public int[] getArray(){
85                 return this.array;      
86         }
87 }