Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sdm.overlays.structured.T4a.chord;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Set;
- import com.sun.xml.internal.ws.api.pipe.NextAction;
- import simsim.core.AbstractNode;
- import simsim.core.Displayable;
- import simsim.core.EndPoint;
- import simsim.core.PeriodicTask;
- import simsim.core.Task;
- import simsim.gui.canvas.Canvas;
- import simsim.gui.canvas.Pen;
- import simsim.gui.canvas.RGB;
- import simsim.gui.geom.Circle;
- import simsim.gui.geom.Line;
- import simsim.gui.geom.QuadCurve;
- import simsim.gui.geom.XY;
- import sdm.overlays.structured.T4a.chord.StoredWords;
- import sdm.overlays.structured.T4a.chord.msgs.*;
- import sdm.overlays.words.Word;
- import sdm.overlays.words.WordsDB;
- public class Node extends AbstractNode implements ExtendedMessageHandler, Displayable {
- public long key;
- public Set<Word> words;
- public XY pos;
- public Line shape;
- ChordRoutingTable rtable;
- LinkedList<StoredWords> storedWords;
- LinkedList<Query> searches;
- // LinkedList<Packet> toSend;
- int messagesSent =0;
- public Node() {
- super();
- key = NodeDB.store(this);
- rtable = new ChordRoutingTable(this);
- final double R = 450.0;
- double a = key * 2 * Math.PI / NodeDB.KEY_RANGE - Math.PI / 2;
- pos = new XY(500 + R * Math.cos(a), 500 + R * Math.sin(a));
- shape = new Line(pos.x, pos.y, pos.x + 1, pos.y);
- }
- // Populate the node's routing table.
- public void init() {
- rtable.populate();
- words = WordsDB.randomWords(25);
- super.setColor(RGB.GREEN);
- storedWords = new LinkedList<StoredWords>();
- searches = new LinkedList<Query>();
- // toSend = new LinkedList<Packet>();
- new Task(0.1) {
- public void run() {
- Iterator<Word> it = words.iterator();
- while(it.hasNext()) {
- Word auxWord = it.next();
- long auxWordKey = auxWord.lHashValue();
- EndPoint nextHop = rtable.nextHop(auxWordKey);
- if(nextHop == null) nextHop = endpoint;
- udpSend(nextHop, new ShareInformationMsg(auxWord));
- }
- }
- };
- }
- // public void spreadInformation( ) {
- // Iterator<Word> it = words.iterator();
- // while(it.hasNext()) {
- // Word auxWord = it.next();
- // long auxWordKey = auxWord.lHashValue();
- // EndPoint nextHop = rtable.nextHop(auxWordKey);
- // if(nextHop == null) nextHop = endpoint;
- // udpSend(nextHop, new ShareInformationMsg(auxWord));
- // }
- // }
- //
- public void onReceive(EndPoint src, ShareInformationMsg m) {
- EndPoint nextHop = rtable.nextHop(m.getWord().lHashValue());
- if( nextHop != null && nextHop != this.endpoint)
- udpSend(nextHop,new ShareInformationMsg(m));
- else {
- Iterator<StoredWords> storedWordsIterator = storedWords.iterator();
- while(storedWordsIterator.hasNext()) {
- StoredWords sw = storedWordsIterator.next();
- if(sw.getWord().equals(m.getWord())) {
- sw.addSources(src);
- return;
- }
- }
- storedWords.add(new StoredWords(m.getWord(), src));
- }
- }
- public void query(Word searchedWord) {
- EndPoint nextHop = rtable.nextHop(searchedWord.lHashValue());
- if(nextHop == this.endpoint || nextHop == null) {
- for(int i =0; i < storedWords.size();i++) {
- if(storedWords.get(i).getWord().equals(searchedWord)) {
- LinkedList<EndPoint> sources = storedWords.get(i).sources;
- Query q = new Query(searchedWord);
- q.addSources(sources);
- searches.add(q);
- return;
- }
- }
- }
- else {
- Query q = new Query(searchedWord);
- searches.add(q);
- udpSend(nextHop, new SearchMsg(searchedWord, this.endpoint));
- messagesSent ++;
- // toSend.add(new Packet(searchedWord,this.endpoint,nextHop));
- }
- }
- public void onReceive(EndPoint src, SearchMsg m) {
- EndPoint nextHop = rtable.nextHop(m.getWord().lHashValue());
- if(nextHop != null && nextHop != this.endpoint) {
- udpSend(nextHop, new SearchMsg(m.getWord(), m.getSrc()));
- messagesSent ++;
- }
- else {
- for(int i =0; i < storedWords.size();i++) {
- if(storedWords.get(i).getWord().equals(m.getWord())) {
- LinkedList<EndPoint> sources = storedWords.get(i).sources;
- udpSend(m.getSrc(), new SearchResultMsg(m.getWord(), sources));
- messagesSent++;
- break;
- }
- }
- }
- }
- public void onReceive (EndPoint src, SearchResultMsg m) {
- for(int i =0; i < searches.size();i++) {
- if(searches.get(i).getQuery().equals(m.getWord())) {
- if(!searches.get(i).getSources().contains(src)){
- searches.get(i).addSources(m.getSources());
- break;
- }
- }
- }
- }
- public void routeTo( long dst) {
- onReceive(endpoint, new ChordMessage(dst));
- }
- public void displayOn( Canvas canvas ) {
- canvas.sDraw(shape);
- }
- public int getMessages() {
- return messagesSent;
- }
- public double getWordHits(Word word) {
- Iterator<Query> it = searches.iterator();
- while(it.hasNext()) {
- Query q = it.next();
- if(q.getQuery().equals(word))
- if(!q.getSources().isEmpty())
- return 1;
- //return q.getHits();
- }
- return 0;
- }
- public String toString() {
- return Long.toString(key);
- }
- public void onReceive(EndPoint src, ChordMessage m) {
- EndPoint nextHop = rtable.nextHop( m.dst);
- if (nextHop != null && nextHop != this.endpoint) {
- Node x = (Node) nextHop.handler;
- System.out.printf("At:%x -> dst: %x relay-> %x\n", key, m.dst, x.key);
- this.udpSend(nextHop, new ChordMessage(m));
- } else {
- System.out.printf("Stopped at: %x-> dst: %x\n", key, m.dst);
- }
- }
- /* Implements the Chord Routing Table */
- class ChordRoutingTable implements Displayable {
- final long ownKey;
- final RTableEntry[] fingers = new RTableEntry[NodeDB.KEY_LENGTH];
- ChordRoutingTable(Node owner) {
- this.ownKey = owner.key;
- }
- void populate() {
- long j = 2 ;
- for( int i = 0 ; i < NodeDB.KEY_LENGTH ; i++ ) {
- Node f = NodeDB.succ( (key + NodeDB.KEY_RANGE / j) % NodeDB.KEY_RANGE ) ;
- fingers[i] = new RTableEntry(f.key, f.endpoint ) ;
- j *= 2 ;
- }
- System.out.println( key + "/" + fingers[0].key ) ;
- }
- EndPoint nextHop(long dst) {
- if (dst != ownKey) {
- long d = distanceBetween(dst, ownKey);
- for( RTableEntry i : fingers ) {
- if (i != null && distanceBetween(dst, i.key) < d)
- return i.endpoint;
- }
- }
- return null;
- }
- long distanceBetween( long a, long b ) {
- long diff = a - b ;
- return diff > 0 ? diff : diff + NodeDB.KEY_RANGE ;
- }
- void dump() {
- System.out.println("RTable for :" + ownKey);
- for (RTableEntry i : fingers)
- System.out.printf("%.8f\n", i.key);
- }
- class RTableEntry {
- long key;
- EndPoint endpoint;
- RTableEntry(long k, EndPoint e) {
- key = k;
- endpoint = e;
- }
- }
- final Pen pen = new Pen( RGB.DARK_GRAY, 2) ;
- public void displayOn( Canvas canvas ) {
- pen.useOn( canvas.gs) ;
- for( RTableEntry i : fingers ) {
- Node other = (Node) i.endpoint.handler ;
- canvas.sFill( new Circle( other.pos, 10 ) ) ;
- }
- int k = 1 ;
- for( RTableEntry i : fingers ) {
- Node j = (Node)i.endpoint.handler ;
- XY m = pos.add( j.pos).mult( 0.5) ;
- XY c = new XY( m.x + (500-m.x) / k, m.y + (500 - m.y) / k) ;
- canvas.sDraw( new QuadCurve(pos, c, j.pos) ) ;
- k++ ;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment