summaryrefslogtreecommitdiff
path: root/test/files/detach-run/basic
diff options
context:
space:
mode:
Diffstat (limited to 'test/files/detach-run/basic')
-rw-r--r--test/files/detach-run/basic/Client.scala44
-rw-r--r--test/files/detach-run/basic/Server.scala17
-rw-r--r--test/files/detach-run/basic/ServerConsole.scala80
-rw-r--r--test/files/detach-run/basic/basic.flags1
-rw-r--r--test/files/detach-run/basic/basic.scala153
-rw-r--r--test/files/detach-run/basic/java.policy26
6 files changed, 321 insertions, 0 deletions
diff --git a/test/files/detach-run/basic/Client.scala b/test/files/detach-run/basic/Client.scala
new file mode 100644
index 0000000000..d3f159fd40
--- /dev/null
+++ b/test/files/detach-run/basic/Client.scala
@@ -0,0 +1,44 @@
+import java.net._, Thread._, ClientHelper._
+import scala.remoting._, Debug._
+
+object Foo {
+ def trace(s: String) { info("[Foo.trace] "+s)}
+}
+object Client {
+ val yInstVal: Int = 10
+ var yInstVar: Int = 99
+ object Bar {
+ def trace(s: String) { info("[Bar.trace] "+s) }
+ }
+ def main(args: Array[String]) {
+ init(args)
+ val server = new Channel(host, port)
+ val zLocVal: Int = 1000
+ var zLocVar: Int = 9998
+ server ! detach(
+ (x: Int) => {
+ println("yInstVal = "+yInstVal)
+ this.trace("yInstVar = "+yInstVar)
+ Bar.trace("zLocVal = "+zLocVal)
+ Foo.trace("zLocVar = "+zLocVar)
+ zLocVar += 2
+ System.out.println("zLocVal = "+zLocVal)
+ Debug.info("zLocVar = "+zLocVar)
+ x + yInstVal + yInstVar + zLocVal + zLocVar
+ })
+ val result = server.receiveInt
+ println("result received: " + result)
+ }
+ private def trace(s: String) { info("[Client.trace] "+s) }
+}
+
+object ClientHelper {
+ private var _host = "127.0.0.1"
+ private var _port = 8888
+ def host = _host
+ def port = _port
+ def init(args: Array[String]) {
+ try { _host = args(0) } catch { case _ => }
+ try { _port = args(1).toInt } catch { case _ => }
+ }
+}
diff --git a/test/files/detach-run/basic/Server.scala b/test/files/detach-run/basic/Server.scala
new file mode 100644
index 0000000000..601eea3c58
--- /dev/null
+++ b/test/files/detach-run/basic/Server.scala
@@ -0,0 +1,17 @@
+import scala.remoting.ServerChannel
+
+object Server extends ServerConsole {
+ private def computation(f: Int => Int): Int = {
+ //some time-consuming task
+ f(2)
+ }
+ def main(args: Array[String]) {
+ val server = new ServerChannel(args(0).toInt)
+ loop {
+ val client = server.accept
+ val f = client.receive[Int => Int]
+ val result = computation(f)
+ client ! result
+ }
+ server.close()
+ } }
diff --git a/test/files/detach-run/basic/ServerConsole.scala b/test/files/detach-run/basic/ServerConsole.scala
new file mode 100644
index 0000000000..bc96bfa329
--- /dev/null
+++ b/test/files/detach-run/basic/ServerConsole.scala
@@ -0,0 +1,80 @@
+import java.io._
+
+import scala.compat.Platform.currentTime
+import scala.remoting.Debug, Debug._
+
+trait ServerConsole extends Thread {
+ private val startTime = currentTime
+
+ start()
+
+ private var isTerminated = false
+
+ def terminate() { isTerminated = true }
+
+ protected def loop(block: => Unit) {
+ while (!isTerminated) {
+ try {
+ block
+ }
+ catch {
+ case e: ObjectStreamException =>
+ trace("Object stream error ("+e.getMessage+")")
+ case e: EOFException =>
+ trace("Connection lost")
+ case e: ClassNotFoundException =>
+ trace("Class not found")
+ case e =>
+ trace("Server error: "+e)
+ }
+ }
+ }
+
+ override def run() {
+ import java.io._
+ val in = new BufferedReader(new InputStreamReader(System.in))
+ var quit = false
+ while (!quit) {
+ val args = getArgs(in)
+ if (args contains "quit")
+ quit = true
+ if (args contains "cls") {
+ println(ERASE_SCREEN)
+ println(CURSOR_HOME)
+ }
+ if (args contains "warning")
+ Debug.level = Level.WARNING
+ if (args contains "info")
+ Debug.level = Level.INFO
+ if (args contains "silent")
+ Debug.level = Level.SILENT
+ }
+ terminate()
+ println("Server exited ("+mkTimeString(currentTime - startTime)+")")
+ exit(0)
+
+ }
+
+ protected def trace(msg: String) {
+ Debug.info("[ServerConsole.trace] "+msg)
+ }
+
+ private def getArgs(in: BufferedReader): List[String] = {
+ print("> ")
+ val input = try { in.readLine() } catch { case _ => null }
+ if (input != null) (input.trim split "\\s+").toList else Nil
+ }
+
+ private def mkTimeString(time: Long): String = {
+ def twoDigits(i: Long) = (if (i < 10) "0" else "")+i
+ val sec = time / 1000
+ val min = sec / 60
+ val h = min / 60
+ twoDigits(h) +":"+
+ twoDigits(min - h * 60)+":"+
+ twoDigits(sec - min * 60)
+ }
+
+ private val ERASE_SCREEN = "\033[2J"
+ private val CURSOR_HOME = "\033[H"
+}
diff --git a/test/files/detach-run/basic/basic.flags b/test/files/detach-run/basic/basic.flags
new file mode 100644
index 0000000000..55eed8bbcd
--- /dev/null
+++ b/test/files/detach-run/basic/basic.flags
@@ -0,0 +1 @@
+-Xpluginsdir ../../../../build/pack/misc/scala-devel/plugins -Xplugin-require:detach -P:detach:enable
diff --git a/test/files/detach-run/basic/basic.scala b/test/files/detach-run/basic/basic.scala
new file mode 100644
index 0000000000..c8a1046d44
--- /dev/null
+++ b/test/files/detach-run/basic/basic.scala
@@ -0,0 +1,153 @@
+object Test {
+
+ val host = "127.0.0.1"
+ val port = 8889
+ val logLevel = "silent"
+ // = "info" // debug user code only
+ // = "info,lib" // debug user & library code
+
+ // we assume an Apache server is running locally for deployment
+ private val sep = java.io.File.separator
+ val docPath = System.getProperty("user.home")+sep+"public_html"
+ val docRoot = "http://127.0.0.1/~"+System.getProperty("user.name")
+
+ private var server = new ServerThread(port)
+ private var client = new ClientThread(host, port)
+
+ def main(args: Array[String]) {
+ setenv()
+ server.start()
+ Thread.sleep(1000)
+ client.start()
+ server.join()
+ client.join()
+ System.exit(0)
+ }
+
+ private class ServerThread(port: Int) extends Thread {
+ override def run() {
+ println("Server.main "+port)
+ Server.main(Array(port.toString))
+ }
+ }
+
+ private class ClientThread(host: String, port: Int) extends Thread {
+ override def run() {
+ println("Client.main "+host+" "+port)
+ Client.main(Array(host, port.toString))
+ Server.terminate()
+ }
+ }
+
+ private def setenv() {
+ import java.io._, java.util.jar._
+
+ val policyTmpl =
+ System.getProperty("partest.cwd")+sep+"basic"+sep+"java.policy"
+ val outPath = System.getProperty("partest.output")
+ val libPath = System.getProperty("partest.lib")
+ val policyFile = outPath+sep+"java.policy"
+ val codebaseDir = outPath+sep+"-"
+
+ assert((new java.io.File(docPath)).isDirectory,
+ "Root directory \""+docPath+"\" not found")
+ val deployJar = docPath+sep+"basic_deploy.jar"
+ val deployUrl = docRoot+"/basic_deploy.jar"
+
+ // Java properties for server & client
+ System.setProperty("scala.remoting.logLevel", logLevel)
+ System.setProperty("java.security.manager", "")
+ System.setProperty("java.security.policy", policyFile)
+ // Java properties for server only
+ System.setProperty("java.rmi.server.codebase", deployUrl)
+ System.setProperty("java.rmi.server.hostname", host)
+ System.setProperty("java.rmi.server.useCodebaseOnly", "true")
+
+ val classNames = List(
+ "Bar$proxy",
+ "Bar$proxyImpl_Stub",
+ "Client$$anonfun$main$1$detach",
+ "Client$proxy",
+ "Client$proxyImpl_Stub",
+ "Foo$proxy",
+ "Foo$proxyImpl_Stub")
+
+ val proxyImplNames =
+ for (n <- classNames; i = n lastIndexOf "_Stub"; if i > 0)
+ yield n.substring(0, i)
+
+ generatePolicyFile()
+ generateRmiStubs(proxyImplNames)
+ generateJarFile(classNames)
+
+ def generatePolicyFile() {
+ val in = new BufferedReader(new FileReader(policyTmpl))
+ val out = new PrintWriter(new BufferedWriter(new FileWriter(policyFile)))
+ var line = in.readLine()
+ while (line != null) {
+ val line1 = line.replaceAll("@PROJECT_LIB_BASE@", codebaseDir)
+ out.println(line1)
+ line = in.readLine()
+ }
+ in.close()
+ out.close()
+ }
+ def exec(command: String) {
+ val proc = Runtime.getRuntime exec command
+ proc.waitFor()
+ val out = new BufferedReader(new InputStreamReader(proc.getInputStream))
+ var line = out.readLine()
+ while (line != null) {
+ println(line)
+ line = out.readLine()
+ }
+ out.close()
+ val err = new BufferedReader(new InputStreamReader(proc.getErrorStream))
+ line = err.readLine()
+ while (line != null) {
+ println(line)
+ line = err.readLine()
+ }
+ err.close()
+ }
+
+ def ls(path: String) { exec("ls -al "+path) }
+ def rmic(options: List[String], classNames: List[String]) {
+ val javaHome = scala.util.Properties.javaHome
+ val jdkHome =
+ if (javaHome endsWith "jre") javaHome.substring(0, javaHome.length-4)
+ else javaHome
+ val rmicExt = if (scala.util.Properties.isWin) ".exe" else ""
+ val rmicCmd = jdkHome+sep+"bin"+sep+"rmic"+rmicExt
+ val cmdLine = rmicCmd+options.mkString(" ", " ", "")+
+ classNames.mkString(" "," ","")
+ // println(cmdLine)
+ exec(cmdLine)
+ }
+ def generateRmiStubs(classNames: List[String]) {
+ val options = List(
+ "-v1.2",
+ "-classpath "+libPath+File.pathSeparator+outPath,
+ "-d "+outPath)
+ rmic(options, classNames)
+ // ls(outPath)
+ }
+ def generateJarFile(classNames: List[String]) {
+ val out = new JarOutputStream(new FileOutputStream(deployJar))
+ classNames foreach (name => {
+ val className = name+".class"
+ out putNextEntry new JarEntry(className)
+ val in = new FileInputStream(outPath+sep+className)
+ val buf = new Array[Byte](256)
+ var len = in read buf
+ while (len != -1) {
+ out.write(buf, 0, len)
+ len = in read buf
+ }
+ in.close()
+ })
+ out.close()
+ }
+ }
+}
+
diff --git a/test/files/detach-run/basic/java.policy b/test/files/detach-run/basic/java.policy
new file mode 100644
index 0000000000..92c1045c3d
--- /dev/null
+++ b/test/files/detach-run/basic/java.policy
@@ -0,0 +1,26 @@
+// See http://java.sun.com/javase/6/docs/technotes/guides/security/permissions.html
+// See http://mindprod.com/jgloss/policyfile.html
+// The policy expands ${/} to the correct path or folder delimiter on your host platform.
+
+// Actions available with SocketPermission: accept, connect, listen, resolve
+// 1) The "resolve" action is implied when any of the other actions are present.
+// 2) The "listen" action is only meaningful when used with "localhost".
+
+grant {
+ permission java.net.SocketPermission "*:80", "connect,accept,listen";
+ permission java.net.SocketPermission "*:1024-", "connect,accept,listen";
+ permission java.util.PropertyPermission "scala.remoting.logLevel", "read";
+ permission java.util.PropertyPermission "scala.remoting.port", "read";
+};
+
+grant codeBase "@PROJECT_LIB_BASE@" {
+ permission java.lang.RuntimePermission "getClassLoader";
+ permission java.lang.RuntimePermission "createClassLoader";
+ permission java.util.PropertyPermission "java.rmi.server.codebase", "read";
+ permission java.util.PropertyPermission "java.rmi.server.hostname", "read";
+ permission java.util.PropertyPermission "sun.rmi.dgc.server.gcInterval", "read,write";
+};
+
+//grant {
+// permission java.security.AllPermission;
+//};