001: package vicazh.hyperpool.stream.net.http;
002:
003: import java.util.*;
004: import vicazh.hyperpool.Start;
005: import vicazh.hyperpool.stream.*;
006: import vicazh.hyperpool.stream.net.Stream;
007: import java.io.*;
008: import vicazh.hyperpool.stream.net.*;
009:
010: class ReconnectConnection extends Connection {
011: ReconnectConnection(Service service) {
012: super (service);
013: }
014:
015: protected Session getSession() {
016: return new ReconnectSession(this );
017: }
018:
019: boolean accept;
020:
021: int retryindex;
022:
023: long index;
024:
025: long length;
026:
027: OutputStream clientout;
028:
029: OutputStream serverout;
030:
031: public void setClient(OutputStream outputstream) throws IOException {
032: super .setClient(outputstream);
033: clientout = outputstream;
034: }
035:
036: String client;
037:
038: public void setServer(OutputStream outputstream) throws IOException {
039: super .setServer(outputstream);
040: serverout = outputstream;
041: Socket socket = ((Stream) getServer()).getSocket();
042: client = socket == null ? Start.NAME : socket.getInetAddress()
043: .getCanonicalHostName().toLowerCase();
044: }
045:
046: boolean check(Session session) throws IOException {
047: Socket socket = ((Stream) getServer()).getSocket();
048: while (accept
049: && (socket == null ? getClient().outputstream != null
050: : !socket.socket.isClosed())
051: && retryindex++ < ((ReconnectService) element)
052: .getRetry() && index < length) {
053: Start.logger.fine("file:" + session.getClient().getFile());
054: try {
055: clientout.flush();
056: } catch (Exception e) {
057: }
058: try {
059: clientout.close();
060: } catch (Exception e) {
061: }
062: // new ServerStream for processing servers headers
063: session.setServer(new NullStream(serverout));
064: // new Stream, because after close() link to ServerStream will lose
065: setServer(new Stream(this , session.getServer()));
066: clientout = ((Element) ((Service) element).getElement())
067: .get(getServer());
068: ((Stream) getClient().outputstream).outputstream = clientout;
069: session.getClient().getFields().put("range",
070: "bytes=" + index + "-");
071: // new Connection for creating new sessions
072: Connection connection = new Connection(new Service());
073: connection.setServer((OutputStream) getServer());
074: connection.setClient(clientout);
075: ClientStream dst = (ClientStream) connection.getClient().outputstream;
076: try {
077: dst.head(session.getClient().getMethod(), session
078: .getClient().getFile(), session.getClient()
079: .getVersion());
080: for (Map.Entry<String, String> e : session.getClient()
081: .getFields().entrySet())
082: dst.field(e.getKey(), e.getValue());
083: dst.header();
084: clientout.flush();
085: return false;
086: } catch (Exception e) {
087: try {
088: Thread.sleep(((ReconnectService) element)
089: .getTimeout() * 1000);
090: } catch (Exception ex) {
091: }
092: }
093: }
094: return true;
095: }
096:
097: synchronized public void remove(Session session) {
098: accept = false;
099: retryindex = 0;
100: index = 0;
101: length = 0;
102: super .remove(session);
103: notify();
104: }
105:
106: synchronized public void close() {
107: super.close();
108: notifyAll();
109: }
110: }
|