Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. package sdm.overlays.structured.T4a.chord;
  2.  
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5. import java.util.Set;
  6.  
  7. import com.sun.xml.internal.ws.api.pipe.NextAction;
  8.  
  9. import simsim.core.AbstractNode;
  10. import simsim.core.Displayable;
  11. import simsim.core.EndPoint;
  12. import simsim.core.PeriodicTask;
  13. import simsim.core.Task;
  14. import simsim.gui.canvas.Canvas;
  15. import simsim.gui.canvas.Pen;
  16. import simsim.gui.canvas.RGB;
  17. import simsim.gui.geom.Circle;
  18. import simsim.gui.geom.Line;
  19. import simsim.gui.geom.QuadCurve;
  20. import simsim.gui.geom.XY;
  21.  
  22.  
  23. import sdm.overlays.structured.T4a.chord.StoredWords;
  24. import sdm.overlays.structured.T4a.chord.msgs.*;
  25.  
  26. import sdm.overlays.words.Word;
  27. import sdm.overlays.words.WordsDB;
  28.  
  29. public class Node extends AbstractNode implements ExtendedMessageHandler, Displayable {
  30.  
  31. public long key;
  32. public Set<Word> words;
  33.  
  34. public XY pos;
  35. public Line shape;
  36.  
  37. ChordRoutingTable rtable;
  38. LinkedList<StoredWords> storedWords;
  39. LinkedList<Query> searches;
  40. // LinkedList<Packet> toSend;
  41. int messagesSent =0;
  42.  
  43. public Node() {
  44. super();
  45. key = NodeDB.store(this);
  46. rtable = new ChordRoutingTable(this);
  47.  
  48. final double R = 450.0;
  49. double a = key * 2 * Math.PI / NodeDB.KEY_RANGE - Math.PI / 2;
  50. pos = new XY(500 + R * Math.cos(a), 500 + R * Math.sin(a));
  51. shape = new Line(pos.x, pos.y, pos.x + 1, pos.y);
  52. }
  53.  
  54. // Populate the node's routing table.
  55. public void init() {
  56. rtable.populate();
  57. words = WordsDB.randomWords(25);
  58. super.setColor(RGB.GREEN);
  59. storedWords = new LinkedList<StoredWords>();
  60. searches = new LinkedList<Query>();
  61. // toSend = new LinkedList<Packet>();
  62.  
  63. new Task(0.1) {
  64. public void run() {
  65. Iterator<Word> it = words.iterator();
  66. while(it.hasNext()) {
  67. Word auxWord = it.next();
  68. long auxWordKey = auxWord.lHashValue();
  69. EndPoint nextHop = rtable.nextHop(auxWordKey);
  70. if(nextHop == null) nextHop = endpoint;
  71. udpSend(nextHop, new ShareInformationMsg(auxWord));
  72. }
  73. }
  74. };
  75. }
  76.  
  77. // public void spreadInformation( ) {
  78. // Iterator<Word> it = words.iterator();
  79. // while(it.hasNext()) {
  80. // Word auxWord = it.next();
  81. // long auxWordKey = auxWord.lHashValue();
  82. // EndPoint nextHop = rtable.nextHop(auxWordKey);
  83. // if(nextHop == null) nextHop = endpoint;
  84. // udpSend(nextHop, new ShareInformationMsg(auxWord));
  85. // }
  86. // }
  87. //
  88. public void onReceive(EndPoint src, ShareInformationMsg m) {
  89. EndPoint nextHop = rtable.nextHop(m.getWord().lHashValue());
  90. if( nextHop != null && nextHop != this.endpoint)
  91. udpSend(nextHop,new ShareInformationMsg(m));
  92. else {
  93. Iterator<StoredWords> storedWordsIterator = storedWords.iterator();
  94. while(storedWordsIterator.hasNext()) {
  95. StoredWords sw = storedWordsIterator.next();
  96. if(sw.getWord().equals(m.getWord())) {
  97. sw.addSources(src);
  98. return;
  99. }
  100. }
  101. storedWords.add(new StoredWords(m.getWord(), src));
  102. }
  103. }
  104.  
  105.  
  106. public void query(Word searchedWord) {
  107. EndPoint nextHop = rtable.nextHop(searchedWord.lHashValue());
  108. if(nextHop == this.endpoint || nextHop == null) {
  109.  
  110. for(int i =0; i < storedWords.size();i++) {
  111. if(storedWords.get(i).getWord().equals(searchedWord)) {
  112. LinkedList<EndPoint> sources = storedWords.get(i).sources;
  113. Query q = new Query(searchedWord);
  114. q.addSources(sources);
  115. searches.add(q);
  116. return;
  117. }
  118. }
  119. }
  120. else {
  121.  
  122. Query q = new Query(searchedWord);
  123. searches.add(q);
  124. udpSend(nextHop, new SearchMsg(searchedWord, this.endpoint));
  125. messagesSent ++;
  126. // toSend.add(new Packet(searchedWord,this.endpoint,nextHop));
  127. }
  128. }
  129.  
  130.  
  131.  
  132.  
  133. public void onReceive(EndPoint src, SearchMsg m) {
  134. EndPoint nextHop = rtable.nextHop(m.getWord().lHashValue());
  135. if(nextHop != null && nextHop != this.endpoint) {
  136. udpSend(nextHop, new SearchMsg(m.getWord(), m.getSrc()));
  137. messagesSent ++;
  138. }
  139. else {
  140. for(int i =0; i < storedWords.size();i++) {
  141. if(storedWords.get(i).getWord().equals(m.getWord())) {
  142. LinkedList<EndPoint> sources = storedWords.get(i).sources;
  143. udpSend(m.getSrc(), new SearchResultMsg(m.getWord(), sources));
  144. messagesSent++;
  145. break;
  146. }
  147. }
  148. }
  149. }
  150.  
  151. public void onReceive (EndPoint src, SearchResultMsg m) {
  152. for(int i =0; i < searches.size();i++) {
  153. if(searches.get(i).getQuery().equals(m.getWord())) {
  154. if(!searches.get(i).getSources().contains(src)){
  155. searches.get(i).addSources(m.getSources());
  156. break;
  157. }
  158. }
  159. }
  160. }
  161.  
  162. public void routeTo( long dst) {
  163. onReceive(endpoint, new ChordMessage(dst));
  164. }
  165.  
  166. public void displayOn( Canvas canvas ) {
  167. canvas.sDraw(shape);
  168. }
  169.  
  170.  
  171. public int getMessages() {
  172. return messagesSent;
  173. }
  174.  
  175. public double getWordHits(Word word) {
  176. Iterator<Query> it = searches.iterator();
  177. while(it.hasNext()) {
  178. Query q = it.next();
  179. if(q.getQuery().equals(word))
  180. if(!q.getSources().isEmpty())
  181. return 1;
  182. //return q.getHits();
  183. }
  184. return 0;
  185. }
  186.  
  187. public String toString() {
  188. return Long.toString(key);
  189. }
  190.  
  191.  
  192. public void onReceive(EndPoint src, ChordMessage m) {
  193.  
  194. EndPoint nextHop = rtable.nextHop( m.dst);
  195. if (nextHop != null && nextHop != this.endpoint) {
  196. Node x = (Node) nextHop.handler;
  197. System.out.printf("At:%x -> dst: %x relay-> %x\n", key, m.dst, x.key);
  198. this.udpSend(nextHop, new ChordMessage(m));
  199. } else {
  200. System.out.printf("Stopped at: %x-> dst: %x\n", key, m.dst);
  201. }
  202. }
  203.  
  204. /* Implements the Chord Routing Table */
  205. class ChordRoutingTable implements Displayable {
  206. final long ownKey;
  207.  
  208. final RTableEntry[] fingers = new RTableEntry[NodeDB.KEY_LENGTH];
  209.  
  210. ChordRoutingTable(Node owner) {
  211. this.ownKey = owner.key;
  212. }
  213.  
  214. void populate() {
  215.  
  216.  
  217. long j = 2 ;
  218. for( int i = 0 ; i < NodeDB.KEY_LENGTH ; i++ ) {
  219. Node f = NodeDB.succ( (key + NodeDB.KEY_RANGE / j) % NodeDB.KEY_RANGE ) ;
  220. fingers[i] = new RTableEntry(f.key, f.endpoint ) ;
  221. j *= 2 ;
  222. }
  223. System.out.println( key + "/" + fingers[0].key ) ;
  224. }
  225.  
  226. EndPoint nextHop(long dst) {
  227. if (dst != ownKey) {
  228. long d = distanceBetween(dst, ownKey);
  229. for( RTableEntry i : fingers ) {
  230. if (i != null && distanceBetween(dst, i.key) < d)
  231. return i.endpoint;
  232. }
  233. }
  234. return null;
  235. }
  236.  
  237. long distanceBetween( long a, long b ) {
  238. long diff = a - b ;
  239. return diff > 0 ? diff : diff + NodeDB.KEY_RANGE ;
  240. }
  241.  
  242. void dump() {
  243. System.out.println("RTable for :" + ownKey);
  244. for (RTableEntry i : fingers)
  245. System.out.printf("%.8f\n", i.key);
  246.  
  247. }
  248.  
  249. class RTableEntry {
  250. long key;
  251. EndPoint endpoint;
  252.  
  253. RTableEntry(long k, EndPoint e) {
  254. key = k;
  255. endpoint = e;
  256. }
  257. }
  258.  
  259.  
  260. final Pen pen = new Pen( RGB.DARK_GRAY, 2) ;
  261.  
  262. public void displayOn( Canvas canvas ) {
  263. pen.useOn( canvas.gs) ;
  264. for( RTableEntry i : fingers ) {
  265. Node other = (Node) i.endpoint.handler ;
  266. canvas.sFill( new Circle( other.pos, 10 ) ) ;
  267. }
  268.  
  269. int k = 1 ;
  270. for( RTableEntry i : fingers ) {
  271. Node j = (Node)i.endpoint.handler ;
  272. XY m = pos.add( j.pos).mult( 0.5) ;
  273. XY c = new XY( m.x + (500-m.x) / k, m.y + (500 - m.y) / k) ;
  274. canvas.sDraw( new QuadCurve(pos, c, j.pos) ) ;
  275. k++ ;
  276. }
  277. }
  278. }
  279.  
  280.  
  281.  
  282.  
  283. }
Add Comment
Please, Sign In to add comment