1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Merge pull request #178 from aneophyte/master

KobaltServer now uses the port specified in the command line argument
This commit is contained in:
Cedric Beust 2016-04-28 05:34:29 -08:00
commit f856082478
3 changed files with 10 additions and 8 deletions

View file

@ -158,7 +158,7 @@ private class Main @Inject constructor(
jc.usage()
} else if (args.serverMode) {
// --server
val port = KobaltServer(args.force,
val port = KobaltServer(args.force, args.port,
{ buildFile -> initForBuildFile(BuildFile(Paths.get(buildFile), buildFile), args)},
{ cleanUp() })
.call()

View file

@ -25,7 +25,7 @@ import java.util.concurrent.Callable
* The callbacks are used to initialize and clean up the state before and after each command, so that Kobalt's state
* can be properly reset, making the server reentrant.
*/
class KobaltServer(val force: Boolean,
class KobaltServer(val force: Boolean, val port: Int = 1234,
val initCallback: (String) -> List<Project>,
val cleanUpCallback: () -> Unit) : Callable<Int>, ICommandSender {
// var outgoing: PrintWriter? = null
@ -37,17 +37,17 @@ class KobaltServer(val force: Boolean,
}.toMap()
override fun call() : Int {
val port = ProcessUtil.findAvailablePort()
val availablePort = ProcessUtil.findAvailablePort(port)
try {
if (createServerFile(port, force)) {
privateRun(port)
if (createServerFile(availablePort, force)) {
privateRun(availablePort)
}
} catch(ex: Exception) {
ex.printStackTrace()
} finally {
deleteServerFile()
}
return port
return availablePort
}
val SERVER_FILE = KFiles.joinDir(homeDir(KFiles.KOBALT_DOT_DIR, "kobaltServer.properties"))

View file

@ -5,8 +5,10 @@ import java.net.Socket
class ProcessUtil {
companion object {
fun findAvailablePort(): Int {
for (i in 1234..65000) {
fun findAvailablePort(port: Int = 1234): Int {
if (isPortAvailable(port)) return port
for (i in 1235..65000) {
if (isPortAvailable(i)) return i
}
throw IllegalArgumentException("Couldn't find any port available, something is very wrong")