[Remoterengine-commits] r133 - in pkg/RemoteREngine.examples: . exec inst inst/examples inst/examples/console inst/examples/console/src inst/examples/helloworld inst/examples/helloworld/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Sep 17 09:34:24 CEST 2009


Author: romain
Date: 2009-09-17 09:34:24 +0200 (Thu, 17 Sep 2009)
New Revision: 133

Added:
   pkg/RemoteREngine.examples/NAMESPACE
   pkg/RemoteREngine.examples/configure
   pkg/RemoteREngine.examples/configure.R
   pkg/RemoteREngine.examples/configure.win
   pkg/RemoteREngine.examples/exec/
   pkg/RemoteREngine.examples/exec/client
   pkg/RemoteREngine.examples/inst/
   pkg/RemoteREngine.examples/inst/examples/
   pkg/RemoteREngine.examples/inst/examples/build.properties
   pkg/RemoteREngine.examples/inst/examples/build.xml
   pkg/RemoteREngine.examples/inst/examples/console/
   pkg/RemoteREngine.examples/inst/examples/console/src/
   pkg/RemoteREngine.examples/inst/examples/console/src/RemoteRConsole.java
   pkg/RemoteREngine.examples/inst/examples/helloworld/
   pkg/RemoteREngine.examples/inst/examples/helloworld/src/
   pkg/RemoteREngine.examples/inst/examples/helloworld/src/RemoteHelloWorld.java
   pkg/RemoteREngine.examples/inst/examples/metadata.properties
Log:
initial version of examples package

Added: pkg/RemoteREngine.examples/NAMESPACE
===================================================================
--- pkg/RemoteREngine.examples/NAMESPACE	                        (rev 0)
+++ pkg/RemoteREngine.examples/NAMESPACE	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,2 @@
+
+

Added: pkg/RemoteREngine.examples/configure
===================================================================
--- pkg/RemoteREngine.examples/configure	                        (rev 0)
+++ pkg/RemoteREngine.examples/configure	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,4 @@
+#!/bin/bash 
+
+"${R_HOME}/bin/Rscript" --default-packages="base,utils,methods,rJava,ant" configure.R
+


Property changes on: pkg/RemoteREngine.examples/configure
___________________________________________________________________
Name: svn:executable
   + *

Added: pkg/RemoteREngine.examples/configure.R
===================================================================
--- pkg/RemoteREngine.examples/configure.R	                        (rev 0)
+++ pkg/RemoteREngine.examples/configure.R	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,27 @@
+#!/bin/env Rscript
+
+require( ant ) 
+
+dir_filter <- function(.) file.info(.)$isdir
+rscript <- file.path( R.home(), "bin", "Rscript" )
+
+root <- getwd()
+dir.create( "inst/java" )
+
+setwd( "inst/examples" )
+dirs <- Filter( dir_filter, dir( "." ) )
+examples_dir <- getwd()
+client.jar <- system.file( "java", "RemoteREngine-client.jar", package = "RemoteREngine" )
+for( ex in dirs ){
+	setwd( ex )
+	file.copy( "../build.xml", "build.xml" )
+	cat( "project=", ex, "\n",
+		"client.jar=", client.jar, "\n",
+		 file = "build.properties", sep = "", 
+		 append = file.exists( "build.properties" ) )
+	ant() 
+	setwd( examples_dir )
+}
+setwd(root)
+
+

Added: pkg/RemoteREngine.examples/configure.win
===================================================================
--- pkg/RemoteREngine.examples/configure.win	                        (rev 0)
+++ pkg/RemoteREngine.examples/configure.win	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,4 @@
+#!/bin/bash 
+
+"${R_HOME}/bin/Rscript" --default-packages="base,utils,methods,rJava,ant" configure.R
+


Property changes on: pkg/RemoteREngine.examples/configure.win
___________________________________________________________________
Name: svn:executable
   + *

