-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSample.java
More file actions
156 lines (137 loc) · 4.83 KB
/
Sample.java
File metadata and controls
156 lines (137 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package peersim.kademlia.das;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import peersim.kademlia.Util;
public class Sample {
/** Row and column numbers of a sample within a block */
private int row, column;
/** The unique ID of the block that this sample belongs to */
private long blockId;
/** The key of a sample in the DHT keyspace using rows number as reference */
private BigInteger idByRow;
/** The key of a sample in the DHT keyspace using column number as reference */
private BigInteger idByColumn;
/** Block that this sample is part of */
private Block block;
/** Initialise a sample instance and map it to the keyspace */
public Sample(long blockId, int row, int column, Block b) {
this.idByColumn = this.idByRow = null;
this.block = b;
this.row = row;
this.column = column;
this.blockId = blockId;
computeID();
}
/**
* Sample numbering to map each sample to an integer in the range 1 to SIZE*SIZE Samples are
* ordered by row
*/
public int sampleNumberByRow() {
return (this.row - 1) * this.block.getSize() + (this.column - 1);
}
/**
* Sample numbering to map each sample to an integer in the range 1 to SIZE*SIZE Samples are
* ordered by column
*/
public int sampleNumberByColumn() {
return (this.column - 1) * this.block.getSize() + (this.row - 1);
}
/** Map this sample to the DHT keyspace */
public void computeID() {
if (KademliaCommonConfigDas.MAPPING_FN == KademliaCommonConfigDas.SAMPLE_MAPPING_RANDOM) {
try {
String idName =
String.valueOf(blockId) + "_" + String.valueOf(row) + "x" + String.valueOf(column);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(idName.getBytes(StandardCharsets.UTF_8));
// this.id = new BigInteger(1, hash);
this.idByColumn = new BigInteger(1, hash);
this.idByRow = new BigInteger(1, hash);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
} else if (KademliaCommonConfigDas.MAPPING_FN
== KademliaCommonConfigDas.SAMPLE_MAPPING_REGION_BASED) {
/*System.out.println(
"ComputeId "
+ this.row
+ " "
+ this.column
+ " "
+ this.sampleNumberByRow()
+ " "
+ this.sampleNumberByColumn());*/
this.idByRow =
Block.INTER_SAMPLE_GAP
.multiply(BigInteger.valueOf(this.sampleNumberByRow()))
.add(BigInteger.valueOf(blockId));
this.idByColumn =
Block.INTER_SAMPLE_GAP
.multiply(BigInteger.valueOf(this.sampleNumberByColumn()))
.add(BigInteger.valueOf(blockId))
.add(BigInteger.valueOf(1));
} else {
System.out.println("Error: invalid selection for sample mapping function");
System.exit(1);
}
}
/** Given the peerID of a node, determine if this sample falls within the region of the node. */
public boolean isInRegion(BigInteger peerID, BigInteger radius) {
/** (peerID - radius) < this.id < (peerID + radius) */
return isInRegionByRow(peerID, radius);
}
/** Given the peerID of a node, determine if this sample falls within the region of the node. */
public boolean isInRegionByColumn(BigInteger peerID, BigInteger radius) {
// if radius is larger or equal than the distance between the node and the sample ID, the peerID
// is in the region
if (radius.compareTo(Util.xorDistance(this.idByColumn, peerID)) > -1) {
return true;
}
{
return false;
}
}
/** Given the peerID of a node, determine if this sample falls within the region of the node. */
public boolean isInRegionByRow(BigInteger peerID, BigInteger radius) {
// if radius is larger or equal than the distance between the node and the sample ID, the peerID
// is in the region
if (radius.compareTo(Util.xorDistance(this.idByRow, peerID)) > -1) {
return true;
}
{
return false;
}
}
/** Row of the sample */
public int getRow() {
return this.row;
}
/** Column of the sample */
public int getColumn() {
return this.column;
}
/** Block id which the sample is part of */
public long getBlockId() {
return blockId;
}
/** Computed identifier of the sample, depending of the mapping mode */
public BigInteger getId() {
return idByRow;
}
/**
* Computed identifier of the sample, using rows as reference The id is equal to general id in
* case of random mapping
*/
public BigInteger getIdByRow() {
return idByRow;
}
/**
* Computed identifier of the sample, using columns as reference The id is equal to general id in
* case of random mapping
*/
public BigInteger getIdByColumn() {
return idByColumn;
}
}