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

Quick Search    Search Deep

Source code: Freenet/client/UnhashedInsertClient.java


1   package Freenet.client;
2   import java.io.*;
3   import java.math.*;
4   import java.util.*;
5   import Freenet.*;
6   import Freenet.crypt.*;
7   import Freenet.support.*;
8   import Freenet.message.*;
9   
10  public class UnhashedInsertClient
11  {
12      private Address nodeAddress;
13      private ListeningAddress myAddress;
14      private long hopsToLive;
15      private long startDepth;
16      
17      public ClientNode cn;
18  
19      static public void main(String[] args)
20      {
21    if(args.length<5)
22        {
23      System.out.println("usage: insert port address key filename hops-to-live");
24      System.out.println("ex.: insert 10010 tcp/localhost:19114 freenet/1.0/insert Insert.java 256");
25      return;
26        }
27  
28    ListeningAddress lstaddr=new ListeningAddress("tcp/" + args[0]);
29    Address addr=new Address(args[1]);
30  
31    Key key=new StringKey(args[2]);
32    String fname=args[3];
33    long htl = Long.parseLong(args[4]);
34  
35          Node.connectTimeout=10000;
36          Node.handshakeTimeout=10000;
37          Node.timer=new Ticker(500);
38     
39    UnhashedInsertClient ic = new UnhashedInsertClient(addr, lstaddr, htl, (Math.abs((new Random()).nextLong()) % 30));
40    
41    InputStream in;
42    
43    try {
44        in = new FileInputStream(fname);
45              BigInteger length = new BigInteger(new Long(new File(fname).length()).toString());
46              ic.getHandshake();
47        ic.insert(key, in, length, new DotCounter(1024));
48          }
49          catch(BadHandshakeException e)
50          {
51            System.out.println("Bad handshake");
52            return;
53    } catch (IOException ioe) {
54        System.out.println("Could not open file");
55        return;
56    } catch (SendFailedException sfe) {
57        System.out.println("Sending insert failed, try a different node");
58               return;
59    } catch (ClientException ce) {
60        if (ce.msg instanceof RequestFailed) {
61      System.out.println("The insert reached all reachable nodes before timing out."); 
62        } else if (ce.msg instanceof DataReply) {
63      System.out.println("Key collision, insert again under a different key.");
64        } else {
65      System.out.println("Recieved unknown reply.");
66        }
67        return;
68    }
69      }
70  
71      public UnhashedInsertClient(Address addr, ListeningAddress myAddr, long htl, long dpth)
72      {
73  
74    myAddress = myAddr;
75    nodeAddress = addr;
76    hopsToLive = htl;
77    startDepth = dpth;
78  
79    cn = new ClientNode(myAddr);
80  
81      }
82  
83      public void getHandshake() throws BadHandshakeException
84      {
85              HandshakeReply shake=null;
86              if(!cn.getHandshake(nodeAddress,false))
87              {
88                try { shake=(HandshakeReply)cn.acceptHandshake(); if(shake==null) throw new InvalidMessageException("Bad handshake"); }
89                catch(InvalidMessageException e)
90                {
91                  System.out.println("Could not get handshake from node");
92                  throw new BadHandshakeException();
93                }
94                if(!shake.check())
95                {
96                  System.out.println("Node is not compatible with client");
97                  throw new BadHandshakeException();
98                }
99              }
100             Node.handshakes.put(nodeAddress,new Date());
101     }
102 
103     public void insert(Key key, InputStream in, BigInteger length, ByteCounter counter) throws ClientException, SendFailedException, BadHandshakeException
104     {
105   long id = (new Random()).nextLong();
106   InsertRequest ir=new InsertRequest(id, hopsToLive, startDepth, key);
107   
108   Message response = null;
109 
110   cn.sendMessage(ir, nodeAddress, null);
111 
112   boolean failed;
113   do {
114       failed = false;
115       try {
116     response = cn.acceptMessage();
117       } catch(InvalidMessageException e) {
118     Logger.log("client/UnhashedInsertClient","Got broken response:" + e,Logger.MINOR);
119     failed = true;
120       }
121       if (response instanceof QueryRestarted) {
122     Logger.log("client/RequestClient","The request got stuck on a broken node but has been restarted at a depth of " + response.depth,Logger.MINOR);
123     failed = true;
124       }
125             else if (response instanceof HandshakeRequest)
126             {
127               Logger.log("client/UnhashedInsertClient.java","Node requested a handshake",Logger.MINOR);
128               response.received(cn, null);
129               failed=true;
130             }
131             else
132             {
133               try { cn.checkid(response, ir.id); }
134               catch(InvalidMessageException e)
135               {
136                 Logger.log("client/RequestClient","Got broken response:" + e, Logger.MINOR);
137                 failed=true;
138               }
139             }
140   } while (failed);
141 
142   if (!(response instanceof InsertReply)) {
143       throw new ClientException(response);
144   } else {
145       DataInsert di = new DataInsert(id, hopsToLive, startDepth, nodeAddress);
146           di.length = length;
147       di.in = in;
148       cn.sendMessage(di, nodeAddress, null, counter);
149   }
150     }
151 }