Added: pkg/RemoteREngine.examples/exec/client
===================================================================
--- pkg/RemoteREngine.examples/exec/client	                        (rev 0)
+++ pkg/RemoteREngine.examples/exec/client	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,106 @@
+#!/bin/env Rscript
+
+# {{{ Copyright (c) 2009, Romain Francois <francoisromain at free.fr>
+# :tabSize=2:indentSize=2:noTabs=false:folding=explicit:collapseFolds=1:
+#
+# This file is part of the RemoteREngine project
+#
+# The RemoteREngine project is free software: 
+# you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# The RemoteREngine project is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the RemoteREngine project. If not, see <http://www.gnu.org/licenses/>.
+# }}}
+
+SEP <- .Platform$path.sep
+client.dir <- system.file( "java", package = "RemoteREngine.examples" ) 
+print.clientlist <- function(){
+	clients <- dir( client.dir , pattern = "\\.jar$" )
+	for( cl in clients ){
+		cat( cl, " : " , "\n" )
+	}
+}
+
+# {{{ command line args
+arguments <- commandArgs(TRUE)
+
+if( is.null(arguments) || length(arguments) == 0 ){
+	print.clientlist()
+	q("no", status = 1 )
+} 
+# }}}
+
+client <- arguments[1]
+client.jar <- file.path( client.dir, sprintf( "example-%s.jar", client ) ) 
+
+metadata <- readLines( system.file( "examples", "metadata.properties", 
+	package = "RemoteREngine.examples") )
+client.mainclass <- sub( " +", "", sub( "^.*=", "", grep( sprintf( "%s[.]mainclass", client), metadata, value = TRUE) ) ) 
+
+# {{{ classpath
+CP <- paste( 
+	system.file( "java", "RemoteREngine-client.jar", package = "RemoteREngine" ), 
+	client.jar, 
+	sep = SEP )
+# }}}
+
+# {{{ policy file
+policy <- system.file( "policy", "client.policy", package = "RemoteREngine" )
+# }}}
+
+# {{{ these functions might move to rJava at some point
+java.home <- function( set = FALSE ){
+	JAVA_HOME <- Sys.getenv( "JAVA_HOME", unset = "" )
+	if( JAVA_HOME != "" ){
+		# then find it from rJava
+		library( utils )
+		require( "methods", quietly = TRUE, character.only = TRUE )
+		require( "rJava", quietly = TRUE, character.only = TRUE )
+		.jinit()
+		JAVA_HOME <- .jcall( "java.lang.System", "S", "getProperty", "java.home" )
+		if( set ) Sys.setenv( JAVA_HOME = JAVA_HOME )
+	}
+	JAVA_HOME
+}
+
+java.cmd <- function( JAVA_HOME = java.home(), set = FALSE ){
+	JAVACMD <- Sys.getenv( "JAVACMD", unset = "" )
+	if( JAVACMD == "" ){
+		# IBM's JDK on AIX uses strange locations for the executables
+	    if( file.exists( cmd <- file.path( JAVA_HOME, "jre", "sh", "java" ) ) ){
+	    	JAVACMD <- cmd
+	    } else if( file.exists( cmd <- file.path( JAVA_HOME, "jre", "bin", "java" ) ) ){
+	    	JAVACMD <- cmd
+	    } else if( file.exists( cmd <- file.path( JAVA_HOME, "bin", "java" ) ) ){
+	    	JAVACMD <- cmd
+	    } else if( file.exists( cmd <- file.path( JAVA_HOME, "jre", "bin", "java.exe" ) ) ){
+	    	JAVACMD <- cmd
+	    } else if( file.exists( cmd <- file.path( JAVA_HOME, "bin", "java.exe" ) ) ){
+	    	JAVACMD <- cmd
+	    } else if( file.exists( cmd <- file.path( JAVA_HOME, "jre", "bin", "javaw.exe" ) ) ){
+	    	JAVACMD <- cmd
+	    } else if( file.exists( cmd <- file.path( JAVA_HOME, "bin", "javaw.exe" ) ) ){
+	    	JAVACMD <- cmd
+	    } else {
+	    	stop( paste( "could not find java command, is java installed in : '" , JAVA_HOME , "' ?" ) )
+		}
+	}
+	if( set ) Sys.setenv( JAVACMD = JAVACMD )
+	JAVACMD
+}
+JAVACMD <- java.cmd( )
+# }}}
+
+cmd <- sprintf( '"%s" -cp "%s" -Xmx1024M -Djava.security.policy="%s" %s', 
+	JAVACMD, CP, policy, client.mainclass )
+cat( "command: \n\n", cmd, "\n\nresult:\n\n" )
+system( cmd )
+# }}}

