Commit 77ec3561 authored by Grigory Fedorov's avatar Grigory Fedorov

ConnectionManager, ConnectionItem: light refactoring & debug printing.

parent e3de3ad8
...@@ -504,7 +504,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -504,7 +504,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
result.setColorIndex(colorIndex); result.setColorIndex(colorIndex);
boolean reconnect = false; boolean reconnect = false;
if (accountItem.getConnectionSettings().isCustom() != custom if (accountItem.getConnectionSettings().isCustomHostAndPort() != custom
|| !accountItem.getConnectionSettings().getHost().equals(host) || !accountItem.getConnectionSettings().getHost().equals(host)
|| accountItem.getConnectionSettings().getPort() != port || accountItem.getConnectionSettings().getPort() != port
|| !accountItem.getConnectionSettings().getPassword().equals(password) || !accountItem.getConnectionSettings().getPassword().equals(password)
...@@ -596,7 +596,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -596,7 +596,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
ConnectionSettings connectionSettings = accountItem.getConnectionSettings(); ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
updateAccount( updateAccount(
account, account,
connectionSettings.isCustom(), connectionSettings.isCustomHostAndPort(),
connectionSettings.getHost(), connectionSettings.getHost(),
connectionSettings.getPort(), connectionSettings.getPort(),
connectionSettings.getServerName(), connectionSettings.getServerName(),
...@@ -625,7 +625,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -625,7 +625,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
ConnectionSettings connectionSettings = accountItem.getConnectionSettings(); ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
updateAccount( updateAccount(
account, account,
connectionSettings.isCustom(), connectionSettings.isCustomHostAndPort(),
connectionSettings.getHost(), connectionSettings.getHost(),
connectionSettings.getPort(), connectionSettings.getPort(),
connectionSettings.getServerName(), connectionSettings.getServerName(),
...@@ -654,7 +654,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -654,7 +654,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
ConnectionSettings connectionSettings = accountItem.getConnectionSettings(); ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
AccountManager.getInstance().updateAccount( AccountManager.getInstance().updateAccount(
account, account,
connectionSettings.isCustom(), connectionSettings.isCustomHostAndPort(),
connectionSettings.getHost(), connectionSettings.getHost(),
connectionSettings.getPort(), connectionSettings.getPort(),
connectionSettings.getServerName(), connectionSettings.getServerName(),
......
...@@ -409,7 +409,7 @@ class AccountTable extends AbstractTable { ...@@ -409,7 +409,7 @@ class AccountTable extends AbstractTable {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(Fields.PROTOCOL, connectionSettings.getProtocol().name()); values.put(Fields.PROTOCOL, connectionSettings.getProtocol().name());
values.put(Fields.CUSTOM, connectionSettings.isCustom() ? 1 : 0); values.put(Fields.CUSTOM, connectionSettings.isCustomHostAndPort() ? 1 : 0);
values.put(Fields.HOST, connectionSettings.getHost()); values.put(Fields.HOST, connectionSettings.getHost());
values.put(Fields.PORT, connectionSettings.getPort()); values.put(Fields.PORT, connectionSettings.getPort());
values.put(Fields.SERVER_NAME, connectionSettings.getServerName()); values.put(Fields.SERVER_NAME, connectionSettings.getServerName());
......
...@@ -42,7 +42,7 @@ public abstract class ConnectionItem { ...@@ -42,7 +42,7 @@ public abstract class ConnectionItem {
/** /**
* Connection was requested by user. * Connection was requested by user.
*/ */
private boolean connectionRequest; private boolean isConnectionRequestedByUser;
/** /**
* Current state. * Current state.
...@@ -69,7 +69,7 @@ public abstract class ConnectionItem { ...@@ -69,7 +69,7 @@ public abstract class ConnectionItem {
serverName, resource, custom, host, port, password, serverName, resource, custom, host, port, password,
saslEnabled, tlsMode, compression, proxyType, proxyHost, saslEnabled, tlsMode, compression, proxyType, proxyHost,
proxyPort, proxyUser, proxyPassword); proxyPort, proxyUser, proxyPassword);
connectionRequest = false; isConnectionRequestedByUser = false;
disconnectionRequested = false; disconnectionRequested = false;
connectionThread = null; connectionThread = null;
state = ConnectionState.offline; state = ConnectionState.offline;
...@@ -152,7 +152,7 @@ public abstract class ConnectionItem { ...@@ -152,7 +152,7 @@ public abstract class ConnectionItem {
if (state == ConnectionState.connected || state == ConnectionState.authentication if (state == ConnectionState.connected || state == ConnectionState.authentication
|| state == ConnectionState.connecting) { || state == ConnectionState.connecting) {
if (userRequest) { if (userRequest) {
connectionRequest = false; isConnectionRequestedByUser = false;
} }
if (connectionThread != null) { if (connectionThread != null) {
disconnect(connectionThread); disconnect(connectionThread);
...@@ -168,17 +168,26 @@ public abstract class ConnectionItem { ...@@ -168,17 +168,26 @@ public abstract class ConnectionItem {
} else { } else {
if (state == ConnectionState.offline || state == ConnectionState.waiting) { if (state == ConnectionState.offline || state == ConnectionState.waiting) {
if (userRequest) { if (userRequest) {
connectionRequest = true; isConnectionRequestedByUser = true;
} }
state = ConnectionState.connecting; state = ConnectionState.connecting;
connectionThread = new ConnectionThread(this); connectionThread = new ConnectionThread(this);
if (connectionSettings.isCustom()) {
connectionThread.start(connectionSettings.getHost(), boolean useSRVLookup;
connectionSettings.getPort(), false, registerNewAccount); String fullyQualifiedDomainName;
int port;
if (connectionSettings.isCustomHostAndPort()) {
fullyQualifiedDomainName = connectionSettings.getHost();
port = connectionSettings.getPort();
useSRVLookup = false;
} else { } else {
connectionThread.start(connectionSettings.getServerName(), fullyQualifiedDomainName = connectionSettings.getServerName();
5222, true, registerNewAccount); port = 5222;
useSRVLookup = true;
} }
connectionThread.start(fullyQualifiedDomainName, port, useSRVLookup, registerNewAccount);
return true; return true;
} else { } else {
return false; return false;
...@@ -194,10 +203,10 @@ public abstract class ConnectionItem { ...@@ -194,10 +203,10 @@ public abstract class ConnectionItem {
return; return;
} }
disconnectionRequested = true; disconnectionRequested = true;
boolean request = connectionRequest; boolean request = isConnectionRequestedByUser;
connectionRequest = false; isConnectionRequestedByUser = false;
updateConnection(false); updateConnection(false);
connectionRequest = request; isConnectionRequestedByUser = request;
disconnectionRequested = false; disconnectionRequested = false;
updateConnection(false); updateConnection(false);
} }
...@@ -319,10 +328,10 @@ public abstract class ConnectionItem { ...@@ -319,10 +328,10 @@ public abstract class ConnectionItem {
if (onDisconnect(connectionThread)) { if (onDisconnect(connectionThread)) {
state = ConnectionState.waiting; state = ConnectionState.waiting;
this.connectionThread = null; this.connectionThread = null;
if (connectionRequest) { if (isConnectionRequestedByUser) {
Application.getInstance().onError(R.string.CONNECTION_FAILED); Application.getInstance().onError(R.string.CONNECTION_FAILED);
} }
connectionRequest = false; isConnectionRequestedByUser = false;
} }
} }
......
...@@ -69,6 +69,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -69,6 +69,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override @Override
public void connectionCreated(final XMPPConnection connection) { public void connectionCreated(final XMPPConnection connection) {
LogManager.i(this, "connectionCreated");
ServiceDiscoveryManager.getInstanceFor(connection).addFeature("sslc2s"); ServiceDiscoveryManager.getInstanceFor(connection).addFeature("sslc2s");
} }
}); });
...@@ -85,6 +86,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -85,6 +86,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
private final NestedMap<RequestHolder> requests; private final NestedMap<RequestHolder> requests;
private ConnectionManager() { private ConnectionManager() {
LogManager.i(this, "ConnectionManager");
managedConnections = new ArrayList<>(); managedConnections = new ArrayList<>();
requests = new NestedMap<>(); requests = new NestedMap<>();
} }
...@@ -95,12 +97,14 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -95,12 +97,14 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
@Override @Override
public void onInitialized() { public void onInitialized() {
LogManager.i(this, "onInitialized");
updateConnections(false); updateConnections(false);
AccountManager.getInstance().onAccountsChanged(new ArrayList<>(AccountManager.getInstance().getAllAccounts())); AccountManager.getInstance().onAccountsChanged(new ArrayList<>(AccountManager.getInstance().getAllAccounts()));
} }
@Override @Override
public void onClose() { public void onClose() {
LogManager.i(this, "onClose");
ArrayList<ConnectionThread> connections = new ArrayList<>(managedConnections); ArrayList<ConnectionThread> connections = new ArrayList<>(managedConnections);
managedConnections.clear(); managedConnections.clear();
for (ConnectionThread connectionThread : connections) { for (ConnectionThread connectionThread : connections) {
...@@ -116,6 +120,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -116,6 +120,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
* @param userRequest * @param userRequest
*/ */
public void updateConnections(boolean userRequest) { public void updateConnections(boolean userRequest) {
LogManager.i(this, "updateConnections");
AccountManager accountManager = AccountManager.getInstance(); AccountManager accountManager = AccountManager.getInstance();
for (String account : accountManager.getAccounts()) { for (String account : accountManager.getAccounts()) {
if (accountManager.getAccount(account).updateConnection(userRequest)) { if (accountManager.getAccount(account).updateConnection(userRequest)) {
...@@ -128,6 +133,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -128,6 +133,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
* Disconnect and connect using new network. * Disconnect and connect using new network.
*/ */
public void forceReconnect() { public void forceReconnect() {
LogManager.i(this, "forceReconnect");
AccountManager accountManager = AccountManager.getInstance(); AccountManager accountManager = AccountManager.getInstance();
for (String account : accountManager.getAccounts()) { for (String account : accountManager.getAccounts()) {
accountManager.getAccount(account).forceReconnect(); accountManager.getAccount(account).forceReconnect();
...@@ -181,6 +187,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -181,6 +187,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
} }
public void onConnection(ConnectionThread connectionThread) { public void onConnection(ConnectionThread connectionThread) {
LogManager.i(this, "onConnection");
managedConnections.add(connectionThread); managedConnections.add(connectionThread);
for (OnConnectionListener listener : Application.getInstance().getManagers(OnConnectionListener.class)) { for (OnConnectionListener listener : Application.getInstance().getManagers(OnConnectionListener.class)) {
listener.onConnection(connectionThread.getConnectionItem()); listener.onConnection(connectionThread.getConnectionItem());
...@@ -188,6 +195,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -188,6 +195,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
} }
public void onConnected(ConnectionThread connectionThread) { public void onConnected(ConnectionThread connectionThread) {
LogManager.i(this, "onConnected");
if (!managedConnections.contains(connectionThread)) { if (!managedConnections.contains(connectionThread)) {
return; return;
} }
...@@ -197,6 +205,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -197,6 +205,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
} }
public void onAuthorized(ConnectionThread connectionThread) { public void onAuthorized(ConnectionThread connectionThread) {
LogManager.i(this, "onAuthorized");
if (!managedConnections.contains(connectionThread)) { if (!managedConnections.contains(connectionThread)) {
return; return;
} }
...@@ -208,6 +217,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -208,6 +217,7 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
} }
public void onDisconnect(ConnectionThread connectionThread) { public void onDisconnect(ConnectionThread connectionThread) {
LogManager.i(this, "onDisconnect");
if (!managedConnections.remove(connectionThread)) { if (!managedConnections.remove(connectionThread)) {
return; return;
} }
...@@ -251,9 +261,9 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -251,9 +261,9 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
@Override @Override
public void onTimer() { public void onTimer() {
if (NetworkManager.getInstance().getState() != NetworkState.suspended) { // if (NetworkManager.getInstance().getState() != NetworkState.suspended) {
Collection<ConnectionItem> reconnect = new ArrayList<>(); // Collection<ConnectionItem> reconnect = new ArrayList<>();
for (ConnectionThread connectionThread : managedConnections) { // for (ConnectionThread connectionThread : managedConnections) {
// if (connectionThread.getConnectionItem().getState().isConnected() // if (connectionThread.getConnectionItem().getState().isConnected()
// // TODO find the way to check if connection is alive // // TODO find the way to check if connection is alive
// // XMPPConnection can`t be null here // // XMPPConnection can`t be null here
...@@ -262,11 +272,11 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener ...@@ -262,11 +272,11 @@ public class ConnectionManager implements OnInitializedListener, OnCloseListener
// LogManager.i(connectionThread.getConnectionItem(), "forceReconnect on checkAlive"); // LogManager.i(connectionThread.getConnectionItem(), "forceReconnect on checkAlive");
// reconnect.add(connectionThread.getConnectionItem()); // reconnect.add(connectionThread.getConnectionItem());
// } // }
} // }
for (ConnectionItem connection : reconnect) { // for (ConnectionItem connection : reconnect) {
connection.forceReconnect(); // connection.forceReconnect();
} // }
} // }
long now = new Date().getTime(); long now = new Date().getTime();
Iterator<NestedMap.Entry<RequestHolder>> iterator = requests.iterator(); Iterator<NestedMap.Entry<RequestHolder>> iterator = requests.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
......
...@@ -133,7 +133,7 @@ public class ConnectionSettings { ...@@ -133,7 +133,7 @@ public class ConnectionSettings {
/** /**
* @return Whether custom host and port must be used. * @return Whether custom host and port must be used.
*/ */
public boolean isCustom() { public boolean isCustomHostAndPort() {
return custom; return custom;
} }
......
...@@ -120,9 +120,10 @@ public class ConnectionThread implements ...@@ -120,9 +120,10 @@ public class ConnectionThread implements
private boolean registerNewAccount; private boolean registerNewAccount;
public ConnectionThread(final ConnectionItem connectionItem) { public ConnectionThread(final ConnectionItem connectionItem) {
LogManager.i(this, "NEW connection thread " + connectionItem.getRealJid());
this.connectionItem = connectionItem; this.connectionItem = connectionItem;
executorService = Executors executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
.newSingleThreadExecutor(new ThreadFactory() {
@Override @Override
public Thread newThread(Runnable runnable) { public Thread newThread(Runnable runnable) {
Thread thread = new Thread( Thread thread = new Thread(
...@@ -751,6 +752,8 @@ public class ConnectionThread implements ...@@ -751,6 +752,8 @@ public class ConnectionThread implements
synchronized void start(final String fqdn, final int port, synchronized void start(final String fqdn, final int port,
final boolean useSRVLookup, final boolean useSRVLookup,
final boolean registerNewAccount) { final boolean registerNewAccount) {
LogManager.i(this, "start: " + fqdn);
if (started) if (started)
throw new IllegalStateException(); throw new IllegalStateException();
started = true; started = true;
......
...@@ -133,7 +133,7 @@ public class AccountEditorFragment extends BaseSettingsFragment ...@@ -133,7 +133,7 @@ public class AccountEditorFragment extends BaseSettingsFragment
putValue(source, R.string.account_color_key, accountItem.getColorIndex()); putValue(source, R.string.account_color_key, accountItem.getColorIndex());
com.xabber.android.data.connection.ConnectionSettings connectionSettings = accountItem.getConnectionSettings(); com.xabber.android.data.connection.ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
putValue(source, R.string.account_custom_key, connectionSettings.isCustom()); putValue(source, R.string.account_custom_key, connectionSettings.isCustomHostAndPort());
putValue(source, R.string.account_host_key, connectionSettings.getHost()); putValue(source, R.string.account_host_key, connectionSettings.getHost());
putValue(source, R.string.account_port_key, connectionSettings.getPort()); putValue(source, R.string.account_port_key, connectionSettings.getPort());
putValue(source, R.string.account_server_key, connectionSettings.getServerName()); putValue(source, R.string.account_server_key, connectionSettings.getServerName());
......
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