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