diff --git a/src/debug/org/luaj/debug/DebugLuaState.java b/src/debug/org/luaj/debug/DebugLuaState.java index 73511ed1..a5c47ca2 100644 --- a/src/debug/org/luaj/debug/DebugLuaState.java +++ b/src/debug/org/luaj/debug/DebugLuaState.java @@ -37,9 +37,8 @@ import org.luaj.debug.request.DebugRequestLineBreakpointToggle; import org.luaj.debug.request.DebugRequestListener; import org.luaj.debug.request.DebugRequestStack; import org.luaj.debug.request.DebugRequestType; -import org.luaj.debug.response.DebugResponse; import org.luaj.debug.response.DebugResponseCallgraph; -import org.luaj.debug.response.DebugResponseSimple; +import org.luaj.debug.response.DebugResponseStack; import org.luaj.debug.response.DebugResponseVariables; import org.luaj.vm.CallInfo; import org.luaj.vm.LClosure; @@ -192,7 +191,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { if (DebugUtils.IS_DEBUG) DebugUtils.println("debugHook - executing line: " + line); - + int i = currentProto.code[pc]; int opCode = LuaState.GET_OPCODE(i); if (isStepping() && opCode == LuaState.OP_RETURN && cc == 0) { @@ -298,7 +297,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { // ------------------ commands coming from the debugger ------------------- - public DebugResponse handleRequest(DebugRequest request) { + public void handleRequest(DebugRequest request) { if (this.debugSupport == null) { throw new IllegalStateException( "DebugStackState is not equiped with DebugSupport."); @@ -313,60 +312,59 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { DebugEvent event = new DebugEvent(DebugEventType.started); debugSupport.notifyDebugEvent(event); cancelSuspendOnStart(); - return DebugResponseSimple.SUCCESS; } else if (DebugRequestType.exit == requestType) { stop(); - return DebugResponseSimple.SUCCESS; } else if (DebugRequestType.suspend == requestType) { suspend(); DebugEvent event = new DebugEvent(DebugEventType.suspendedByClient); debugSupport.notifyDebugEvent(event); - return DebugResponseSimple.SUCCESS; } else if (DebugRequestType.resume == requestType) { resume(); DebugEvent event = new DebugEvent(DebugEventType.resumedByClient); debugSupport.notifyDebugEvent(event); - return DebugResponseSimple.SUCCESS; } else if (DebugRequestType.lineBreakpointSet == requestType) { - DebugRequestLineBreakpointToggle setBreakpointRequest = (DebugRequestLineBreakpointToggle) request; + DebugRequestLineBreakpointToggle setBreakpointRequest + = (DebugRequestLineBreakpointToggle) request; setBreakpoint(setBreakpointRequest.getSource(), setBreakpointRequest.getLineNumber()); - return DebugResponseSimple.SUCCESS; } else if (DebugRequestType.lineBreakpointClear == requestType) { - DebugRequestLineBreakpointToggle clearBreakpointRequest = (DebugRequestLineBreakpointToggle) request; + DebugRequestLineBreakpointToggle clearBreakpointRequest + = (DebugRequestLineBreakpointToggle) request; clearBreakpoint(clearBreakpointRequest.getSource(), clearBreakpointRequest.getLineNumber()); - return DebugResponseSimple.SUCCESS; } else if (DebugRequestType.callgraph == requestType) { - return new DebugResponseCallgraph(getCallgraph()); + DebugResponseCallgraph callgraph + = new DebugResponseCallgraph(getCallgraph()); + debugSupport.notifyDebugEvent(callgraph); } else if (DebugRequestType.stack == requestType) { DebugRequestStack stackRequest = (DebugRequestStack) request; int index = stackRequest.getIndex(); - return new DebugResponseVariables(getStack(index)); + DebugResponseStack stackState + = new DebugResponseStack(index, getStack(index)); + debugSupport.notifyDebugEvent(stackState); } else if (DebugRequestType.global == requestType) { - return new DebugResponseVariables(getGlobals()); + DebugResponseVariables globals + = new DebugResponseVariables(getGlobals(), DebugEventType.clientRequestGlobalReply); + debugSupport.notifyDebugEvent(globals); } else if (DebugRequestType.stepInto == requestType) { DebugEvent event = new DebugEvent( DebugEventType.resumedOnSteppingInto); debugSupport.notifyDebugEvent(event); stepInto(); - return DebugResponseSimple.SUCCESS; } else if (DebugRequestType.stepOver == requestType) { DebugEvent event = new DebugEvent( DebugEventType.resumedOnSteppingOver); debugSupport.notifyDebugEvent(event); stepOver(); - return DebugResponseSimple.SUCCESS; } else if (DebugRequestType.stepReturn == requestType) { DebugEvent event = new DebugEvent( DebugEventType.resumedOnSteppingReturn); debugSupport.notifyDebugEvent(event); stepReturn(); - return DebugResponseSimple.SUCCESS; + } else { + throw new java.lang.IllegalArgumentException("unkown request type: " + + request.getType()); } - - throw new java.lang.IllegalArgumentException("unkown request type: " - + request.getType()); } /** diff --git a/src/debug/org/luaj/debug/DebugSupport.java b/src/debug/org/luaj/debug/DebugSupport.java index 75a775d6..7cf7dd22 100644 --- a/src/debug/org/luaj/debug/DebugSupport.java +++ b/src/debug/org/luaj/debug/DebugSupport.java @@ -9,37 +9,28 @@ import org.luaj.debug.event.DebugEvent; import org.luaj.debug.event.DebugEventListener; import org.luaj.debug.request.DebugRequest; import org.luaj.debug.request.DebugRequestListener; -import org.luaj.debug.response.DebugResponse; /** * DebugSupport provides the network communication support for the debugger and * debugee. */ -public class DebugSupport implements DebugRequestListener, DebugEventListener { +public abstract class DebugSupport implements DebugRequestListener, DebugEventListener { protected static final int UNKNOWN = 0; protected static final int RUNNING = 1; protected static final int STOPPED = 2; protected DebugLuaState vm; - protected int requestPort; - protected int eventPort; + protected int debugPort; protected Thread requestWatcherThread; protected int state = UNKNOWN; protected DataInputStream requestReader; - protected DataOutputStream requestWriter; protected DataOutputStream eventWriter; - public DebugSupport(int requestPort, int eventPort) { - if (requestPort == -1) { + public DebugSupport(int debugPort) { + if (debugPort == -1) { throw new IllegalArgumentException("requestPort is invalid"); } - - if (eventPort == -1) { - throw new IllegalArgumentException("eventPort is invalid"); - } - - this.requestPort = requestPort; - this.eventPort = eventPort; + this.debugPort = debugPort; } public void setDebugStackState(DebugLuaState vm) { @@ -57,14 +48,6 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener { } } - if (requestWriter != null) { - try { - requestWriter.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (eventWriter != null) { try { eventWriter.close(); @@ -81,7 +64,7 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener { public synchronized void start() throws IOException { if (this.vm == null) { throw new IllegalStateException( - "DebugStackState is not set. Please call setDebugStackState first."); + "DebugLuaState is not set. Please call setDebugStackState first."); } this.requestWatcherThread = new Thread(new Runnable() { @@ -93,8 +76,7 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener { this.requestWatcherThread.start(); this.state = RUNNING; - System.out.println("LuaJ debug server is started on ports: " - + requestPort + ", " + eventPort); + System.out.println("LuaJ debug server is listening on port: " + debugPort); } protected synchronized int getState() { @@ -107,28 +89,26 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener { this.state = STOPPED; } + public abstract Object getClientConnection(); + protected void loopForRequests() { try { while (getState() != STOPPED) { + byte[] data = null; int size = requestReader.readInt(); - byte[] data = new byte[size]; + data = new byte[size]; requestReader.readFully(data); + DebugRequest request = (DebugRequest) SerializationHelper - .deserialize(data); - if (DebugUtils.IS_DEBUG) - DebugUtils.println("SERVER receives request: " - + request.toString()); - - DebugResponse response = handleRequest(request); - data = SerializationHelper.serialize(response); - requestWriter.writeInt(data.length); - requestWriter.write(data); - requestWriter.flush(); - if (DebugUtils.IS_DEBUG) - DebugUtils.println("SERVER sends response: " + response); + .deserialize(data); + if (DebugUtils.IS_DEBUG) { + DebugUtils.println("SERVER receives request: " + request.toString()); + } + + handleRequest(request); } } catch (EOFException e) { - // expected during shutdown + // expected. it may occur depending on the timing during the termination } catch (Exception e) { e.printStackTrace(); } @@ -184,12 +164,11 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener { * * @see org.luaj.debug.request.DebugRequestListener#handleRequest(org.luaj.debug.request.DebugRequest) */ - public DebugResponse handleRequest(DebugRequest request) { + public void handleRequest(DebugRequest request) { if (DebugUtils.IS_DEBUG) { - DebugUtils.println("handling request: " + request.toString()); + DebugUtils.println("SERVER handling request: " + request.toString()); } - DebugResponse response = vm.handleRequest(request); - return response; + vm.handleRequest(request); } } \ No newline at end of file diff --git a/src/debug/org/luaj/debug/SerializationHelper.java b/src/debug/org/luaj/debug/SerializationHelper.java index 18574276..7e52092f 100644 --- a/src/debug/org/luaj/debug/SerializationHelper.java +++ b/src/debug/org/luaj/debug/SerializationHelper.java @@ -15,7 +15,7 @@ import org.luaj.debug.request.DebugRequestLineBreakpointToggle; import org.luaj.debug.request.DebugRequestStack; import org.luaj.debug.request.DebugRequestType; import org.luaj.debug.response.DebugResponseCallgraph; -import org.luaj.debug.response.DebugResponseSimple; +import org.luaj.debug.response.DebugResponseStack; import org.luaj.debug.response.DebugResponseVariables; public class SerializationHelper { @@ -46,21 +46,21 @@ public class SerializationHelper { return object; } - static final int SERIAL_TYPE_NullableString = 0; - static final int SERIAL_TYPE_TableVariable = 1; - static final int SERIAL_TYPE_Variable = 2; - static final int SERIAL_TYPE_DebugResponseCallgraph = 3; - static final int SERIAL_TYPE_DebugResponseVariables = 4; - static final int SERIAL_TYPE_DebugResponseSimple = 5; - static final int SERIAL_TYPE_StackFrame = 6; - static final int SERIAL_TYPE_DebugRequestType = 7; - static final int SERIAL_TYPE_DebugRequest = 8; - static final int SERIAL_TYPE_DebugRequestStack = 9; - static final int SERIAL_TYPE_DebugRequestLineBreakpointToggle = 10; - static final int SERIAL_TYPE_DebugEventType = 11; - static final int SERIAL_TYPE_DebugEvent = 12; - static final int SERIAL_TYPE_DebugEventBreakpoint = 13; - static final int SERIAL_TYPE_DebugEventError = 14; + static final int SERIAL_TYPE_NullableString = 0; + static final int SERIAL_TYPE_TableVariable = 1; + static final int SERIAL_TYPE_Variable = 2; + static final int SERIAL_TYPE_DebugResponseCallgraph = 3; + static final int SERIAL_TYPE_DebugResponseVariables = 4; + static final int SERIAL_TYPE_DebugResponseStack = 5; + static final int SERIAL_TYPE_StackFrame = 6; + static final int SERIAL_TYPE_DebugRequestType = 7; + static final int SERIAL_TYPE_DebugRequest = 8; + static final int SERIAL_TYPE_DebugRequestStack = 9; + static final int SERIAL_TYPE_DebugRequestLineBreakpointToggle = 10; + static final int SERIAL_TYPE_DebugEventType = 11; + static final int SERIAL_TYPE_DebugEvent = 12; + static final int SERIAL_TYPE_DebugEventBreakpoint = 13; + static final int SERIAL_TYPE_DebugEventError = 14; public static void serialize(Serializable object, DataOutputStream dout) throws IOException { @@ -76,9 +76,9 @@ public class SerializationHelper { } else if (object instanceof StackFrame) { dout.writeInt(SERIAL_TYPE_StackFrame); StackFrame.serialize(dout, (StackFrame) object); - } else if (object instanceof DebugResponseSimple) { - dout.writeInt(SERIAL_TYPE_DebugResponseSimple); - DebugResponseSimple.serialize(dout, (DebugResponseSimple) object); + } else if (object instanceof DebugResponseStack) { + dout.writeInt(SERIAL_TYPE_DebugResponseStack); + DebugResponseStack.serialize(dout, (DebugResponseStack) object); } else if (object instanceof DebugResponseVariables) { dout.writeInt(SERIAL_TYPE_DebugResponseVariables); DebugResponseVariables.serialize(dout, (DebugResponseVariables) object); @@ -136,12 +136,12 @@ public class SerializationHelper { case SERIAL_TYPE_StackFrame: object = StackFrame.deserialize(din); break; - case SERIAL_TYPE_DebugResponseSimple: - object = DebugResponseSimple.deserialize(din); - break; case SERIAL_TYPE_DebugResponseCallgraph: object = DebugResponseCallgraph.deserialize(din); break; + case SERIAL_TYPE_DebugResponseStack: + object = DebugResponseStack.deserialize(din); + break; case SERIAL_TYPE_DebugResponseVariables: object = DebugResponseVariables.deserialize(din); break; diff --git a/src/debug/org/luaj/debug/event/DebugEvent.java b/src/debug/org/luaj/debug/event/DebugEvent.java index d345d15b..4fbfbf40 100644 --- a/src/debug/org/luaj/debug/event/DebugEvent.java +++ b/src/debug/org/luaj/debug/event/DebugEvent.java @@ -1,24 +1,24 @@ /******************************************************************************* -* Copyright (c) 2007 LuaJ. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -* THE SOFTWARE. -******************************************************************************/ + * Copyright (c) 2007 LuaJ. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ package org.luaj.debug.event; import java.io.DataInputStream; @@ -29,14 +29,14 @@ import org.luaj.debug.Serializable; import org.luaj.debug.SerializationHelper; -public class DebugEvent implements Serializable { - +public class DebugEvent implements Serializable { + protected DebugEventType type; public DebugEvent(DebugEventType type) { this.type = type; } - + public DebugEventType getType() { return type; } @@ -45,20 +45,23 @@ public class DebugEvent implements Serializable { this.type = type; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see java.lang.Object#toString() */ public String toString() { return type.toString(); } - - public static void serialize(DataOutputStream out, DebugEvent object) - throws IOException { - SerializationHelper.serialize(object.getType(), out); - } - public static DebugEvent deserialize(DataInputStream in) throws IOException { - DebugEventType type = (DebugEventType) SerializationHelper.deserialize(in); - return new DebugEvent(type); - } + public static void serialize(DataOutputStream out, DebugEvent object) + throws IOException { + SerializationHelper.serialize(object.getType(), out); + } + + public static DebugEvent deserialize(DataInputStream in) throws IOException { + DebugEventType type = (DebugEventType) SerializationHelper + .deserialize(in); + return new DebugEvent(type); + } } diff --git a/src/debug/org/luaj/debug/event/DebugEventBreakpoint.java b/src/debug/org/luaj/debug/event/DebugEventBreakpoint.java index daa5fd36..39e10eec 100644 --- a/src/debug/org/luaj/debug/event/DebugEventBreakpoint.java +++ b/src/debug/org/luaj/debug/event/DebugEventBreakpoint.java @@ -1,24 +1,24 @@ /******************************************************************************* -* Copyright (c) 2007 LuaJ. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -* THE SOFTWARE. -******************************************************************************/ + * Copyright (c) 2007 LuaJ. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ package org.luaj.debug.event; import java.io.DataInputStream; @@ -27,47 +27,52 @@ import java.io.IOException; public class DebugEventBreakpoint extends DebugEvent { protected String source; + protected int lineNumber; public DebugEventBreakpoint(String source, int lineNumber) { super(DebugEventType.suspendedOnBreakpoint); if (source == null) { - throw new IllegalArgumentException("argument source cannot be null"); + throw new IllegalArgumentException("argument source cannot be null"); } - + if (lineNumber <= 0) { - throw new IllegalArgumentException("argument lineNumber must be positive integer"); + throw new IllegalArgumentException( + "argument lineNumber must be positive integer"); } - + this.source = source; this.lineNumber = lineNumber; } - + public String getSource() { - return this.source; + return this.source; } - + public int getLineNumber() { return this.lineNumber; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see lua.debug.DebugEvent#toString() */ public String toString() { - return super.toString() + " source:" + getSource() + " line:" + getLineNumber(); + return super.toString() + " source:" + getSource() + " line:" + + getLineNumber(); } - - public static void serialize(DataOutputStream out, DebugEventBreakpoint object) - throws IOException { - out.writeUTF(object.getSource()); - out.writeInt(object.getLineNumber()); - } - public static DebugEvent deserialize(DataInputStream in) throws IOException { - String source = in.readUTF(); - int lineNo = in.readInt(); - - return new DebugEventBreakpoint(source, lineNo); - } + public static void serialize(DataOutputStream out, + DebugEventBreakpoint object) throws IOException { + out.writeUTF(object.getSource()); + out.writeInt(object.getLineNumber()); + } + + public static DebugEvent deserialize(DataInputStream in) throws IOException { + String source = in.readUTF(); + int lineNo = in.readInt(); + + return new DebugEventBreakpoint(source, lineNo); + } } diff --git a/src/debug/org/luaj/debug/event/DebugEventType.java b/src/debug/org/luaj/debug/event/DebugEventType.java index 5367dc9f..e4caa29c 100644 --- a/src/debug/org/luaj/debug/event/DebugEventType.java +++ b/src/debug/org/luaj/debug/event/DebugEventType.java @@ -29,7 +29,7 @@ import org.luaj.debug.EnumType; public class DebugEventType extends EnumType { - public static DebugEventType started = new DebugEventType("started", 0); + public static DebugEventType started = new DebugEventType("started", 0); public static DebugEventType suspendedByClient = new DebugEventType("suspendedByClient", 1); public static DebugEventType suspendedOnBreakpoint = new DebugEventType("suspendedOnBreakpoint", 2); public static DebugEventType suspendedOnWatchpoint = new DebugEventType("suspendedOnWatchpoint", 3); @@ -43,6 +43,12 @@ public class DebugEventType extends EnumType { public static DebugEventType resumedOnError = new DebugEventType("resumedOnError", 11); public static DebugEventType error = new DebugEventType("error", 12); public static DebugEventType terminated = new DebugEventType("terminated", 13); + public static DebugEventType clientRequestCallgraphReply + = new DebugEventType("clientRequestCallgraphReply", 14); + public static DebugEventType clientRequestStackReply + = new DebugEventType("clientRequestStackReply", 15); + public static DebugEventType clientRequestGlobalReply + = new DebugEventType("clientRequestGlobalReply", 16); protected static DebugEventType[] ENUMS = new DebugEventType[] { started, @@ -58,7 +64,10 @@ public class DebugEventType extends EnumType { resumedOnSteppingEnd, resumedOnError, error, - terminated + terminated, + clientRequestCallgraphReply, + clientRequestStackReply, + clientRequestGlobalReply }; protected DebugEventType(String name, int ordinal) { diff --git a/src/debug/org/luaj/debug/j2me/DebugSupportImpl.java b/src/debug/org/luaj/debug/j2me/DebugSupportImpl.java index 4e8f1eba..7a7e16b3 100644 --- a/src/debug/org/luaj/debug/j2me/DebugSupportImpl.java +++ b/src/debug/org/luaj/debug/j2me/DebugSupportImpl.java @@ -9,90 +9,63 @@ import javax.microedition.io.SocketConnection; import org.luaj.debug.DebugSupport; import org.luaj.debug.event.DebugEvent; - public class DebugSupportImpl extends DebugSupport { - protected ServerSocketConnection requestServerConnection; - protected SocketConnection requestSocketConnection; + protected ServerSocketConnection serverConnection; + protected SocketConnection clientDebugConnection; + protected SocketConnection eventSocketConnection; - protected ServerSocketConnection eventServerConnection; - protected SocketConnection eventSocketConnection; - - public DebugSupportImpl(int requestPort, - int eventPort) { - super(requestPort, eventPort); - } - - /* (non-Javadoc) - * @see lua.debug.j2se.DebugSupport#start() - */ + public DebugSupportImpl(int debugPort) { + super(debugPort); + } + + /* + * (non-Javadoc) + * + * @see lua.debug.j2se.DebugSupport#start() + */ public synchronized void start() throws IOException { - System.out.println("Starting the sockets...."); - // Set up the request socket and request input + output streams - this.requestServerConnection - = (ServerSocketConnection)Connector.open("socket://:" + this.requestPort); - this.requestSocketConnection = - (SocketConnection) requestServerConnection.acceptAndOpen(); - requestSocketConnection.setSocketOption(SocketConnection.DELAY, 0); - requestSocketConnection.setSocketOption(SocketConnection.LINGER, 0); - requestSocketConnection.setSocketOption(SocketConnection.KEEPALIVE, 1); - requestSocketConnection.setSocketOption(SocketConnection.RCVBUF, 1024); - requestSocketConnection.setSocketOption(SocketConnection.SNDBUF, 1024); - this.requestReader = requestSocketConnection.openDataInputStream(); - this.requestWriter = requestSocketConnection.openDataOutputStream(); + // Set up the request socket and request input + event output streams + this.serverConnection = (ServerSocketConnection) Connector + .open("socket://:" + this.debugPort); + this.clientDebugConnection = (SocketConnection) serverConnection + .acceptAndOpen(); + clientDebugConnection.setSocketOption(SocketConnection.DELAY, 0); + clientDebugConnection.setSocketOption(SocketConnection.LINGER, 0); + clientDebugConnection.setSocketOption(SocketConnection.KEEPALIVE, 1); + clientDebugConnection.setSocketOption(SocketConnection.RCVBUF, 1024); + clientDebugConnection.setSocketOption(SocketConnection.SNDBUF, 1024); + this.requestReader = clientDebugConnection.openDataInputStream(); + this.eventWriter = clientDebugConnection.openDataOutputStream(); - // Set up the event socket and event output stream - this.eventServerConnection - = (ServerSocketConnection)Connector.open("socket://:" + this.eventPort); - this.eventSocketConnection - = (SocketConnection) eventServerConnection.acceptAndOpen(); - eventSocketConnection.setSocketOption(SocketConnection.DELAY, 0); - eventSocketConnection.setSocketOption(SocketConnection.LINGER, 0); - eventSocketConnection.setSocketOption(SocketConnection.KEEPALIVE, 1); - eventSocketConnection.setSocketOption(SocketConnection.RCVBUF, 1024); - eventSocketConnection.setSocketOption(SocketConnection.SNDBUF, 1024); - this.eventWriter = eventSocketConnection.openDataOutputStream();; - - System.out.println("Lua debug server is started on ports: " + requestPort + ", " + eventPort); + System.out.println("Lua debug server is started on ports: " + debugPort); super.start(); } protected void dispose() { - super.dispose(); - - if (requestSocketConnection != null) { + super.dispose(); + + if (this.clientDebugConnection != null) { try { - requestSocketConnection.close(); - } catch (IOException e) {} + clientDebugConnection.close(); + } catch (IOException e) { + } } - - if (requestServerConnection != null) { + + if (this.serverConnection != null) { try { - requestServerConnection.close(); - } catch (IOException e) {} - } - - if (eventSocketConnection != null) { - try { - eventSocketConnection.close(); - } catch (IOException e) {} - } - - if (eventServerConnection != null){ - try { - eventServerConnection.close(); - } catch (IOException e) {} + serverConnection.close(); + } catch (IOException e) { + } } } - - protected void loopForRequests() { - synchronized (requestSocketConnection) { - super.loopForRequests(); - } + + public Object getClientConnection() { + return clientDebugConnection; } - + protected void sendEvent(DebugEvent event) { synchronized (eventSocketConnection) { - super.sendEvent(event); + super.sendEvent(event); } } } diff --git a/src/debug/org/luaj/debug/j2se/DebugSupportImpl.java b/src/debug/org/luaj/debug/j2se/DebugSupportImpl.java index 6c3e728f..038edcd5 100644 --- a/src/debug/org/luaj/debug/j2se/DebugSupportImpl.java +++ b/src/debug/org/luaj/debug/j2se/DebugSupportImpl.java @@ -32,31 +32,23 @@ import org.luaj.debug.event.DebugEvent; public class DebugSupportImpl extends DebugSupport { - protected ServerSocket requestSocket; - protected Socket clientRequestSocket; - - protected ServerSocket eventSocket; - protected Socket clientEventSocket; + protected ServerSocket serverSocket; + protected Socket clientSocket; - public DebugSupportImpl(int requestPort, int eventPort) { - super(requestPort, eventPort); + public DebugSupportImpl(int debugPort) { + super(debugPort); } /* (non-Javadoc) * @see lua.debug.j2se.DebugSupport#start() */ public synchronized void start() throws IOException { - this.requestSocket = new ServerSocket(requestPort); - this.clientRequestSocket = requestSocket.accept(); + this.serverSocket = new ServerSocket(debugPort, 1); + this.clientSocket = serverSocket.accept(); this.requestReader - = new DataInputStream(clientRequestSocket.getInputStream()); - this.requestWriter - = new DataOutputStream(clientRequestSocket.getOutputStream()); - - this.eventSocket = new ServerSocket(eventPort); - this.clientEventSocket = eventSocket.accept(); + = new DataInputStream(clientSocket.getInputStream()); this.eventWriter - = new DataOutputStream(clientEventSocket.getOutputStream()); + = new DataOutputStream(clientSocket.getOutputStream()); super.start(); } @@ -64,35 +56,21 @@ public class DebugSupportImpl extends DebugSupport { protected void dispose() { super.dispose(); - if (clientRequestSocket != null) { + if (this.clientSocket != null) { try { - clientRequestSocket.close(); + clientSocket.close(); } catch (IOException e) {} } - if (requestSocket != null) { + if (this.serverSocket != null) { try { - requestSocket.close(); + serverSocket.close(); } catch (IOException e) {} - } - - if (clientEventSocket != null) { - try { - clientEventSocket.close(); - } catch (IOException e) {} - } - - if (eventSocket != null){ - try { - eventSocket.close(); - } catch (IOException e) {} - } + } } - protected void loopForRequests() { - synchronized (clientRequestSocket) { - super.loopForRequests(); - } + public Object getClientConnection() { + return clientSocket; } /** @@ -113,8 +91,8 @@ public class DebugSupportImpl extends DebugSupport { * @param event */ protected void sendEvent(DebugEvent event) { - synchronized (eventSocket) { - super.sendEvent(event); + synchronized (clientSocket) { + super.sendEvent(event); } } } diff --git a/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java b/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java index fec0c522..ce454bd7 100644 --- a/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java +++ b/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java @@ -1,24 +1,24 @@ /******************************************************************************* -* Copyright (c) 2007 LuaJ. All rights reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -* THE SOFTWARE. -******************************************************************************/ + * Copyright (c) 2007 LuaJ. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + ******************************************************************************/ package org.luaj.debug.j2se; import java.io.File; @@ -39,7 +39,6 @@ import org.luaj.vm.LValue; import org.luaj.vm.LoadState; import org.luaj.vm.LuaState; - /** * StandardLuaJVM executes a lua program in normal run mode or debug mode. * @@ -47,106 +46,123 @@ import org.luaj.vm.LuaState; * @version: 1.0 */ public class StandardLuaJVM { + private static final String CMD_LINE_DEBUG_OPTION_SUSPEND_ON_START = "suspendOnStart="; + private static final String CMD_LINE_DEBUG_OPTION_PORT = "port="; protected boolean isDebugMode = false; - protected int requestPort; - protected int eventPort; + protected int debugPort = -1;; + protected boolean bSuspendOnStart = false; protected String script; protected String[] scriptArgs; protected LuaState state; protected boolean isReady = false; - protected boolean isTerminated = false; - + protected boolean isTerminated = false; + // command line parsing utilities class ParseException extends Exception { - private static final long serialVersionUID = -3134938136698856577L; + private static final long serialVersionUID = -3134938136698856577L; - public ParseException(String message) { - super(message); - } + public ParseException(String message) { + super(message); + } } - + protected void printUsage() { System.out.println("Usage:"); - System.out.println("\t java StandardLuaJVM [-debug ]