NebulaGraph Java Client  release-3.8
AbstractMetaClient.java
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.meta;
7 
8 import com.facebook.thrift.protocol.THeaderProtocol;
9 import com.facebook.thrift.protocol.TProtocol;
10 import com.facebook.thrift.transport.THeaderTransport;
11 import com.facebook.thrift.transport.TTransport;
12 import com.google.common.base.Preconditions;
13 import com.google.common.net.InetAddresses;
14 import com.google.common.net.InternetDomainName;
15 import com.vesoft.nebula.client.graph.data.HostAddress;
16 import java.io.Serializable;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.List;
20 
21 public class AbstractMetaClient implements Serializable {
22  protected final List<HostAddress> addresses;
23  protected final int connectionRetry;
24  protected final int executionRetry;
25  protected final int timeout;
26 
27  protected THeaderProtocol protocol;
28  protected THeaderTransport transport;
29 
30  public AbstractMetaClient(List<HostAddress> addresses, int timeout,
31  int connectionRetry, int executionRetry) throws UnknownHostException {
32  Preconditions.checkArgument(timeout > 0);
33  Preconditions.checkArgument(connectionRetry >= 0);
34  Preconditions.checkArgument(executionRetry >= 0);
35  for (HostAddress address : addresses) {
36  String host = InetAddress.getByName(address.getHost()).getHostAddress();
37  int port = address.getPort();
38  // check if the address is a valid ip, uri address or domain name and port is valid
39  if (!(InetAddresses.isInetAddress(host)
40  || InetAddresses.isUriInetAddress(host)
41  || InternetDomainName.isValid(host))
42  || (port <= 0 || port >= 65535)) {
43  throw new IllegalArgumentException(String.format("%s:%d is not a valid address",
44  host, port));
45  }
46  }
47 
48  this.addresses = addresses;
49  this.timeout = timeout;
50  this.connectionRetry = connectionRetry;
51  this.executionRetry = executionRetry;
52  }
53 
54  public int getConnectionRetry() {
55  return connectionRetry;
56  }
57 
58  public int getExecutionRetry() {
59  return executionRetry;
60  }
61 
62  public int getTimeout() {
63  return timeout;
64  }
65 }