1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
49
50 public class TestSendThroughClient extends SendThroughClient {
51
52
53
54
55
56
57
58
59
60
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
78 retrieveDone = true;
79 return retrieveDone;
80 }
81 if (block == null) {
82
83 retrieveDone = true;
84 return retrieveDone;
85 }
86 ChannelFuture future1 = null, future2 = null;
87 if (block != null) {
88 future1 = this.writeWhenPossible(block);
89 }
90
91 while (block != null && !block.isEOF()) {
92 try {
93 block = r66file.readDataBlock();
94 } catch (FileEndOfTransferException e) {
95
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
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
125 this.transferInError(new OpenR66ProtocolSystemException(e));
126 return false;
127 } catch (OpenR66ProtocolPacketException e) {
128
129 this.transferInError(e);
130 return false;
131 } catch (OpenR66RunnerErrorException e) {
132
133 this.transferInError(e);
134 return false;
135 } catch (OpenR66ProtocolSystemException e) {
136
137 this.transferInError(e);
138 return false;
139 }
140 }
141
142
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
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 }