Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. import java.util.Vector;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Join extends Algorithm {
  5.  
  6. private final int m = 6; // Ring of identifiers has size 2^m
  7. private int SizeRing = exp(2,m);
  8. String result = ""; // Locations of searched-for keys will be stored here
  9.  
  10. public Object run() {
  11. return findFingersAndKeys(getID());
  12. }
  13.  
  14. // Each message sent by this algorithm has the form: flag, value, ID, where:
  15. // - if flag = "GET" then the message is a request to get the document with the given key
  16. // - if flag = "LOOKUP" then the message is request to forward the message to the successor of
  17. // this processor
  18. // - if flag = "FOUND" then the message contains the key and processor that stores it
  19. // - if flag = "NOT_FOUND" then the requested data is not in the system
  20. // - if flag = "END" the algorithm terminates
  21.  
  22. /* Implements the simple algorithm to find a key in a peer-to peer system assuming that each
  23. processor only knows the address of its successor. The algorithm will return a String
  24. of the form: "k1:p1 k2:p2 ... kn:pn"m where k1, k2, ... kn are the keys that this processor
  25. needs to find and pi is the processor that stores the document with key ki for each
  26. i = 1, 2, ..., n */
  27.  
  28. /* ----------------------------- */
  29. public Object findFingersAndKeys(String id) {
  30. /* ------------------------------ */
  31. try {
  32. Vector<Integer> searchKeys; // Keys that this processor needs to find in the P2P system
  33. Vector<Integer> localKeys; // Keys stored in this processor
  34. Vector<Integer> fingers;
  35. String[] fingerTable;
  36. int m; // The ring of identifiers has size 2^m
  37.  
  38. int globalIndex =0;
  39.  
  40. localKeys = new Vector<Integer>();
  41. fingers = new Vector<Integer>();
  42. searchKeys = keysToFind(); // Read information from configuration file
  43.  
  44. if (searchKeys.size() > 0) {
  45. for (int i = 0; i < searchKeys.size();) {
  46. if (searchKeys.elementAt(i) < 0) { // Negative keys are the keys that must be stored locally
  47. localKeys.add(-searchKeys.elementAt(i));
  48. searchKeys.remove(i);
  49. }
  50. else if (searchKeys.elementAt(i) > 1000) {
  51. fingers.add(searchKeys.elementAt(i)-1000);
  52. searchKeys.remove(i);
  53. }
  54. else ++i; // Key that needs to be searched for
  55. }
  56. }
  57.  
  58. if(fingers.stream().distinct().collect(Collectors.toList()).size()==1){
  59. for(int i = 0 ; i< fingers.size(); i++){
  60. searchKeys.add(Integer.parseInt(id) + (int)Math.pow(2,i));
  61. }
  62. } // if this is the collection we are searching for
  63.  
  64. m = fingers.size();
  65. // Store the finger table in an array of Strings
  66. fingerTable = new String[m+1];
  67. for (int i = 0; i < m; ++i) fingerTable[i] = integerToString(fingers.elementAt(i));
  68. fingerTable[m] = id;
  69.  
  70. ////////////////////// END OF CODE THAT POPULATES FINGER TABLES
  71.  
  72. String succ = successor(); // Successor of this processor in the ring of identifiers
  73. Message mssg = null, message; // Message buffers
  74. int indexNextKey = 0;
  75. boolean keyProcessed = false; // This variable takes value true when the algorithm has
  76. // determined that the current key being searched either
  77. // is in the system or it is not in the system
  78. int keyValue; // Key that is being searched
  79. int hashID = hp(id); // Identifier for this processor
  80. int hashSucc = hp(succ); // Identifier for the successor
  81. String[] data;
  82.  
  83.  
  84. if (searchKeys.size() > 0) { // Get next key to find and check if it is stored locally
  85. keyValue = searchKeys.elementAt(0);
  86. searchKeys.remove(0); // Do not search for the same key twice
  87. if (localKeys.contains(keyValue)) {
  88. result = result + keyValue + ":" + id + " "; // Store location of key in the result
  89. keyProcessed = true;
  90. }
  91. else { // Key was not stored locally
  92. mssg = makeMessage(succ, pack("LOOKUP", keyValue, id));
  93. }
  94. }
  95.  
  96. // Synchronous loop
  97. while (waitForNextRound()) {
  98. if (mssg != null) {
  99. send(mssg);
  100. data = unpack(mssg.data());
  101. if (data[0].equals("END") && searchKeys.size() == 0) return result;
  102. }
  103. mssg = null;
  104. message = receive();
  105. while (message != null) {
  106. data = unpack(message.data());
  107. if (data[0].equals("GET")) {
  108. // If this is the same GET message that this processor originally sent, then the
  109. // key is not in the system
  110. if (data[2].equals(id)) {
  111. result = result + data[1] + ":not found ";
  112. keyProcessed = true;
  113. }
  114. // This processor must contain the key, if it is in the system
  115. else {
  116. mssg = makeMessage(data[2], pack("FOUND", data[1], id));
  117. }
  118. }
  119. else if (data[0].equals("LOOKUP")) {
  120. // Forward the request
  121. keyValue = stringToInteger(data[1]);
  122. if (inSegment(hk(keyValue), hashID, hashSucc))
  123. mssg = makeMessage (succ,pack("GET",keyValue,data[2]));
  124. else{
  125. for(int i=0; i < m-1; i++){
  126. String currrentProc = fingerTable[i];
  127. String currentSucessor = fingerTable[i+1];
  128.  
  129. if(inSegment(hk(keyValue), hp(currrentProc), hp(currentSucessor))){
  130. mssg = makeMessage(currrentProc, pack("LOOKUP", keyValue, data[2]));
  131. }
  132. }
  133. }
  134. }
  135. else if (data[0].equals("FOUND")) {
  136. result = result + data[1] + ":" + data[2] + " ";
  137. keyProcessed = true;
  138. }
  139. else if (data[0].equals("END"))
  140. if (searchKeys.size() > 0) return result;
  141. else mssg = makeMessage(succ,"END");
  142. message = receive();
  143. }
  144.  
  145. if (keyProcessed) { // Search for the next key
  146. if (searchKeys.size() == 0) // There are no more keys to find
  147. mssg = makeMessage(succ,"END");
  148. else {
  149. keyValue = searchKeys.elementAt(0);
  150. searchKeys.remove(0); // DO not search for same key twice
  151. if (localKeys.contains(keyValue)) {
  152. result = result + keyValue + ":" + id + " "; // Store location of key in the result
  153. keyProcessed = true;
  154. }
  155. mssg = makeMessage(succ, pack("LOOKUP", keyValue, id));
  156. }
  157. if (mssg != null) keyProcessed = false;
  158. }
  159. }
  160.  
  161.  
  162. } catch(SimulatorException e){
  163. System.out.println("ERROR: " + e.toString());
  164. }
  165.  
  166. /* At this point something likely went wrong. If you do not have a result you can return null */
  167. return null;
  168. }
  169.  
  170. /* Determine the keys that need to be stored locally and the keys that the processor needs to find.
  171. Negative keys returned by the simulator's method keysToFind() are to be stored locally in this
  172. processor as positive numbers. */
  173. /* ---------------------------------------------------------------------------------------------------- */
  174. private void getKeys (Vector<Integer> searchKeys, Vector<Integer> localKeys) throws SimulatorException {
  175. /* ---------------------------------------------------------------------------------------------------- */
  176. String local = ""; // Keys to be stored locally in this processor
  177. if (numKeysToFind() > 0) {
  178. for (int i = 0; i < searchKeys.size();) {
  179. if (searchKeys.elementAt(i) < 0) { // Negative keys are the keys that must be stored locally
  180. localKeys.add(-searchKeys.elementAt(i));
  181. searchKeys.remove(i);
  182. }
  183. else if (searchKeys.elementAt(i) > 1000) searchKeys.remove(i);
  184. else ++i;
  185. }
  186. }
  187. for (int i = 0; i < localKeys.size(); ++i) local = local + localKeys.elementAt(i) + " ";
  188. showMessage(local); // Show in the simulator the keys stored in this processor
  189. }
  190.  
  191. /* Determine whether hk(value) is in (hp(ID),hp(succ)] */
  192. /* ---------------------------------------------------------------- */
  193. private boolean inSegment(int hashValue, int hashID, int hashSucc) {
  194. /* ----------------------------------------------------------------- */
  195. if (hashID == hashSucc)
  196. if (hashValue == hashID) return true;
  197. else return false;
  198. else if (hashID < hashSucc)
  199. if ((hashValue > hashID) && (hashValue <= hashSucc)) return true;
  200. else return false;
  201. else
  202. if (((hashValue > hashID) && (hashValue < SizeRing)) ||
  203. ((0 <= hashValue) && (hashValue <= hashSucc))) return true;
  204. else return false;
  205. }
  206.  
  207. /* Hash function for processors. We use the simple mod function */
  208. /* ------------------------------- */
  209. private int hp(String ID) throws SimulatorException{
  210. /* ------------------------------- */
  211. return stringToInteger(ID) % SizeRing;
  212. }
  213.  
  214. /* Hash function for processors. We use the simple mod function */
  215. /* ------------------------------- */
  216. private int hk(int key) {
  217. /* ------------------------------- */
  218. return key % SizeRing;
  219. }
  220.  
  221. /* Compute base^exponent ("base" to the power "exponent") */
  222. /* --------------------------------------- */
  223. private int exp(int base, int exponent) {
  224. /* --------------------------------------- */
  225. int i = 0;
  226. int result = 1;
  227.  
  228. while (i < exponent) {
  229. result = result * base;
  230. ++i;
  231. }
  232. return result;
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement