mirror of
https://github.com/fluencelabs/scala-multiaddr
synced 2025-03-14 23:30:50 +00:00
add travis, add custom grpc protocol to list, small refactoring and bug fixes, add strict int type for parameters of TCP and UDP protocol
This commit is contained in:
parent
22e7a9f3c3
commit
0480ab3710
19
.travis.yml
Normal file
19
.travis.yml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
sudo: required
|
||||||
|
|
||||||
|
language: scala
|
||||||
|
scala:
|
||||||
|
- 2.12.5
|
||||||
|
jdk:
|
||||||
|
- oraclejdk8
|
||||||
|
|
||||||
|
# These directories are cached to S3 at the end of the build
|
||||||
|
cache:
|
||||||
|
directories:
|
||||||
|
- $HOME/.ivy2/cache
|
||||||
|
- $HOME/.sbt/boot
|
||||||
|
- $HOME/.sbt/launchers
|
||||||
|
|
||||||
|
before_cache:
|
||||||
|
# Tricks to avoid unnecessary cache updates
|
||||||
|
- find $HOME/.sbt -name "*.lock" | xargs rm
|
||||||
|
- find $HOME/.ivy2 -name "ivydata-*.properties" | xargs rm
|
@ -39,6 +39,7 @@ lazy val `scala-multiaddr-core` = crossProject(JVMPlatform, JSPlatform)
|
|||||||
commons,
|
commons,
|
||||||
libraryDependencies ++= Seq(
|
libraryDependencies ++= Seq(
|
||||||
"com.beachape" %%% "enumeratum" % "1.5.13",
|
"com.beachape" %%% "enumeratum" % "1.5.13",
|
||||||
|
// "org.typelevel" %%% "cats-core" % "1.1.0",
|
||||||
"org.scalatest" %%% "scalatest" % "3.0.+" % Test
|
"org.scalatest" %%% "scalatest" % "3.0.+" % Test
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -17,16 +17,18 @@
|
|||||||
|
|
||||||
package fluence.multiaddr
|
package fluence.multiaddr
|
||||||
|
|
||||||
case class Multiaddr private(stringAddress: String, protocolsWithParameters: List[(Protocol, Option[String])]) {
|
import fluence.multiaddr.Multiaddr.ErrorMessage
|
||||||
override def toString: String = stringAddress
|
|
||||||
|
case class Multiaddr private(address: String, protoParameters: List[ProtoParameter]) {
|
||||||
|
override def toString: String = address
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps a given Multiaddr, returning the resulting joined Multiaddr.
|
* Wraps a given Multiaddr, returning the resulting joined Multiaddr.
|
||||||
*
|
*
|
||||||
* @return new joined Multiaddr
|
* @return new joined Multiaddr
|
||||||
*/
|
*/
|
||||||
def encapsulate(addr: Multiaddr): Either[Throwable, Multiaddr] = {
|
def encapsulate(addr: Multiaddr): Either[ErrorMessage, Multiaddr] = {
|
||||||
Multiaddr(stringAddress + addr.toString)
|
Multiaddr(address + addr.toString)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,20 +36,22 @@ case class Multiaddr private(stringAddress: String, protocolsWithParameters: Lis
|
|||||||
*
|
*
|
||||||
* @return decapsulated Multiaddr
|
* @return decapsulated Multiaddr
|
||||||
*/
|
*/
|
||||||
def decapsulate(addr: Multiaddr): Either[Throwable, Multiaddr] = {
|
def decapsulate(addr: Multiaddr): Either[ErrorMessage, Multiaddr] = {
|
||||||
val strAddr = addr.toString
|
val strAddr = addr.toString
|
||||||
val lastIndex = stringAddress.lastIndexOf(strAddr)
|
val lastIndex = address.lastIndexOf(strAddr)
|
||||||
if (lastIndex < 0)
|
if (lastIndex < 0)
|
||||||
Right(this.copy())
|
Right(this)
|
||||||
else
|
else
|
||||||
Multiaddr(stringAddress.slice(0, lastIndex))
|
Multiaddr(address.slice(0, lastIndex))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
object Multiaddr {
|
object Multiaddr {
|
||||||
|
|
||||||
def apply(addr: String): Either[Throwable, Multiaddr] = MultiaddrParser.parse(addr).map {
|
type ErrorMessage = String
|
||||||
case (trimmed, protocols) => new Multiaddr(trimmed, protocols)
|
|
||||||
|
def apply(addr: String): Either[ErrorMessage, Multiaddr] = MultiaddrParser.parse(addr).map {
|
||||||
|
case (trimmed, protoParameters) => new Multiaddr(trimmed, protoParameters)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,18 +17,22 @@
|
|||||||
|
|
||||||
package fluence.multiaddr
|
package fluence.multiaddr
|
||||||
|
|
||||||
|
import fluence.multiaddr.Multiaddr.ErrorMessage
|
||||||
|
import fluence.multiaddr.Protocol._
|
||||||
|
|
||||||
import scala.annotation.tailrec
|
import scala.annotation.tailrec
|
||||||
|
import scala.util.Try
|
||||||
|
|
||||||
private[multiaddr] object MultiaddrParser {
|
private[multiaddr] object MultiaddrParser {
|
||||||
|
|
||||||
def parse(addr: String): Either[Throwable, (String, List[(Protocol, Option[String])])] = {
|
def parse(addr: String): Either[ErrorMessage, (String, List[ProtoParameter])] = {
|
||||||
if (!addr.startsWith("/")) {
|
if (!addr.startsWith("/")) {
|
||||||
Left(new IllegalArgumentException("Address must be started with '/'."))
|
Left("Address must be started with '/'.")
|
||||||
} else {
|
} else {
|
||||||
val parts = addr.stripPrefix("/").stripSuffix("/").split("/").toList
|
val parts = addr.stripPrefix("/").stripSuffix("/").split("/").toList
|
||||||
|
|
||||||
if (parts.isEmpty) {
|
if (parts.isEmpty) {
|
||||||
Left(new IllegalArgumentException("Address must be non-empty."))
|
Left("Address must be non-empty.")
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
parsePrepared(parts).map(protocols ⇒ (addr.stripSuffix("/"), protocols))
|
parsePrepared(parts).map(protocols ⇒ (addr.stripSuffix("/"), protocols))
|
||||||
@ -36,31 +40,53 @@ private[multiaddr] object MultiaddrParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def parsePrepared(list: List[String]): Either[Throwable, List[(Protocol, Option[String])]] = {
|
private def parseParameter(parameter: String, protocol: Protocol): Either[String, ProtoParameter] = {
|
||||||
|
protocol match {
|
||||||
|
case TCP | UDP ⇒
|
||||||
|
Try(parameter.toInt).toEither.right
|
||||||
|
.map(n ⇒ IntProtoParameter(protocol, n))
|
||||||
|
.left
|
||||||
|
.map(_ ⇒ s"Parameter for protocol $protocol must be a number.")
|
||||||
|
case _ ⇒
|
||||||
|
Right(StringProtoParameter(protocol, parameter))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private def parsePrepared(list: List[String]): Either[ErrorMessage, List[ProtoParameter]] = {
|
||||||
|
|
||||||
@tailrec
|
@tailrec
|
||||||
def parseRec(
|
def parseRec(
|
||||||
list: List[String],
|
list: List[String],
|
||||||
res: Either[Throwable, List[(Protocol, Option[String])]]
|
accum: Either[ErrorMessage, List[ProtoParameter]]
|
||||||
): Either[Throwable, List[(Protocol, Option[String])]] = {
|
): Either[ErrorMessage, List[ProtoParameter]] = {
|
||||||
list match {
|
list match {
|
||||||
case Nil ⇒ res
|
case Nil ⇒ accum
|
||||||
case head :: tail ⇒
|
case head :: tail ⇒
|
||||||
//todo per-protocol validation
|
//todo per-protocol validation
|
||||||
val protocolOp = Protocol.withNameOption(head)
|
val protocolOp = Protocol.withNameOption(head)
|
||||||
|
|
||||||
protocolOp match {
|
protocolOp match {
|
||||||
case None ⇒
|
case None ⇒
|
||||||
Left(new IllegalArgumentException(s"There is no protocol with name '$head'."))
|
Left(s"There is no protocol with name '$head'.")
|
||||||
case Some(protocol) ⇒
|
case Some(protocol) ⇒
|
||||||
protocol.size match {
|
protocol.size match {
|
||||||
case 0 ⇒ parseRec(tail, res.map(els ⇒ els :+ (protocol, None)))
|
case 0 ⇒
|
||||||
|
parseRec(tail, accum.map(els ⇒ els :+ EmptyProtoParameter(protocol)))
|
||||||
case _ ⇒
|
case _ ⇒
|
||||||
tail match {
|
tail match {
|
||||||
case Nil ⇒
|
case Nil ⇒
|
||||||
Left(new IllegalArgumentException(s"There is no parameter for protocol with name '$head'."))
|
Left(s"There is no parameter for protocol with name '$head'.")
|
||||||
case innerHead :: innerTail ⇒
|
case parameter :: innerTail ⇒
|
||||||
parseRec(innerTail, res.map(els ⇒ els :+ (protocol, Some(innerHead))))
|
val partialResult =
|
||||||
|
for {
|
||||||
|
elements ← accum
|
||||||
|
parameter ← parseParameter(parameter, protocol)
|
||||||
|
} yield elements :+ parameter
|
||||||
|
|
||||||
|
parseRec(
|
||||||
|
innerTail,
|
||||||
|
partialResult
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
25
core/src/main/scala/fluence/multiaddr/ProtoParameter.scala
Normal file
25
core/src/main/scala/fluence/multiaddr/ProtoParameter.scala
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Fluence Labs Limited
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package fluence.multiaddr
|
||||||
|
|
||||||
|
sealed trait ProtoParameter
|
||||||
|
|
||||||
|
//todo generalize parameters per protocol type
|
||||||
|
case class EmptyProtoParameter(protocol: Protocol) extends ProtoParameter
|
||||||
|
case class StringProtoParameter(protocol: Protocol, parameter: String) extends ProtoParameter
|
||||||
|
case class IntProtoParameter(protocol: Protocol, parameter: Int) extends ProtoParameter
|
@ -61,4 +61,7 @@ object Protocol extends Enum[Protocol] {
|
|||||||
case object P2PWebrtcStar extends Protocol(275, 0, "p2p-webrtc-star")
|
case object P2PWebrtcStar extends Protocol(275, 0, "p2p-webrtc-star")
|
||||||
case object P2PWebrtcDirect extends Protocol(276, 0, "p2p-webrtc-direct")
|
case object P2PWebrtcDirect extends Protocol(276, 0, "p2p-webrtc-direct")
|
||||||
case object P2PCircuit extends Protocol(290, 0, "p2p-circuit")
|
case object P2PCircuit extends Protocol(290, 0, "p2p-circuit")
|
||||||
|
|
||||||
|
//custom
|
||||||
|
case object GRPC extends Protocol(1001, 0, "grpc")
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ class MultiaddrParseSpec extends WordSpec with Matchers {
|
|||||||
"throw exception if there is no leading '/'" in {
|
"throw exception if there is no leading '/'" in {
|
||||||
val m = Multiaddr("ip4/127.0.0.1/tcp/123")
|
val m = Multiaddr("ip4/127.0.0.1/tcp/123")
|
||||||
m.isLeft shouldBe true
|
m.isLeft shouldBe true
|
||||||
m.left.get.getMessage shouldBe "Address must be started with '/'."
|
m.left.get shouldBe "Address must be started with '/'."
|
||||||
}
|
}
|
||||||
|
|
||||||
"parse correct multiaddresses right" in {
|
"parse correct multiaddresses right" in {
|
||||||
@ -35,27 +35,33 @@ class MultiaddrParseSpec extends WordSpec with Matchers {
|
|||||||
val m1Either = Multiaddr(addr1)
|
val m1Either = Multiaddr(addr1)
|
||||||
m1Either.isRight shouldBe true
|
m1Either.isRight shouldBe true
|
||||||
val m1 = m1Either.right.get
|
val m1 = m1Either.right.get
|
||||||
m1.protocolsWithParameters shouldBe List((IP4, Some("127.0.0.1")), (TCP, Some("123")))
|
|
||||||
m1.stringAddress shouldBe addr1
|
m1.protoParameters shouldBe List(StringProtoParameter(IP4, "127.0.0.1"), IntProtoParameter(TCP, 123))
|
||||||
|
m1.address shouldBe addr1
|
||||||
|
|
||||||
val addr2 = "/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/5000/https"
|
val addr2 = "/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/udp/5000/https"
|
||||||
val m2Either = Multiaddr(addr2)
|
val m2Either = Multiaddr(addr2)
|
||||||
m2Either.isRight shouldBe true
|
m2Either.isRight shouldBe true
|
||||||
val m2 = m2Either.right.get
|
val m2 = m2Either.right.get
|
||||||
m2.protocolsWithParameters shouldBe List((IP6, Some("2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095")), (UDP, Some("5000")), (HTTPS, None))
|
|
||||||
m2.stringAddress shouldBe addr2
|
m2.protoParameters shouldBe List(
|
||||||
|
StringProtoParameter(IP6, "2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095"),
|
||||||
|
IntProtoParameter(UDP, 5000),
|
||||||
|
EmptyProtoParameter(HTTPS)
|
||||||
|
)
|
||||||
|
m2.address shouldBe addr2
|
||||||
}
|
}
|
||||||
|
|
||||||
"throw exception if there is no protocol" in {
|
"throw exception if there is no protocol" in {
|
||||||
val m = Multiaddr("/ip4/127.0.0.1/tc/123")
|
val m = Multiaddr("/ip4/127.0.0.1/tc/123")
|
||||||
m.isLeft shouldBe true
|
m.isLeft shouldBe true
|
||||||
m.left.get.getMessage shouldBe "There is no protocol with name 'tc'."
|
m.left.get shouldBe "There is no protocol with name 'tc'."
|
||||||
}
|
}
|
||||||
|
|
||||||
"throw exception if there is no parameter in protocol with parameter" in {
|
"throw exception if there is no parameter in protocol with parameter" in {
|
||||||
val m = Multiaddr("/ip4/127.0.0.1/tcp/")
|
val m = Multiaddr("/ip4/127.0.0.1/tcp/")
|
||||||
m.isLeft shouldBe true
|
m.isLeft shouldBe true
|
||||||
m.left.get.getMessage shouldBe "There is no parameter for protocol with name 'tcp'."
|
m.left.get shouldBe "There is no parameter for protocol with name 'tcp'."
|
||||||
}
|
}
|
||||||
|
|
||||||
"encapsulate and decapsulate correct multiaddr" in {
|
"encapsulate and decapsulate correct multiaddr" in {
|
||||||
@ -73,11 +79,17 @@ class MultiaddrParseSpec extends WordSpec with Matchers {
|
|||||||
m3Either.isRight shouldBe true
|
m3Either.isRight shouldBe true
|
||||||
val m3 = m3Either.right.get
|
val m3 = m3Either.right.get
|
||||||
|
|
||||||
val result = List((IP4, Some("127.0.0.1")), (TCP, Some("123")), (IP6, Some("2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095")), (UDP, Some("5000")), (HTTPS, None))
|
val result = List(
|
||||||
m3.protocolsWithParameters shouldBe result
|
StringProtoParameter(IP4, "127.0.0.1"),
|
||||||
m3.stringAddress shouldBe (addr1 + addr2)
|
IntProtoParameter(TCP, 123),
|
||||||
|
StringProtoParameter(IP6, "2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095"),
|
||||||
|
IntProtoParameter(UDP, 5000),
|
||||||
|
EmptyProtoParameter(HTTPS)
|
||||||
|
)
|
||||||
|
m3.protoParameters shouldBe result
|
||||||
|
m3.address shouldBe (addr1 + addr2)
|
||||||
|
|
||||||
m3.decapsulate(m2).right.get.stringAddress shouldBe addr1
|
m3.decapsulate(m2).right.get.address shouldBe addr1
|
||||||
}
|
}
|
||||||
|
|
||||||
"decapsulate correct multiaddr" in {
|
"decapsulate correct multiaddr" in {
|
||||||
@ -87,7 +99,7 @@ class MultiaddrParseSpec extends WordSpec with Matchers {
|
|||||||
val m1 = m1Either.right.get
|
val m1 = m1Either.right.get
|
||||||
|
|
||||||
val decapsulated = m1.decapsulate(Multiaddr("/sctp/5678").right.get).right.get
|
val decapsulated = m1.decapsulate(Multiaddr("/sctp/5678").right.get).right.get
|
||||||
decapsulated.stringAddress shouldBe "/ip4/127.0.0.1/udp/1234"
|
decapsulated.address shouldBe "/ip4/127.0.0.1/udp/1234"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user