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

Quick Search    Search Deep

Source code: Freenet/Core.java


1   package Freenet;
2   import Freenet.support.*;
3   import Freenet.message.HandshakeRequest;
4   import java.util.*;
5   import java.net.*;
6   import java.io.*;
7   
8   /*
9     This code is part of the Java Adaptive Network Client by Ian Clarke. 
10    It is distributed under the GNU Public Licence (GPL) version 2.  See
11    http://www.gnu.org/ for further details of the GPL.
12  */
13  
14  /**
15   * This is a Wrapper object that contains the components of a Node in the
16   * Adaptive Network.  It uses a Network object to communicate with other
17   * Nodes in the Network.
18   *
19   * @author <A HREF="mailto:I.Clarke@strs.co.uk">Ian Clarke</A>
20   * @author <a href="mailto:blanu@uts.cc.utexas.edu">Brandon Wiley</a>
21   **/
22  
23  public class Core
24  {
25      public static final String freenetVersion = "0.1";
26      public static final String protocolVersion = "1.2";
27      public static final String buildNumber = "117";
28      public static String serverRevision;
29      public static Params params;
30      public static boolean debug;
31      public static int timePerHop;
32      public static int connectTimeout;
33      public static int handshakeTimeout;
34      public static int handshakeLife;
35      public static boolean tunneling;
36  
37      public static Ticker timer;
38      public ListeningAddress myAddress;
39      public MessageHandler mh;
40      public HandshakeHandler hh;
41      public Listener listener;
42      public boolean listen = true; // provides a way to turn off listening
43  
44      public Core(ListeningAddress myAddress, MessageHandler mh, HandshakeHandler hh) {
45    try {
46        this.listener = ListenerFactory.listen(myAddress);
47    } catch (BindException e) {
48        throw new RuntimeException("Port cannot be opened (illegal or in use)");
49    }
50    this.myAddress = listener.address;
51    this.mh = mh;
52    this.hh = hh;
53    Logger.log("Core.java","Node running on "+listener,Logger.NORMAL);
54      }
55      
56      public void acceptConnections()
57      {
58        Connection conn;
59        RawMessage m;
60        ConnectionHandler c;
61        while(listen)
62          {
63            try
64              {
65                conn = listener.accept();
66          Logger.log("Node.java","Accepted connection:"+conn,Logger.MINOR);
67              }
68            catch (IOException e)
69              {
70                throw new RuntimeException("Problem accepting next connection");
71              }
72            c = new ConnectionHandler(conn, mh);
73            c.start();
74          }
75      }
76      
77  
78      /**
79       * Makes a connection from this Node to another
80       * @param peer The Address of the other node
81       * @return A ConnectionHandler that handles the new connection
82       **/
83  
84      public ConnectionHandler makeConnection(Address peer) throws ConnectFailedException {
85    
86    if(peer==null) throw new ConnectFailedException(peer);
87    
88    ConnectionHandler ch = connect(peer);
89  
90    if (!hh.getHandshake(this,ch))
91        throw new ConnectFailedException(peer);
92      
93    if (!ch.isOpen()) { // didn't stay open after handshake
94        ch = connect(peer);
95    }
96  
97    return ch;
98      }
99  
100     /**
101      * This is the raw connection maker, it will not handshake
102      * or check the status of the connection in any way.
103      **/
104 
105     public ConnectionHandler connect(Address peer) throws ConnectFailedException {
106   Connection c = null;
107   try {
108       c = ConnectionFactory.connect(peer);
109   } catch(Exception e) {
110       Logger.log("Node.java","Connection Error: " + e,Logger.ERROR);
111       throw new ConnectFailedException(peer);
112   }
113 
114   Logger.log("Node.java","Connection between: " + c.getMyAddress(myAddress) + " and " + c.getPeerAddress(),Logger.DEBUGGING);
115   if (c.getMyAddress(myAddress).equals(c.getPeerAddress())) {
116       Logger.log("Node.java","No connecting to yourself",Logger.MINOR);      
117       c.close();
118       throw new ConnectFailedException(peer);
119   }
120   
121   ConnectionHandler ch = new ConnectionHandler(c, mh);
122   ch.start(); // start listening on this connection
123   return ch;
124     }
125 
126   /**
127    * Send the message using the appropriate protocol
128    * @depreciated Use makeConnection() to grab a ConnectionHandler and send from that instead.
129    **/
130     public void sendMessage(Message m, Address destination) throws SendFailedException
131     {
132   sendMessage(m, destination, null, null);
133     }
134 
135     /**
136      * Send the message using the appropriate protocol streaming
137      * the actual data through the SplitOutputStream tunnel
138      * @depreciated Use makeConnection to grab a ConnectionHandler and send from it instead
139      **/
140 
141   public void sendMessage(Message m, Address destination, SplitOutputStream datatunnel) throws SendFailedException
142     {
143   sendMessage(m, destination, datatunnel, null);
144     }
145 
146     /**
147      * Send the message using the appropriate protocol, streaming
148      * the actual data through the SplitOutputStream tunnel, and
149      * counting the bytes sent with ByteCounter. This opens a connection as well.
150      * @depreciated Use makeConnection to grab a ConnectionHandler and send from it instead
151      **/
152 
153     public void sendMessage(Message m, Address destination, SplitOutputStream datatunnel, ByteCounter count) throws SendFailedException
154     {
155   try {
156       ConnectionHandler ch = makeConnection(destination);
157       ch.sendMessage(m, datatunnel, count);
158   } catch (ConnectFailedException e) {
159       throw new SendFailedException(destination);
160   }
161     }
162 }