1 18 package net.sf.drftpd.mirroring; 19 import java.io.IOException ; 20 import java.net.InetSocketAddress ; 21 import java.rmi.RemoteException ; 22 23 import net.sf.drftpd.NoAvailableSlaveException; 24 import net.sf.drftpd.SlaveUnavailableException; 25 import net.sf.drftpd.master.RemoteSlave; 26 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 27 import net.sf.drftpd.slave.Transfer; 28 29 34 public class SlaveTransfer { 35 class DstXfer extends Thread { 36 private Transfer dstxfer; 37 private Throwable e; 38 public DstXfer(Transfer dstxfer) { 39 this.dstxfer = dstxfer; 40 } 41 public long getChecksum() throws RemoteException { 42 return dstxfer.getChecksum(); 43 } 44 47 public int getLocalPort() throws RemoteException { 48 return dstxfer.getLocalPort(); 49 } 50 public void run() { 51 try { 52 _file.receiveFile(dstxfer, 'I', 0L); 53 } catch (Throwable e) { 54 this.e = e; 55 } 56 } 57 } 58 class SrcXfer extends Thread { 59 private Throwable e; 60 private Transfer srcxfer; 61 public SrcXfer(Transfer srcxfer) { 62 this.srcxfer = srcxfer; 63 } 64 public long getChecksum() throws RemoteException { 65 return srcxfer.getChecksum(); 66 } 67 public void run() { 68 try { 69 _file.sendFile(srcxfer, 'I', 0L); 70 } catch (Throwable e) { 71 this.e = e; 72 } 73 } 74 } 75 private RemoteSlave _destSlave; 76 private LinkedRemoteFileInterface _file; 77 private RemoteSlave _sourceSlave; 78 private boolean finished = false; 79 private Throwable stackTrace; 80 83 public SlaveTransfer( 84 LinkedRemoteFileInterface file, 85 RemoteSlave sourceSlave, 86 RemoteSlave destSlave) { 87 _file = file; 88 _sourceSlave = sourceSlave; 89 _destSlave = destSlave; 90 } 91 public void interruptibleSleepUntilFinished() throws Throwable { 92 while (!finished) { 93 try { 94 Thread.sleep(1000); } catch (InterruptedException e) { 97 e.printStackTrace(); 98 } 99 } 100 if (stackTrace != null) 101 throw stackTrace; 102 } 103 109 public boolean transfer(boolean checkCRC) 110 throws IOException , SlaveUnavailableException { 111 DstXfer dstxfer; 112 try { 113 dstxfer = new DstXfer(_destSlave.getSlave().listen(false)); 114 } catch (RemoteException e1) { 115 _destSlave.handleRemoteException(e1); 116 throw new RuntimeException (e1); 117 } 118 SrcXfer srcxfer; 119 try { 120 srcxfer = 121 new SrcXfer( 122 _sourceSlave.getSlave().connect( 123 new InetSocketAddress ( 124 _destSlave.getInetAddress(), 125 dstxfer.getLocalPort()), 126 false)); 127 } catch (RemoteException e2) { 128 _sourceSlave.handleRemoteException(e2); 129 throw new RuntimeException (e2); 130 } 131 dstxfer.start(); 132 srcxfer.start(); 133 while (srcxfer.isAlive() || dstxfer.isAlive()) { 134 try { 135 Thread.sleep(100); 136 } catch (InterruptedException e) { 137 } 138 } 139 if (srcxfer.e != null) { 140 if (srcxfer.e instanceof IOException ) 141 throw (IOException ) srcxfer.e; 142 throw new RuntimeException (srcxfer.e); 143 } 144 if (dstxfer.e != null) { 145 if (dstxfer.e instanceof IOException ) 146 throw (IOException ) dstxfer.e; 147 throw new RuntimeException (dstxfer.e); 148 } 149 if (!checkCRC) { 150 _file.addSlave(_destSlave); 152 return true; 153 } 154 long dstxferCheckSum = dstxfer.getChecksum(); 155 try { 156 if (dstxferCheckSum == 0 157 || _file.getCheckSumCached() == dstxferCheckSum 158 || _file.getCheckSumFromSlave() == dstxferCheckSum) { 159 _file.addSlave(_destSlave); 160 return true; 161 } 162 } catch (NoAvailableSlaveException e) { 163 return false; 164 } 165 return false; 166 } 167 } 168 | Popular Tags |