1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.gateway.ftp.exec;
22
23 import io.netty.bootstrap.Bootstrap;
24 import io.netty.channel.Channel;
25 import io.netty.channel.ChannelFuture;
26 import io.netty.channel.EventLoopGroup;
27 import io.netty.channel.nio.NioEventLoopGroup;
28 import org.waarp.commandexec.client.LocalExecClientHandler;
29 import org.waarp.commandexec.client.LocalExecClientInitializer;
30 import org.waarp.commandexec.utils.LocalExecResult;
31 import org.waarp.common.crypto.ssl.WaarpSslUtility;
32 import org.waarp.common.future.WaarpFuture;
33 import org.waarp.common.logging.SysErrLogger;
34 import org.waarp.common.logging.WaarpLogger;
35 import org.waarp.common.logging.WaarpLoggerFactory;
36 import org.waarp.common.utility.WaarpNettyUtil;
37 import org.waarp.common.utility.WaarpThreadFactory;
38 import org.waarp.openr66.protocol.configuration.Configuration;
39
40 import java.net.InetSocketAddress;
41
42
43
44
45 public class LocalExecClient {
46
47
48
49 private static final WaarpLogger logger =
50 WaarpLoggerFactory.getLogger(LocalExecClient.class);
51
52 private static InetSocketAddress address;
53
54 private static Bootstrap bootstrapLocalExec;
55
56 private static LocalExecClientInitializer localExecClientInitializer;
57
58
59
60
61 public static void initialize(final int clientThread,
62 final long maxGlobalMemory) {
63 final EventLoopGroup localPipelineExecutor =
64 new NioEventLoopGroup(clientThread * 2,
65 new WaarpThreadFactory("LocalExecutor"));
66
67 bootstrapLocalExec = new Bootstrap();
68 WaarpNettyUtil.setBootstrap(bootstrapLocalExec, localPipelineExecutor,
69 (int) Configuration.configuration.getTimeoutCon());
70
71 localExecClientInitializer = new LocalExecClientInitializer();
72 bootstrapLocalExec.handler(localExecClientInitializer);
73 }
74
75
76
77
78 public static void releaseResources() {
79 if (bootstrapLocalExec == null) {
80 return;
81 }
82
83 bootstrapLocalExec.config().group().shutdownGracefully();
84 localExecClientInitializer.releaseResources();
85 }
86
87 private Channel channel;
88 private LocalExecResult result;
89
90 public LocalExecClient() {
91
92 }
93
94 public final LocalExecResult getLocalExecResult() {
95 return result;
96 }
97
98
99
100
101
102
103
104
105
106
107
108 public final void runOneCommand(final String command, final long delay,
109 final WaarpFuture futureCompletion) {
110
111 final LocalExecClientHandler clientHandler =
112 (LocalExecClientHandler) channel.pipeline().last();
113
114 clientHandler.initExecClient(delay, command);
115
116 result = clientHandler.waitFor(delay * 2);
117 if (futureCompletion == null) {
118 return;
119 }
120 if (result.getStatus() == 0) {
121 futureCompletion.setSuccess();
122 logger.info("Exec OK with {}", command);
123 } else if (result.getStatus() == 1) {
124 logger.warn("Exec in warning with {}", command);
125 futureCompletion.setSuccess();
126 } else {
127 logger.error(
128 "Status: " + result.getStatus() + " Exec in error with " + command +
129 '\n' + result.getResult());
130 futureCompletion.cancel();
131 }
132 }
133
134
135
136
137 public final boolean connect() {
138
139 final ChannelFuture future = bootstrapLocalExec.connect(getAddress());
140
141
142 try {
143 channel = future.await().sync().channel();
144 } catch (final InterruptedException e) {
145 SysErrLogger.FAKE_LOGGER.ignoreLog(e);
146 }
147 if (!future.isSuccess()) {
148 logger.error("Client Not Connected", future.cause());
149 return false;
150 }
151 return true;
152 }
153
154
155
156
157 public final void disconnect() {
158
159
160 WaarpNettyUtil.awaitOrInterrupted(
161 WaarpSslUtility.closingSslChannel(channel));
162 }
163
164
165
166
167 public static InetSocketAddress getAddress() {
168 return address;
169 }
170
171
172
173
174 public static void setAddress(final InetSocketAddress address) {
175 LocalExecClient.address = address;
176 }
177 }