KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > connection > Connection


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.connection;
30
31 import com.caucho.vfs.ReadStream;
32 import com.caucho.vfs.WriteStream;
33
34 import java.net.InetAddress JavaDoc;
35
36 /**
37  * Represents a protocol-independent connection. Prococol servers and
38  * their associated Requests use Connection to retrieve the read and
39  * write streams and to get information about the connection.
40  *
41  * <p>TcpConnection is the most common implementation. The test harness
42  * provides a string based Connection.
43  */

44 public abstract class Connection {
45   private final ReadStream _readStream;
46   private final WriteStream _writeStream;
47
48   public Connection()
49   {
50     _readStream = new ReadStream();
51     _readStream.setReuseBuffer(true);
52     _writeStream = new WriteStream();
53     _writeStream.setReuseBuffer(true);
54   }
55   
56   /**
57    * Returns the connection id. Primarily for debugging.
58    */

59   abstract public int getId();
60
61   /**
62    * Returns the connection's buffered read stream. If the ReadStream
63    * needs to block, it will automatically flush the corresponding
64    * WriteStream.
65    */

66   public final ReadStream getReadStream()
67   {
68     return _readStream;
69   }
70
71   /**
72    * Returns the connection's buffered write stream. If the ReadStream
73    * needs to block, it will automatically flush the corresponding
74    * WriteStream.
75    */

76   public final WriteStream getWriteStream()
77   {
78     return _writeStream;
79   }
80
81   /**
82    * Returns true if secure (ssl)
83    */

84   public boolean isSecure()
85   {
86     return false;
87   }
88   /**
89    * Returns the static virtual host
90    */

91   public String JavaDoc getVirtualHost()
92   {
93     return null;
94   }
95   /**
96    * Returns the local address of the connection
97    */

98   public abstract InetAddress JavaDoc getLocalAddress();
99
100   /**
101    * Returns the local port of the connection
102    */

103   public abstract int getLocalPort();
104
105   /**
106    * Returns the remote address of the connection
107    */

108   public abstract InetAddress JavaDoc getRemoteAddress();
109
110   /**
111    * Returns the remote client's inet address.
112    */

113   public String JavaDoc getRemoteHost()
114   {
115     return getRemoteAddress().getHostAddress();
116   }
117
118   /**
119    * Returns the remote address of the connection
120    */

121   public int getRemoteAddress(byte []buffer, int offset, int length)
122   {
123     InetAddress JavaDoc remote = getRemoteAddress();
124     String JavaDoc name = remote.getHostAddress();
125     int len = name.length();
126
127     for (int i = 0; i < len; i++)
128       buffer[offset + i] = (byte) name.charAt(i);
129
130     return len;
131   }
132
133   /**
134    * Returns the remove port of the connection
135    */

136   public abstract int getRemotePort();
137
138   /**
139    * Sends a broadcast request.
140    */

141   public void sendBroadcast(BroadcastTask task)
142   {
143   }
144 }
145
Popular Tags