Skip to content

Commit 965ffeb

Browse files
Remove JNI stuff remnants (#12)
1 parent aaf7dd7 commit 965ffeb

File tree

8 files changed

+30
-99
lines changed

8 files changed

+30
-99
lines changed

Diff for: README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ import java.nio.file._
5858

5959
val daemonDirectory: Path = ??? // pass a directory under the user home dir, computed with directories-jvm for example
6060
val lockFiles = LockFiles.under(daemonDirectory, "my-app-name\\daemon") // second argument is the Windows named pipe path (that doesn't live in the file system)
61-
val res = Lock.tryAcquire(lockFiles) { serverSocket: Either[ServerSocket, ServerSocketChannel] =>
62-
// serverSocket is a Right(…) when Java >= 16 Unix domain socket support is used,
63-
// it's Left(…) when ipcsocket JNI support is used
64-
61+
val res = Lock.tryAcquire(lockFiles) { serverSocket: ServerSocketChannel =>
6562
// you should start listening on serverSocket here, and as much as possible,
6663
// only exit this block when you are actually accepting incoming connections
6764
}

Diff for: library/src/libdaemonjvm/client/Connect.scala

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package libdaemonjvm.client
22

3-
import java.net.Socket
43
import java.nio.channels.SocketChannel
54
import java.nio.charset.StandardCharsets
65
import java.nio.file.Files
@@ -10,23 +9,19 @@ import libdaemonjvm.LockFiles
109

1110
object Connect {
1211

13-
def tryConnect(files: LockFiles): Option[Either[ConnectError, Either[Socket, SocketChannel]]] =
12+
def tryConnect(files: LockFiles): Option[Either[ConnectError, SocketChannel]] =
1413
tryConnect(files, LockProcess.default)
1514

1615
def tryConnect(
1716
files: LockFiles,
1817
proc: LockProcess
19-
): Option[Either[ConnectError, Either[Socket, SocketChannel]]] = {
20-
21-
def ifProcessRunning(pid: Int): Either[ConnectError, Either[Socket, SocketChannel]] =
22-
SocketFile.connect(files.socketPaths) match {
23-
case Left(e) =>
24-
Left(new ConnectError.ZombieFound(pid, e))
25-
case Right(s) =>
26-
Right(s)
27-
}
18+
): Option[Either[ConnectError, SocketChannel]] = {
19+
20+
def ifProcessRunning(pid: Int): Either[ConnectError, SocketChannel] =
21+
SocketFile.connect(files.socketPaths)
22+
.left.map(e => new ConnectError.ZombieFound(pid, e))
2823

29-
def ifFiles(hasLock: Boolean): Option[Either[ConnectError, Either[Socket, SocketChannel]]] = {
24+
def ifFiles(hasLock: Boolean): Option[Either[ConnectError, SocketChannel]] = {
3025
val b = Files.readAllBytes(files.pidFile)
3126

3227
// FIXME Catch malformed content errors here?

Diff for: library/src/libdaemonjvm/internal/Java16SocketHandler.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ import java.nio.file.Path
77

88
import scala.util.control.NonFatal
99
import java.nio.channels.ServerSocketChannel
10-
import java.net.ServerSocket
11-
import java.net.Socket
1210
import java.nio.file.Paths
1311

1412
import libdaemonjvm.SocketPaths
1513

1614
object Java16SocketHandler extends SocketHandler {
1715
def usesWindowsPipe: Boolean = false
1816

19-
def client(paths: SocketPaths): Either[Socket, SocketChannel] = {
17+
def client(paths: SocketPaths): SocketChannel = {
2018
val a = UnixDomainSocketAddress.of(paths.path)
2119
var s: SocketChannel = null
2220
try {
2321
s = SocketChannel.open(StandardProtocolFamily.UNIX)
2422
s.connect(a)
2523
s.finishConnect()
26-
Right(s)
24+
s
2725
}
2826
catch {
2927
case NonFatal(ex) =>
@@ -37,10 +35,10 @@ object Java16SocketHandler extends SocketHandler {
3735
}
3836
}
3937

40-
def server(paths: SocketPaths): Either[ServerSocket, ServerSocketChannel] = {
38+
def server(paths: SocketPaths): ServerSocketChannel = {
4139
val a = UnixDomainSocketAddress.of(paths.path)
4240
val s = ServerSocketChannel.open(StandardProtocolFamily.UNIX)
4341
s.bind(a)
44-
Right(s)
42+
s
4543
}
4644
}

Diff for: library/src/libdaemonjvm/internal/SocketFile.scala

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,34 @@ package libdaemonjvm.internal
33
import java.net.SocketException
44
import java.nio.channels.SocketChannel
55
import java.nio.file.Path
6-
import java.net.Socket
76

87
import libdaemonjvm.errors._
98
import libdaemonjvm.SocketPaths
109

1110
object SocketFile {
1211
def canConnect(paths: SocketPaths): Either[Throwable, Unit] = {
13-
var s: Either[Throwable, Either[Socket, SocketChannel]] = null
12+
var s: Either[Throwable, SocketChannel] = null
1413
try {
1514
s = connect(paths)
1615
s.map(_ => ())
1716
}
1817
finally if (s != null)
19-
s.toOption.foreach(_.merge.close())
18+
s.toOption.foreach(_.close())
2019
}
21-
def connect(paths: SocketPaths): Either[Throwable, Either[Socket, SocketChannel]] = {
22-
var s: Either[Socket, SocketChannel] = null
20+
def connect(paths: SocketPaths): Either[Throwable, SocketChannel] = {
21+
var s: SocketChannel = null
2322
try {
2423
s = SocketHandler.client(paths)
2524
Right(s)
2625
}
2726
catch {
2827
case e: SocketException =>
2928
if (s != null)
30-
s.merge.close()
29+
s.close()
3130
Left(e)
3231
case e: SocketExceptionLike =>
3332
if (s != null)
34-
s.merge.close()
33+
s.close()
3534
Left(e)
3635
}
3736
}

Diff for: library/src/libdaemonjvm/internal/SocketHandler.scala

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ package libdaemonjvm.internal
33
import java.nio.channels.{ServerSocketChannel, SocketChannel}
44
import java.nio.file.Path
55

6-
import java.net.Socket
7-
import java.net.ServerSocket
8-
96
import libdaemonjvm.SocketPaths
107

118
trait SocketHandler {
129
def usesWindowsPipe: Boolean
13-
def client(paths: SocketPaths): Either[Socket, SocketChannel]
14-
def server(paths: SocketPaths): Either[ServerSocket, ServerSocketChannel]
10+
def client(paths: SocketPaths): SocketChannel
11+
def server(paths: SocketPaths): ServerSocketChannel
1512
}
1613

1714
object SocketHandler {
1815
def usesWindowsPipe: Boolean =
1916
DefaultSocketHandler.default.usesWindowsPipe
20-
def client(paths: SocketPaths): Either[Socket, SocketChannel] =
17+
def client(paths: SocketPaths): SocketChannel =
2118
DefaultSocketHandler.default.client(paths)
22-
def server(paths: SocketPaths): Either[ServerSocket, ServerSocketChannel] =
19+
def server(paths: SocketPaths): ServerSocketChannel =
2320
DefaultSocketHandler.default.server(paths)
2421
}

Diff for: library/src/libdaemonjvm/server/Lock.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import java.nio.file.{Files, Path}
66

77
import libdaemonjvm.LockFiles
88
import libdaemonjvm.internal.{LockProcess, SocketFile, SocketHandler}
9-
import java.net.ServerSocket
109

1110
object Lock {
1211

1312
def tryAcquire[T](
1413
files: LockFiles
1514
)(
16-
startListening: Either[ServerSocket, ServerSocketChannel] => T
15+
startListening: ServerSocketChannel => T
1716
): Either[LockError, T] =
1817
tryAcquire(files, LockProcess.default) {
1918
val socket = SocketHandler.server(files.socketPaths)

Diff for: library/test/src/libdaemonjvm/tests/TestUtil.scala

+6-60
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import java.nio.channels.ServerSocketChannel
77
import libdaemonjvm._
88
import libdaemonjvm.internal._
99
import libdaemonjvm.server._
10-
import java.net.ServerSocket
1110
import scala.util.Properties
1211
import java.util.concurrent.CountDownLatch
1312
import java.net.Socket
@@ -49,89 +48,36 @@ object TestUtil {
4948
}
5049
def tryAcquire[T](dir: os.Path)(f: (
5150
LockFiles,
52-
Either[LockError, Either[ServerSocket, ServerSocketChannel]]
51+
Either[LockError, ServerSocketChannel]
5352
) => T): T = {
5453
val files = lockFiles(dir)
5554
tryAcquire(files) { maybeChannel =>
5655
f(files, maybeChannel)
5756
}
5857
}
59-
def tryAcquire[T](files: LockFiles)(f: Either[
60-
LockError,
61-
Either[ServerSocket, ServerSocketChannel]
62-
] => T): T =
58+
def tryAcquire[T](files: LockFiles)(f: Either[LockError, ServerSocketChannel] => T): T =
6359
tryAcquire(files, LockProcess.default)(f)
6460
def tryAcquire[T](
6561
files: LockFiles,
6662
proc: LockProcess
67-
)(f: Either[LockError, Either[ServerSocket, ServerSocketChannel]] => T): T = {
68-
var serverChannel: Either[ServerSocket, ServerSocketChannel] = null
69-
var acceptThreadOpt = Option.empty[Thread]
70-
val accepting = new CountDownLatch(1)
71-
val shouldStop = new AtomicBoolean(false)
63+
)(f: Either[LockError, ServerSocketChannel] => T): T = {
64+
var serverChannel: ServerSocketChannel = null
65+
val accepting = new CountDownLatch(1)
7266
try {
7367
val maybeServerChannel = Lock.tryAcquire(files, proc) {
7468
serverChannel = SocketHandler.server(files.socketPaths)
75-
if (Properties.isWin)
76-
// Windows named pipes seem no to accept clients unless accept is being called on the server socket
77-
acceptThreadOpt =
78-
serverChannel.left.toOption.map(acceptAndDiscard(
79-
_,
80-
accepting,
81-
() => shouldStop.get()
82-
))
83-
for (t <- acceptThreadOpt) {
84-
t.start()
85-
accepting.await()
86-
// waiting so that the accept call below effectively awaits client... :|
87-
Thread.sleep(
88-
1000L
89-
)
90-
}
9169
serverChannel
9270
}
9371
f(maybeServerChannel)
9472
}
9573
finally {
96-
shouldStop.set(true)
9774
try SocketFile.canConnect(files.socketPaths) // unblock the server thread last accept
9875
catch {
9976
case NonFatal(e) =>
10077
System.err.println(s"Ignoring $e while trying to unblock last accept")
10178
}
10279
for (channel <- Option(serverChannel))
103-
channel.merge.close()
80+
channel.close()
10481
}
10582
}
106-
107-
val acceptAndDiscardCount = new AtomicInteger
108-
def acceptAndDiscard(
109-
s: ServerSocket,
110-
accepting: CountDownLatch,
111-
shouldStop: () => Boolean
112-
): Thread =
113-
new Thread(
114-
s"libdaemonjvm-tests-accept-and-discard-${acceptAndDiscardCount.incrementAndGet()}"
115-
) {
116-
setDaemon(true)
117-
val closeCount = new AtomicInteger
118-
def closeSocket(socket: Socket): Unit = {
119-
val t = new Thread(s"$getName-close-${closeCount.incrementAndGet()}") {
120-
setDaemon(true)
121-
override def run(): Unit = {
122-
socket.close()
123-
}
124-
}
125-
t.start()
126-
}
127-
override def run(): Unit = {
128-
accepting.countDown()
129-
while (!shouldStop()) {
130-
val client = s.accept()
131-
// closing the client socket in the background, as this call seems to block a few seconds
132-
closeSocket(client)
133-
}
134-
}
135-
}
136-
13783
}

Diff for: manual/server/src/libdaemonjvm/TestServer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object TestServer {
4949
)
5050
}
5151
val files = LockFiles.under(path)
52-
Lock.tryAcquire(files)(s => runServer(() => s.fold(_.accept(), _.accept()))) match {
52+
Lock.tryAcquire(files)(s => runServer(() => s.accept())) match {
5353
case Left(e) => throw e
5454
case Right(()) =>
5555
}

0 commit comments

Comments
 (0)