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.logging.GgInternalLogger;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  import goldengate.common.logging.GgSlf4JLoggerFactory;
26  
27  import java.net.SocketAddress;
28  import java.util.concurrent.ExecutorService;
29  import java.util.concurrent.Executors;
30  
31  import openr66.configuration.FileBasedConfiguration;
32  import openr66.context.R66FiniteDualStates;
33  import openr66.database.data.DbHostAuth;
34  import openr66.protocol.configuration.Configuration;
35  import openr66.protocol.exception.OpenR66Exception;
36  import openr66.protocol.exception.OpenR66ProtocolNetworkException;
37  import openr66.protocol.exception.OpenR66ProtocolNoConnectionException;
38  import openr66.protocol.exception.OpenR66ProtocolPacketException;
39  import openr66.protocol.exception.OpenR66ProtocolRemoteShutdownException;
40  import openr66.protocol.localhandler.LocalChannelReference;
41  import openr66.protocol.localhandler.packet.TestPacket;
42  import openr66.protocol.networkhandler.NetworkTransaction;
43  import openr66.protocol.utils.ChannelUtils;
44  import openr66.protocol.utils.R66Future;
45  
46  import org.jboss.netty.channel.Channels;
47  import org.jboss.netty.logging.InternalLoggerFactory;
48  
49  /**
50   * Test class for internal ping pong test
51   * @author Frederic Bregier
52   *
53   */
54  public class TestTransaction implements Runnable {
55      /**
56       * Internal Logger
57       */
58      private static GgInternalLogger logger;
59  
60      final private NetworkTransaction networkTransaction;
61  
62      final private R66Future future;
63  
64      private final SocketAddress socketAddress;
65  
66      final private TestPacket testPacket;
67  
68      public TestTransaction(NetworkTransaction networkTransaction,
69              R66Future future, SocketAddress socketAddress, TestPacket packet) {
70          if (logger == null) {
71              logger = GgInternalLoggerFactory.getLogger(TestTransaction.class);
72          }
73          this.networkTransaction = networkTransaction;
74          this.future = future;
75          this.socketAddress = socketAddress;
76          testPacket = packet;
77      }
78  
79      public void run() {
80          LocalChannelReference localChannelReference = null;
81          OpenR66Exception lastException = null;
82          for (int i = 0; i < Configuration.RETRYNB; i ++) {
83              try {
84                  localChannelReference = networkTransaction
85                          .createConnection(socketAddress, false, future);
86                  break;
87              } catch (OpenR66ProtocolNetworkException e1) {
88                  lastException = e1;
89                  localChannelReference = null;
90              } catch (OpenR66ProtocolRemoteShutdownException e1) {
91                  lastException = e1;
92                  localChannelReference = null;
93                  break;
94              } catch (OpenR66ProtocolNoConnectionException e1) {
95                  lastException = e1;
96                  localChannelReference = null;
97                  break;
98              }
99          }
100         if (localChannelReference == null) {
101             logger.error("Cannot connect: " + lastException.getMessage());
102             future.setResult(null);
103             future.setFailure(lastException);
104             return;
105         } else if (lastException != null) {
106             logger.info("Connection retry since ", lastException);
107         }
108         localChannelReference.sessionNewState(R66FiniteDualStates.TEST);
109         try {
110             ChannelUtils.writeAbstractLocalPacket(localChannelReference, testPacket, false);
111         } catch (OpenR66ProtocolPacketException e) {
112             future.setResult(null);
113             future.setFailure(e);
114             Channels.close(localChannelReference.getLocalChannel());
115             return;
116         }
117     }
118 
119     public static void main(String[] args) {
120         InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(
121                 null));
122         if (logger == null) {
123             logger = GgInternalLoggerFactory.getLogger(TestTransaction.class);
124         }
125         if (args.length < 1) {
126             logger
127                     .error("Needs at least the configuration file as first argument");
128             return;
129         }
130         if (! FileBasedConfiguration
131                 .setClientConfigurationFromXml(Configuration.configuration, args[0])) {
132             logger
133                     .error("Needs a correct configuration file as first argument");
134             return;
135         }
136         Configuration.configuration.pipelineInit();
137 
138         final NetworkTransaction networkTransaction = new NetworkTransaction();
139         DbHostAuth host = Configuration.configuration.HOST_AUTH;
140         final SocketAddress socketServerAddress = host.getSocketAddress();
141         ExecutorService executorService = Executors.newCachedThreadPool();
142         int nb = 100;
143 
144         R66Future[] arrayFuture = new R66Future[nb];
145         logger.info("Start Test of Transaction");
146         long time1 = System.currentTimeMillis();
147         for (int i = 0; i < nb; i ++) {
148             arrayFuture[i] = new R66Future(true);
149             TestPacket packet = new TestPacket("Test", "" + i, 0);
150             TestTransaction transaction = new TestTransaction(
151                     networkTransaction, arrayFuture[i], socketServerAddress,
152                     packet);
153             executorService.execute(transaction);
154         }
155         int success = 0;
156         int error = 0;
157         for (int i = 0; i < nb; i ++) {
158             arrayFuture[i].awaitUninterruptibly();
159             if (arrayFuture[i].isSuccess()) {
160                 success ++;
161             } else {
162                 error ++;
163             }
164         }
165         long time2 = System.currentTimeMillis();
166         logger.warn("Success: " + success + " Error: " + error + " NB/s: " +
167                 success * TestPacket.pingpong * 1000 / (time2 - time1));
168         executorService.shutdown();
169         networkTransaction.closeAll();
170     }
171 
172 }