KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > mirroring > SlaveTransfer


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.mirroring;
19 import java.io.IOException JavaDoc;
20 import java.net.InetSocketAddress JavaDoc;
21 import java.rmi.RemoteException JavaDoc;
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 /**
30  * @author mog
31  * @author zubov
32  * @version $Id: SlaveTransfer.java,v 1.15 2004/05/31 02:47:18 mog Exp $
33  */

34 public class SlaveTransfer {
35     class DstXfer extends Thread JavaDoc {
36         private Transfer dstxfer;
37         private Throwable JavaDoc e;
38         public DstXfer(Transfer dstxfer) {
39             this.dstxfer = dstxfer;
40         }
41         public long getChecksum() throws RemoteException JavaDoc {
42             return dstxfer.getChecksum();
43         }
44         /**
45          * @return
46          */

47         public int getLocalPort() throws RemoteException JavaDoc {
48             return dstxfer.getLocalPort();
49         }
50         public void run() {
51             try {
52                 _file.receiveFile(dstxfer, 'I', 0L);
53             } catch (Throwable JavaDoc e) {
54                 this.e = e;
55             }
56         }
57     }
58     class SrcXfer extends Thread JavaDoc {
59         private Throwable JavaDoc e;
60         private Transfer srcxfer;
61         public SrcXfer(Transfer srcxfer) {
62             this.srcxfer = srcxfer;
63         }
64         public long getChecksum() throws RemoteException JavaDoc {
65             return srcxfer.getChecksum();
66         }
67         public void run() {
68             try {
69                 _file.sendFile(srcxfer, 'I', 0L);
70             } catch (Throwable JavaDoc 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 JavaDoc stackTrace;
80     /**
81      * Slave to Slave Transfers
82      */

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 JavaDoc {
92         while (!finished) {
93             try {
94                 Thread.sleep(1000); // 1 sec
95
//System.err.println("slept 1 secs");
96
} catch (InterruptedException JavaDoc e) {
97                 e.printStackTrace();
98             }
99         }
100         if (stackTrace != null)
101             throw stackTrace;
102     }
103     /**
104      * Returns true if the crc passed, false otherwise
105      *
106      * @return @throws
107      * IOException
108      */

109     public boolean transfer(boolean checkCRC)
110         throws IOException JavaDoc, SlaveUnavailableException {
111         DstXfer dstxfer;
112         try {
113             dstxfer = new DstXfer(_destSlave.getSlave().listen(false));
114         } catch (RemoteException JavaDoc e1) {
115             _destSlave.handleRemoteException(e1);
116             throw new RuntimeException JavaDoc(e1);
117         }
118         SrcXfer srcxfer;
119         try {
120             srcxfer =
121                 new SrcXfer(
122                     _sourceSlave.getSlave().connect(
123                         new InetSocketAddress JavaDoc(
124                             _destSlave.getInetAddress(),
125                             dstxfer.getLocalPort()),
126                         false));
127         } catch (RemoteException JavaDoc e2) {
128             _sourceSlave.handleRemoteException(e2);
129             throw new RuntimeException JavaDoc(e2);
130         }
131         dstxfer.start();
132         srcxfer.start();
133         while (srcxfer.isAlive() || dstxfer.isAlive()) {
134             try {
135                 Thread.sleep(100);
136             } catch (InterruptedException JavaDoc e) {
137             }
138         }
139         if (srcxfer.e != null) {
140             if (srcxfer.e instanceof IOException JavaDoc)
141                 throw (IOException JavaDoc) srcxfer.e;
142             throw new RuntimeException JavaDoc(srcxfer.e);
143         }
144         if (dstxfer.e != null) {
145             if (dstxfer.e instanceof IOException JavaDoc)
146                 throw (IOException JavaDoc) dstxfer.e;
147             throw new RuntimeException JavaDoc(dstxfer.e);
148         }
149         if (!checkCRC) {
150             // crc passes if we're not using it
151
_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