com.google.common.net.InetAddresses Scala Examples

The following examples show how to use com.google.common.net.InetAddresses. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: Ipv6AddressJvmTest.scala    From ip4s   with Apache License 2.0 5 votes vote down vote up
package com.comcast.ip4s

import com.google.common.net.InetAddresses
import java.net.{InetAddress, Inet6Address}
import org.scalacheck.{Arbitrary, Gen}

import Arbitraries._

class Ipv6AddressJvmTest extends BaseTestSuite {
  implicit override val generatorDrivenConfig =
    PropertyCheckConfiguration(minSuccessful = 10000)

  "Ipv6Address" should {
    "support converting to string form".which {
      "roundtrips through strings" in {
        forAll(Gen.listOfN(16, Arbitrary.arbitrary[Byte])) { bytesList =>
          if (bytesList.size == 16) {
            val bytes = bytesList.toArray
            val str =
              InetAddresses.toAddrString(InetAddress.getByAddress(bytes))
            Ipv6Address(str).map(_.toString) shouldBe Some(str)
          }
        }
      }
      "follows RFC5952" in {
        forAll(Gen.listOfN(16, Arbitrary.arbitrary[Byte])) { bytesList =>
          if (bytesList.size == 16) {
            val bytes = bytesList.toArray
            val expected =
              InetAddresses.toAddrString(InetAddress.getByAddress(bytes))
            Ipv6Address.fromBytes(bytes).map(_.toString) shouldBe Some(expected)
          }
        }
      }
    }
    "support conversion to Inet6Address" in {
      forAll { (ip: Ipv6Address) => ip.toInetAddress shouldBe an[Inet6Address] }
    }
  }
} 
Example 2
Source File: GRPCServer.scala    From NSDb   with Apache License 2.0 5 votes vote down vote up
package io.radicalbit.nsdb.client.rpc

import java.net.InetSocketAddress

import com.google.common.net.InetAddresses
import io.grpc.Server
import io.grpc.netty.NettyServerBuilder
import io.radicalbit.nsdb.rpc.health.HealthGrpc
import io.radicalbit.nsdb.rpc.health.HealthGrpc.Health
import io.radicalbit.nsdb.rpc.init.InitMetricGrpc
import io.radicalbit.nsdb.rpc.init.InitMetricGrpc.InitMetric
import io.radicalbit.nsdb.rpc.restore.RestoreGrpc
import io.radicalbit.nsdb.rpc.restore.RestoreGrpc.Restore
import io.radicalbit.nsdb.rpc.service.NSDBServiceCommandGrpc.NSDBServiceCommand
import io.radicalbit.nsdb.rpc.service.NSDBServiceSQLGrpc.NSDBServiceSQL
import io.radicalbit.nsdb.rpc.service.{NSDBServiceCommandGrpc, NSDBServiceSQLGrpc}
import io.radicalbit.nsdb.sql.parser.SQLStatementParser

import scala.concurrent.ExecutionContext
import scala.util.Try


trait GRPCServer {

  protected[this] def executionContextExecutor: ExecutionContext

  protected[this] def interface: String

  protected[this] def port: Int

  protected[this] def serviceSQL: NSDBServiceSQL

  protected[this] def serviceCommand: NSDBServiceCommand

  protected[this] def initMetricService: InitMetric

  protected[this] def health: Health

  protected[this] def restore: Restore

  protected[this] def parserSQL: SQLStatementParser

  sys.addShutdownHook {
    if (!server.isTerminated) {
      System.err.println(s"Shutting down gRPC server at interface $interface and port $port since JVM is shutting down")
      stop()
      System.err.println(s"Server at interface $interface and port $port shut down")
    }
  }

  lazy val server: Server = NettyServerBuilder
    .forAddress(new InetSocketAddress(InetAddresses.forString(interface), port))
    .addService(NSDBServiceSQLGrpc.bindService(serviceSQL, executionContextExecutor))
    .addService(NSDBServiceCommandGrpc.bindService(serviceCommand, executionContextExecutor))
    .addService(InitMetricGrpc.bindService(initMetricService, executionContextExecutor))
    .addService(HealthGrpc.bindService(health, executionContextExecutor))
    .addService(RestoreGrpc.bindService(restore, executionContextExecutor))
    .build

  def start(): Try[Server] = Try(server.start())

  def stop(): Unit = server.shutdownNow().awaitTermination()
}