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.server;
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  
29  import openr66.configuration.FileBasedConfiguration;
30  import openr66.context.ErrorCode;
31  import openr66.context.R66FiniteDualStates;
32  import openr66.context.R66Result;
33  import openr66.database.DbConstant;
34  import openr66.database.data.DbHostAuth;
35  import openr66.protocol.configuration.Configuration;
36  import openr66.protocol.exception.OpenR66ProtocolNoConnectionException;
37  import openr66.protocol.exception.OpenR66ProtocolPacketException;
38  import openr66.protocol.localhandler.LocalChannelReference;
39  import openr66.protocol.localhandler.packet.LocalPacketFactory;
40  import openr66.protocol.localhandler.packet.ValidPacket;
41  import openr66.protocol.networkhandler.NetworkTransaction;
42  import openr66.protocol.utils.ChannelUtils;
43  import openr66.protocol.utils.R66Future;
44  
45  import org.jboss.netty.channel.Channels;
46  import org.jboss.netty.logging.InternalLoggerFactory;
47  
48  /**
49   * This command enables the dynamic change of bandwidth limitation.
50   * It does not changed the valuesin the database but only dynamic values while the
51   * server is running and until it is shutdown.
52   *
53   * @author Frederic Bregier
54   *
55   */
56  public class ChangeBandwidthLimits implements Runnable {
57      /**
58       * Internal Logger
59       */
60      static volatile GgInternalLogger logger;
61  
62      protected final R66Future future;
63      protected final long writeGlobalLimit;
64      protected final long readGlobalLimit;
65      protected final long writeSessionLimit;
66      protected final long readSessionLimit;
67      protected final NetworkTransaction networkTransaction;
68  
69      public ChangeBandwidthLimits(R66Future future, long wgl, long rgl, long wsl, long rsl,
70              NetworkTransaction networkTransaction) {
71          this.future = future;
72          this.writeGlobalLimit = wgl;
73          this.readGlobalLimit = rgl;
74          this.writeSessionLimit = wsl;
75          this.readSessionLimit = rsl;
76          this.networkTransaction = networkTransaction;
77      }
78  
79      /**
80       * Prior to call this method, the pipeline and NetworkTransaction must have been initialized.
81       * It is the responsibility of the caller to finish all network resources.
82       */
83      public void run() {
84          if (logger == null) {
85              logger = GgInternalLoggerFactory.getLogger(ChangeBandwidthLimits.class);
86          }
87          ValidPacket valid = new ValidPacket(writeGlobalLimit+" "+readGlobalLimit,
88                  writeSessionLimit+" "+readSessionLimit, LocalPacketFactory.BANDWIDTHPACKET);
89          DbHostAuth host = Configuration.configuration.HOST_SSLAUTH;
90          SocketAddress socketAddress = host.getSocketAddress();
91          boolean isSSL = host.isSsl();
92          LocalChannelReference localChannelReference = networkTransaction
93              .createConnectionWithRetry(socketAddress, isSSL, future);
94          socketAddress = null;
95          if (localChannelReference == null) {
96              host = null;
97              logger.error("Cannot Connect");
98              future.setResult(new R66Result(
99                      new OpenR66ProtocolNoConnectionException("Cannot connect to server"),
100                     null, true, ErrorCode.Internal, null));
101             future.setFailure(future.getResult().exception);
102             return;
103         }
104         localChannelReference.sessionNewState(R66FiniteDualStates.VALIDOTHER);
105         try {
106             ChannelUtils.writeAbstractLocalPacket(localChannelReference, valid, false);
107         } catch (OpenR66ProtocolPacketException e) {
108             logger.error("Bad Protocol", e);
109             Channels.close(localChannelReference.getLocalChannel());
110             localChannelReference = null;
111             host = null;
112             valid = null;
113             future.setResult(new R66Result(e, null, true,
114                     ErrorCode.TransferError, null));
115             future.setFailure(e);
116             return;
117         }
118         host = null;
119         future.awaitUninterruptibly();
120         logger.info("Request done with "+(future.isSuccess()?"success":"error"));
121         Channels.close(localChannelReference.getLocalChannel());
122         localChannelReference = null;
123     }
124 
125     protected static long swriteGlobalLimit = -1;
126     protected static long sreadGlobalLimit = -1;
127     protected static long swriteSessionLimit = -1;
128     protected static long sreadSessionLimit = -1;
129 
130     protected static boolean getParams(String [] args) {
131         if (args.length < 3) {
132             logger.error("Need the configuration file as first argument then at least one of\n" +
133             		"   -wglob limitGlobalWrite\n" +
134             		"   -rglob limitGlobalRead\n" +
135             		"   -wsess limitSessionWrite\n" +
136             		"   -rsess limitSessionWrite");
137             return false;
138         }
139         if (! FileBasedConfiguration
140                 .setClientConfigurationFromXml(Configuration.configuration, args[0])) {
141             logger.error("Need the configuration file as first argument then at least one of\n" +
142                     "   -wglob limitGlobalWrite\n" +
143                     "   -rglob limitGlobalRead\n" +
144                     "   -wsess limitSessionWrite\n" +
145                     "   -rsess limitSessionWrite");
146             return false;
147         }
148         for (int i = 1; i < args.length; i++) {
149             if (args[i].equalsIgnoreCase("-wglob")) {
150                 i++;
151                 swriteGlobalLimit = Long.parseLong(args[i]);
152             } else if (args[i].equalsIgnoreCase("-rglob")) {
153                 i++;
154                 sreadGlobalLimit = Long.parseLong(args[i]);
155             } else if (args[i].equalsIgnoreCase("-wsess")) {
156                 i++;
157                 swriteSessionLimit = Long.parseLong(args[i]);
158             } else if (args[i].equalsIgnoreCase("-rsess")) {
159                 i++;
160                 sreadSessionLimit = Long.parseLong(args[i]);
161             }
162         }
163         if (swriteGlobalLimit == -1 && sreadGlobalLimit == -1 &&
164                 swriteSessionLimit == -1 && sreadSessionLimit == -1) {
165             logger.error("Need the configuration file as first argument then at least one of\n" +
166                     "   -wglob limitGlobalWrite\n" +
167                     "   -rglob limitGlobalRead\n" +
168                     "   -wsess limitSessionWrite\n" +
169                     "   -rsess limitSessionWrite");
170             return false;
171         }
172         return true;
173     }
174 
175     public static void main(String[] args) {
176         InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
177         if (logger == null) {
178             logger = GgInternalLoggerFactory.getLogger(ChangeBandwidthLimits.class);
179         }
180         if (! getParams(args)) {
181             logger.error("Wrong initialization");
182             if (DbConstant.admin != null && DbConstant.admin.isConnected) {
183                 DbConstant.admin.close();
184             }
185             System.exit(1);
186         }
187         long time1 = System.currentTimeMillis();
188         R66Future future = new R66Future(true);
189 
190         Configuration.configuration.pipelineInit();
191         NetworkTransaction networkTransaction = new NetworkTransaction();
192         try {
193             ChangeBandwidthLimits transaction = new ChangeBandwidthLimits(future,
194                     swriteGlobalLimit, sreadGlobalLimit, swriteSessionLimit, sreadSessionLimit,
195                     networkTransaction);
196             transaction.run();
197             future.awaitUninterruptibly();
198             long time2 = System.currentTimeMillis();
199             long delay = time2 - time1;
200             R66Result result = future.getResult();
201             if (future.isSuccess()) {
202                 if (result.code == ErrorCode.Warning) {
203                     logger.warn("WARNED on bandwidth:\n    " +
204                             (result.other != null? ((ValidPacket)result.other).getSheader() :
205                                 "no file")
206                             +"\n    delay: "+delay);
207                 } else {
208                     logger.warn("SUCCESS on Bandwidth:\n    " +
209                             (result.other != null? ((ValidPacket)result.other).getSheader() :
210                             "no file")
211                             +"\n    delay: "+delay);
212                 }
213             } else {
214                 if (result.code == ErrorCode.Warning) {
215                     logger.warn("Bandwidth is\n    WARNED", future.getCause());
216                     networkTransaction.closeAll();
217                     System.exit(result.code.ordinal());
218                 } else {
219                     logger.error("Bandwidth in\n    FAILURE", future.getCause());
220                     networkTransaction.closeAll();
221                     System.exit(result.code.ordinal());
222                 }
223             }
224         } finally {
225             networkTransaction.closeAll();
226         }
227     }
228 
229 }