Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: Freenet/message/Echo.java


1   package Freenet.message;
2   import Freenet.*;
3   import Freenet.node.*;
4   
5   /*
6     This code is part of the Java Adaptive Network Client by Ian Clarke. 
7     It is distributed under the GNU General Public Licence (GPL) 
8     version 2.  See http://www.gnu.org/ for further details of the GPL.
9    */
10  
11  /**
12   * This implements a simple echo message which will pass through several
13   * nodes, and then pass back through them again.
14   *
15   * @author <A HREF="mailto:I.Clarke@strs.co.uk">Ian Clarke</A>
16   **/
17  
18  public class Echo extends Message
19  {
20  
21      public static final String messageName = "Echo";
22  
23    public Address[] visits;
24  
25    /**
26     * Creates a new echo message which will pass through all nodes 
27     * specified in v, and then back again - eventually returning
28     * to the user.
29     * @param v List of addresses the message should be passed through
30     **/
31  
32    public Echo(long idnum, long htl, Address[] v)
33      {
34        super(idnum, htl, (long)0);
35        visits = v;
36      }
37  
38    public Echo(RawMessage r) throws InvalidMessageException
39      {
40        super(r);
41        String no_vis;
42        no_vis = r.readField("Hops");
43        if (no_vis == null)
44    throw new InvalidMessageException
45      ("Echo: No Hops field");
46        visits = new Address[(new Integer(no_vis)).intValue()];
47        for (int x=0; x<visits.length; x++)
48    {
49      String rd = r.readField("hop"+x);
50      if (rd == null)
51        throw new InvalidMessageException
52          ("Echo: No hop"+x+" field");
53      visits[x] = new Address(rd);
54    }
55      }
56  
57    // Public Methods
58  
59    public RawMessage toRawMessage()
60      {
61        RawMessage r = super.toRawMessage();
62        r.messageType = "Echo";
63        r.setField("Hops", String.valueOf(visits.length));
64        for (int x=0; x<visits.length; x++)
65    {
66      r.setField("hop"+x, visits[x].toString());
67    }
68        return r;
69      }
70  
71    public MessageMemory pReceived(Node n, MessageMemory sb)
72      {
73        if (sb==null)
74    { // This is the first time this message was received by this node
75      if (visits.length == 0)
76        { // No hops left, back to where we came from
77      try {
78          n.sendMessage(this, source);
79      } catch (SendFailedException sfe) {}        
80          return null;
81        }
82      Address src = this.source;
83      // Move everything up in queue
84      Address dest = this.visits[0];
85      Address[] tmp = new Address[visits.length-1];
86      for (int x=0; x<tmp.length; x++)
87        {
88          tmp[x] = visits[x+1];
89        }
90      visits = tmp;
91      // And send this message!
92      try {
93          n.sendMessage(this, dest);
94      } catch(SendFailedException sfe) {}
95      // And return the source address to be picked up the next time
96      // this message passes this node
97      return new MessageMemory(src, depth);
98    }
99        else
100   { // This is not the first time this node has seen this message,
101     // just forward it back to where it originally came from (this
102     // address should be stored in the passed through object sb)
103       try {
104     n.sendMessage(this, sb.origRec);
105       } catch(SendFailedException sfe) {}  
106     return null;
107   }
108     }
109 
110 }
111 
112 
113