本文整理汇总了Scala中java.net.NetworkInterface类的典型用法代码示例。如果您正苦于以下问题:Scala NetworkInterface类的具体用法?Scala NetworkInterface怎么用?Scala NetworkInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NetworkInterface类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ProtonConfig
//设置package包名称以及导入依赖的类
package proton.game
import java.net.NetworkInterface
import com.typesafe.config._
import scala.collection.JavaConversions._
case class ProtonConfig(file: String) {
import ConfigFactory._
lazy val config = asConfig()
private def asConfig(): Config = {
val config = load(
getClass.getClassLoader,
file,
ConfigParseOptions.defaults,
ConfigResolveOptions.defaults.setAllowUnresolved(true)
)
val ip = if (config hasPath "proton.ip") config getString "proton.ip" else getIpAddress getOrElse "127.0.0.1"
ConfigFactory.empty
.withValue("proton.ip", ConfigValueFactory fromAnyRef ip)
.withFallback(config)
.resolve
}
def getIpAddress: Option[String] = {
val interfaces = NetworkInterface.getNetworkInterfaces
val inetAddresses = interfaces.flatMap(interface => interface.getInetAddresses)
inetAddresses.find(_.isSiteLocalAddress).map(_.getHostAddress)
}
}
object ProtonConfig {
def parse(moduleName: String, args: Seq[String]): Option[ProtonConfig] = {
val parser = new scopt.OptionParser[ProtonConfig]("proton-game") {
head("proton-game-" + moduleName, "1.x")
opt[String]("file") optional() action { (fileParam, c) =>
c.copy(file = fileParam)
} text "specifies the config file to use"
}
parser.parse(args, ProtonConfig(moduleName))
}
}
示例2: ProtonConfig
//设置package包名称以及导入依赖的类
package proton.users
import java.net.NetworkInterface
import com.typesafe.config._
import scala.collection.JavaConversions._
case class ProtonConfig(file: String) {
import ConfigFactory._
lazy val config = asConfig()
private def asConfig(): Config = {
val config = load(
getClass.getClassLoader,
file,
ConfigParseOptions.defaults,
ConfigResolveOptions.defaults.setAllowUnresolved(true)
)
val ip = if (config hasPath "proton.ip") config getString "proton.ip" else getIpAddress getOrElse "127.0.0.1"
ConfigFactory.empty
.withValue("proton.ip", ConfigValueFactory fromAnyRef ip)
.withFallback(config)
.resolve
}
def getIpAddress: Option[String] = {
val interfaces = NetworkInterface.getNetworkInterfaces
val inetAddresses = interfaces.flatMap(interface => interface.getInetAddresses)
inetAddresses.find(_.isSiteLocalAddress).map(_.getHostAddress)
}
}
object ProtonConfig {
def parse(moduleName: String, args: Seq[String]): Option[ProtonConfig] = {
val parser = new scopt.OptionParser[ProtonConfig]("proton-users") {
head("proton-users", "1.x")
opt[String]("file") optional() action { (fileParam, c) =>
c.copy(file = fileParam)
} text "specifies the config file to use"
}
parser.parse(args, ProtonConfig(moduleName))
}
}
示例3: HostIP
//设置package包名称以及导入依赖的类
package hk.edu.polyu.datamining.pamap2
import java.io.{BufferedReader, InputStreamReader}
import java.net.{InetAddress, NetworkInterface, URL, UnknownHostException}
import scala.collection.JavaConversions._
import scala.language.postfixOps
object HostIP {
def load(): Option[String] = {
val interfaces = NetworkInterface.getNetworkInterfaces
val interface = interfaces find (_.getName equals "eth0")
interface flatMap { inet =>
// the docker adress should be siteLocal
inet.getInetAddresses find (_ isSiteLocalAddress) map (_ getHostAddress)
}
}
def all(): java.util.List[String] =
NetworkInterface.getNetworkInterfaces
.flatMap {
_.getInetAddresses.map(_.getHostAddress)
}
.filterNot(ip => ip.contains("%"))
.toList
def hexToString(byte: Byte): String =
byte match {
case 10 => "A"
case 11 => "B"
case 12 => "C"
case 13 => "D"
case 14 => "E"
case 15 => "F"
case x: Byte => ("0".toByte + x).toChar + ""
}
def +(a: String, b: String) = a + b
def PublicIP: String = {
val reader = new BufferedReader(new InputStreamReader(new URL("http://icanhazip.com").openStream()))
val ip = reader.readLine()
reader.close()
ip
}
def LocalIP: String = try {
InetAddress.getLocalHost.getHostAddress
} catch {
case e: UnknownHostException => "127.0.0.1"
case e: Throwable => throw e
}
}
示例4: NetworkUtil
//设置package包名称以及导入依赖的类
package com.github.cuzfrog.eft
import java.net.{InetSocketAddress, NetworkInterface, ServerSocket, Socket}
import java.io.IOException
object NetworkUtil {
def getLocalIpAddress: Seq[String] = {
import scala.collection.JavaConverters._
val enumeration = NetworkInterface.getNetworkInterfaces.asScala.toVector
val ipAddresses = enumeration.flatMap(p =>
p.getInetAddresses.asScala.toSeq
)
val address = ipAddresses.filter { address =>
val host = address.getHostAddress
host.contains(".") && !address.isLoopbackAddress
}.toList
address.map(_.getHostAddress)
}
def freeLocalPort: Int = try {
val serverSocket = new ServerSocket(0)
val port = serverSocket.getLocalPort
serverSocket.close()
port
} catch {
case e: IOException =>
throw new IllegalStateException(e)
}
def checkPortReachable(ip: String, port: Int, timeoutInMilliSec: Int = 500): Boolean = {
val s = new Socket()
try {
s.setReuseAddress(true)
val sa = new InetSocketAddress(ip, port)
s.connect(sa, timeoutInMilliSec)
s.isConnected
} catch {
case e: IOException => false
} finally {
if (s.isConnected) s.close()
}
}
}
示例5: Main
//设置package包名称以及导入依赖的类
package org.parity
import java.net.{InetAddress, NetworkInterface}
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
object Main extends App {
implicit val system = ActorSystem("MAINSYS")
implicit val materializer = ActorMaterializer()
val multicastInterface = NetworkInterface.getByInetAddress(InetAddress.getByName("127.0.0.1"))
val multicastGroup = InetAddress.getByName("224.0.0.1")
val multicastPort = 5000
val requestAddress = InetAddress.getByName("127.0.0.1")
val requestPort = 5001
val s = ParitySourceSettings(
multicastInterface,
multicastGroup,
multicastPort,
requestAddress,
requestPort)
PMDSource(settings = s, bufferSize = 1000)
.via(ParityOrderBookReconstructionFlow())
.runWith(Sink.foreach(println(_)))
}
示例6: NetUtils
//设置package包名称以及导入依赖的类
package io.hydrosphere.mist.utils
import java.net.{Inet4Address, InetAddress, NetworkInterface}
import scala.collection.JavaConverters._
object NetUtils {
def findLocalInetAddress(): InetAddress = {
val address = InetAddress.getLocalHost
if (address.isLoopbackAddress) {
val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toList
for (ni <- activeNetworkIFs) {
val addresses = ni.getInetAddresses.asScala.toList
.filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress)
if (addresses.nonEmpty) {
val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head)
val strippedAddress = InetAddress.getByAddress(addr.getAddress)
return strippedAddress
}
}
}
address
}
}
示例7: SystemInfo
//设置package包名称以及导入依赖的类
package slide
import java.net.NetworkInterface
import enums.OperatingSystem
object SystemInfo {
private val os: String = System.getProperty("os.name").toUpperCase
var operatingSystem: OperatingSystem = OperatingSystem.UNKNOWN
var systemExtension: String = ""
var chmod: String = "+x"
if (os.toUpperCase.contains("WIN")) {
chmod = ""
systemExtension = ".exe"
operatingSystem = OperatingSystem.WINDOWS
}
else if (os.toUpperCase.contains("MAC"))
operatingSystem = OperatingSystem.OSX
else
operatingSystem = OperatingSystem.NIX
def isNetworkIsAvailable: Boolean = {
val interfaces: java.util.Enumeration[NetworkInterface] = NetworkInterface.getNetworkInterfaces
while (interfaces.hasMoreElements) {
val interf: NetworkInterface = interfaces.nextElement()
if (interf.isUp && !interf.isLoopback)
return true
}
false
}
}
示例8: LocalhostTransformerInitializer
//设置package包名称以及导入依赖的类
package io.buoyant.transformer
package perHost
import com.fasterxml.jackson.annotation.JsonIgnore
import com.twitter.finagle.{Path, Stack}
import io.buoyant.namer.{NameTreeTransformer, TransformerConfig, TransformerInitializer}
import java.net.NetworkInterface
import scala.collection.JavaConverters._
class LocalhostTransformerInitializer extends TransformerInitializer {
val configClass = classOf[LocalhostTransformerConfig]
override val configId = "io.l5d.localhost"
}
class LocalhostTransformerConfig extends TransformerConfig {
@JsonIgnore
val defaultPrefix = Path.read("/io.l5d.localhost")
@JsonIgnore
override def mk(params: Stack.Params): NameTreeTransformer = {
val localIPs = for {
interface <- NetworkInterface.getNetworkInterfaces.asScala
if interface.isUp
inet <- interface.getInetAddresses.asScala
} yield inet
new SubnetLocalTransformer(prefix, localIPs.toSeq, Netmask("255.255.255.255"))
}
}
示例9: HostIP
//设置package包名称以及导入依赖的类
package org.unfairfunction.smartsox.util
import scala.collection.JavaConversions._
import java.net.NetworkInterface
object HostIP {
def load(): Option[String] = {
val interfaces = NetworkInterface.getNetworkInterfaces()
val interface = interfaces find (_.getName equals "eth0")
interface flatMap { inet =>
// the docker adress should be siteLocal
inet.getInetAddresses find (_ isSiteLocalAddress) map (_ getHostAddress)
}
}
}
示例10: getHttpAddress
//设置package包名称以及导入依赖的类
package io.corbel.http
import java.net.NetworkInterface
import com.typesafe.config.{ConfigException, Config}
import grizzled.slf4j.Logging
import io.corbel.actor.AkkaModule
trait HttpModule extends AkkaModule with Logging {
lazy val server: Server = new Server(
getHttpAddress,
config.getInt(Config.HttpPort),
httpHandler
)
private def getHttpAddress: String =
if(config.hasPath(Config.HttpAddress)) config.getString(Config.HttpAddress)
else getAddressFromInterface
private def getAddressFromInterface: String = {
try {
val interface = config.getString(Config.HttpInterface)
val networkInterface = NetworkInterface.getByName(interface)
val inetAddress = networkInterface.getInetAddresses.nextElement()
inetAddress.getHostAddress
}
catch {
case e: ConfigException.Missing =>
error(s"You must specify either ${Config.HttpAddress} or ${Config.HttpInterface} in your configuration", e)
throw e
case e: Throwable => throw e
}
}
object Config {
val HttpAddress = "http.address"
val HttpInterface = "http.interface"
val HttpPort = "http.port"
}
//module dependencies ----------
def config: Config
def httpHandler: Server.Handler
}