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 ...@@ -53,7 +53,6 @@ public class FileTransferProxy extends BasicModule
private IQHandlerInfo info; private IQHandlerInfo info;
private RoutingTable routingTable; private RoutingTable routingTable;
private PacketRouter router; private PacketRouter router;
private int proxyPort;
private String proxyIP; private String proxyIP;
private ProxyConnectionManager connectionManager; private ProxyConnectionManager connectionManager;
...@@ -104,7 +103,7 @@ public class FileTransferProxy extends BasicModule ...@@ -104,7 +103,7 @@ public class FileTransferProxy extends BasicModule
Element response = DocumentHelper.createElement(QName.get("streamhost")); Element response = DocumentHelper.createElement(QName.get("streamhost"));
response.addAttribute("jid", getServiceDomain()); response.addAttribute("jid", getServiceDomain());
response.addAttribute("host", proxyIP); response.addAttribute("host", proxyIP);
response.addAttribute("port", String.valueOf(proxyPort)); response.addAttribute("port", String.valueOf(connectionManager.getProxyPort()));
reply.getChildElement().add(response); reply.getChildElement().add(response);
router.route(reply); router.route(reply);
return true; return true;
...@@ -150,21 +149,53 @@ public class FileTransferProxy extends BasicModule ...@@ -150,21 +149,53 @@ public class FileTransferProxy extends BasicModule
catch (UnknownHostException e) { catch (UnknownHostException e) {
Log.error("Couldn't discover local host", 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() { public void start() {
super.start(); super.start();
if (isEnabled()) {
connectionManager.processConnections(getProxyPort());
routingTable.addRoute(getAddress(), this); routingTable.addRoute(getAddress(), this);
connectionManager.processConnections(); }
} }
public void stop() { public void stop() {
super.stop(); super.stop();
routingTable.removeRoute(getAddress()); 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; ...@@ -20,6 +20,7 @@ import java.security.NoSuchAlgorithmException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/** /**
* Manages the connections to the proxy server. The connections go through two stages before * Manages the connections to the proxy server. The connections go through two stages before
...@@ -31,8 +32,6 @@ import java.util.concurrent.Executors; ...@@ -31,8 +32,6 @@ import java.util.concurrent.Executors;
*/ */
public class ProxyConnectionManager { public class ProxyConnectionManager {
private int port;
private Map<String, ProxyTransfer> connectionMap = private Map<String, ProxyTransfer> connectionMap =
new Cache("File Transfer Cache", -1, 1000 * 60 * 10); new Cache("File Transfer Cache", -1, 1000 * 60 * 10);
...@@ -40,16 +39,28 @@ public class ProxyConnectionManager { ...@@ -40,16 +39,28 @@ public class ProxyConnectionManager {
private ExecutorService executor = Executors.newCachedThreadPool(); private ExecutorService executor = Executors.newCachedThreadPool();
public ProxyConnectionManager(int port) { private Future<?> socketProcess;
this.port = port; private int proxyPort;
public ProxyConnectionManager() {
} }
/* /*
* Processes the clients connecting to the proxy matching the initiator and target together. * 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. * This is the main loop of the manager which will run until the process is canceled.
*/ */
public void processConnections() { synchronized void processConnections(final int port) {
executor.submit(new Runnable() { if (socketProcess != null) {
if (port != proxyPort) {
socketProcess.cancel(true);
socketProcess = null;
}
else {
return;
}
}
socketProcess = executor.submit(new Runnable() {
public void run() { public void run() {
ServerSocket serverSocket = null; ServerSocket serverSocket = null;
try { try {
...@@ -82,7 +93,11 @@ public class ProxyConnectionManager { ...@@ -82,7 +93,11 @@ public class ProxyConnectionManager {
} }
} }
}); });
proxyPort = port;
}
public int getProxyPort() {
return proxyPort;
} }
private void processConnection(Socket connection) throws IOException { private void processConnection(Socket connection) throws IOException {
...@@ -175,6 +190,11 @@ public class ProxyConnectionManager { ...@@ -175,6 +190,11 @@ public class ProxyConnectionManager {
return data; return data;
} }
synchronized void shutdown() {
disable();
executor.shutdown();
}
/** /**
* Activates the stream, this method should be called when the initiator sends the activate * Activates the stream, this method should be called when the initiator sends the activate
* packet after both parties have connected to the proxy. * packet after both parties have connected to the proxy.
...@@ -236,6 +256,9 @@ public class ProxyConnectionManager { ...@@ -236,6 +256,9 @@ public class ProxyConnectionManager {
// read more bytes from the input stream // read more bytes from the input stream
count = in.read(b); count = in.read(b);
} }
transfer.getInitiatorSocket().close();
transfer.getTargetSocket().close();
} }
/** /**
...@@ -291,4 +314,16 @@ public class ProxyConnectionManager { ...@@ -291,4 +314,16 @@ public class ProxyConnectionManager {
return hex.toString(); 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