Skip to content

Commit

Permalink
Refactored interpreter core
Browse files Browse the repository at this point in the history
Added dispatch() method, improved decoupling and cleaned up.

Refers #228, #225
  • Loading branch information
otsakir committed Sep 22, 2017
1 parent 1eb598e commit d4e73ef
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.restcomm.connect.rvd.storage.FsProjectStorage;
import org.restcomm.connect.rvd.storage.WorkspaceStorage;
import org.restcomm.connect.rvd.storage.exceptions.StorageException;
import org.restcomm.connect.rvd.utils.RvdUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -72,6 +71,7 @@ public class Interpreter {

private Map<String, String> variables = new HashMap<String, String>();
private List<NodeName> nodeNames;
private RcmlResponse rcmlResult;

public static RcmlResponse rcmlOnException() {
RcmlResponse response = new RcmlResponse();
Expand Down Expand Up @@ -132,20 +132,7 @@ public void setVariables(Map<String, String> variables) {
this.variables = variables;
}


public Target getTarget() {
return target;
}


public void setTarget(Target target) {
this.target = target;
}


public RcmlResponse interpret() throws RvdException {
RcmlResponse response = null;

ProjectOptions projectOptions = FsProjectStorage.loadProjectOptions(appName, workspaceStorage); //rvdContext.getRuntimeProjectOptions();
nodeNames = projectOptions.getNodeNames();

Expand All @@ -163,11 +150,10 @@ public RcmlResponse interpret() throws RvdException {
//processRequestHeaders(httpRequest);
//handleStickyParameters(); // create local copies of sticky_* parameters

response = interpret(targetParam, null, null, null);
return response;
dispatch(targetParam);
return rcmlResult;
}


public MultivaluedMap<String, String> getRequestParams() {
return requestParams;
}
Expand All @@ -176,71 +162,78 @@ public String getContextPath() {
return contextPath;
}

public RcmlResponse interpret(String targetParam, RcmlResponse rcmlModel, Step prependStep, Target originTarget ) throws InterpreterException, StorageException {
public void dispatch(String targetParam) throws StorageException, InterpreterException {
if (RvdLoggers.local.isTraceEnabled())
RvdLoggers.local.log(Level.TRACE, LoggingHelper.buildMessage(getClass(),"interpret", loggingContext.getPrefix(), "starting interpeter for " + targetParam));
if ( projectSettings.getLogging() )
projectLogger.log("Running target: " + targetParam).tag("app",appName).done();

target = Interpreter.parseTarget(targetParam);
target = Interpreter.parseTarget(targetParam); // TODO - target can be made local variable (?)
if (target.action != null) {
// Event handling
loadStep(target.stepname, target.getNodename()).handleAction(this, target.getNodename());
} else {
interpret(target.getNodename(),null,null,null);
}
}

/**
* Interprets module moduleName. If startingStepName is provided, interpretation will start from there. In case there is a 'prependStep'
* it will render it first. originModuleName is the invoking module.
*
* @param moduleName - not null
* @param startingStepName
* @param prependStep
* @param originModuleName - can be null
* @throws StorageException
* @throws InterpreterException
*/
public void interpret(String moduleName, String startingStepName, Step prependStep, String originModuleName) throws StorageException, InterpreterException {
// make sure there is a valid RcmlResponse object. We will definitely return an <RcmlResponse></RcmlResponse> block.
if ( this.rcmlResult == null )
this.rcmlResult = new RcmlResponse();
// if we are switching modules, remove module-scoped variables
if ( originTarget != null && ! RvdUtils.safeEquals(target.getNodename(), originTarget.getNodename() ) ) {
if (originModuleName != null && ! originModuleName.equals(moduleName) )
clearModuleVariables();
// load steps for this module
List<String> nodeStepnames = FsProjectStorage.loadNodeStepnames(appName, moduleName, workspaceStorage);
// if no starting step has been specified in the target, use the first step of the node as default
if (startingStepName == null && !nodeStepnames.isEmpty())
startingStepName = nodeStepnames.get(0);
// Prepend step if required. Usually used for error messages
if ( prependStep != null ) {
RcmlStep rcmlStep = prependStep.render(this, moduleName );
if(RvdLoggers.local.isTraceEnabled())
RvdLoggers.local.log(Level.TRACE,LoggingHelper.buildMessage(getClass(),"interpret", "Prepending say step: " + rcmlStep ));
this.rcmlResult.steps.add( rcmlStep );
}

boolean startstep_found = false;
for (String stepname : nodeStepnames) {

// TODO make sure all the required components of the target are available here

if (target.action != null) {
// Event handling
loadStep(target.stepname).handleAction(this, target);
} else {
// RCML Generation
if (rcmlModel == null )
rcmlModel = new RcmlResponse();
List<String> nodeStepnames = FsProjectStorage.loadNodeStepnames(appName, target.getNodename(), workspaceStorage);

// if no starting step has been specified in the target, use the first step of the node as default
if (target.getStepname() == null && !nodeStepnames.isEmpty())
target.setStepname(nodeStepnames.get(0));

// Prepend step if required. Usually used for error messages
if ( prependStep != null ) {
RcmlStep rcmlStep = prependStep.render(this);
if(RvdLoggers.local.isTraceEnabled())
RvdLoggers.local.log(Level.TRACE,LoggingHelper.buildMessage(getClass(),"interpret", "Prepending say step: " + rcmlStep ));
rcmlModel.steps.add( rcmlStep );
}
if (stepname.equals(startingStepName))
startstep_found = true;

boolean startstep_found = false;
for (String stepname : nodeStepnames) {

if (stepname.equals(target.getStepname()))
startstep_found = true;

if (startstep_found) {
// we found our starting step. Let's start processing
Step step = loadStep(stepname);
String rerouteTo = step.process(this, httpRequest); // is meaningful only for some of the steps like ExternalService steps
// check if we have to break the currently rendered module
if ( rerouteTo != null )
return interpret(rerouteTo, rcmlModel, null, target);
// otherwise continue rendering the current module
RcmlStep rcmlStep = step.render(this);
if ( rcmlStep != null)
rcmlModel.steps.add(rcmlStep);
if (startstep_found) {
// we found our starting step. Let's start processing
Step step = loadStep(stepname, moduleName);
String rerouteTo = step.process(this, httpRequest); // is meaningful only for some of the steps like ExternalService steps
// check if we have to break the currently rendered module
if ( rerouteTo != null ) {
interpret(rerouteTo, null, null, moduleName);
return;
}
// otherwise continue rendering the current module
RcmlStep rcmlStep = step.render(this, moduleName);
if ( rcmlStep != null)
this.rcmlResult.steps.add(rcmlStep);
}

//rcmlResult = xstream.toXML(rcmlModel);
}

//return rcmlResult; // this is in case of an error
return rcmlModel;
}

private Step loadStep(String stepname) throws StorageException {
String stepfile_json = FsProjectStorage.loadStep(appName, target.getNodename(), stepname, workspaceStorage);

private Step loadStep(String stepname, String moduleName) throws StorageException {
String stepfile_json = FsProjectStorage.loadStep(appName, moduleName, stepname, workspaceStorage);
Step step = gson.fromJson(stepfile_json, Step.class);

return step;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public RcmlSerializer() {
}

public String serialize(RcmlResponse rcmlResponse) {
return xstream.toXML(rcmlResponse);
if (rcmlResponse == null)
return null;
else
return xstream.toXML(rcmlResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.restcomm.connect.rvd.exceptions.InterpreterException;
import org.restcomm.connect.rvd.interpreter.Interpreter;
import org.restcomm.connect.rvd.interpreter.Target;
import org.restcomm.connect.rvd.interpreter.exceptions.RVDUnsupportedHandlerVerb;
import org.restcomm.connect.rvd.jsonvalidation.ValidationErrorItem;
import org.restcomm.connect.rvd.model.rcml.RcmlStep;
Expand Down Expand Up @@ -51,9 +50,9 @@ public void setName(String name) {
this.name = name;
}

public abstract RcmlStep render(Interpreter interpreter) throws InterpreterException;
public abstract RcmlStep render(Interpreter interpreter, String containerModule) throws InterpreterException;

public void handleAction(Interpreter interpreter, Target originTarget) throws InterpreterException, StorageException {
public void handleAction(Interpreter interpreter, String handlerModule) throws InterpreterException, StorageException {
throw new RVDUnsupportedHandlerVerb();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ControlStep extends Step {


@Override
public RcmlStep render(Interpreter interpreter) throws InterpreterException {
public RcmlStep render(Interpreter interpreter, String containerModule) throws InterpreterException {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.restcomm.connect.rvd.utils.RvdUtils;
import org.restcomm.connect.rvd.exceptions.InterpreterException;
import org.restcomm.connect.rvd.interpreter.Interpreter;
import org.restcomm.connect.rvd.interpreter.Target;
import org.restcomm.connect.rvd.model.project.Step;
import org.restcomm.connect.rvd.storage.exceptions.StorageException;

Expand All @@ -28,15 +27,15 @@ public class DialStep extends Step {
private String nextModule;
private Boolean record;

public RcmlDialStep render(Interpreter interpreter) throws InterpreterException {
public RcmlDialStep render(Interpreter interpreter, String containerModule) throws InterpreterException {
RcmlDialStep rcmlStep = new RcmlDialStep();

for ( DialNoun noun: dialNouns ) {
rcmlStep.nouns.add( noun.render(interpreter) );
}

if ( ! RvdUtils.isEmpty(nextModule) ) {
String newtarget = interpreter.getTarget().getNodename() + "." + getName() + ".actionhandler";
String newtarget = containerModule + "." + getName() + ".actionhandler";
Map<String, String> pairs = new HashMap<String, String>();
pairs.put("target", newtarget);
String action = interpreter.buildAction(pairs);
Expand All @@ -53,7 +52,7 @@ public RcmlDialStep render(Interpreter interpreter) throws InterpreterException
}

@Override
public void handleAction(Interpreter interpreter, Target originTarget) throws InterpreterException, StorageException {
public void handleAction(Interpreter interpreter, String handlerModule) throws InterpreterException, StorageException {
LoggingContext logging = interpreter.getLoggingContext();
if (RvdLoggers.local.isEnabledFor(Level.INFO))
RvdLoggers.local.log(Level.INFO, LoggingHelper.buildMessage(getClass(),"handleAction", logging.getPrefix(), "handling dial action"));
Expand Down Expand Up @@ -91,7 +90,7 @@ public void handleAction(Interpreter interpreter, Target originTarget) throws In
if ( DialRingDuration != null )
interpreter.getVariables().put(RvdConfiguration.CORE_VARIABLE_PREFIX + "DialRingDuration", DialRingDuration);

interpreter.interpret( nextModule, null, null, originTarget );
interpreter.interpret( nextModule, null, null, handlerModule);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.restcomm.connect.rvd.RvdConfiguration;
import org.restcomm.connect.rvd.exceptions.InterpreterException;
import org.restcomm.connect.rvd.interpreter.Interpreter;
import org.restcomm.connect.rvd.interpreter.Target;
import org.restcomm.connect.rvd.logging.system.LoggingContext;
import org.restcomm.connect.rvd.logging.system.LoggingHelper;
import org.restcomm.connect.rvd.logging.system.RvdLoggers;
Expand Down Expand Up @@ -85,11 +84,11 @@ public void setText(String text) {
this.text = text;
}

public RcmlEmailStep render(Interpreter interpreter) {
public RcmlEmailStep render(Interpreter interpreter, String containerModule) {
RcmlEmailStep rcmlStep = new RcmlEmailStep();

if ( ! RvdUtils.isEmpty(getNext()) ) {
String newtarget = interpreter.getTarget().getNodename() + "." + getName() + ".actionhandler";
String newtarget = containerModule + "." + getName() + ".actionhandler";
Map<String, String> pairs = new HashMap<String, String>();
pairs.put("target", newtarget);
String action = interpreter.buildAction(pairs);
Expand All @@ -109,7 +108,7 @@ public RcmlEmailStep render(Interpreter interpreter) {
}

@Override
public void handleAction(Interpreter interpreter, Target originTarget) throws InterpreterException, StorageException {
public void handleAction(Interpreter interpreter, String handlerModule) throws InterpreterException, StorageException {
LoggingContext logging = interpreter.getLoggingContext();
if (RvdLoggers.local.isEnabledFor(Level.INFO))
RvdLoggers.local.log(Level.INFO, LoggingHelper.buildMessage(getClass(),"handleAction", logging.getPrefix(), "handling email action"));
Expand All @@ -125,7 +124,7 @@ public void handleAction(Interpreter interpreter, Target originTarget) throws In
if (EmailStatus != null )
interpreter.getVariables().put(RvdConfiguration.CORE_VARIABLE_PREFIX + "EmailStatus", EmailStatus);

interpreter.interpret( getNext(), null, null, originTarget );
interpreter.interpret( getNext(), null, null, handlerModule);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Integer getTimeout() {
}

@Override
public RcmlStep render(Interpreter interpreter) throws InterpreterException {
public RcmlStep render(Interpreter interpreter, String containerModule) throws InterpreterException {
// TODO Auto-generated method stub
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.restcomm.connect.rvd.utils.RvdUtils;
import org.restcomm.connect.rvd.exceptions.InterpreterException;
import org.restcomm.connect.rvd.interpreter.Interpreter;
import org.restcomm.connect.rvd.interpreter.Target;
import org.restcomm.connect.rvd.model.project.Step;
import org.restcomm.connect.rvd.storage.exceptions.StorageException;

Expand Down Expand Up @@ -60,11 +59,11 @@ public void setStatusCallback(String statusCallback) {
this.statusCallback = statusCallback;
}

public RcmlFaxStep render(Interpreter interpreter) {
public RcmlFaxStep render(Interpreter interpreter, String containerModule) {
RcmlFaxStep rcmlStep = new RcmlFaxStep();

if ( ! RvdUtils.isEmpty(getNext()) ) {
String newtarget = interpreter.getTarget().getNodename() + "." + getName() + ".actionhandler";
String newtarget = containerModule + "." + getName() + ".actionhandler";
Map<String, String> pairs = new HashMap<String, String>();
pairs.put("target", newtarget);
String action = interpreter.buildAction(pairs);
Expand All @@ -81,7 +80,7 @@ public RcmlFaxStep render(Interpreter interpreter) {
}

@Override
public void handleAction(Interpreter interpreter, Target originTarget) throws InterpreterException, StorageException {
public void handleAction(Interpreter interpreter, String handlerModule) throws InterpreterException, StorageException {
LoggingContext logging = interpreter.getLoggingContext();
if (RvdLoggers.local.isEnabledFor(Level.INFO))
RvdLoggers.local.log(Level.INFO, LoggingHelper.buildMessage(getClass(),"handleAction", logging.getPrefix(), "handling fax action"));
Expand All @@ -97,6 +96,6 @@ public void handleAction(Interpreter interpreter, Target originTarget) throws In
if (FaxStatus != null )
interpreter.getVariables().put(RvdConfiguration.CORE_VARIABLE_PREFIX + "FaxStatus", FaxStatus);

interpreter.interpret( getNext(), null, null, originTarget );
interpreter.interpret( getNext(), null, null, handlerModule);
}
}

0 comments on commit d4e73ef

Please sign in to comment.