summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTarun Gupta <tarun1995gupta@gmail.com>2015-07-27 15:01:45 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2016-01-12 11:31:04 (GMT)
commitb4cf2bb8d7b69d95b4a10d610ad259998d2aee5b (patch)
treeb942953236a7c712eed6ce4a5f261019691c0dca /src/com/isode/stroke/network/ProxiedConnection.java
parentc168fcd0c2468ec939b8d164175e9c5776a63147 (diff)
downloadstroke-b4cf2bb8d7b69d95b4a10d610ad259998d2aee5b.zip
stroke-b4cf2bb8d7b69d95b4a10d610ad259998d2aee5b.tar.bz2
Make Networks equivalent with Swiften.
Adds ProxyProviders, DomainNameResolvers and DummyConnection. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests added for ChainedConnector, Connector and HostAddress. Test also added for ComponentConnector, which needed bits of Network. Five assertions are commented in ConnectorTest, which fails and will be updated after review. Change-Id: I8a62841eb2f9c109bc3a94865b7a003b33493e11
Diffstat (limited to 'src/com/isode/stroke/network/ProxiedConnection.java')
-rw-r--r--src/com/isode/stroke/network/ProxiedConnection.java150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/com/isode/stroke/network/ProxiedConnection.java b/src/com/isode/stroke/network/ProxiedConnection.java
new file mode 100644
index 0000000..a94fbc5
--- /dev/null
+++ b/src/com/isode/stroke/network/ProxiedConnection.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2012-2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.network;
+
+import com.isode.stroke.signals.SignalConnection;
+import com.isode.stroke.signals.Slot2;
+import com.isode.stroke.signals.Slot1;
+import com.isode.stroke.base.SafeByteArray;
+
+public abstract class ProxiedConnection extends Connection {
+
+ private boolean connected_;
+ private DomainNameResolver resolver_;
+ private ConnectionFactory connectionFactory_;
+ private TimerFactory timerFactory_;
+ private String proxyHost_ = "";
+ private int proxyPort_;
+ private HostAddressPort server_;
+ private Connector connector_;
+ private Connection connection_;
+ private SignalConnection onDataReadConnection;
+ private SignalConnection onDisconnectedConnection;
+ private SignalConnection onConnectFinishedConnection;
+
+ public ProxiedConnection(DomainNameResolver resolver, ConnectionFactory connectionFactory, TimerFactory timerFactory, final String proxyHost, int proxyPort) {
+ this.resolver_ = resolver;
+ this.connectionFactory_ = connectionFactory;
+ this.timerFactory_ = timerFactory;
+ this.proxyHost_ = proxyHost;
+ this.proxyPort_ = proxyPort;
+ this.server_ = new HostAddressPort(new HostAddress("0.0.0.0"), 0);
+ this.connected_ = false;
+ }
+
+ protected void finalize() throws Throwable {
+ try {
+ cancelConnector();
+ if (connection_ != null) {
+ onDataReadConnection.disconnect();
+ onDisconnectedConnection.disconnect();
+ }
+ if (connected_) {
+ System.err.println("Warning: Connection was still established.");
+ }
+ }
+ finally {
+ super.finalize();
+ }
+ }
+
+ public void listen() {
+ assert(false);
+ connection_.listen();
+ }
+
+ public void connect(final HostAddressPort server) {
+ server_ = server;
+
+ connector_ = Connector.create(proxyHost_, proxyPort_, null, resolver_, connectionFactory_, timerFactory_);
+ onConnectFinishedConnection = connector_.onConnectFinished.connect(new Slot2<Connection, com.isode.stroke.base.Error>() {
+ @Override
+ public void call(Connection c, com.isode.stroke.base.Error e) {
+ handleConnectFinished(c);
+ }
+ });
+ connector_.start();
+ }
+
+ public void disconnect() {
+ connected_ = false;
+ connection_.disconnect();
+ }
+
+ public void write(final SafeByteArray data) {
+ connection_.write(data);
+ }
+
+ public HostAddressPort getLocalAddress() {
+ return connection_.getLocalAddress();
+ }
+
+ private void handleConnectFinished(Connection connection) {
+ cancelConnector();
+ if (connection != null) {
+ connection_ = connection;
+ connection_.onDataRead.connect(new Slot1<SafeByteArray>() {
+ @Override
+ public void call(SafeByteArray s) {
+ handleDataRead(s);
+ }
+ });
+ connection_.onDisconnected.connect(new Slot1<Error>() {
+ @Override
+ public void call(Error e) {
+ handleDisconnected(e);
+ }
+ });
+
+ initializeProxy();
+ }
+ else {
+ onConnectFinished.emit(true);
+ }
+ }
+
+ private void handleDataRead(SafeByteArray data) {
+ if (!connected_) {
+ handleProxyInitializeData(data);
+ }
+ else {
+ onDataRead.emit(data);
+ }
+ }
+
+ private void handleDisconnected(final Error error) {
+ onDisconnected.emit(error);
+ }
+
+ private void cancelConnector() {
+ if (connector_ != null) {
+ onConnectFinishedConnection.disconnect();
+ connector_.stop();
+ connector_ = null;
+ }
+ }
+
+ protected void setProxyInitializeFinished(boolean success) {
+ connected_ = success;
+ if (!success) {
+ disconnect();
+ }
+ onConnectFinished.emit(!success);
+ }
+
+ protected abstract void initializeProxy();
+ protected abstract void handleProxyInitializeData(SafeByteArray data);
+
+ protected HostAddressPort getServer() {
+ return server_;
+ }
+} \ No newline at end of file