Source code: Freenet/client/UnhashedRequestClient.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 UnhashedRequestClient
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<4)
22 {
23 System.out.println("usage: request port address key hops-to-live");
24 System.out.println("ex.: request 19115 tcp/localhost:19114 freenet/1.0/insert 256");
25 return;
26 }
27
28 Node.connectTimeout = 30000;
29 Node.handshakeTimeout = 30000;
30
31 ListeningAddress lstaddr=new ListeningAddress("tcp/" + args[0]);
32 Address addr=new Address(args[1]);
33 Key key=new StringKey(args[2]);
34 long htl = Long.parseLong(args[3]);
35
36 Node.connectTimeout=10000;
37 Node.timer=new Ticker(500);
38
39 UnhashedRequestClient rc = new UnhashedRequestClient(addr, lstaddr, htl, (Math.abs((new Random()).nextLong()) % 30));
40
41 DataReply reply;
42 InputStream stream;
43
44 try {
45 rc.getHandshake();
46 reply = rc.request(key);
47 stream=reply.in;
48 } catch(BadHandshakeException e) {
49 System.out.println("Bad handshake from node");
50 return;
51 } catch (SendFailedException sfe) {
52 System.out.println("Sending request failed, try a different node");
53 return;
54 } catch (ClientException ce) {
55 if (ce.msg instanceof RequestFailed) {
56 System.out.println("Request Failed");
57 } else if (ce.msg instanceof TimedOut) {
58 System.out.println("Request Timed Out");
59 } else {
60 System.out.println("Recieved unknown reply");
61 }
62 return;
63 }
64
65 try {
66 FileOutputStream out=new FileOutputStream("result");
67 DotCounter counter=new DotCounter(1024);
68 Conduit cond=new Conduit(stream, out, counter);
69 cond.syncFeed();
70 stream.close();
71 out.close();
72 if(!reply.length.equals(new BigInteger(new Long(counter.getTotal()).toString())))
73 System.out.println("Received truncated reply: "+reply.length + " | " + counter.getTotal());
74 } catch(IOException e) {
75 System.out.println(e);
76 }
77 }
78
79 public void getHandshake() throws BadHandshakeException
80 {
81 HandshakeReply shake=null;
82 if(!cn.getHandshake(nodeAddress,false))
83 {
84 try { shake=(HandshakeReply)cn.acceptHandshake(); if(shake==null) throw new InvalidMessageException("Bad handshake"); }
85 catch(InvalidMessageException e)
86 {
87 System.out.println("Could not get handshake from node");
88 throw new BadHandshakeException();
89 }
90 if(!shake.check())
91 {
92 System.out.println("Node is not compatible with client");
93 throw new BadHandshakeException();
94 }
95 }
96 Node.handshakes.put(nodeAddress,new Date());
97 }
98
99 public UnhashedRequestClient(Address addr, ListeningAddress myAddr, long htl, long dpth)
100 {
101
102 myAddress = myAddr;
103 nodeAddress = addr;
104 hopsToLive = htl;
105 startDepth = dpth;
106 cn = new ClientNode(myAddr);
107
108 }
109
110 public DataReply request(Key key) throws ClientException, SendFailedException, BadHandshakeException
111 {
112
113 DataRequest dr=new DataRequest((new Random()).nextLong(), hopsToLive, startDepth , key);
114
115 Message response = null;
116
117 cn.sendMessage(dr, nodeAddress);
118
119 boolean failed;
120 do {
121 failed = false;
122 try {
123 response = cn.acceptMessage();
124 } catch(InvalidMessageException e) {
125 Logger.log("client/UnhashedRequestClient","Got broken response:" + e,Logger.MINOR);
126 failed = true;
127 }
128 if (response instanceof QueryRestarted) {
129 Logger.log("client/UnhashedRequestClient","The request got stuck on a broken node but has been restarted at a depth of " + response.depth,Logger.MINOR);
130 failed = true;
131 }
132 else if (response instanceof HandshakeRequest)
133 {
134 Logger.log("client/UnhashedRequestClient","Node requested a handshake",Logger.MINOR);
135 response.received((Node)cn, (MessageMemory)null);
136 failed = true;
137 }
138 else
139 {
140 try { cn.checkid(response, dr.id); }
141 catch(InvalidMessageException e)
142 {
143 Logger.log("client/UnhashedRequestClient","Got broken response:" + e,Logger.MINOR);
144 failed=true;
145 }
146 }
147 } while (failed);
148
149 if (!(response instanceof DataReply)) {
150 throw new ClientException(response);
151 } else {
152 DataReply dp=(DataReply)response;
153 return dp;
154 }
155 }
156 }