1 7 package winstone.cluster; 8 9 import java.net.*; 10 import java.io.*; 11 12 import winstone.Logger; 13 import winstone.WinstoneSession; 14 15 21 public class ClusterSessionSearch implements Runnable { 22 final int TIMEOUT = 2000; 23 public static final byte SESSION_CHECK_TYPE = (byte) '1'; 24 public static final String SESSION_NOT_FOUND = "NOTFOUND"; 25 public static final String SESSION_FOUND = "FOUND"; 26 public static final String SESSION_RECEIVED = "OK"; 27 private boolean isFinished; 28 private WinstoneSession result; 30 private String searchWebAppHostname; 31 private String searchWebAppPrefix; 32 private String searchId; 33 private String searchAddressPort; 34 private int controlPort; 35 36 39 public ClusterSessionSearch(String webAppPrefix, String hostName, String sessionId, 40 String ipPort, int controlPort) { 41 this.isFinished = false; 42 this.searchWebAppHostname = hostName; 43 this.searchWebAppPrefix = webAppPrefix; 44 this.searchId = sessionId; 46 this.searchAddressPort = ipPort; 47 this.result = null; 48 this.controlPort = controlPort; 49 50 Thread searchThread = new Thread (this); 52 searchThread.setDaemon(true); 53 searchThread.start(); 54 } 55 56 59 public void run() { 60 try { 61 int colonPos = this.searchAddressPort.indexOf(':'); 62 String ipAddress = this.searchAddressPort.substring(0, colonPos); 63 String port = this.searchAddressPort.substring(colonPos + 1); 64 65 Socket controlConnection = new Socket(ipAddress, Integer.parseInt(port)); 66 controlConnection.setSoTimeout(TIMEOUT); 67 OutputStream out = controlConnection.getOutputStream(); 68 out.write(SESSION_CHECK_TYPE); 69 out.flush(); 70 71 ObjectOutputStream outControl = new ObjectOutputStream(out); 72 outControl.writeInt(this.controlPort); 73 outControl.writeUTF(this.searchId); 74 outControl.writeUTF(this.searchWebAppHostname); 75 outControl.writeUTF(this.searchWebAppPrefix); 76 outControl.flush(); 77 InputStream in = controlConnection.getInputStream(); 78 ObjectInputStream inSession = new ObjectInputStream(in); 79 String reply = inSession.readUTF(); 80 if ((reply != null) && reply.equals(SESSION_FOUND)) { 81 WinstoneSession session = (WinstoneSession) inSession 82 .readObject(); 83 outControl.writeUTF(SESSION_RECEIVED); 84 this.result = session; 85 } 86 outControl.close(); 87 inSession.close(); 88 out.close(); 89 in.close(); 90 controlConnection.close(); 91 } catch (Throwable err) { 92 Logger.log(Logger.WARNING, SimpleCluster.CLUSTER_RESOURCES, 93 "ClusterSessionSearch.Error", err); 94 } 95 this.isFinished = true; 96 } 97 98 public boolean isFinished() { 99 return this.isFinished; 100 } 101 102 public WinstoneSession getResult() { 103 return this.result; 104 } 105 106 public void destroy() { 107 } 109 110 public String getAddressPort() { 111 return this.searchAddressPort; 112 } 113 } 114 | Popular Tags |