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