Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
copied all py4j sources to Eclipse. fixed a compilation error in SWTG…
Browse files Browse the repository at this point in the history
…atewayServer (really!!)
  • Loading branch information
Barthelemy Dagenais committed Dec 26, 2013
1 parent 09bbf33 commit 21f956a
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public SWTGatewayServer(Object entryPoint, int port) {
public SWTGatewayServer(Object entryPoint) {
super(entryPoint);
}

protected GatewayConnection createConnection(Gateway gateway, Socket socket)
throws IOException {
return new SWTGatewayConnection(gateway, socket, customCommands, listeners);
return new SWTGatewayConnection(gateway, socket, getCustomCommands(), getListeners());
}


Expand Down
2 changes: 1 addition & 1 deletion net.sf.py4j/src/py4j/CallbackClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void periodicCleanup() {
if (!isShutdown) {
int size = connections.size();
for (int i = 0; i < size; i++) {
CallbackConnection cc = connections.getLast();
CallbackConnection cc = connections.pollLast();
if (cc.wasUsed()) {
cc.setUsed(false);
connections.addFirst(cc);
Expand Down
53 changes: 53 additions & 0 deletions net.sf.py4j/src/py4j/DefaultApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2009, 2013, Barthelemy Dagenais All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package py4j;

import py4j.GatewayServer;

/**
*
* <p>
* Default application that can be used to quickly test Py4J.
* </p>
*
* @author Barthelemy Dagenais
*
*/
public class DefaultApplication {

/**
* @param args
*/
public static void main(String[] args) {
GatewayServer server = new GatewayServer(new Object());
server.start();
}

}
61 changes: 39 additions & 22 deletions net.sf.py4j/src/py4j/GatewayServer.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/**
* Copyright (c) 2009, 2011, Barthelemy Dagenais All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -37,6 +37,7 @@
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
Expand All @@ -52,31 +53,31 @@
* GatewayServer instance is started, Python programs can connect to the JVM by
* calling:
* </p>
*
*
* <p>
* <code>gateway = JavaGateway()</code>
* </p>
*
*
* <p>
* The
* <code>entryPoint</entry> passed to a GatewayServer can be accessed with the <code>entry_point</code>
* member:
* </p>
*
*
* <p>
* <code>gateway.entry_point</code>
* </p>
*
*
* <p>
* Technically, a GatewayServer is only responsible for accepting connection.
* Each connection is then handled by a {@link py4j.GatewayConnection
* GatewayConnection} instance and the various states (e.g., entryPoint,
* reference to returned objects) are managed by a {@link py4j.Gateway Gateway}
* instance.
* </p>
*
*
* @author Barthelemy Dagenais
*
*
*/
public class GatewayServer extends DefaultGatewayServerListener implements
Runnable {
Expand Down Expand Up @@ -143,9 +144,9 @@ public static void turnLoggingOn() {

private final CallbackClient cbClient;

protected final List<Class<? extends Command>> customCommands;
private final List<Class<? extends Command>> customCommands;

protected final List<GatewayServerListener> listeners;
private final List<GatewayServerListener> listeners;

private ServerSocket sSocket;

Expand All @@ -162,7 +163,7 @@ public static void turnLoggingOn() {
* Creates a GatewayServer instance with default port (25333), default
* address (127.0.0.1), and default timeout value (no timeout).
* </p>
*
*
* @param entryPoint
* The entry point of this Gateway. Can be null.
*/
Expand All @@ -172,7 +173,7 @@ public GatewayServer(Object entryPoint) {
}

/**
*
*
* @param entryPoint
* The entry point of this Gateway. Can be null.
* @param port
Expand All @@ -183,7 +184,7 @@ public GatewayServer(Object entryPoint, int port) {
}

/**
*
*
* @param entryPoint
* The entry point of this Gateway. Can be null.
* @param port
Expand Down Expand Up @@ -228,7 +229,7 @@ public GatewayServer(Object entryPoint, int port, int pythonPort,
}

/**
*
*
* @param entryPoint
* The entry point of this Gateway. Can be null.
* @param port
Expand All @@ -250,7 +251,7 @@ public GatewayServer(Object entryPoint, int port, int connectTimeout,
}

/**
*
*
* @param entryPoint
* The entry point of this Gateway. Can be null.
* @param port
Expand Down Expand Up @@ -431,7 +432,7 @@ public int getConnectTimeout() {
}

/**
*
*
* @return The port the server socket is listening on. It will be different
* than the specified port if the socket is listening on an
* ephemeral port (specified port = 0). Returns -1 if the server
Expand All @@ -450,7 +451,7 @@ public int getListeningPort() {
}

/**
*
*
* @return The port specified when the gateway server is initialized. This
* is the port that is passed to the server socket.
*/
Expand Down Expand Up @@ -547,7 +548,7 @@ public void start() {
* <p>
* Starts to accept connections.
* </p>
*
*
* @param fork
* If true, the GatewayServer accepts connection in another
* thread and this call is non-blocking. If False, the
Expand All @@ -571,7 +572,7 @@ public void start(boolean fork) {
* <p>
* Starts the ServerSocket.
* </p>
*
*
* @throws Py4JNetworkException
* If the port is busy.
*/
Expand Down Expand Up @@ -626,4 +627,20 @@ public static void main(String[] args) {
}
}
}

/**
*
* @return An unmodifiable list of custom commands
*/
public List<Class<? extends Command>> getCustomCommands() {
return Collections.unmodifiableList(customCommands);
}

/**
*
* @return An unmodifiable list of listeners
*/
public List<GatewayServerListener> getListeners() {
return Collections.unmodifiableList(listeners);
}
}
6 changes: 4 additions & 2 deletions net.sf.py4j/src/py4j/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ public final static Object getReturnValue(String returnMessage,

if (isError(returnMessage)) {
throw new Py4JException(
"An exception was raised by the Python Proxy.");
"An exception was raised by the Python Proxy. Return Message: "
+ returnMessage);
} else {
returnValue = getObject(returnMessage.substring(1), gateway);
}
Expand Down Expand Up @@ -541,7 +542,8 @@ public final static boolean isEnd(String commandPart) {
* @return True if the return message is an error
*/
public final static boolean isError(String returnMessage) {
return returnMessage.length() == 0 || returnMessage.charAt(0) == ERROR;
return returnMessage == null || returnMessage.length() == 0
|| returnMessage.charAt(0) == ERROR;
}

/**
Expand Down
Loading

0 comments on commit 21f956a

Please sign in to comment.