Commit 54db31da authored by Lubomir Bulej's avatar Lubomir Bulej

shvm, shvm-dispatch: cleaned up white-space to simplify future merges.

parent 6925171c
......@@ -2,55 +2,55 @@ package ch.usi.dag.dislre;
public class REDispatch {
/**
* Register method and receive id for this transmission
*
* @param analysisMethodDesc
* @return
*/
public static native short registerMethod(String analysisMethodDesc);
/**
* Announce start of an analysis transmission
*
* @param analysisMethodDesc remote analysis method id
*/
public static native void analysisStart(short analysisMethodId);
/**
* Announce start of an analysis transmission with total ordering (among
* several threads) under the same orderingId
*
* @param analysisMethodId remote analysis method id
* @param orderingId analyses with the same orderingId are guaranteed to
* be ordered. Only non-negative values are valid.
*/
public static native void analysisStart(short analysisMethodId,
byte orderingId);
/**
* Announce end of an analysis transmission
*/
public static native void analysisEnd();
// allows transmitting types
public static native void sendBoolean(boolean booleanToSend);
public static native void sendByte(byte byteToSend);
public static native void sendChar(char charToSend);
public static native void sendShort(short shortToSend);
public static native void sendInt(int intToSend);
public static native void sendLong(long longToSend);
public static native void sendObject(Object objToSend);
public static native void sendObjectPlusData(Object objToSend);
// Methods use similar logic as Float.floatToIntBits() and
// Double.doubleToLongBits() but implemented in the native code
// to avoid perturbation
public static native void sendFloat(float floatToSend);
public static native void sendDouble(double doubleToSend);
// TODO re - basic type array support
// - send length + all values in for cycle - all in native code
// PROBLEM: somebody can change the values from the outside
// - for example different thread
/**
* Register method and receive id for this transmission
*
* @param analysisMethodDesc
* @return
*/
public static native short registerMethod(String analysisMethodDesc);
/**
* Announce start of an analysis transmission
*
* @param analysisMethodDesc remote analysis method id
*/
public static native void analysisStart(short analysisMethodId);
/**
* Announce start of an analysis transmission with total ordering (among
* several threads) under the same orderingId
*
* @param analysisMethodId remote analysis method id
* @param orderingId analyses with the same orderingId are guaranteed to
* be ordered. Only non-negative values are valid.
*/
public static native void analysisStart(short analysisMethodId,
byte orderingId);
/**
* Announce end of an analysis transmission
*/
public static native void analysisEnd();
// allows transmitting types
public static native void sendBoolean(boolean booleanToSend);
public static native void sendByte(byte byteToSend);
public static native void sendChar(char charToSend);
public static native void sendShort(short shortToSend);
public static native void sendInt(int intToSend);
public static native void sendLong(long longToSend);
public static native void sendObject(Object objToSend);
public static native void sendObjectPlusData(Object objToSend);
// Methods use similar logic as Float.floatToIntBits() and
// Double.doubleToLongBits() but implemented in the native code
// to avoid perturbation
public static native void sendFloat(float floatToSend);
public static native void sendDouble(double doubleToSend);
// TODO re - basic type array support
// - send length + all values in for cycle - all in native code
// PROBLEM: somebody can change the values from the outside
// - for example different thread
}
......@@ -14,104 +14,104 @@ import ch.usi.dag.dislreserver.reqdispatch.RequestDispatcher;
public abstract class DiSLREServer {
private static final String PROP_DEBUG = "debug";
private static final boolean debug = Boolean.getBoolean(PROP_DEBUG);
private static final String PROP_PORT = "dislreserver.port";
private static final int DEFAULT_PORT = 11218;
private static final int port = Integer.getInteger(PROP_PORT, DEFAULT_PORT);
private static ServerSocket listenSocket;
//
public static void main (final String [] args) {
if (debug) {
System.out.println("DiSL-RE: starting analysis server...");
}
try {
listenSocket = new ServerSocket (port);
if (debug) {
System.out.printf (
"DiSL-RE: listening at %s:%d\n",
listenSocket.getInetAddress ().getHostAddress (),
listenSocket.getLocalPort ()
);
}
Socket socket = listenSocket.accept ();
if (debug) {
System.out.printf (
"DiSL-RE: accepting connection from %s:%d\n",
socket.getInetAddress ().getHostAddress (),
socket.getPort ()
);
}
requestLoop (socket);
socket.close ();
} catch (final IOException ioe) {
reportError (new DiSLREServerException (ioe));
} catch (final Throwable throwable) {
reportError (throwable);
}
if (debug) {
System.out.println ("DiSL-RE: shutting down analysis server...");
}
System.exit(0); // to kill other threads
}
private static void requestLoop(Socket sock) throws DiSLREServerException {
try {
final DataInputStream is = new DataInputStream(
new BufferedInputStream(sock.getInputStream()));
final DataOutputStream os = new DataOutputStream(
new BufferedOutputStream(sock.getOutputStream()));
REQUEST_LOOP: while (true) {
final byte requestNo = is.readByte();
if (RequestDispatcher.dispatch (requestNo, is, os, debug)) {
break REQUEST_LOOP;
}
}
} catch (final IOException ioe) {
throw new DiSLREServerException (ioe);
}
}
private static void reportInnerError(Throwable e) {
Throwable cause = e.getCause();
while (cause != null && cause.getMessage() != null) {
System.err.println(" Inner error: " + cause.getMessage());
cause = cause.getCause();
}
}
private static void reportError (final Throwable throwable) {
if (throwable instanceof DiSLREServerException) {
System.err.print ("DiSL-RE: server error");
final String message = throwable.getMessage ();
System.err.println ((message != null) ? ": "+ message : "");
reportInnerError (throwable);
if (debug) {
throwable.printStackTrace ();
}
} else {
// some other exception
System.err.print ("DiSL-RE: fatal error: ");
throwable.printStackTrace ();
}
}
private static final String PROP_DEBUG = "debug";
private static final boolean debug = Boolean.getBoolean(PROP_DEBUG);
private static final String PROP_PORT = "dislreserver.port";
private static final int DEFAULT_PORT = 11218;
private static final int port = Integer.getInteger(PROP_PORT, DEFAULT_PORT);
private static ServerSocket listenSocket;
//
public static void main (final String [] args) {
if (debug) {
System.out.println("DiSL-RE: starting analysis server...");
}
try {
listenSocket = new ServerSocket (port);
if (debug) {
System.out.printf (
"DiSL-RE: listening at %s:%d\n",
listenSocket.getInetAddress ().getHostAddress (),
listenSocket.getLocalPort ()
);
}
Socket socket = listenSocket.accept ();
if (debug) {
System.out.printf (
"DiSL-RE: accepting connection from %s:%d\n",
socket.getInetAddress ().getHostAddress (),
socket.getPort ()
);
}
requestLoop (socket);
socket.close ();
} catch (final IOException ioe) {
reportError (new DiSLREServerException (ioe));
} catch (final Throwable throwable) {
reportError (throwable);
}
if (debug) {
System.out.println ("DiSL-RE: shutting down analysis server...");
}
System.exit(0); // to kill other threads
}
private static void requestLoop(Socket sock) throws DiSLREServerException {
try {
final DataInputStream is = new DataInputStream(
new BufferedInputStream(sock.getInputStream()));
final DataOutputStream os = new DataOutputStream(
new BufferedOutputStream(sock.getOutputStream()));
REQUEST_LOOP: while (true) {
final byte requestNo = is.readByte();
if (RequestDispatcher.dispatch (requestNo, is, os, debug)) {
break REQUEST_LOOP;
}
}
} catch (final IOException ioe) {
throw new DiSLREServerException (ioe);
}
}
private static void reportInnerError(Throwable e) {
Throwable cause = e.getCause();
while (cause != null && cause.getMessage() != null) {
System.err.println(" Inner error: " + cause.getMessage());
cause = cause.getCause();
}
}
private static void reportError (final Throwable throwable) {
if (throwable instanceof DiSLREServerException) {
System.err.print ("DiSL-RE: server error");
final String message = throwable.getMessage ();
System.err.println ((message != null) ? ": "+ message : "");
reportInnerError (throwable);
if (debug) {
throwable.printStackTrace ();
}
} else {
// some other exception
System.err.print ("DiSL-RE: fatal error: ");
throwable.printStackTrace ();
}
}
}
......@@ -2,21 +2,21 @@ package ch.usi.dag.dislreserver.exception;
public class DiSLREServerException extends Exception {
private static final long serialVersionUID = 5272000884539359236L;
private static final long serialVersionUID = 5272000884539359236L;
public DiSLREServerException() {
super();
}
public DiSLREServerException() {
super();
}
public DiSLREServerException(String message, Throwable cause) {
super(message, cause);
}
public DiSLREServerException(String message, Throwable cause) {
super(message, cause);
}
public DiSLREServerException(String message) {
super(message);
}
public DiSLREServerException(String message) {
super(message);
}
public DiSLREServerException(Throwable cause) {
super(cause);
}
public DiSLREServerException(Throwable cause) {
super(cause);
}
}
......@@ -2,20 +2,20 @@ package ch.usi.dag.dislreserver.exception;
public class DiSLREServerFatalException extends RuntimeException {
private static final long serialVersionUID = -8431771285237240263L;
private static final long serialVersionUID = -8431771285237240263L;
public DiSLREServerFatalException() {
}
public DiSLREServerFatalException() {
}
public DiSLREServerFatalException(String message) {
super(message);
}
public DiSLREServerFatalException(String message) {
super(message);
}
public DiSLREServerFatalException(Throwable cause) {
super(cause);
}
public DiSLREServerFatalException(Throwable cause) {
super(cause);
}
public DiSLREServerFatalException(String message, Throwable cause) {
super(message, cause);
}
public DiSLREServerFatalException(String message, Throwable cause) {
super(message, cause);
}
}
......@@ -5,35 +5,35 @@ import java.util.List;
public class AnalysisInvocation {
private Method analysisMethod;
private Object analysisInstance;
private List<Object> args;
public AnalysisInvocation (
Method analysisMethod, Object analysisInstance,
List<Object> args
) {
super();
this.analysisMethod = analysisMethod;
this.analysisInstance = analysisInstance;
this.args = args;
}
public void invoke () {
try {
analysisMethod.invoke (analysisInstance, args.toArray());
} catch (final Exception e) {
// report error during analysis invocation
final Throwable cause = e.getCause ();
System.err.format (
"DiSL-RE: exception in analysis %s.%s(): ",
analysisMethod.getDeclaringClass ().getName (),
analysisMethod.getName ()
);
cause.printStackTrace();
}
}
private Method analysisMethod;
private Object analysisInstance;
private List<Object> args;
public AnalysisInvocation (
Method analysisMethod, Object analysisInstance,
List<Object> args
) {
super();
this.analysisMethod = analysisMethod;
this.analysisInstance = analysisInstance;
this.args = args;
}
public void invoke () {
try {
analysisMethod.invoke (analysisInstance, args.toArray());
} catch (final Exception e) {
// report error during analysis invocation
final Throwable cause = e.getCause ();
System.err.format (
"DiSL-RE: exception in analysis %s.%s(): ",
analysisMethod.getDeclaringClass ().getName (),
analysisMethod.getName ()
);
cause.printStackTrace();
}
}
}
......@@ -13,131 +13,131 @@ import ch.usi.dag.dislreserver.exception.DiSLREServerFatalException;
import ch.usi.dag.dislreserver.remoteanalysis.RemoteAnalysis;
public final class AnalysisResolver {
private static final String METHOD_DELIM = ".";
private static final Map <Short, AnalysisMethodHolder>
methodMap = new HashMap <Short, AnalysisMethodHolder> ();
private static final Map <String, RemoteAnalysis>
analysisMap = new HashMap <String, RemoteAnalysis> ();
// for fast set access - contains all values from analysisMap
private static final Set <RemoteAnalysis>
analysisSet = new HashSet <RemoteAnalysis> ();
//
public static final class AnalysisMethodHolder {
private final RemoteAnalysis analysisInstance;
private final Method analysisMethod;
public AnalysisMethodHolder(
final RemoteAnalysis analysisInstance, final Method analysisMethod
) {
this.analysisInstance = analysisInstance;
this.analysisMethod = analysisMethod;
}
public RemoteAnalysis getAnalysisInstance() {
return analysisInstance;
}
public Method getAnalysisMethod() {
return analysisMethod;
}
}
//
private static AnalysisMethodHolder resolveMethod (String methodStr
) throws DiSLREServerException {
try {
int classNameEnd = methodStr.lastIndexOf (METHOD_DELIM);
// without METHOD_DELIM
String className = methodStr.substring (0, classNameEnd);
String methodName = methodStr.substring (classNameEnd + 1);
// resolve analysis instance
RemoteAnalysis raInst = analysisMap.get (className);
if (raInst == null) {
// resolve class
Class <?> raClass = Class.forName (className);
// create instance
raInst = (RemoteAnalysis) raClass.newInstance ();
analysisMap.put (className, raInst);
analysisSet.add (raInst);
}
// resolve analysis method
final Method raMethod = __getAnalysisMethod (raInst, methodName);
return new AnalysisMethodHolder(raInst, raMethod);
}
catch (ClassNotFoundException e) {
throw new DiSLREServerException(e);
} catch (InstantiationException e) {
throw new DiSLREServerException(e);
} catch (IllegalAccessException e) {
throw new DiSLREServerException(e);
}
}
private static Method __getAnalysisMethod (
final RemoteAnalysis analysis, final String methodName
) throws DiSLREServerException {
final Class <?> analysisClass = analysis.getClass ();
final List <Method> methods = new ArrayList <Method> ();
for (final Method analysisMethod : analysisClass.getMethods ()) {
if (analysisMethod.getName ().equals (methodName)) {
methods.add (analysisMethod);
}
}
//
// Throw an exception if there are multiple methods
//
final int methodCount = methods.size ();
if (methodCount == 1) {
return methods.get (0);
} else if (methodCount > 1) {
throw new DiSLREServerException (String.format (
"Multiple methods matching \"%s\" found in %s",
methodName, analysisClass.getName ()
));
} else {
throw new DiSLREServerException (String.format (
"No method matching \"%s\" found in %s",
methodName, analysisClass.getName ()
));
}
}
static AnalysisMethodHolder getMethod (final short methodId)
throws DiSLREServerException {
AnalysisMethodHolder result = methodMap.get (methodId);
if (result == null) {
throw new DiSLREServerFatalException ("Unknown method id: "+ methodId);
}
return result;
}
public static void registerMethodId (
final short methodId, String methodString
) throws DiSLREServerException {
methodMap.put(methodId, resolveMethod(methodString));
}
public static Set <RemoteAnalysis> getAllAnalyses () {
return analysisSet;
}
private static final String METHOD_DELIM = ".";
private static final Map <Short, AnalysisMethodHolder>
methodMap = new HashMap <Short, AnalysisMethodHolder> ();
private static final Map <String, RemoteAnalysis>
analysisMap = new HashMap <String, RemoteAnalysis> ();
// for fast set access - contains all values from analysisMap
private static final Set <RemoteAnalysis>
analysisSet = new HashSet <RemoteAnalysis> ();
//
public static final class AnalysisMethodHolder {
private final RemoteAnalysis analysisInstance;
private final Method analysisMethod;
public AnalysisMethodHolder(
final RemoteAnalysis analysisInstance, final Method analysisMethod
) {
this.analysisInstance = analysisInstance;
this.analysisMethod = analysisMethod;
}
public RemoteAnalysis getAnalysisInstance() {
return analysisInstance;
}
public Method getAnalysisMethod() {
return analysisMethod;
}
}
//
private static AnalysisMethodHolder resolveMethod (String methodStr
) throws DiSLREServerException {
try {
int classNameEnd = methodStr.lastIndexOf (METHOD_DELIM);
// without METHOD_DELIM
String className = methodStr.substring (0, classNameEnd);
String methodName = methodStr.substring (classNameEnd + 1);
// resolve analysis instance
RemoteAnalysis raInst = analysisMap.get (className);
if (raInst == null) {
// resolve class
Class <?> raClass = Class.forName (className);
// create instance
raInst = (RemoteAnalysis) raClass.newInstance ();
analysisMap.put (className, raInst);
analysisSet.add (raInst);
}
// resolve analysis method
final Method raMethod = __getAnalysisMethod (raInst, methodName);
return new AnalysisMethodHolder(raInst, raMethod);
}
catch (ClassNotFoundException e) {
throw new DiSLREServerException(e);
} catch (InstantiationException e) {
throw new DiSLREServerException(e);
} catch (IllegalAccessException e) {
throw new DiSLREServerException(e);
}
}
private static Method __getAnalysisMethod (
final RemoteAnalysis analysis, final String methodName
) throws DiSLREServerException {
final Class <?> analysisClass = analysis.getClass ();
final List <Method> methods = new ArrayList <Method> ();
for (final Method analysisMethod : analysisClass.getMethods ()) {
if (analysisMethod.getName ().equals (methodName)) {
methods.add (analysisMethod);
}
}
//
// Throw an exception if there are multiple methods
//
final int methodCount = methods.size ();
if (methodCount == 1) {
return methods.get (0);
} else if (methodCount > 1) {
throw new DiSLREServerException (String.format (