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