001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid.server;
020:
021: import java.io.*;
022: import java.net.*;
023: import java.util.*;
024: import javax.net.ssl.*;
025:
026: import org.apache.log4j.*;
027:
028: import org.homedns.dade.jcgrid.message.*;
029: import org.homedns.dade.jcgrid.*;
030: import org.homedns.dade.jcgrid.util.*;
031:
032: public class MainDemon extends Thread {
033: protected final static String className = MainDemon.class.getName();
034: protected static Logger log = Logger.getLogger(className);
035: protected static Logger logDetail = Logger.getLogger("DETAIL."
036: + className);
037:
038: private GridServer gridServer;
039: private ServerSocket socket;
040: private String handlerType;
041:
042: public MainDemon(GridServer server, String type) throws Exception {
043: if (log.isDebugEnabled())
044: log.debug("Start MainDemon(" + server + ")");
045:
046: gridServer = server;
047: handlerType = type;
048:
049: int port;
050: if (handlerType.equals(GridNodeConfig.TYPE_CLIENT))
051: port = gridServer.getNodeConfig().getGridConfig()
052: .getServerClientPort();
053: else if (handlerType.equals(GridNodeConfig.TYPE_WORKER))
054: port = gridServer.getNodeConfig().getGridConfig()
055: .getServerWorkerPort();
056: else if (handlerType.equals(GridNodeConfig.TYPE_ADMIN))
057: port = gridServer.getNodeConfig().getGridConfig()
058: .getServerAdminPort();
059: else
060: throw new Exception("Unknown handler type: " + handlerType);
061:
062: if (server.getNodeConfig().getGridConfig()
063: .getUseSecureConnection()) {
064: SSLServerSocketFactory sslSrvFact = (SSLServerSocketFactory) SSLServerSocketFactory
065: .getDefault();
066: socket = sslSrvFact.createServerSocket(port);
067: } else
068: socket = new ServerSocket(port);
069:
070: if (log.isDebugEnabled())
071: log.debug("End MainDemon()");
072: }
073:
074: public void run() {
075: if (log.isDebugEnabled())
076: log.debug("Start run()");
077:
078: while (!this .isInterrupted()) {
079: try {
080: // Accept worker connection
081:
082: Socket hSocket = socket.accept();
083: SocketAddress sa = hSocket.getRemoteSocketAddress();
084: if ((sa != null) && (sa instanceof InetSocketAddress)) {
085: InetSocketAddress isa = (InetSocketAddress) sa;
086: log.warn("Connection from " + isa.getAddress()
087: + ":" + isa.getPort());
088: } else
089: log.warn("Connection from unknow");
090:
091: if (handlerType.equals(GridNodeConfig.TYPE_CLIENT)) {
092: // Check the number of connection
093:
094: int count = gridServer.getClientHandlers()
095: .getConnectedHandlerCount();
096:
097: long maxConn = ((GridNodeServerConfig) gridServer
098: .getNodeConfig()).getMaxConnections();
099: if (count > maxConn) {
100: log.warn("Too many connections");
101: break;
102: }
103: }
104:
105: // Spawn a thread to handle worker connection
106:
107: HandlerThread ht;
108: if (handlerType.equals(GridNodeConfig.TYPE_CLIENT))
109: ht = new ClientHandlerThread(gridServer, hSocket);
110: else if (handlerType.equals(GridNodeConfig.TYPE_WORKER))
111: ht = new WorkerHandlerThread(gridServer, hSocket);
112: else if (handlerType.equals(GridNodeConfig.TYPE_ADMIN))
113: ht = new AdminHandlerThread(gridServer, hSocket);
114: else
115: throw new Exception("Unknown handler type: "
116: + handlerType);
117: ht.start();
118: } catch (Exception ex) {
119: log.warn("Error in MainDemon.run()", ex);
120:
121: try {
122: this .sleep(500);
123: } catch (InterruptedException iex) {
124: break;
125: }
126: }
127: }
128:
129: try {
130: socket.close();
131: } catch (Exception ex) {
132: }
133:
134: if (log.isDebugEnabled())
135: log.debug("End run()");
136: }
137: }
|