Added: pkg/RemoteREngine.examples/inst/examples/build.properties
===================================================================
--- pkg/RemoteREngine.examples/inst/examples/build.properties	                        (rev 0)
+++ pkg/RemoteREngine.examples/inst/examples/build.properties	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,12 @@
+compiler.debug=on
+compiler.debuglevel=lines,vars,source
+compiler.optimize=off
+compiler.deprecation=off
+compiler.verbose=off
+compiler.nowarn=off
+compiler.target=1.5
+compiler.source=1.5
+
+build.dir=build
+inst.dir=../../java
+

Added: pkg/RemoteREngine.examples/inst/examples/build.xml
===================================================================
--- pkg/RemoteREngine.examples/inst/examples/build.xml	                        (rev 0)
+++ pkg/RemoteREngine.examples/inst/examples/build.xml	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,41 @@
+<?xml version="1.0"?>
+<project default="all">
+
+	<property file="../build.properties"/>
+	<property file="build.properties"/>
+	
+	<path id="project.classpath">
+		<pathelement location="${client.jar}" />
+	</path>
+	
+	<target name="compile">
+		
+		<mkdir dir="build" />
+		<javac
+			destdir="${build.dir}"  
+			includeJavaRuntime="yes"
+			debug="${compiler.debug}"
+			debuglevel="${compiler.debuglevel}"
+			optimize="${compiler.optimize}"
+			deprecation="${compiler.deprecation}"
+			verbose="${compiler.verbose}"
+			nowarn="${compiler.nowarn}"
+			target="${compiler.target}"
+			source="${compiler.source}">
+  
+			<src path="src" />
+			<include name="**/*.java"/>
+			<classpath refid="project.classpath" />
+		</javac>
+	</target>
+	          
+	<target name="build">
+		<jar jarfile="${inst.dir}/example-${project}.jar">
+			<fileset dir="${build.dir}" />
+		</jar>
+	</target>
+	
+	<target name="all" depends="compile,build" />
+	
+</project>
+

