• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala NetworkInterface类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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))
  }
} 
开发者ID:Morgan-Stanley,项目名称:proton,代码行数:52,代码来源:ProtonConfig.scala


示例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))
  }
} 
开发者ID:Morgan-Stanley,项目名称:proton,代码行数:52,代码来源:ProtonConfig.scala


示例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
  }
} 
开发者ID:polyu-datamining-2016,项目名称:data-mining-pamap2,代码行数:57,代码来源:HostIP.scala


示例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()
    }
  }
} 
开发者ID:cuzfrog,项目名称:eft,代码行数:47,代码来源:NetworkUtil.scala


示例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(_)))
} 
开发者ID:favetelinguis,项目名称:parityakkastreams,代码行数:33,代码来源:Main.scala


示例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
  }
} 
开发者ID:Hydrospheredata,项目名称:mist,代码行数:27,代码来源:NetUtils.scala


示例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
    }
} 
开发者ID:murizaky,项目名称:dasdasd,代码行数:34,代码来源:SystemInfo.scala


示例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"))
  }

} 
开发者ID:linkerd,项目名称:linkerd,代码行数:31,代码来源:LocalhostTransformerInitializer.scala


示例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)
    }
  }
} 
开发者ID:ehudkaldor,项目名称:SmartSox,代码行数:19,代码来源:HostIP.scala


示例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


} 
开发者ID:alexdeleon,项目名称:corbel-2.0,代码行数:52,代码来源:HttpModule.scala



注:本文中的java.net.NetworkInterface类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala PrivateMethodTester类代码示例发布时间:2022-05-23
下一篇:
Scala ByteStreams类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap