[Remoterengine-commits] r212 - pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Oct 28 11:49:20 CET 2009
Author: romain
Date: 2009-10-28 11:49:20 +0100 (Wed, 28 Oct 2009)
New Revision: 212
Modified:
pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server/RemoteREngine_Server.java
pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server/RemoteRMainLoopCallbacks.java
Log:
adapt to new interfaces in org.rosuda.REngine
Modified: pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server/RemoteREngine_Server.java
===================================================================
--- pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server/RemoteREngine_Server.java 2009-10-28 09:14:19 UTC (rev 211)
+++ pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server/RemoteREngine_Server.java 2009-10-28 10:49:20 UTC (rev 212)
@@ -175,10 +175,10 @@
addCallbackListener( consoleCallbackHandler ) ;
logger.debug("About to construct JRIEngine");
- r = new JRIEngine( args, callbackLoop ) ;
+ r = (JRIEngine)JRIEngine.createEngine( args, callbackLoop, true) ;
// Execute R prepare script before R server is available via RMI
- runInitScript(r, initScript);
+ runInitScript( initScript );
/* TODO: forbid the q function */
@@ -262,7 +262,7 @@
* @throws RemoteException
* @throws AccessException
*/
- private void runInitScript(JRIEngine r, String initScript) throws REngineException, RemoteException, AccessException{
+ private void runInitScript(String initScript) throws REngineException, RemoteException, AccessException{
// if no init script provided
if (initScript == null || initScript.trim().length() == 0){
@@ -272,6 +272,9 @@
logger.info("REngine execute init script '"+initScript+"' ...");
// read the whole content of the init script
+ // FIXME [ian]: this is no good; we either need to call source or parseAndEval the
+ // result of readFile at once, this current way of doing it fails as soon
+ // as one R command spans multiple lines, which is bread and butter
String[] scriptCommands = (new FileParser()).readLines(initScript);
// parse and evaluate the script
@@ -284,7 +287,7 @@
throw e;
} catch (REXPMismatchException e) {
logger.error("Unable to run init script '"+initScript+"', "+e.getMessage(),e);
- throw new REngineException(REngine.getLastEngine(), "Unable to run init script '"+initScript+"', "+e.getMessage());
+ throw new REngineException( r, "Unable to run init script '"+initScript+"', "+e.getMessage());
}
}
Modified: pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server/RemoteRMainLoopCallbacks.java
===================================================================
--- pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server/RemoteRMainLoopCallbacks.java 2009-10-28 09:14:19 UTC (rev 211)
+++ pkg/RemoteREngine/inst/java_src/src/server/org/rosuda/REngine/remote/server/RemoteRMainLoopCallbacks.java 2009-10-28 10:49:20 UTC (rev 212)
@@ -22,6 +22,12 @@
import org.rosuda.JRI.RMainLoopCallbacks;
import org.rosuda.JRI.Rengine;
+import org.rosuda.REngine.REngine;
+import org.rosuda.REngine.REngineCallbacks;
+import org.rosuda.REngine.REngineConsoleHistoryInterface;
+import org.rosuda.REngine.REngineInputInterface;
+import org.rosuda.REngine.REngineOutputInterface;
+import org.rosuda.REngine.REngineUIInterface;
import org.rosuda.REngine.remote.common.callbacks.CallbackResponse;
import org.rosuda.REngine.remote.common.callbacks.CancelCallback;
import org.rosuda.REngine.remote.common.callbacks.ChooseFileCallback;
@@ -41,7 +47,8 @@
/**
* Remote main loop callbacks
*/
-public class RemoteRMainLoopCallbacks implements RMainLoopCallbacks {
+public class RemoteRMainLoopCallbacks implements REngineCallbacks, REngineInputInterface,
+REngineConsoleHistoryInterface, REngineUIInterface, REngineOutputInterface {
final Logger logger = LoggerFactory.getLogger(org.rosuda.REngine.remote.server.RemoteRMainLoopCallbacks.class);
/**
@@ -64,88 +71,96 @@
waiter = new CallbackResponseWaiter() ;
}
- /**
- * called when R prints output to the console
- *
- * @param re calling engine
- * @param text text to display in the console
- * @param oType output type (0=regular, 1=error/warning)
- */
- public void rWriteConsole(Rengine re, String text, int oType) {
- logger.debug("rWriteConsole: {}",text);
- server.sendCallbackToListeners( new RWriteConsoleCallback( text, oType ) ) ;
+
+ /**
+ * Adds a response to a callback
+ * @param response the response to a callback
+ */
+ public void addResponse( CallbackResponse<? extends RCallbackWithResponse> response){
+ logger.debug("addResponse");
+ int id = response.getCallbackId() ;
+ if( waiter.isWaitingFor( id )){
+
+ /* send a cancel callback */
+ CancelCallback cancel = new CancelCallback( id ) ;
+ server.sendCallbackToListeners( cancel ) ;
+
+ /* put the response in the queue */
+ waiter.put( new Integer(response.getCallbackId()), response) ;
+ }
}
- /**
- * called when R enters or exits a longer evaluation.
- * It is usually a good idea to signal this state to the user, e.g. by changing the cursor to a "hourglass" and back.
- *
- * @param re calling engine
- * @param which identifies whether R enters (1) or exits (0) the busy state
+ /**
+ * called when R enters the read stage of the event loop.
+ *
+ * @param eng calling engine
+ * @param prompt prompt to display in the console
+ * @param addToHistory flag indicating whether the input is transient (<code>false</code>) or to be recorded in the command history (<code>true</code>).
+ * @return string to be processed as console input
*/
- public void rBusy(Rengine re, int which) {
- logger.debug("rBusy: " + (which == 1? "entered" : "exited"));
- server.sendCallbackToListeners( new RBusyCallback( which == 1 ) ) ;
+ public String RReadConsole(REngine eng, String prompt, int addToHistory){
+ logger.debug("rReadConsole");
+ /* send the callback to clients (they might be interested in the prompt) */
+ ReadConsoleCallback callback = new ReadConsoleCallback( prompt) ;
+ server.sendCallbackToListeners( callback ) ;
+
+ /* wait for the next available command: this blocks */
+ Command cmd = server.getConsoleSync().next() ;
+ String result = cmd.getCommand() ;
+ if (result == null) logger.warn("Null command returned from R Console");
+
+ InputCallback input = new InputCallback( result, cmd.getSender() ) ;
+ server.sendCallbackToListeners( input ) ;
+
+ if( result != null && !result.endsWith( "\n" ) ){
+ result += "\n" ;
+ }
+ logger.debug("rReadConsole sending '{}'", result);
+ return result ;
+
}
+
+ /** called when R wants to save the history content.
+ * @param eng calling engine
+ * @param filename name of the file to save command history to
+ */
+ public void RSaveHistory (REngine eng, String filename){
+ logger.debug("rSaveHistory {}",filename);
+ // TODO: implement RSaveHistory
+ }
- /**
- * called when R requests the console to flush any buffered output
- *
- * @param re calling engine
- */
- public void rFlushConsole(Rengine re) {
- logger.debug("rFlush");
- server.sendCallbackToListeners( new RFlushConsoleCallback() ) ;
- }
-
- /**
- * called when R want to show a warning/error message
- * (not to be confused with messages displayed in the console output)
- *
- * @param re calling engine
- * @param message message to display
+ /** called when R wants to load the history content.
+ * @param eng calling engine
+ * @param filename name of the file to load the command history from
*/
- public void rShowMessage(Rengine re, String message) {
- logger.debug("rShowMessage: {}",message);
- server.sendCallbackToListeners( new RShowMessageCallback( message ) ) ;
+ public void RLoadHistory (REngine eng, String filename){
+ logger.debug("rSaveHistory {}",filename);
+ // TODO: implement RLoadHistory
}
- /* history management */
- /**
- * called to save the contents of the history (the implementation is responsible of keeping track of the history)
- *
- * @param re calling engine
- * @param filename name of the history file
+ /**
+ * called when the busy state of R changes - usual response is to change the shape of the cursor
+ *
+ * @param eng calling engine
+ * @param state busy state of R (0 = not busy)
*/
- public void rSaveHistory (Rengine re, String filename){
- logger.debug("rSaveHistory {}",filename);
- // TODO: read the history file
- // TODO: Make the history available to clients
- }
-
- /**
- * called to load the contents of the history
- *
- * @param re calling engine
- * @param filename name of the history file
- */
- public void rLoadHistory (Rengine re, String filename){
- logger.debug("rLoadHistory: {}" + filename);
- // TODO: load the history file
- }
+ public void RBusyState(REngine eng, int which) {
+ logger.debug("rBusy: " + (which == 1? "entered" : "exited"));
+ server.sendCallbackToListeners( new RBusyCallback( which == 1 ) ) ;
+ }
- /**
- * called when R expects the user to choose a file
- *
- * @param re calling engine
- * @param newFile flag determining whether an existing or new file is to be selected
- * @return path/name of the selected file
+ /**
+ * called when R wants the user to choose a file.
+ *
+ * @param re calling engine
+ * @param newFile if <code>true</code> then the user can specify a non-existing file to be created, otherwise an existing file must be selected.
+ * @return full path and name of the selected file or <code>null</code> if the selection was cancelled.
*/
- public String rChooseFile(Rengine re, int newFile) {
- logger.debug("rChooseFile");
+ public String RChooseFile(REngine re, boolean newFile) {
+ logger.debug("RChooseFile");
// TODO: choose a file on the client(s), bring the file back and return the name of the file in the server
- ChooseFileCallback callback = new ChooseFileCallback( ( newFile != 0) ) ;
+ ChooseFileCallback callback = new ChooseFileCallback( newFile ) ;
waiter.waitingFor( callback.getId() ) ;
server.sendCallbackToListeners(callback) ;
@@ -154,58 +169,41 @@
ChooseFileCallbackResponse response = (ChooseFileCallbackResponse)waiter.get( callback.getId() ) ;
return response.getFilename() ;
+
}
/**
- * called when R waits for user input. During the duration of this callback it
- * is safe to re-enter R, and very often it is also the only time. The implementation
- * is free to block on this call until the user hits Enter, but in JRI it is
- * a good idea to call <code>Rengine.rniIdle()</code> occasionally to allow other
- * event handlers (e.g graphics device UIs) to run. Implementations should NEVER return
- * immediately even if there is no input - such behavior will result in a fast cycling event
- * loop which makes the use of R pretty much impossible.
- *
+ * called when R requests the console to flush any buffered output
+ *
* @param re calling engine
- * @param prompt prompt to be displayed at the console prior to user's input
- * @param addToHistory flag telling the handler whether the input should be considered for adding to history (!=0) or not (0)
- * @return user's input to be passed to R for evaluation */
- public synchronized String rReadConsole(Rengine re, String prompt, int addToHistory) {
- logger.debug("rReadConsole");
- /* send the callback to clients (they might be interested in the prompt) */
- ReadConsoleCallback callback = new ReadConsoleCallback( prompt) ;
- server.sendCallbackToListeners( callback ) ;
-
- /* wait for the next available command: this blocks */
- Command cmd = server.getConsoleSync().next() ;
- String result = cmd.getCommand() ;
- if (result == null) logger.warn("Null command returned from R Console");
-
- InputCallback input = new InputCallback( result, cmd.getSender() ) ;
- server.sendCallbackToListeners( input ) ;
-
- if( result != null && !result.endsWith( "\n" ) ){
- result += "\n" ;
- }
- logger.debug("rReadConsole sending '{}'", result);
- return result ;
+ */
+ public void RFlushConsole(REngine re) {
+ logger.debug("RFlushConsole");
+ server.sendCallbackToListeners( new RFlushConsoleCallback() ) ;
}
- /**
- * Adds a response to a callback
- * @param response the response to a callback
+ /**
+ * called when R want to show a warning/error message
+ * (not to be confused with messages displayed in the console output)
+ *
+ * @param re calling engine
+ * @param message message to display
*/
- public void addResponse( CallbackResponse<? extends RCallbackWithResponse> response){
- logger.debug("addResponse");
- int id = response.getCallbackId() ;
- if( waiter.isWaitingFor( id )){
-
- /* send a cancel callback */
- CancelCallback cancel = new CancelCallback( id ) ;
- server.sendCallbackToListeners( cancel ) ;
-
- /* put the response in the queue */
- waiter.put( new Integer(response.getCallbackId()), response) ;
- }
+ public void RShowMessage(REngine re, String message) {
+ logger.debug("RShowMessage: {}",message);
+ server.sendCallbackToListeners( new RShowMessageCallback( message ) ) ;
}
+
+ /**
+ * called when R prints output to the console
+ *
+ * @param re calling engine
+ * @param text text to display in the console
+ * @param oType output type (0=regular, 1=error/warning)
+ */
+ public void RWriteConsole(REngine re, String text, int oType) {
+ logger.debug("RWriteConsole: {}",text);
+ server.sendCallbackToListeners( new RWriteConsoleCallback( text, oType ) ) ;
+ }
}
More information about the Remoterengine-commits
mailing list