001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package winstone.cluster;
008:
009: import java.net.*;
010: import java.io.*;
011:
012: import winstone.Logger;
013: import winstone.WinstoneSession;
014:
015: /**
016: * Contains all the logic for reading in sessions
017: *
018: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
019: * @version $Id: ClusterSessionSearch.java,v 1.6 2006/03/24 17:24:18 rickknowles Exp $
020: */
021: public class ClusterSessionSearch implements Runnable {
022: final int TIMEOUT = 2000;
023: public static final byte SESSION_CHECK_TYPE = (byte) '1';
024: public static final String SESSION_NOT_FOUND = "NOTFOUND";
025: public static final String SESSION_FOUND = "FOUND";
026: public static final String SESSION_RECEIVED = "OK";
027: private boolean isFinished;
028: // private boolean interrupted;
029: private WinstoneSession result;
030: private String searchWebAppHostname;
031: private String searchWebAppPrefix;
032: private String searchId;
033: private String searchAddressPort;
034: private int controlPort;
035:
036: /**
037: * Sets up for a threaded search
038: */
039: public ClusterSessionSearch(String webAppPrefix, String hostName,
040: String sessionId, String ipPort, int controlPort) {
041: this .isFinished = false;
042: this .searchWebAppHostname = hostName;
043: this .searchWebAppPrefix = webAppPrefix;
044: // this.interrupted = false;
045: this .searchId = sessionId;
046: this .searchAddressPort = ipPort;
047: this .result = null;
048: this .controlPort = controlPort;
049:
050: // Start the search thread
051: Thread searchThread = new Thread(this );
052: searchThread.setDaemon(true);
053: searchThread.start();
054: }
055:
056: /**
057: * Actually implements the search
058: */
059: public void run() {
060: try {
061: int colonPos = this .searchAddressPort.indexOf(':');
062: String ipAddress = this .searchAddressPort.substring(0,
063: colonPos);
064: String port = this .searchAddressPort
065: .substring(colonPos + 1);
066:
067: Socket controlConnection = new Socket(ipAddress, Integer
068: .parseInt(port));
069: controlConnection.setSoTimeout(TIMEOUT);
070: OutputStream out = controlConnection.getOutputStream();
071: out.write(SESSION_CHECK_TYPE);
072: out.flush();
073:
074: ObjectOutputStream outControl = new ObjectOutputStream(out);
075: outControl.writeInt(this .controlPort);
076: outControl.writeUTF(this .searchId);
077: outControl.writeUTF(this .searchWebAppHostname);
078: outControl.writeUTF(this .searchWebAppPrefix);
079: outControl.flush();
080: InputStream in = controlConnection.getInputStream();
081: ObjectInputStream inSession = new ObjectInputStream(in);
082: String reply = inSession.readUTF();
083: if ((reply != null) && reply.equals(SESSION_FOUND)) {
084: WinstoneSession session = (WinstoneSession) inSession
085: .readObject();
086: outControl.writeUTF(SESSION_RECEIVED);
087: this .result = session;
088: }
089: outControl.close();
090: inSession.close();
091: out.close();
092: in.close();
093: controlConnection.close();
094: } catch (Throwable err) {
095: Logger.log(Logger.WARNING, SimpleCluster.CLUSTER_RESOURCES,
096: "ClusterSessionSearch.Error", err);
097: }
098: this .isFinished = true;
099: }
100:
101: public boolean isFinished() {
102: return this .isFinished;
103: }
104:
105: public WinstoneSession getResult() {
106: return this .result;
107: }
108:
109: public void destroy() {
110: // this.interrupted = true;
111: }
112:
113: public String getAddressPort() {
114: return this.searchAddressPort;
115: }
116: }
|