KickJava   Java API By Example, From Geeks To Geeks.

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


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.netbios.RFCNetBIOSProtocol;
24 import org.alfresco.filesys.server.config.ServerConfiguration;
25 import org.alfresco.filesys.smb.mailslot.TcpipNetBIOSHostAnnouncer;
26
27 /**
28  * NetBIOS Socket Session Handler Class
29  */

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

41     public NetBIOSSessionSocketHandler(SMBServer srv, int port, InetAddress JavaDoc bindAddr, boolean debug)
42     {
43         super("NetBIOS", srv, port, bindAddr, debug);
44     }
45
46     /**
47      * Run the NetBIOS session socket handler
48      */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

155         SessionSocketHandler sessHandler = new NetBIOSSessionSocketHandler(server, RFCNetBIOSProtocol.PORT, config
156                 .getSMBBindAddress(), sockDbg);
157         sessHandler.initialize();
158
159         // Add the session handler to the list of active handlers
160

161         server.addSessionHandler(sessHandler);
162
163         // Run the NetBIOS session handler in a seperate thread
164

165         Thread JavaDoc nbThread = new Thread JavaDoc(sessHandler);
166         nbThread.setName("NetBIOS_Handler");
167         nbThread.start();
168
169         // DEBUG
170

171         if (logger.isDebugEnabled() && sockDbg)
172             logger.debug("[SMB] TCP NetBIOS session handler created");
173
174         // Check if a host announcer should be created
175

176         if (config.hasEnableAnnouncer())
177         {
178
179             // Create the TCP NetBIOS host announcer
180

181             TcpipNetBIOSHostAnnouncer announcer = new TcpipNetBIOSHostAnnouncer();
182
183             // Set the host name to be announced
184

185             announcer.addHostName(config.getServerName());
186             announcer.setDomain(config.getDomainName());
187             announcer.setComment(config.getComment());
188             announcer.setBindAddress(config.getSMBBindAddress());
189
190             // Set the announcement interval
191

192             if (config.getHostAnnounceInterval() > 0)
193                 announcer.setInterval(config.getHostAnnounceInterval());
194
195             try
196             {
197                 announcer.setBroadcastAddress(config.getBroadcastMask());
198             }
199             catch (Exception JavaDoc ex)
200             {
201             }
202
203             // Set the server type flags
204

205             announcer.setServerType(config.getServerType());
206
207             // Enable debug output
208

209             if (config.hasHostAnnounceDebug())
210                 announcer.setDebug(true);
211
212             // Add the host announcer to the SMS servers list
213

214             server.addHostAnnouncer(announcer);
215
216             // Start the host announcer thread
217

218             announcer.start();
219
220             // DEBUG
221

222             if (logger.isDebugEnabled() && sockDbg)
223                 logger.debug("[SMB] TCP NetBIOS host announcer created");
224         }
225     }
226 }
227
Popular Tags