@@ -32,7 +32,6 @@ public abstract class ProtoUtils {
32
32
public static final int LENGTH_OF_SIZE_MESSAGE = 5 ;
33
33
34
34
private static final int DEFAULT_INITIAL_REQUEST_SIZE = 4096 ;
35
- private static final String WELCOME = "Tarantool " ;
36
35
37
36
/**
38
37
* Reads tarantool binary protocol's packet from {@code inputStream}.
@@ -65,7 +64,7 @@ public static TarantoolPacket readPacket(InputStream inputStream, MsgPackLite ms
65
64
*
66
65
* @param bufferReader readable channel that have to be in blocking mode
67
66
* or instance of {@link ReadableViaSelectorChannel}
68
- * @param msgPackLite MessagePack decoder instance
67
+ * @param msgPackLite MessagePack decoder instance
69
68
*
70
69
* @return tarantool binary protocol message wrapped by instance of {@link TarantoolPacket}
71
70
*
@@ -120,9 +119,9 @@ public static TarantoolPacket readPacket(ReadableByteChannel bufferReader, MsgPa
120
119
/**
121
120
* Connects to a tarantool node described by {@code socket}. Performs an authentication if required
122
121
*
123
- * @param socket a socket channel to a tarantool node
124
- * @param username auth username
125
- * @param password auth password
122
+ * @param socket a socket channel to a tarantool node
123
+ * @param username auth username
124
+ * @param password auth password
126
125
* @param msgPackLite MessagePack encoder / decoder instance
127
126
*
128
127
* @return object with information about a connection/
@@ -141,8 +140,7 @@ public static TarantoolGreeting connect(Socket socket,
141
140
inputStream .read (inputBytes );
142
141
143
142
String firstLine = new String (inputBytes );
144
- assertCorrectWelcome (firstLine , socket .getRemoteSocketAddress ());
145
- String serverVersion = firstLine .substring (WELCOME .length ());
143
+ TarantoolGreeting greeting = parseGreetingLine (firstLine , socket .getRemoteSocketAddress ());
146
144
147
145
inputStream .read (inputBytes );
148
146
String salt = new String (inputBytes );
@@ -157,15 +155,15 @@ public static TarantoolGreeting connect(Socket socket,
157
155
assertNoErrCode (responsePacket );
158
156
}
159
157
160
- return new TarantoolGreeting ( serverVersion ) ;
158
+ return greeting ;
161
159
}
162
160
163
161
/**
164
162
* Connects to a tarantool node described by {@code socketChannel}. Performs an authentication if required.
165
163
*
166
- * @param channel a socket channel to tarantool node. The channel have to be in blocking mode
167
- * @param username auth username
168
- * @param password auth password
164
+ * @param channel a socket channel to tarantool node. The channel have to be in blocking mode
165
+ * @param username auth username
166
+ * @param password auth password
169
167
* @param msgPackLite MessagePack encoder / decoder instance
170
168
*
171
169
* @return object with information about a connection/
@@ -182,10 +180,9 @@ public static TarantoolGreeting connect(SocketChannel channel,
182
180
channel .read (welcomeBytes );
183
181
184
182
String firstLine = new String (welcomeBytes .array ());
185
- assertCorrectWelcome (firstLine , channel .getRemoteAddress ());
186
- final String serverVersion = firstLine .substring (WELCOME .length ());
183
+ TarantoolGreeting greeting = parseGreetingLine (firstLine , channel .getRemoteAddress ());
187
184
188
- ((Buffer )welcomeBytes ).clear ();
185
+ ((Buffer ) welcomeBytes ).clear ();
189
186
channel .read (welcomeBytes );
190
187
String salt = new String (welcomeBytes .array ());
191
188
@@ -197,17 +194,7 @@ public static TarantoolGreeting connect(SocketChannel channel,
197
194
assertNoErrCode (authResponse );
198
195
}
199
196
200
- return new TarantoolGreeting (serverVersion );
201
- }
202
-
203
- private static void assertCorrectWelcome (String firstLine , SocketAddress remoteAddress ) {
204
- if (!firstLine .startsWith (WELCOME )) {
205
- String errMsg = "Failed to connect to node " + remoteAddress .toString () +
206
- ": Welcome message should starts with tarantool but starts with '" +
207
- firstLine +
208
- "'" ;
209
- throw new CommunicationException (errMsg , new IllegalStateException ("Invalid welcome packet" ));
210
- }
197
+ return greeting ;
211
198
}
212
199
213
200
private static void assertNoErrCode (TarantoolPacket authResponse ) {
@@ -331,4 +318,13 @@ ByteBuffer toByteBuffer() {
331
318
332
319
}
333
320
321
+ private static TarantoolGreeting parseGreetingLine (String line , SocketAddress remoteAddress ) {
322
+ try {
323
+ return new TarantoolGreeting (line );
324
+ } catch (Exception cause ) {
325
+ String message = "Failed to connect to node " + remoteAddress .toString ();
326
+ throw new CommunicationException (message , cause );
327
+ }
328
+ }
329
+
334
330
}
0 commit comments