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;
020:
021: import java.io.*;
022: import java.net.*;
023: import javax.net.ssl.*;
024:
025: import org.apache.log4j.*;
026:
027: import org.homedns.dade.jcgrid.message.*;
028: import org.homedns.dade.jcgrid.server.*;
029: import org.homedns.dade.jcgrid.util.*;
030:
031: public abstract class GridNodeGeneric extends GridNode {
032: private final static String className = GridNodeGeneric.class
033: .getName();
034: private static Logger log = Logger.getLogger(className);
035: private static Logger logDetail = Logger.getLogger("DETAIL."
036: + className);
037:
038: private Socket serverSocket;
039: private GridMessageChannel serverChannel;
040:
041: public GridNodeGeneric(GridNodeGenericConfig cfg) {
042: super (cfg);
043:
044: if (log.isDebugEnabled())
045: log.debug("Start GridNode(" + cfg + ")");
046:
047: serverSocket = null;
048: serverChannel = null;
049:
050: if (log.isDebugEnabled())
051: log.debug("End GridNode()");
052: }
053:
054: //----------------------------- Start & Stop -------------------------------
055:
056: public void start() throws Exception {
057: if (logDetail.isDebugEnabled())
058: logDetail.debug("Start start()");
059:
060: super .start();
061:
062: // Connect to server
063:
064: log.warn(" Connecting to server...");
065:
066: // Selecting the right port
067:
068: GridConfig gCfg = this .getNodeConfig().getGridConfig();
069:
070: int port = gCfg.getServerClientPort();
071: if (GridNodeConfig.TYPE_WORKER.equals(this .getNodeConfig()
072: .getType()))
073: port = gCfg.getServerWorkerPort();
074: else if (GridNodeConfig.TYPE_ADMIN.equals(this .getNodeConfig()
075: .getType()))
076: port = gCfg.getServerAdminPort();
077:
078: if (gCfg.getUseSecureConnection()) {
079: SSLSocketFactory sslFact = (SSLSocketFactory) SSLSocketFactory
080: .getDefault();
081: serverSocket = sslFact.createSocket(
082: gCfg.getServerAddress(), port);
083: } else
084: serverSocket = new Socket(gCfg.getServerAddress(), port);
085:
086: if (gCfg.getUseCompression())
087: serverChannel = new GridMessageGZIPChannel(serverSocket);
088: else
089: serverChannel = new GridMessageFlatChannel(serverSocket,
090: false);
091:
092: log.warn(" Ok");
093:
094: // Send login information
095:
096: log.warn(" Sending login information...");
097: serverChannel.send(new GridMessageBeginSession(
098: ((GridNodeGenericConfig) this .getNodeConfig())
099: .getSessionName(), gCfg.getServerPassword()));
100:
101: log.warn(" Ok");
102:
103: if (logDetail.isDebugEnabled())
104: logDetail.debug("End start()");
105: }
106:
107: public void stop() throws Exception {
108: if (logDetail.isDebugEnabled())
109: logDetail.debug("Start stop()");
110:
111: try {
112: serverChannel.send(new GridMessageEndSession());
113: } catch (Exception ex) {
114: if (log.isDebugEnabled())
115: log.debug("End session failed", ex);
116: }
117:
118: try {
119: serverChannel.close();
120: } catch (Exception ex) {
121: if (logDetail.isDebugEnabled())
122: logDetail.debug(
123: "Close server channel connection failed", ex);
124: }
125:
126: if (logDetail.isDebugEnabled())
127: logDetail.debug("End stop()");
128: }
129:
130: //------------------------------- Get & Set --------------------------------
131:
132: public GridMessageChannel getGridMessageChannel() {
133: if (logDetail.isDebugEnabled())
134: logDetail.debug("Start getGridMessageChannel()");
135: if (logDetail.isDebugEnabled())
136: logDetail.debug("End getGridMessageChannel("
137: + serverChannel + ")");
138:
139: return serverChannel;
140: }
141: }
|