KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.InetAddress JavaDoc;
20 import java.net.Socket JavaDoc;
21 import java.net.SocketException JavaDoc;
22
23 import org.alfresco.filesys.server.config.ServerConfiguration;
24 import org.alfresco.filesys.smb.TcpipSMB;
25
26 /**
27  * Native SMB Session Socket Handler Class
28  */

29 public class TcpipSMBSessionSocketHandler extends SessionSocketHandler
30 {
31
32     /**
33      * Class constructor
34      *
35      * @param srv SMBServer
36      * @param port int
37      * @param bindAddr InetAddress
38      * @param debug boolean
39      */

40     public TcpipSMBSessionSocketHandler(SMBServer srv, int port, InetAddress JavaDoc bindAddr, boolean debug)
41     {
42         super("TCP-SMB", srv, port, bindAddr, debug);
43     }
44
45     /**
46      * Run the native SMB session socket handler
47      */

48     public void run()
49     {
50
51         try
52         {
53
54             // Clear the shutdown flag
55

56             clearShutdown();
57
58             // Wait for incoming connection requests
59

60             while (hasShutdown() == false)
61             {
62
63                 // Debug
64

65                 if (logger.isDebugEnabled() && hasDebug())
66                     logger.debug("[SMB] Waiting for TCP-SMB session request ...");
67
68                 // Wait for a connection
69

70                 Socket JavaDoc sessSock = getSocket().accept();
71
72                 // Debug
73

74                 if (logger.isDebugEnabled() && hasDebug())
75                     logger.debug("[SMB] TCP-SMB session request received from "
76                             + sessSock.getInetAddress().getHostAddress());
77
78                 try
79                 {
80
81                     // Create a packet handler for the session
82

83                     PacketHandler pktHandler = new TcpipSMBPacketHandler(sessSock);
84
85                     // Create a server session for the new request, and set the session id.
86

87                     SMBSrvSession srvSess = new SMBSrvSession(pktHandler, getServer());
88                     srvSess.setSessionId(getNextSessionId());
89                     srvSess.setUniqueId(pktHandler.getShortName() + srvSess.getSessionId());
90                     srvSess.setDebugPrefix("[" + pktHandler.getShortName() + srvSess.getSessionId() + "] ");
91
92                     // Add the session to the active session list
93

94                     getServer().addSession(srvSess);
95
96                     // Start the new session in a seperate thread
97

98                     Thread JavaDoc srvThread = new Thread JavaDoc(srvSess);
99                     srvThread.setDaemon(true);
100                     srvThread.setName("Sess_T" + srvSess.getSessionId() + "_"
101                             + sessSock.getInetAddress().getHostAddress());
102                     srvThread.start();
103                 }
104                 catch (Exception JavaDoc ex)
105                 {
106
107                     // Debug
108

109                     logger.error("[SMB] TCP-SMB Failed to create session, ", ex);
110                 }
111             }
112         }
113         catch (SocketException JavaDoc ex)
114         {
115
116             // Do not report an error if the server has shutdown, closing the server socket
117
// causes an exception to be thrown.
118

119             if (hasShutdown() == false)
120                 logger.error("[SMB] TCP-SMB Socket error : ", ex);
121         }
122         catch (Exception JavaDoc ex)
123         {
124
125             // Do not report an error if the server has shutdown, closing the server socket
126
// causes an exception to be thrown.
127

128             if (hasShutdown() == false)
129                 logger.error("[SMB] TCP-SMB Server error : ", ex);
130         }
131
132         // Debug
133

134         if (logger.isDebugEnabled() && hasDebug())
135             logger.debug("[SMB] TCP-SMB session handler closed");
136     }
137
138     /**
139      * Create the TCP/IP native SMB/CIFS session socket handlers for the main SMB/CIFS server
140      *
141      * @param server SMBServer
142      * @param sockDbg boolean
143      * @exception Exception
144      */

145     public final static void createSessionHandlers(SMBServer server, boolean sockDbg) throws Exception JavaDoc
146     {
147
148         // Access the server configuration
149

150         ServerConfiguration config = server.getConfiguration();
151
152         // Create the NetBIOS SMB handler
153

154         SessionSocketHandler sessHandler = new TcpipSMBSessionSocketHandler(server, TcpipSMB.PORT, config
155                 .getSMBBindAddress(), sockDbg);
156
157         sessHandler.initialize();
158         server.addSessionHandler(sessHandler);
159
160         // Run the TCP/IP SMB session handler in a seperate thread
161

162         Thread JavaDoc tcpThread = new Thread JavaDoc(sessHandler);
163         tcpThread.setName("TcpipSMB_Handler");
164         tcpThread.start();
165
166         // DEBUG
167

168         if (logger.isDebugEnabled() && sockDbg)
169             logger.debug("[SMB] Native SMB TCP session handler created");
170     }
171 }
172
Popular Tags