View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.protocol.test;
22  
23  import goldengate.common.database.exception.GoldenGateDatabaseException;
24  import goldengate.common.exception.FileEndOfTransferException;
25  import goldengate.common.exception.FileTransferException;
26  import goldengate.common.file.DataBlock;
27  import goldengate.common.logging.GgInternalLoggerFactory;
28  import goldengate.common.logging.GgSlf4JLoggerFactory;
29  import openr66.client.SendThroughClient;
30  import openr66.context.ErrorCode;
31  import openr66.context.R66Result;
32  import openr66.context.filesystem.R66File;
33  import openr66.context.task.exception.OpenR66RunnerErrorException;
34  import openr66.database.DbConstant;
35  import openr66.protocol.configuration.Configuration;
36  import openr66.protocol.exception.OpenR66ProtocolPacketException;
37  import openr66.protocol.exception.OpenR66ProtocolSystemException;
38  import openr66.protocol.networkhandler.NetworkTransaction;
39  import openr66.protocol.utils.R66Future;
40  
41  import org.jboss.netty.channel.ChannelFuture;
42  import org.jboss.netty.logging.InternalLoggerFactory;
43  
44  /**
45   * Test class for Send Through client
46   *
47   * @author Frederic Bregier
48   *
49   */
50  public class TestSendThroughClient extends SendThroughClient {
51  
52      /**
53       * @param future
54       * @param remoteHost
55       * @param filename
56       * @param rulename
57       * @param fileinfo
58       * @param isMD5
59       * @param blocksize
60       * @param networkTransaction
61       */
62      public TestSendThroughClient(R66Future future, String remoteHost,
63              String filename, String rulename, String fileinfo, boolean isMD5,
64              int blocksize, NetworkTransaction networkTransaction) {
65          super(future, remoteHost, filename, rulename, fileinfo, isMD5, blocksize,
66                  DbConstant.ILLEGALVALUE, networkTransaction);
67      }
68  
69      public boolean sendFile() {
70          R66File r66file = localChannelReference.getSession().getFile();
71          boolean retrieveDone = false;
72          try {
73              DataBlock block = null;
74              try {
75                  block = r66file.readDataBlock();
76              } catch (FileEndOfTransferException e) {
77                  // Last block (in fact, no data to read)
78                  retrieveDone = true;
79                  return retrieveDone;
80              }
81              if (block == null) {
82                  // Last block (in fact, no data to read)
83                  retrieveDone = true;
84                  return retrieveDone;
85              }
86              ChannelFuture future1 = null, future2 = null;
87              if (block != null) {
88                  future1 = this.writeWhenPossible(block);
89              }
90              // While not last block
91              while (block != null && !block.isEOF()) {
92                  try {
93                      block = r66file.readDataBlock();
94                  } catch (FileEndOfTransferException e) {
95                      // Wait for last write
96                      retrieveDone = true;
97                      try {
98                          future1.await();
99                      } catch (InterruptedException e1) {
100                     }
101                     return future1.isSuccess();
102                 }
103                 future2 = this.writeWhenPossible(block);
104                 try {
105                     future1.await();
106                 } catch (InterruptedException e) {
107                 }
108                 if (!future1.isSuccess()) {
109                     return false;
110                 }
111                 future1 = future2;
112             }
113             // Wait for last write
114             if (future1 != null) {
115                 try {
116                     future1.await();
117                 } catch (InterruptedException e) {
118                 }
119                 return future1.isSuccess();
120             }
121             retrieveDone = true;
122             return retrieveDone;
123         } catch (FileTransferException e) {
124             // An error occurs!
125             this.transferInError(new OpenR66ProtocolSystemException(e));
126             return false;
127         } catch (OpenR66ProtocolPacketException e) {
128             // An error occurs!
129             this.transferInError(e);
130             return false;
131         } catch (OpenR66RunnerErrorException e) {
132             // An error occurs!
133             this.transferInError(e);
134             return false;
135         } catch (OpenR66ProtocolSystemException e) {
136             // An error occurs!
137             this.transferInError(e);
138             return false;
139         }
140     }
141     /**
142      * @param args
143      */
144     public static void main(String[] args) {
145         InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(
146                 null));
147         if (logger == null) {
148             logger = GgInternalLoggerFactory.getLogger(TestSendThroughClient.class);
149         }
150         if (! getParams(args, false)) {
151             logger.error("Wrong initialization");
152             if (DbConstant.admin != null && DbConstant.admin.isConnected) {
153                 DbConstant.admin.close();
154             }
155             System.exit(1);
156         }
157         Configuration.configuration.pipelineInit();
158         NetworkTransaction networkTransaction = new NetworkTransaction();
159         try {
160             R66Future future = new R66Future(true);
161             TestSendThroughClient transaction = new TestSendThroughClient(future,
162                     rhost, localFilename, rule, fileInfo, ismd5, block,
163                     networkTransaction);
164             long time1 = System.currentTimeMillis();
165             if (! transaction.initiateRequest()) {
166                 logger.error("Transfer in Error", future.getCause());
167                 return;
168             }
169             if (transaction.sendFile()) {
170                 transaction.finalizeRequest();
171             } else {
172                 transaction.transferInError(null);
173             }
174             future.awaitUninterruptibly();
175 
176             long time2 = System.currentTimeMillis();
177             long delay = time2 - time1;
178             R66Result result = future.getResult();
179             if (future.isSuccess()) {
180                 if (result.runner.getErrorInfo() == ErrorCode.Warning) {
181                     logger.warn("Warning with Id: " +
182                             result.runner.getSpecialId()+" on file: " +
183                             (result.file != null? result.file.toString() : "no file")
184                             +" delay: "+delay);
185                 } else {
186                     logger.warn("Success with Id: " +
187                             result.runner.getSpecialId()+" on Final file: " +
188                             (result.file != null? result.file.toString() : "no file")
189                             +" delay: "+delay);
190                 }
191                 if (nolog) {
192                     // In case of success, delete the runner
193                     try {
194                         result.runner.delete();
195                     } catch (GoldenGateDatabaseException e) {
196                         logger.warn("Cannot apply nolog to "+result.runner.toString(), e);
197                     }
198                 }
199             } else {
200                 if (result == null || result.runner == null) {
201                     logger.warn("Transfer in Error with no Id", future.getCause());
202                     networkTransaction.closeAll();
203                     System.exit(1);
204                 }
205                 if (result.runner.getErrorInfo() == ErrorCode.Warning) {
206                     logger.warn("Transfer in Warning with Id: " +
207                             result.runner.getSpecialId(), future.getCause());
208                     networkTransaction.closeAll();
209                     System.exit(result.code.ordinal());
210                 } else {
211                     logger.error("Transfer in Error with Id: " +
212                             result.runner.getSpecialId(), future.getCause());
213                     networkTransaction.closeAll();
214                     System.exit(result.code.ordinal());
215                 }
216             }
217         } finally {
218             networkTransaction.closeAll();
219         }
220 
221     }
222 
223 }