KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > PacketHandler


1 /*
2  * Copyright (C) 2005 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.smb.server;
18
19 import java.io.DataInputStream JavaDoc;
20 import java.io.DataOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.Socket JavaDoc;
24
25 /**
26  * Protocol Packet Handler Interface
27  */

28 public abstract class PacketHandler
29 {
30
31     // Protocol type and name
32

33     private int m_protoType;
34     private String JavaDoc m_protoName;
35     private String JavaDoc m_shortName;
36
37     // Socket that this session is using.
38

39     private Socket JavaDoc m_socket;
40
41     // Input/output streams for receiving/sending SMB requests.
42

43     private DataInputStream JavaDoc m_in;
44     private DataOutputStream JavaDoc m_out;
45
46     // Client caller name
47

48     private String JavaDoc m_clientName;
49
50     /**
51      * Class constructor
52      *
53      * @param sock Socket
54      * @param typ int
55      * @param name String
56      * @param shortName String
57      * @exception IOException If a network error occurs
58      */

59     public PacketHandler(Socket JavaDoc sock, int typ, String JavaDoc name, String JavaDoc shortName) throws IOException JavaDoc
60     {
61         m_socket = sock;
62         m_protoType = typ;
63         m_protoName = name;
64         m_shortName = shortName;
65
66         // Set socket options
67

68         sock.setTcpNoDelay(true);
69
70         // Open the input/output streams
71

72         m_in = new DataInputStream JavaDoc(m_socket.getInputStream());
73         m_out = new DataOutputStream JavaDoc(m_socket.getOutputStream());
74     }
75
76     /**
77      * Class constructor
78      *
79      * @param typ int
80      * @param name String
81      * @param shortName String
82      */

83     public PacketHandler(int typ, String JavaDoc name, String JavaDoc shortName, String JavaDoc clientName)
84     {
85         m_protoType = typ;
86         m_protoName = name;
87         m_shortName = shortName;
88
89         m_clientName = clientName;
90     }
91
92     /**
93      * Return the protocol type
94      *
95      * @return int
96      */

97     public final int isProtocol()
98     {
99         return m_protoType;
100     }
101
102     /**
103      * Return the protocol name
104      *
105      * @return String
106      */

107     public final String JavaDoc isProtocolName()
108     {
109         return m_protoName;
110     }
111
112     /**
113      * Return the short protocol name
114      *
115      * @return String
116      */

117     public final String JavaDoc getShortName()
118     {
119         return m_shortName;
120     }
121
122     /**
123      * Check if there is a remote address available
124      *
125      * @return boolean
126      */

127     public final boolean hasRemoteAddress()
128     {
129         return m_socket != null ? true : false;
130     }
131
132     /**
133      * Return the remote address for the socket connection
134      *
135      * @return InetAddress
136      */

137     public final InetAddress JavaDoc getRemoteAddress()
138     {
139         return m_socket != null ? m_socket.getInetAddress() : null;
140     }
141
142     /**
143      * Determine if the client name is available
144      *
145      * @return boolean
146      */

147     public final boolean hasClientName()
148     {
149         return m_clientName != null ? true : false;
150     }
151
152     /**
153      * Return the client name
154      *
155      * @return
156      */

157     public final String JavaDoc getClientName()
158     {
159         return m_clientName;
160     }
161
162     /**
163      * Return the count of available bytes in the receive input stream
164      *
165      * @return int
166      * @exception IOException If a network error occurs.
167      */

168     public final int availableBytes() throws IOException JavaDoc
169     {
170         if (m_in != null)
171             return m_in.available();
172         return 0;
173     }
174
175     /**
176      * Read a packet
177      *
178      * @param pkt byte[]
179      * @param off int
180      * @param len int
181      * @return int
182      * @exception IOException If a network error occurs.
183      */

184     public final int readPacket(byte[] pkt, int off, int len) throws IOException JavaDoc
185     {
186
187         // Read a packet of data
188

189         if (m_in != null)
190             return m_in.read(pkt, off, len);
191         return 0;
192     }
193
194     /**
195      * Receive an SMB request packet
196      *
197      * @param pkt SMBSrvPacket
198      * @return int
199      * @exception IOException If a network error occurs.
200      */

201     public abstract int readPacket(SMBSrvPacket pkt) throws IOException JavaDoc;
202
203     /**
204      * Send an SMB request packet
205      *
206      * @param pkt byte[]
207      * @param off int
208      * @param len int
209      * @exception IOException If a network error occurs.
210      */

211     public final void writePacket(byte[] pkt, int off, int len) throws IOException JavaDoc
212     {
213
214         // Output the raw packet
215

216         if (m_out != null)
217             m_out.write(pkt, off, len);
218     }
219
220     /**
221      * Send an SMB response packet
222      *
223      * @param pkt SMBSrvPacket
224      * @param len int
225      * @exception IOException If a network error occurs.
226      */

227     public abstract void writePacket(SMBSrvPacket pkt, int len) throws IOException JavaDoc;
228
229     /**
230      * Send an SMB response packet
231      *
232      * @param pkt SMBSrvPacket
233      * @exception IOException If a network error occurs.
234      */

235     public final void writePacket(SMBSrvPacket pkt) throws IOException JavaDoc
236     {
237         writePacket(pkt, pkt.getLength());
238     }
239
240     /**
241      * Flush the output socket
242      *
243      * @exception IOException If a network error occurs
244      */

245     public final void flushPacket() throws IOException JavaDoc
246     {
247         if (m_out != null)
248             m_out.flush();
249     }
250
251     /**
252      * Close the protocol handler
253      */

254     public void closeHandler()
255     {
256
257         // Close the input stream
258

259         if (m_in != null)
260         {
261             try
262             {
263                 m_in.close();
264             }
265             catch (Exception JavaDoc ex)
266             {
267             }
268             m_in = null;
269         }
270
271         // Close the output stream
272

273         if (m_out != null)
274         {
275             try
276             {
277                 m_out.close();
278             }
279             catch (Exception JavaDoc ex)
280             {
281             }
282             m_out = null;
283         }
284
285         // Close the socket
286

287         if (m_socket != null)
288         {
289             try
290             {
291                 m_socket.close();
292             }
293             catch (Exception JavaDoc ex)
294             {
295             }
296             m_socket = null;
297         }
298     }
299 }
300
Popular Tags