1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package openr66.client;
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.context.authentication.R66Auth;
34 import openr66.database.DbConstant;
35 import openr66.database.data.DbHostAuth;
36 import openr66.protocol.configuration.Configuration;
37 import openr66.protocol.exception.OpenR66ProtocolPacketException;
38 import openr66.protocol.localhandler.LocalChannelReference;
39 import openr66.protocol.localhandler.packet.TestPacket;
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
50
51
52
53 public class Message implements Runnable {
54
55
56
57 private static GgInternalLogger logger;
58
59 final private NetworkTransaction networkTransaction;
60
61 final private R66Future future;
62
63 private final String requested;
64
65 private final DbHostAuth hostAuth;
66
67 final private TestPacket testPacket;
68
69 static String srequested = null;
70 static String smessage = "MESSAGE";
71
72
73
74
75
76
77 protected static boolean getParams(String []args) {
78 if (args.length < 5) {
79 logger
80 .error("Needs 5 arguments:\n" +
81 " the XML client configuration file,\n" +
82 " '-to' the remoteHost Id,\n" +
83 " '-msg' the message\n");
84 return false;
85 }
86 if (! FileBasedConfiguration
87 .setClientConfigurationFromXml(Configuration.configuration, args[0])) {
88 logger
89 .error("Needs a correct configuration file as first argument");
90 return false;
91 }
92 for (int i = 1; i < args.length; i++) {
93 if (args[i].equalsIgnoreCase("-to")) {
94 i++;
95 srequested = args[i];
96 } else if (args[i].equalsIgnoreCase("-msg")) {
97 i++;
98 smessage = args[i];
99 }
100 }
101 if (srequested == null) {
102 logger.error("Requested HostId must be set");
103 return false;
104 }
105 return true;
106 }
107
108
109 public Message(NetworkTransaction networkTransaction,
110 R66Future future, String requested, TestPacket packet) {
111 if (logger == null) {
112 logger = GgInternalLoggerFactory.getLogger(Message.class);
113 }
114 this.networkTransaction = networkTransaction;
115 this.future = future;
116 this.requested = requested;
117 testPacket = packet;
118 this.hostAuth = null;
119 }
120
121 public Message(NetworkTransaction networkTransaction,
122 R66Future future, DbHostAuth hostAuth, TestPacket packet) {
123 if (logger == null) {
124 logger = GgInternalLoggerFactory.getLogger(Message.class);
125 }
126 this.networkTransaction = networkTransaction;
127 this.future = future;
128 this.requested = null;
129 testPacket = packet;
130 this.hostAuth = hostAuth;
131 }
132
133 public void run() {
134 if (logger == null) {
135 logger = GgInternalLoggerFactory.getLogger(
136 Message.class);
137 }
138
139 DbHostAuth host = null;
140 if (hostAuth == null) {
141 host = R66Auth.getServerAuth(DbConstant.admin.session,
142 requested);
143 } else {
144 host = hostAuth;
145 }
146 if (host == null) {
147 logger.debug("Requested host cannot be found: "+requested);
148 R66Result result = new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
149 this.future.setResult(result);
150 this.future.cancel();
151 return;
152 }
153 if (host.isClient()) {
154 logger.error("Requested host is a client and cannot be requested: "+requested);
155 R66Result result = new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
156 this.future.setResult(result);
157 this.future.cancel();
158 return;
159 }
160 SocketAddress socketAddress = host.getSocketAddress();
161 boolean isSSL = host.isSsl();
162 LocalChannelReference localChannelReference = null;
163 localChannelReference = networkTransaction
164 .createConnectionWithRetry(socketAddress, isSSL, future);
165 socketAddress = null;
166 if (localChannelReference == null) {
167 logger.debug("Cannot connect to server: "+requested);
168 R66Result result = new R66Result(null, true, ErrorCode.ConnectionImpossible, null);
169 this.future.setResult(result);
170 this.future.cancel();
171 return;
172 }
173 localChannelReference.sessionNewState(R66FiniteDualStates.TEST);
174 try {
175 ChannelUtils.writeAbstractLocalPacket(localChannelReference, testPacket, false);
176 } catch (OpenR66ProtocolPacketException e) {
177 future.setResult(null);
178 future.setFailure(e);
179 Channels.close(localChannelReference.getLocalChannel());
180 return;
181 }
182 }
183
184 public static void main(String[] args) {
185 InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(null));
186 if (logger == null) {
187 logger = GgInternalLoggerFactory.getLogger(Message.class);
188 }
189 if (args.length < 5) {
190 logger
191 .error("Needs 5 arguments:\n" +
192 " the XML client configuration file,\n" +
193 " '-to' the remoteHost Id,\n" +
194 " '-msg' the message\n");
195 System.exit(1);
196 }
197 if (! getParams(args)) {
198 logger.error("Wrong initialization");
199 if (DbConstant.admin != null && DbConstant.admin.isConnected) {
200 DbConstant.admin.close();
201 }
202 ChannelUtils.stopLogger();
203 System.exit(1);
204 }
205 NetworkTransaction networkTransaction = null;
206 int value = 3;
207 try {
208 Configuration.configuration.pipelineInit();
209 networkTransaction = new NetworkTransaction();
210 R66Future result = new R66Future(true);
211 TestPacket packet = new TestPacket("MSG", smessage, 100);
212 Message transaction = new Message(
213 networkTransaction, result, srequested,
214 packet);
215 transaction.run();
216 result.awaitUninterruptibly();
217 if (result.isSuccess()) {
218 value = 0;
219 R66Result r66result = result.getResult();
220 ValidPacket info = (ValidPacket) r66result.other;
221 logger.warn("Test Message\n SUCCESS\n "+info.getSheader());
222 } else {
223 value = 2;
224 logger.error("Test Message\n FAILURE\n " +
225 result.getResult().toString());
226 }
227 } finally {
228 if (networkTransaction != null) {
229 networkTransaction.closeAll();
230 }
231 if (DbConstant.admin != null) {
232 DbConstant.admin.close();
233 }
234 System.exit(value);
235 }
236 }
237
238 }