/
NetUtils.scala
136 lines (117 loc) · 3.62 KB
/
NetUtils.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package io.iohk.scalanet
import java.net._
import java.nio.ByteBuffer
import io.iohk.decco.Codec
import io.iohk.scalanet.peergroup.{InetMultiAddress, PeerGroup, TCPPeerGroup, UDPPeerGroup}
import monix.execution.Scheduler
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.util.Random
object NetUtils {
def aRandomAddress(): InetSocketAddress = {
val s = new ServerSocket(0)
try {
new InetSocketAddress("localhost", s.getLocalPort)
} finally {
s.close()
}
}
def isListening(address: InetSocketAddress): Boolean = {
try {
new Socket(address.getHostName, address.getPort).close()
true
} catch {
case _: Exception =>
false
}
}
def isListeningUDP(address: InetSocketAddress): Boolean = {
try {
new DatagramSocket(address).close()
false
} catch {
case _: Exception =>
true
}
}
def toArray(b: ByteBuffer): Array[Byte] = {
val a = new Array[Byte](b.remaining())
b.get(a)
a
}
def withAddressInUse(testCode: InetSocketAddress => Any): Unit = {
val address = aRandomAddress()
val socket = new ServerSocket(address.getPort, 0, InetAddress.getLoopbackAddress)
try {
testCode(address)
} finally {
socket.close()
}
}
def withUDPAddressInUse(testCode: InetSocketAddress => Any): Unit = {
val socket = new DatagramSocket()
val address = socket.getLocalSocketAddress.asInstanceOf[InetSocketAddress]
try {
testCode(address)
} finally {
socket.close()
}
}
def randomBytes(n: Int): Array[Byte] = {
val a = new Array[Byte](n)
Random.nextBytes(a)
a
}
sealed trait SimpleTerminalPeerGroup
case object TcpTerminalPeerGroup extends SimpleTerminalPeerGroup
case object UdpTerminalPeerGroup extends SimpleTerminalPeerGroup
def randomTerminalPeerGroup[M](
t: SimpleTerminalPeerGroup
)(implicit scheduler: Scheduler, codec: Codec[M]): PeerGroup[InetMultiAddress, M] =
t match {
case TcpTerminalPeerGroup => randomTCPPeerGroup
case UdpTerminalPeerGroup => randomUDPPeerGroup
}
def randomTCPPeerGroup[M](implicit scheduler: Scheduler, codec: Codec[M]): TCPPeerGroup[M] = {
val pg = new TCPPeerGroup(TCPPeerGroup.Config(aRandomAddress()))
Await.result(pg.initialize().runAsync, 10 seconds)
pg
}
def randomUDPPeerGroup[M](implicit scheduler: Scheduler, codec: Codec[M]): UDPPeerGroup[M] = {
val pg = new UDPPeerGroup(UDPPeerGroup.Config(aRandomAddress()))
Await.result(pg.initialize().runAsync, 10 seconds)
pg
}
def withTwoRandomTCPPeerGroups[M](
testCode: (TCPPeerGroup[M], TCPPeerGroup[M]) => Any
)(implicit scheduler: Scheduler, codec: Codec[M]): Unit = {
val (pg1, pg2) = random2TCPPeerGroup(scheduler, codec)
try {
testCode(pg1, pg2)
} finally {
pg1.shutdown()
pg2.shutdown()
}
}
def random2TCPPeerGroup[M](implicit scheduler: Scheduler, codec: Codec[M]): (TCPPeerGroup[M], TCPPeerGroup[M]) = {
val address = aRandomAddress()
val address2 = aRandomAddress()
val pg1 = new TCPPeerGroup(TCPPeerGroup.Config(address))
val pg2 = new TCPPeerGroup(TCPPeerGroup.Config(address2))
Await.result(pg1.initialize().runAsync, 10 seconds)
Await.result(pg2.initialize().runAsync, 10 seconds)
(pg1, pg2)
}
def withTwoRandomUDPPeerGroups[M](
testCode: (UDPPeerGroup[M], UDPPeerGroup[M]) => Any
)(implicit scheduler: Scheduler, codec: Codec[M]): Unit = {
val pg1 = randomUDPPeerGroup
val pg2 = randomUDPPeerGroup
try {
testCode(pg1, pg2)
} finally {
pg1.shutdown()
pg2.shutdown()
}
}
}