KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > net > SocketAcceptThread


1 /**
2  * $RCSfile: SocketAcceptThread.java,v $
3  * $Revision: 1.11 $
4  * $Date: 2005/07/03 20:55:39 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.net;
13
14 import org.jivesoftware.messenger.ConnectionManager;
15 import org.jivesoftware.messenger.ServerPort;
16 import org.jivesoftware.util.LocaleUtils;
17 import org.jivesoftware.util.Log;
18
19 import java.io.IOException JavaDoc;
20 import java.net.InetAddress JavaDoc;
21 import java.net.ServerSocket JavaDoc;
22 import java.net.Socket JavaDoc;
23
24 /**
25  * Implements a network front end with a dedicated thread reading
26  * each incoming socket.
27  *
28  * @author Iain Shigeoka
29  */

30 public class SocketAcceptThread extends Thread JavaDoc {
31
32     /**
33      * The default XMPP port for clients.
34      */

35     public static final int DEFAULT_PORT = 5222;
36
37     /**
38      * The default XMPP port for external components.
39      */

40     public static final int DEFAULT_COMPONENT_PORT = 10015;
41
42     /**
43      * The default XMPP port for server2server communication.
44      */

45     public static final int DEFAULT_SERVER_PORT = 5269;
46
47     /**
48      * Holds information about the port on which the server will listen for connections.
49      */

50     private ServerPort serverPort;
51
52     /**
53      * Interface to bind to.
54      */

55     private InetAddress JavaDoc bindInterface;
56
57     /**
58      * True while this thread should continue running.
59      */

60     private boolean notTerminated = true;
61
62     /**
63      * socket that listens for connections.
64      */

65     ServerSocket JavaDoc serverSocket;
66
67     private ConnectionManager connManager;
68
69     public SocketAcceptThread(ConnectionManager connManager, ServerPort serverPort)
70             throws IOException JavaDoc {
71         super("Socket Listener at port " + serverPort.getPort());
72         this.connManager = connManager;
73         this.serverPort = serverPort;
74         String JavaDoc interfaceName = serverPort.getInterfaceName();
75         bindInterface = null;
76         if (interfaceName != null) {
77             if (interfaceName.trim().length() > 0) {
78                 bindInterface = InetAddress.getByName(interfaceName);
79             }
80         }
81         serverSocket = new ServerSocket JavaDoc(serverPort.getPort(), -1, bindInterface);
82     }
83
84     /**
85      * Retrieve the port this server socket is bound to.
86      *
87      * @return the port the socket is bound to.
88      */

89     public int getPort() {
90         return serverPort.getPort();
91     }
92
93     /**
94      * Returns information about the port on which the server is listening for connections.
95      *
96      * @return information about the port on which the server is listening for connections.
97      */

98     public ServerPort getServerPort() {
99         return serverPort;
100     }
101
102     /**
103      * Unblock the thread and force it to terminate.
104      */

105     public void shutdown() {
106         notTerminated = false;
107
108         try {
109             ServerSocket JavaDoc sSock = serverSocket;
110             serverSocket = null;
111             if (sSock != null) {
112                 sSock.close();
113             }
114         }
115         catch (IOException JavaDoc e) {
116             // we don't care, no matter what, the socket should be dead
117
}
118
119     }
120
121     /**
122      * About as simple as it gets. The thread spins around an accept
123      * call getting sockets and handing them to the SocketManager.
124      */

125     public void run() {
126         while (notTerminated) {
127             try {
128                 Socket JavaDoc sock = serverSocket.accept();
129                 if (sock != null) {
130                     Log.debug("Connect " + sock.toString());
131                     connManager.addSocket(sock, false, serverPort);
132                 }
133             }
134             catch (IOException JavaDoc ie) {
135                 if (notTerminated) {
136                     Log.error(LocaleUtils.getLocalizedString("admin.error.accept"),
137                             ie);
138                 }
139             }
140             catch (Exception JavaDoc e) {
141                 Log.error(LocaleUtils.getLocalizedString("admin.error.accept"), e);
142             }
143         }
144
145         try {
146             ServerSocket JavaDoc sSock = serverSocket;
147             serverSocket = null;
148             if (sSock != null) {
149                 sSock.close();
150             }
151         }
152         catch (IOException JavaDoc e) {
153             // we don't care, no matter what, the socket should be dead
154
}
155     }
156 }
157
Popular Tags