KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > passthru > TcpipSMBNetworkSession


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth.passthru;
18
19 import java.io.DataInputStream JavaDoc;
20 import java.io.DataOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.net.Socket JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25
26 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol;
27 import org.alfresco.filesys.smb.NetworkSession;
28 import org.alfresco.filesys.smb.TcpipSMB;
29 import org.alfresco.filesys.util.DataPacker;
30
31 /**
32  * Native TCP/IP SMB Network Session Class
33  */

34 public class TcpipSMBNetworkSession implements NetworkSession
35 {
36
37     // Default socket timeout value
38

39     private static int _defTimeout = 30000; // 30 seconds, in milliseconds
40

41     // Socket used to connect and read/write to remote host
42

43     private Socket JavaDoc m_socket;
44
45     // Input and output data streams, from the socket network connection
46

47     private DataInputStream JavaDoc m_in;
48     private DataOutputStream JavaDoc m_out;
49
50     // Socket timeout
51

52     private int m_tmo = _defTimeout;
53
54     // Debug enable flag and debug output stream
55

56     private static boolean m_debug = false;
57     private static PrintStream JavaDoc m_dbg = System.out;
58
59     /**
60      * Default constructor
61      */

62     public TcpipSMBNetworkSession()
63     {
64     }
65
66     /**
67      * Class constructor
68      *
69      * @param tmo Socket timeout, in milliseconds
70      */

71     public TcpipSMBNetworkSession(int tmo)
72     {
73         m_tmo = tmo;
74     }
75
76     /**
77      * Return the protocol name
78      *
79      * @return String
80      */

81     public String JavaDoc getProtocolName()
82     {
83         return "Native SMB (port 445)";
84     }
85
86     /**
87      * Open a connection to a remote host
88      *
89      * @param toName Host name/address being called
90      * @param fromName Local host name/address
91      * @param toAddr Optional address
92      * @exception IOException
93      */

94     public void Open(String JavaDoc toName, String JavaDoc fromName, String JavaDoc toAddr) throws IOException JavaDoc, UnknownHostException JavaDoc
95     {
96
97         // Create the socket
98

99         m_socket = new Socket JavaDoc(toName, TcpipSMB.PORT);
100
101         // Enable the timeout on the socket, disable the Nagle algorithm
102

103         m_socket.setSoTimeout(m_tmo);
104         m_socket.setTcpNoDelay(true);
105
106         // Attach input/output streams to the socket
107

108         m_in = new DataInputStream JavaDoc(m_socket.getInputStream());
109         m_out = new DataOutputStream JavaDoc(m_socket.getOutputStream());
110     }
111
112     /**
113      * Determine if the session is connected to a remote host
114      *
115      * @return boolean
116      */

117     public boolean isConnected()
118     {
119         return m_socket != null ? true : false;
120     }
121
122     /**
123      * Check if there is data available on this network session
124      *
125      * @return boolean
126      * @exception IOException
127      */

128     public final boolean hasData() throws IOException JavaDoc
129     {
130
131         // Check if the connection is active
132

133         if (m_socket == null || m_in == null)
134             return false;
135
136         // Check if there is data available
137

138         return m_in.available() > 0 ? true : false;
139     }
140
141     /**
142      * Receive a data packet from the remote host.
143      *
144      * @param buf Byte buffer to receive the data into.
145      * @param tmo Receive timeout in milliseconds, or zero for no timeout
146      * @return Length of the received data.
147      * @exception java.io.IOException I/O error occurred.
148      */

149     public int Receive(byte[] buf, int tmo) throws IOException JavaDoc
150     {
151
152         // Set the read timeout
153

154         m_socket.setSoTimeout(tmo);
155
156         // Read a data packet of data
157

158         int rdlen = m_in.read(buf, 0, RFCNetBIOSProtocol.HEADER_LEN);
159
160         // Check if a header was received
161

162         if (rdlen < RFCNetBIOSProtocol.HEADER_LEN)
163             throw new java.io.IOException JavaDoc("TCP/IP SMB Short Read");
164
165         // Get the packet data length
166

167         int pktlen = DataPacker.getInt(buf, 0);
168
169         // Debug mode
170

171         if (m_debug)
172             m_dbg.println("TcpSMB: Rx " + pktlen + " bytes");
173
174         // Read the data part of the packet into the users buffer, this may take
175
// several reads
176

177         int totlen = 0;
178         int offset = RFCNetBIOSProtocol.HEADER_LEN;
179
180         while (pktlen > 0)
181         {
182
183             // Read the data
184

185             rdlen = m_in.read(buf, offset, pktlen);
186
187             // Update the received length and remaining data length
188

189             totlen += rdlen;
190             pktlen -= rdlen;
191
192             // Update the user buffer offset as more reads will be required
193
// to complete the data read
194

195             offset += rdlen;
196
197         } // end while reading data
198

199         // Return the received data length, not including the header
200

201         return totlen;
202     }
203
204     /**
205      * Send a data packet to the remote host.
206      *
207      * @param data Byte array containing the data to be sent.
208      * @param siz Length of the data to send.
209      * @return true if the data was sent successfully, else false.
210      * @exception java.io.IOException I/O error occurred.
211      */

212     public boolean Send(byte[] data, int siz) throws IOException JavaDoc
213     {
214
215         // Pack the data length as the first four bytes of the packet
216

217         DataPacker.putInt(siz, data, 0);
218
219         // Send the packet to the remote host
220

221         int len = siz + RFCNetBIOSProtocol.HEADER_LEN;
222         m_out.write(data, 0, len);
223         return true;
224     }
225
226     /**
227      * Close the network session
228      *
229      * @exception java.io.IOException I/O error occurred
230      */

231     public void Close() throws IOException JavaDoc
232     {
233
234         // Close the input/output streams
235

236         if (m_in != null)
237         {
238             m_in.close();
239             m_in = null;
240         }
241
242         if (m_out != null)
243         {
244             m_out.close();
245             m_out = null;
246         }
247
248         // Close the socket
249

250         if (m_socket != null)
251         {
252             m_socket.close();
253             m_socket = null;
254         }
255     }
256
257     /**
258      * Enable/disable session debugging output
259      *
260      * @param dbg true to enable debugging, else false
261      */

262     public static void setDebug(boolean dbg)
263     {
264         m_debug = dbg;
265     }
266
267     /**
268      * Return the default socket timeout value
269      *
270      * @return int
271      */

272     public static final int getDefaultTimeout()
273     {
274         return _defTimeout;
275     }
276
277     /**
278      * Set the default socket timeout for new sessions
279      *
280      * @param tmo int
281      */

282     public static final void setDefaultTimeout(int tmo)
283     {
284         _defTimeout = tmo;
285     }
286 }
287
Popular Tags