Added: pkg/RemoteREngine.examples/inst/examples/console/src/RemoteRConsole.java
===================================================================
--- pkg/RemoteREngine.examples/inst/examples/console/src/RemoteRConsole.java	                        (rev 0)
+++ pkg/RemoteREngine.examples/inst/examples/console/src/RemoteRConsole.java	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,59 @@
+import java.rmi.*; 
+import org.rosuda.REngine.remote.client.RemoteREngine ;
+import org.rosuda.REngine.remote.common.callbacks.*;
+import org.rosuda.REngine.remote.common.console.* ;
+ 
+public class RemoteRConsole {
+ 
+	public static void main( String[] args) {
+		try{
+			if (System.getSecurityManager() == null) {
+				System.setSecurityManager(new RMISecurityManager());
+			}
+			RemoteREngine r = new RemoteREngine( "RemoteREngine" , "localhost", 1099 );
+			r.addCallbackListener( new ConsoleCallbackListener() ) ; 
+			ConsoleThread console = new ConsoleThread( r ) ; 
+			console.start( ) ; 
+	  } catch( Exception e){
+	  	e.printStackTrace(); 
+	  }
+	}
+	
+	private static class ConsoleCallbackListener implements CallbackListener {
+		public void handleCallback( RCallback callback ){
+			if( callback instanceof RWriteConsoleCallback ){
+				System.out.print( ( (RWriteConsoleCallback)callback).getMessage()  ) ;
+			} else if( callback instanceof RShowMessageCallback){
+				System.out.print( ( (RShowMessageCallback)callback).getMessage()  ) ;
+			} else if( callback instanceof ReadConsoleCallback){
+				System.out.print( ( (ReadConsoleCallback)callback).getPrompt()  ) ;
+			} else if( callback instanceof InputCallback ){
+				System.out.print( ((InputCallback)callback).getCommand() + "\n" ) ;
+			}
+		}
+	}
+	
+	private static class ConsoleThread extends Thread {
+		
+		private DefaultConsoleReadLine readline ;
+		private RemoteREngine engine; 
+		
+		private ConsoleThread( RemoteREngine engine){
+			super() ; 
+			this.engine = engine ; 
+			this.readline = new DefaultConsoleReadLine( ); 
+		}
+		
+		public void run(){
+			/* just the first time */
+			System.out.print( "> " ) ;
+			
+			while( true ){
+				String line = readline.readLine(); 
+				engine.sendToConsole( line ) ; 
+			}
+		}
+		
+	}
+}
+

Added: pkg/RemoteREngine.examples/inst/examples/helloworld/src/RemoteHelloWorld.java
===================================================================
--- pkg/RemoteREngine.examples/inst/examples/helloworld/src/RemoteHelloWorld.java	                        (rev 0)
+++ pkg/RemoteREngine.examples/inst/examples/helloworld/src/RemoteHelloWorld.java	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,47 @@
+import java.rmi.*; 
+import org.rosuda.REngine.remote.client.RemoteREngine ;
+
+/**
+ * Hello world example of a client to the RemoteREngine server
+ * 
+ * <p>This client connects to the <i>RemoteREngine</i> servant on <i>localhost</i>
+ * and grabs the result of <strong>rnorm(5)</strong>
+ */
+public class RemoteHelloWorld {
+ 
+	public static void main( String[] args) {
+		try{
+			
+			/* standard RMI code, sets the system security manager to a
+			   RMI security manager. */
+			if (System.getSecurityManager() == null) {
+				System.setSecurityManager(new RMISecurityManager());
+			}
+			
+			/* creates the connection to the servant called "RemoteREngine" (default)
+			   on port 1099 of localhost */
+			RemoteREngine r = new RemoteREngine( "RemoteREngine" , "localhost", 1099 );
+			
+			/* grab five random numbers */
+			double[] result = r.parseAndEval( "rnorm( 5 )" ).asDoubles() ; 
+	  	
+			/* boring java code to print the numbers */
+			StringBuilder b = new StringBuilder("five random numbers from R :"); 
+	  	for( int i=0; i<result.length; i++){
+	  		b.append( " " + result[i]) ;
+	  	}
+	  	System.out.println( b ) ;
+	  	
+	  	/* close the connection. 
+	  	   This ends the subscription of this client with the server */
+	  	r.close( ) ;
+	  	
+	  	/* quit */
+	  	 
+	  	System.exit( 0 );
+	  } catch( Exception e){
+	  	e.printStackTrace(); 
+	  }
+	}
+	
+}

Added: pkg/RemoteREngine.examples/inst/examples/metadata.properties
===================================================================
--- pkg/RemoteREngine.examples/inst/examples/metadata.properties	                        (rev 0)
+++ pkg/RemoteREngine.examples/inst/examples/metadata.properties	2009-09-17 07:34:24 UTC (rev 133)
@@ -0,0 +1,6 @@
+helloworld.mainclass=RemoteHelloWorld
+helloworld.description=Simple Hello World example
+
+console.mainclass=RemoteRConsole
+console.description=console that runs on the remote R repl
+



More information about the Remoterengine-commits mailing list