Commit 2f0a3d25 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

Worked on startup and shutdown for transfer proxy.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3454 b35dd754-fafc-0310-a699-88a17e54d16e
parent d16a7828
......@@ -53,7 +53,6 @@ public class FileTransferProxy extends BasicModule
private IQHandlerInfo info;
private RoutingTable routingTable;
private PacketRouter router;
private int proxyPort;
private String proxyIP;
private ProxyConnectionManager connectionManager;
......@@ -104,7 +103,7 @@ public class FileTransferProxy extends BasicModule
Element response = DocumentHelper.createElement(QName.get("streamhost"));
response.addAttribute("jid", getServiceDomain());
response.addAttribute("host", proxyIP);
response.addAttribute("port", String.valueOf(proxyPort));
response.addAttribute("port", String.valueOf(connectionManager.getProxyPort()));
reply.getChildElement().add(response);
router.route(reply);
return true;
......@@ -150,21 +149,53 @@ public class FileTransferProxy extends BasicModule
catch (UnknownHostException e) {
Log.error("Couldn't discover local host", e);
}
proxyPort = JiveGlobals.getIntProperty("xmpp.proxy.port", 7777);
connectionManager = new ProxyConnectionManager(proxyPort);
connectionManager = new ProxyConnectionManager();
}
public void start() {
super.start();
routingTable.addRoute(getAddress(), this);
connectionManager.processConnections();
if (isEnabled()) {
connectionManager.processConnections(getProxyPort());
routingTable.addRoute(getAddress(), this);
}
}
public void stop() {
super.stop();
routingTable.removeRoute(getAddress());
connectionManager.disable();
}
public void destroy() {
super.destroy();
connectionManager.shutdown();
}
public void setEnabled(boolean isEnabled) {
JiveGlobals.setProperty("xmpp.proxy.enabled", Boolean.toString(isEnabled));
if (isEnabled) {
start();
}
else {
stop();
}
}
public boolean isEnabled() {
return connectionManager.isRunning() ||
JiveGlobals.getBooleanProperty("xmpp.proxy.enabled", true);
}
public void setProxyPort(int port) {
JiveGlobals.setProperty("xmpp.proxy.port", Integer.toString(port));
}
public int getProxyPort() {
return JiveGlobals.getIntProperty("xmpp.proxy.port", 7777);
}
/**
......
......@@ -20,6 +20,7 @@ import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Manages the connections to the proxy server. The connections go through two stages before
......@@ -31,8 +32,6 @@ import java.util.concurrent.Executors;
*/
public class ProxyConnectionManager {
private int port;
private Map<String, ProxyTransfer> connectionMap =
new Cache("File Transfer Cache", -1, 1000 * 60 * 10);
......@@ -40,16 +39,28 @@ public class ProxyConnectionManager {
private ExecutorService executor = Executors.newCachedThreadPool();
public ProxyConnectionManager(int port) {
this.port = port;
private Future<?> socketProcess;
private int proxyPort;
public ProxyConnectionManager() {
}
/*
* Processes the clients connecting to the proxy matching the initiator and target together.
* This is the main loop of the manager which will run until the process is canceled.
*/
public void processConnections() {
executor.submit(new Runnable() {
synchronized void processConnections(final int port) {
if (socketProcess != null) {
if (port != proxyPort) {
socketProcess.cancel(true);
socketProcess = null;
}
else {
return;
}
}
socketProcess = executor.submit(new Runnable() {
public void run() {
ServerSocket serverSocket = null;
try {
......@@ -82,7 +93,11 @@ public class ProxyConnectionManager {
}
}
});
proxyPort = port;
}
public int getProxyPort() {
return proxyPort;
}
private void processConnection(Socket connection) throws IOException {
......@@ -175,6 +190,11 @@ public class ProxyConnectionManager {
return data;
}
synchronized void shutdown() {
disable();
executor.shutdown();
}
/**
* Activates the stream, this method should be called when the initiator sends the activate
* packet after both parties have connected to the proxy.
......@@ -236,6 +256,9 @@ public class ProxyConnectionManager {
// read more bytes from the input stream
count = in.read(b);
}
transfer.getInitiatorSocket().close();
transfer.getTargetSocket().close();
}
/**
......@@ -291,4 +314,16 @@ public class ProxyConnectionManager {
return hex.toString();
}
public boolean isRunning() {
return socketProcess != null;
}
public void disable() {
if (socketProcess != null) {
socketProcess.cancel(true);
socketProcess = null;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment