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

Quick Search    Search Deep

Source code: Freenet/contrib/fproxy/ProxyClient.java


1   package Freenet.contrib.fproxy;
2   import Freenet.*;
3   import Freenet.client.*;
4   import Freenet.crypt.*;
5   import Freenet.message.*;
6   import Freenet.support.*;
7   import java.io.*;
8   import java.math.*;
9   import java.util.*;
10  
11  /*
12    This code is part of fproxy, an HTTP proxy server for Freenet.
13    It is distributed under the GNU Public Licence (GPL) version 2.  See
14    http://www.gnu.org/ for further details of the GPL.
15  */
16  
17  
18  /**
19   * Wrapper to send Freenet requests on behalf of the HttpHandler
20   *
21   * @author <a href="http://www.doc.ic.ac.uk/~twh1/">Theodore Hong</a>
22   **/
23  
24  public class ProxyClient
25  {
26      // local variables
27      public RequestClient rc;
28  
29      private ListeningAddress lstaddr;
30      private String serverAddress;
31      private long htl;
32      private OutputStream out;
33  
34  
35      /**
36       * Creates a new ProxyClient with the given parameters
37       **/
38      public ProxyClient (int listenPort, String serverAddress, long htl,
39        OutputStream out) {
40    Logger.log("ProxyClient.java", "In constructor", Logger.DEBUGGING);
41    this.lstaddr = new ListeningAddress("tcp/" + listenPort);
42    this.serverAddress = serverAddress;
43    this.htl = htl;
44    this.out = out;
45      }
46  
47      /**
48       * Main routine to wrap a Freenet request
49       **/
50      public void request (String keyString) {
51    // hash the key
52    SHA1 sha = new SHA1(true);
53    StringKey key = new StringKey(sha.doHash(keyString));
54  
55    // set parameters
56    Core.connectTimeout = 30000;
57          Core.handshakeTimeout = 30000;
58    Core.handshakeLife=1000000;
59          Core.connectTimeout=10000;
60          Core.timer=new Ticker(500);
61  
62    // create Freenet client
63    Logger.log("ProxyClient.java", "Creating request client",
64         Logger.DEBUGGING);
65    rc = new RequestClient(new Address(serverAddress), lstaddr, htl,
66               (Math.abs((new Random()).nextLong()) % 30));
67    rc.setCloseOnExit(false);
68  
69    PrintWriter pw = new PrintWriter(out);
70    DataReply reply;
71    InputStream stream;
72    
73    // do it
74    try {
75        reply = rc.request(key);
76              stream = reply.in;
77          } catch (Exception e) {
78  
79        // something went wrong - send error message to browser
80        // for now, HttpHandler will send the header
81        // pw.print("Content-type: text/plain\015\012\015\012");
82        pw.print("\n");
83        pw.println("Couldn't retrieve key: "+keyString);
84  
85        if (e instanceof BadHandshakeException) {
86      pw.println("Bad handshake from node");
87        } else if (e instanceof SendFailedException) {
88      pw.print("Couldn't connect to the specified Freenet node - ");
89      if (serverAddress.startsWith("tcp/localhost")) {
90          pw.println("have you started it?");
91      }
92      else {
93          pw.println("please restart fproxy with a different one.");
94      }
95        } else if (e instanceof ClientException) {
96      ClientException ce = (ClientException) e;
97      if (ce.msg == null) {
98          pw.println("Network timed out waiting for a reply from the Freenet node.");
99      } else if (ce.msg instanceof RequestFailed) {
100         pw.println("The key you requested could not be found anywhere in the network."); 
101     } else if (ce.msg instanceof TimedOut) {
102         pw.println("The key you requested could not be found within HTL hops.");
103     } else {
104         pw.println("Received unexpected reply: " + ce.msg);
105     }
106       }
107       pw.flush();
108       return;
109   }
110 
111   // start feeding data through
112   // for now, HttpHandler will send the header
113   // pw.print("Content-length: " + reply.length.longValue() + "\015\012");
114   // pw.print("Content-type: text/plain\015\012\015\012");
115   // pw.flush();
116   Logger.log("ProxyClient.java",
117        "\n--------------------------- data follows -----------------------------",
118        Logger.NORMAL);
119   Conduit cond = new Conduit(stream, out, null);
120   cond.asyncFeed(null, null, reply.length.longValue());
121     }
122 }