1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.client;
21
22 import org.waarp.common.logging.SysErrLogger;
23 import org.waarp.common.logging.WaarpLogger;
24 import org.waarp.common.logging.WaarpLoggerFactory;
25 import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
26 import org.waarp.common.utility.WaarpSystemUtil;
27 import org.waarp.openr66.client.utils.OutputFormat;
28 import org.waarp.openr66.configuration.FileBasedConfiguration;
29 import org.waarp.openr66.context.ErrorCode;
30 import org.waarp.openr66.context.R66FiniteDualStates;
31 import org.waarp.openr66.context.R66Result;
32 import org.waarp.openr66.context.authentication.R66Auth;
33 import org.waarp.openr66.database.data.DbHostAuth;
34 import org.waarp.openr66.protocol.configuration.Configuration;
35 import org.waarp.openr66.protocol.configuration.Messages;
36 import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
37 import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
38 import org.waarp.openr66.protocol.localhandler.packet.TestPacket;
39 import org.waarp.openr66.protocol.localhandler.packet.ValidPacket;
40 import org.waarp.openr66.protocol.networkhandler.NetworkTransaction;
41 import org.waarp.openr66.protocol.utils.ChannelUtils;
42 import org.waarp.openr66.protocol.utils.R66Future;
43
44 import java.net.SocketAddress;
45
46 import static org.waarp.common.database.DbConstant.*;
47
48
49
50
51 public class Message implements Runnable {
52
53
54
55 private static WaarpLogger logger;
56
57 protected static String infoArgs = Messages.getString("Message.0") +
58 Messages.getString("Message.OutputFormat");
59
60
61 private final NetworkTransaction networkTransaction;
62
63 private final R66Future future;
64
65 private final String requested;
66
67 private final DbHostAuth hostAuth;
68
69 private final TestPacket testPacket;
70
71 static String srequested;
72 static String smessage = "MESSAGE";
73
74
75
76
77
78
79
80
81 protected static boolean getParams(final String[] args) {
82 if (logger == null) {
83 logger = WaarpLoggerFactory.getLogger(Message.class);
84 }
85 infoArgs = Messages.getString("Message.0") +
86 Messages.getString("Message.OutputFormat");
87 if (args.length < 5) {
88 logger.error(infoArgs);
89 return false;
90 }
91 if (!FileBasedConfiguration.setClientConfigurationFromXml(
92 Configuration.configuration, args[0])) {
93 logger.error(
94 Messages.getString("Configuration.NeedCorrectConfig"));
95 return false;
96 }
97 for (int i = 1; i < args.length; i++) {
98 if ("-to".equalsIgnoreCase(args[i])) {
99 i++;
100 srequested = args[i];
101 if (Configuration.configuration.getAliases().containsKey(srequested)) {
102 srequested = Configuration.configuration.getAliases().get(srequested);
103 }
104 } else if ("-msg".equalsIgnoreCase(args[i])) {
105 i++;
106 smessage = args[i];
107 }
108 }
109 OutputFormat.getParams(args);
110 if (srequested == null) {
111 logger.error(Messages.getString("Message.HostIdMustBeSet") +
112 infoArgs);
113 return false;
114 }
115 return true;
116 }
117
118 public Message(final NetworkTransaction networkTransaction,
119 final R66Future future, final String requested,
120 final TestPacket packet) {
121 if (logger == null) {
122 logger = WaarpLoggerFactory.getLogger(Message.class);
123 }
124 this.networkTransaction = networkTransaction;
125 this.future = future;
126 this.requested = requested;
127 testPacket = packet;
128 hostAuth = null;
129 }
130
131 public Message(final NetworkTransaction networkTransaction,
132 final R66Future future, final DbHostAuth hostAuth,
133 final TestPacket packet) {
134 if (logger == null) {
135 logger = WaarpLoggerFactory.getLogger(Message.class);
136 }
137 this.networkTransaction = networkTransaction;
138 this.future = future;
139 requested = null;
140 testPacket = packet;
141 this.hostAuth = hostAuth;
142 }
143
144 @Override
145 public void run() {
146 if (logger == null) {
147 logger = WaarpLoggerFactory.getLogger(Message.class);
148 }
149
150 final DbHostAuth host;
151 if (hostAuth == null) {
152 host = R66Auth.getServerAuth(requested);
153 } else {
154 host = hostAuth;
155 }
156 if (host == null) {
157 logger.info("{} {}", Messages.getString("Message.HostNotFound"),
158 requested);
159 final R66Result result =
160 new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
161 future.setResult(result);
162 future.cancel();
163 return;
164 }
165 if (host.isClient()) {
166 logger.error(
167 Messages.getString("Message.HostIsClient") + requested);
168 final R66Result result =
169 new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
170 future.setResult(result);
171 future.cancel();
172 return;
173 }
174 SocketAddress socketAddress = host.getSocketAddress();
175 final boolean isSSL = host.isSsl();
176 final LocalChannelReference localChannelReference;
177 localChannelReference =
178 networkTransaction.createConnectionWithRetry(socketAddress, isSSL,
179 future);
180 socketAddress = null;
181 if (localChannelReference == null) {
182 logger.info("{} {}", Messages.getString("AdminR66OperationsGui.188"),
183 requested);
184 final R66Result result =
185 new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
186 future.setResult(result);
187 future.cancel();
188 return;
189 }
190 localChannelReference.sessionNewState(R66FiniteDualStates.TEST);
191 try {
192 ChannelUtils.writeAbstractLocalPacket(localChannelReference, testPacket,
193 false);
194 } catch (final OpenR66ProtocolPacketException e) {
195 future.setResult(null);
196 future.setFailure(e);
197 localChannelReference.close();
198 }
199 }
200
201 public static void main(final String[] args) {
202 WaarpLoggerFactory.setDefaultFactoryIfNotSame(
203 new WaarpSlf4JLoggerFactory(null));
204 if (logger == null) {
205 logger = WaarpLoggerFactory.getLogger(Message.class);
206 }
207 if (args.length < 5) {
208 logger.error(infoArgs);
209 WaarpSystemUtil.systemExit(1);
210 return;
211 }
212 if (!getParams(args)) {
213 logger.error(Messages.getString("Configuration.WrongInit"));
214 if (admin != null) {
215 admin.close();
216 }
217 WaarpSystemUtil.systemExit(1);
218 return;
219 }
220 NetworkTransaction networkTransaction = null;
221 int value = 3;
222 try {
223 Configuration.configuration.pipelineInit();
224 networkTransaction = new NetworkTransaction();
225 final R66Future result = new R66Future(true);
226 final TestPacket packet = new TestPacket("MSG", smessage, 100);
227 final Message transaction =
228 new Message(networkTransaction, result, srequested, packet);
229 transaction.run();
230 result.awaitOrInterruptible();
231 if (result.isSuccess()) {
232 value = 0;
233 final R66Result r66result = result.getResult();
234 final ValidPacket info = (ValidPacket) r66result.getOther();
235 logger.warn(Messages.getString("Message.11") +
236 Messages.getString("RequestInformation.Success") +
237 info.getSheader());
238 if (!OutputFormat.isQuiet()) {
239 SysErrLogger.FAKE_LOGGER.sysout(Messages.getString("Message.11") +
240 Messages.getString(
241 "RequestInformation.Success") +
242 info.getSheader());
243 }
244 } else {
245 value = 2;
246 logger.error(Messages.getString("Message.11") +
247 Messages.getString("RequestInformation.Failure") +
248
249 result.getResult());
250 if (!OutputFormat.isQuiet()) {
251 SysErrLogger.FAKE_LOGGER.sysout(Messages.getString("Message.11") +
252 Messages.getString(
253 "RequestInformation.Failure") +
254
255 result.getResult());
256 }
257 }
258 } finally {
259 if (!WaarpSystemUtil.isJunit()) {
260 if (networkTransaction != null) {
261 networkTransaction.closeAll();
262 }
263 if (admin != null) {
264 admin.close();
265 }
266 WaarpSystemUtil.systemExit(value);
267 }
268 }
269 }
270
271 }