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 java.security.*;
025:
026: import org.apache.log4j.*;
027:
028: import org.homedns.dade.jcgrid.*;
029: import org.homedns.dade.jcgrid.vfs.*;
030: import org.homedns.dade.jcgrid.message.*;
031: import org.homedns.dade.jcgrid.util.*;
032:
033: public class GridServer extends GridNode {
034: private final static String className = GridServer.class.getName();
035: private static Logger log = Logger.getLogger(className);
036: private static Logger logDetail = Logger.getLogger("DETAIL."
037: + className);
038:
039: private ClientMainDemon clientMainDemon;
040: private WorkerMainDemon workerMainDemon;
041: private AdminMainDemon adminMainDemon;
042:
043: private HandlerThreads connectedClients;
044: private HandlerThreads connectedWorkers;
045: private HandlerThreads connectedAdmins;
046:
047: private WorkRequestQueue workRequestQueue;
048:
049: private vfsSessionPool sessions;
050:
051: public GridServer() {
052: super (new GridNodeServerConfig());
053:
054: if (log.isDebugEnabled())
055: log.debug("Start GridServer()");
056:
057: clientMainDemon = null;
058: workerMainDemon = null;
059: adminMainDemon = null;
060:
061: connectedClients = new HandlerThreads();
062: connectedWorkers = new HandlerThreads();
063: connectedAdmins = new HandlerThreads();
064:
065: workRequestQueue = new WorkRequestQueue();
066:
067: sessions = null;
068:
069: if (log.isDebugEnabled())
070: log.debug("End GridServer()");
071: }
072:
073: //----------------------- Work requests & results --------------------------
074:
075: protected void pushWorkRequest(WorkRequest wr) {
076: if (log.isDebugEnabled())
077: log.debug("Start pushWorkRequest()");
078:
079: workRequestQueue.push(wr);
080:
081: if (log.isDebugEnabled())
082: log.debug("End pushWorkRequest()");
083: }
084:
085: protected WorkRequest popWorkRequest(long timeout)
086: throws InterruptedException {
087: if (log.isDebugEnabled())
088: log.debug("Start popWorkRequest(" + timeout + ")");
089:
090: WorkRequest wr = (WorkRequest) workRequestQueue.pop(timeout);
091:
092: if (log.isDebugEnabled())
093: log.debug("End popWorkRequest(" + wr + ")");
094:
095: return wr;
096: }
097:
098: protected WorkRequest popWorkRequest() throws InterruptedException {
099: if (log.isDebugEnabled())
100: log.debug("Start popWorkRequest()");
101:
102: WorkRequest wr = (WorkRequest) workRequestQueue.pop();
103:
104: if (log.isDebugEnabled())
105: log.debug("End popWorkRequest(" + wr + ")");
106:
107: return wr;
108: }
109:
110: protected void removePendingWorkRequests(String sessionName) {
111: if (log.isDebugEnabled())
112: log.debug("Start removePendingWorkRequests(" + sessionName
113: + ")");
114:
115: workRequestQueue.removePendingWorkRequests(sessionName);
116:
117: if (log.isDebugEnabled())
118: log.debug("End removePendingWorkRequests()");
119: }
120:
121: protected void pushWorkResult(WorkResult wr) throws Exception {
122: if (log.isDebugEnabled())
123: log.debug("Start pushWorkResult(" + wr + ")");
124:
125: String sessionName = wr.getSessionName();
126:
127: ClientHandlerThread cht = (ClientHandlerThread) connectedClients
128: .getConnectedHandler(sessionName);
129:
130: // If the session doesn't exist do nothing
131:
132: if (cht != null)
133: cht.pushWorkResult(wr);
134:
135: if (log.isDebugEnabled())
136: log.debug("End pushWorkResult()");
137: }
138:
139: //----------------------------- Start & Stop -------------------------------
140:
141: public void start() throws Exception {
142: if (log.isDebugEnabled())
143: log.debug("Start start()");
144:
145: super .start();
146:
147: if (this .getNodeConfig().getGridConfig().getUseVFS()) {
148: long maxCacheSize = ((GridNodeServerConfig) this
149: .getNodeConfig()).getMaxCacheSize();
150: sessions = new vfsSessionPool(this .getNodeConfig()
151: .getWorkingDir(), maxCacheSize);
152: sessions.start();
153: log.warn("VFS Session cache size: " + sessions.getSize()
154: / (1024 * 1024) + "/" + maxCacheSize
155: / (1024 * 1024) + "MB");
156: } else
157: sessions = null;
158:
159: clientMainDemon = new ClientMainDemon(this );
160: clientMainDemon.start();
161:
162: workerMainDemon = new WorkerMainDemon(this );
163: workerMainDemon.start();
164:
165: adminMainDemon = new AdminMainDemon(this );
166: adminMainDemon.start();
167:
168: log.warn("Ok");
169:
170: if (log.isDebugEnabled())
171: log.debug("End start()");
172: }
173:
174: public void stop() throws InterruptedException {
175: if (log.isDebugEnabled())
176: log.debug("Start stop()");
177:
178: clientMainDemon.interrupt();
179: clientMainDemon.join();
180:
181: workerMainDemon.interrupt();
182: workerMainDemon.join();
183:
184: adminMainDemon.interrupt();
185: adminMainDemon.join();
186:
187: if (log.isDebugEnabled())
188: log.debug("End stop()");
189: }
190:
191: //------------------------------- Handlers ---------------------------------
192:
193: protected HandlerThreads getClientHandlers() {
194: if (logDetail.isDebugEnabled())
195: logDetail.debug("Start getClientHandlers()");
196: if (logDetail.isDebugEnabled())
197: logDetail.debug("End getClientHandlers(" + connectedClients
198: + ")");
199:
200: return connectedClients;
201: }
202:
203: protected HandlerThreads getWorkerHandlers() {
204: if (logDetail.isDebugEnabled())
205: logDetail.debug("Start getWorkerHandlers()");
206: if (logDetail.isDebugEnabled())
207: logDetail.debug("End getWorkerHandlers(" + connectedWorkers
208: + ")");
209:
210: return connectedWorkers;
211: }
212:
213: protected HandlerThreads getAdminHandlers() {
214: if (logDetail.isDebugEnabled())
215: logDetail.debug("Start getAdminHandlers()");
216: if (logDetail.isDebugEnabled())
217: logDetail.debug("End getAdminHandlers(" + connectedAdmins
218: + ")");
219:
220: return connectedAdmins;
221: }
222:
223: //------------------------------- Get & Set --------------------------------
224:
225: public vfsSessionPool getVFSSessionPool() {
226: if (logDetail.isDebugEnabled())
227: logDetail.debug("Start getVFSSessionPool()");
228: if (logDetail.isDebugEnabled())
229: logDetail.debug("End getVFSSessionPool(" + sessions + ")");
230:
231: return sessions;
232: }
233:
234: public int getRequestQueueSize() {
235: if (logDetail.isDebugEnabled())
236: logDetail.debug("Start getRequestQueueSize()");
237:
238: int res = workRequestQueue.size();
239:
240: if (logDetail.isDebugEnabled())
241: logDetail.debug("End getRequestQueueSize(" + res + ")");
242:
243: return res;
244: }
245: }
|