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

Quick Search    Search Deep

Source code: Freenet/tcpConnection.java


1   package Freenet;
2   
3   import java.io.*;
4   import java.net.*;
5   
6   public class tcpConnection extends Connection
7   {
8     protected Socket sock=null;
9   
10    private class ConnectThread extends Thread
11    { 
12      private tcpAddress tcpaddr;
13        private Thread parent;
14      public ConnectThread(tcpAddress addr,Thread parent)
15      {
16        super();
17        tcpaddr=addr;
18        this.parent=parent;
19      }
20  
21      public void run()
22      { 
23        try { sock=new Socket(tcpaddr.host, tcpaddr.port); }
24        catch(IOException e) { sock = null; }
25        if (parent!=null) parent.interrupt();
26      }
27        public void timedout() {
28      parent = null;
29        }
30  
31    }
32  
33    public String toString()
34    {
35      return getPeerAddress().toString();
36    }
37  
38    public tcpConnection(tcpAddress addr) throws UnknownHostException,
39        IOException, ConnectTimedOutException
40    {
41        ConnectThread st = new ConnectThread(addr,Thread.currentThread()); 
42        st.start();
43        try {
44      Thread.sleep(Core.connectTimeout); 
45        } catch(InterruptedException e) {
46      //interrupted is a good thing here
47        }
48        if(sock==null) {
49      st.timedout();
50      throw new ConnectTimedOutException();
51        }
52        in=sock.getInputStream();
53        out=sock.getOutputStream();
54    }
55  
56    public tcpConnection(Socket sock) throws IOException
57      {
58        this.sock = sock;
59        in=sock.getInputStream();
60        out=sock.getOutputStream();
61      }
62  
63    public void close()
64    {
65      try { 
66        in.close();
67        in=null;
68        out.close();
69        out=null;
70        sock.close(); 
71      }
72      catch(Exception e) 
73        {
74    // It may have been closed remotely in which case
75    // sock.close will throw an exception.  We really don't
76    // care though.
77        }
78    }
79  
80      public void setSoTimeout(int timeout) throws IOException {
81    if (sock != null)
82        sock.setSoTimeout(timeout);
83    else
84        throw new IOException();
85      }
86      
87      public Address getMyAddress(ListeningAddress lstaddr)
88      {
89    return (sock == null)? null : 
90        new Address("tcp",
91        new tcpAddress(sock.getLocalAddress(),
92                 ((tcpListeningAddress)lstaddr.address).port));
93      }
94  
95      public Address getMyAddress()
96      {
97    return (sock == null)? null :
98        new Address("tcp",
99        new tcpAddress(sock.getLocalAddress(),
100                (short)sock.getLocalPort()));
101     }
102     public Address getPeerAddress(ListeningAddress lstaddr)
103     {
104   return (sock == null)? null :
105       new Address("tcp",
106       new tcpAddress(sock.getInetAddress(),
107                ((tcpListeningAddress)lstaddr.address).port));
108     }
109     public Address getPeerAddress()
110     {
111   return (sock == null)? null : 
112       new Address("tcp",
113       new tcpAddress(sock.getInetAddress(),
114                (short)sock.getPort()));
115     }
116 }