forked from akhikhl/gretty
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from f4lco/bugfix/atomic-random-free-port
Choose random free ports atomically
- Loading branch information
Showing
26 changed files
with
210 additions
and
415 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apply from: rootProject.file('common.gradle') | ||
|
||
dependencies { | ||
compile "org.codehaus.groovy:groovy:$groovy_version" | ||
compile "org.slf4j:slf4j-api:$slf4j_version" | ||
} |
10 changes: 10 additions & 0 deletions
10
libs/gretty-common/src/main/groovy/org/akhikhl/gretty/PortUtils.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.akhikhl.gretty | ||
|
||
import groovy.transform.CompileStatic | ||
import groovy.transform.TypeCheckingMode | ||
|
||
@CompileStatic(TypeCheckingMode.SKIP) | ||
class PortUtils { | ||
|
||
static final int RANDOM_FREE_PORT = -1 | ||
} |
98 changes: 98 additions & 0 deletions
98
libs/gretty-common/src/main/groovy/org/akhikhl/gretty/ServiceProtocol.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Gretty | ||
* | ||
* Copyright (C) 2013-2015 Andrey Hihlovskiy and contributors. | ||
* | ||
* See the file "LICENSE" for copying and usage permission. | ||
* See the file "CONTRIBUTORS" for complete list of contributors. | ||
*/ | ||
package org.akhikhl.gretty | ||
|
||
import groovy.util.logging.Slf4j | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.util.concurrent.CompletableFuture | ||
|
||
@Slf4j | ||
final class ServiceProtocol { | ||
|
||
static Reader createReader(int port = 0) { | ||
final ServerSocket serverSocket = new ServerSocket(port, 1, InetAddress.localHost) | ||
return new Reader(serverSocket) | ||
} | ||
|
||
static class Reader implements Closeable { | ||
private final ServerSocket serverSocket | ||
|
||
private Reader(final ServerSocket serverSocket) { | ||
this.serverSocket = serverSocket | ||
} | ||
|
||
CompletableFuture<String> readMessageAsync() { | ||
return CompletableFuture.supplyAsync(this.&readMessage) | ||
} | ||
|
||
synchronized String readMessage() { | ||
def data = new StringBuilder() | ||
Socket acceptSocket = serverSocket.accept() | ||
try { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(acceptSocket.getInputStream(), StandardCharsets.UTF_8)) | ||
while(true) { | ||
String line = reader.readLine() | ||
if(line == '<<EOF>>') | ||
break | ||
data << line | ||
} | ||
} finally { | ||
acceptSocket.close() | ||
} | ||
log.debug 'ServiceProtocol.readMessageFromServerSocket({}) -> {}', serverSocket.getLocalPort(), data | ||
return data | ||
} | ||
|
||
int getPort() { | ||
return serverSocket.localPort | ||
} | ||
|
||
@Override | ||
void close() { | ||
serverSocket.close() | ||
} | ||
} | ||
|
||
static Writer createWriter(int port) { | ||
return new Writer(port) | ||
} | ||
|
||
static class Writer { | ||
private final int port | ||
|
||
private Writer(final int port) { | ||
this.port = port | ||
} | ||
|
||
def write(final String command) { | ||
log.debug 'ServiceProtocol.send({}, {})', port, command | ||
Socket s = new Socket(InetAddress.localHost, port) | ||
try { | ||
OutputStream out = s.getOutputStream() | ||
out.write(("${command}\n<<EOF>>\n").getBytes(StandardCharsets.UTF_8)) | ||
out.flush() | ||
} finally { | ||
s.close() | ||
} | ||
} | ||
|
||
def writeMayFail(final String command) { | ||
try { | ||
write(command) | ||
} catch (e) { | ||
log.debug(e.getMessage(), e) | ||
} | ||
} | ||
|
||
int getPort() { | ||
return port | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
libs/gretty-core/src/main/groovy/org/akhikhl/gretty/AsyncResponse.groovy
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.