@@ -13,6 +13,7 @@ import io.grpc.stub.StreamObserver
1313import okhttp3.OkHttpClient
1414import okhttp3.Request
1515import org.apache.commons.lang3.StringUtils
16+ import org.apache.commons.lang3.math.NumberUtils
1617import org.slf4j.LoggerFactory
1718import java.util.concurrent.CompletableFuture
1819import java.util.concurrent.CountDownLatch
@@ -39,7 +40,7 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
3940
4041// private var finishLatch:CountDownLatch? = null
4142
42- private fun discoverHostieIpa (): String {
43+ private fun discoverHostieIp ():Pair < String , String > {
4344
4445 val token = puppetOptions!! .token
4546
@@ -51,7 +52,11 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
5152 response ->
5253 val string = response.body!! .string()
5354 val readValue = JsonUtils .readValue<Map <String , String >>(string)
54- return readValue[" ip" ] ? : error(" " )
55+
56+ val ip = readValue[" ip" ] ? : error(" cannot get ip by token, check token" );
57+ val port = readValue[" port" ] ? : " 8788"
58+
59+ return Pair (ip,port)
5560 }
5661 }
5762
@@ -130,16 +135,26 @@ class GrpcPuppet(puppetOptions: PuppetOptions) : Puppet(puppetOptions) {
130135 }
131136
132137 private fun startGrpcClient (): Future <Void > {
133- var endPoint = puppetOptions?.endPoint
134- if (StringUtils .isEmpty(endPoint)) {
135- endPoint = discoverHostieIpa()
138+ val endPoint = puppetOptions?.endPoint
139+ val discoverHostieIp: Pair <String ,String >
140+ discoverHostieIp = if (StringUtils .isEmpty(endPoint)) {
141+ discoverHostieIp()
142+ }else {
143+ val split = StringUtils .split(endPoint, " :" )
144+ if (split.size == 1 ) {
145+ Pair (split[0 ]," 8788" )
146+ }else {
147+ Pair (split[0 ],split[1 ])
148+ }
149+
136150 }
137151
138- if (StringUtils .isEmpty(endPoint) || StringUtils .equals(endPoint, " 0.0.0.0" )) {
139- throw Exception ()
152+ if (StringUtils .isEmpty(endPoint) || StringUtils .equals(discoverHostieIp.first, " 0.0.0.0" )) {
153+ log.error(" cannot get ip by token, check token" )
154+ throw Exception (" cannot get ip by token, check token" )
140155 }
141156 val newFixedThreadPool = newFixedThreadPool(16 )
142- channel = ManagedChannelBuilder .forAddress(endPoint, GRPC_PROT ).usePlaintext().executor(newFixedThreadPool).build()
157+ channel = ManagedChannelBuilder .forAddress(discoverHostieIp.first, NumberUtils .toInt(discoverHostieIp.second) ).usePlaintext().executor(newFixedThreadPool).build()
143158
144159 grpcClient = PuppetGrpc .newBlockingStub(channel)
145160 grpcAsyncClient = PuppetGrpc .newStub(channel)
0 commit comments