Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move server's output collector to ProgramThread in order to use it fr…
…om Cient
- Loading branch information
Showing
3 changed files
with
49 additions
and
21 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
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
package models | |||
|
|||
import java.io.{InputStreamReader, BufferedReader} | |||
|
|||
class ProgramThread(builder: ProcessBuilder) extends Thread { | |||
private var process: Process = null | |||
private val stringBuilder = new StringBuilder() | |||
private var callback: Option[String => Unit] = None | |||
|
|||
def setCallback(acb: String => Unit) = callback = Some(acb) | |||
|
|||
override def run() = { | |||
process = builder.start | |||
val streamReader = new InputStreamReader(process.getInputStream) | |||
val bufferedReader = new BufferedReader(streamReader, 1) | |||
var line:String = null | |||
|
|||
while({line = bufferedReader.readLine; line != null}){ | |||
stringBuilder.append(line) | |||
stringBuilder.append("\n") | |||
callback.map(_(line)) | |||
} | |||
bufferedReader.close | |||
|
|||
process.waitFor | |||
} | |||
|
|||
def waitFor: Unit = if (process != null) process.waitFor | |||
|
|||
def forceExit: Unit = if (process != null) process.destroy | |||
|
|||
def getOutput: String = stringBuilder.toString | |||
|
|||
def exitValue: Int = process.exitValue | |||
} |
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