Issue an error when VPN connection is lost.

+ Add new error code CONNECTION_LOST to VpnManager.
+ Make VpnService call onError() instead of onDisconnect() when
connection is lost.
+ Make VpnService broadcast CONNECTION_LOST when that happens.
This commit is contained in:
Hung-ying Tyan
2009-08-03 16:22:24 +08:00
parent dc1d5704a7
commit 935406709e
2 changed files with 23 additions and 9 deletions

View File

@ -147,8 +147,7 @@ abstract class VpnService<E extends VpnProfile> implements Serializable {
synchronized boolean onConnect(String username, String password) { synchronized boolean onConnect(String username, String password) {
try { try {
mState = VpnState.CONNECTING; setState(VpnState.CONNECTING);
broadcastConnectivity(VpnState.CONNECTING);
stopPreviouslyRunDaemons(); stopPreviouslyRunDaemons();
String serverIp = getIp(getProfile().getServerName()); String serverIp = getIp(getProfile().getServerName());
@ -166,8 +165,7 @@ abstract class VpnService<E extends VpnProfile> implements Serializable {
synchronized void onDisconnect() { synchronized void onDisconnect() {
try { try {
Log.i(TAG, "disconnecting VPN..."); Log.i(TAG, "disconnecting VPN...");
mState = VpnState.DISCONNECTING; setState(VpnState.DISCONNECTING);
broadcastConnectivity(VpnState.DISCONNECTING);
mNotification.showDisconnect(); mNotification.showDisconnect();
mDaemonHelper.stopAll(); mDaemonHelper.stopAll();
@ -235,14 +233,13 @@ abstract class VpnService<E extends VpnProfile> implements Serializable {
saveOriginalDns(); saveOriginalDns();
saveAndSetDomainSuffices(); saveAndSetDomainSuffices();
mState = VpnState.CONNECTED;
mStartTime = System.currentTimeMillis(); mStartTime = System.currentTimeMillis();
// set DNS after saving the states in case the process gets killed // set DNS after saving the states in case the process gets killed
// before states are saved // before states are saved
saveSelf(); saveSelf();
setVpnDns(); setVpnDns();
broadcastConnectivity(VpnState.CONNECTED); setState(VpnState.CONNECTED);
enterConnectivityLoop(); enterConnectivityLoop();
} }
@ -256,16 +253,23 @@ abstract class VpnService<E extends VpnProfile> implements Serializable {
if (mState == VpnState.IDLE) return; if (mState == VpnState.IDLE) return;
// keep the notification when error occurs
if (!anyError()) mNotification.disableNotification();
restoreOriginalDns(); restoreOriginalDns();
restoreOriginalDomainSuffices(); restoreOriginalDomainSuffices();
mState = VpnState.IDLE; setState(VpnState.IDLE);
broadcastConnectivity(VpnState.IDLE);
// stop the service itself // stop the service itself
SystemProperties.set(VPN_STATUS, VPN_IS_DOWN);
mContext.removeStates(); mContext.removeStates();
mContext.stopSelf(); mContext.stopSelf();
} }
private boolean anyError() {
return (mError != null);
}
private void restoreOriginalDns() { private void restoreOriginalDns() {
// restore only if they are not overridden // restore only if they are not overridden
String vpnDns1 = SystemProperties.get(VPN_DNS1); String vpnDns1 = SystemProperties.get(VPN_DNS1);
@ -309,6 +313,11 @@ abstract class VpnService<E extends VpnProfile> implements Serializable {
SystemProperties.set(DNS_DOMAIN_SUFFICES, mOriginalDomainSuffices); SystemProperties.set(DNS_DOMAIN_SUFFICES, mOriginalDomainSuffices);
} }
private void setState(VpnState newState) {
mState = newState;
broadcastConnectivity(newState);
}
private void broadcastConnectivity(VpnState s) { private void broadcastConnectivity(VpnState s) {
VpnManager m = new VpnManager(mContext); VpnManager m = new VpnManager(mContext);
Throwable err = mError; Throwable err = mError;
@ -319,6 +328,9 @@ abstract class VpnService<E extends VpnProfile> implements Serializable {
} else if (err instanceof VpnConnectingError) { } else if (err instanceof VpnConnectingError) {
m.broadcastConnectivity(mProfile.getName(), s, m.broadcastConnectivity(mProfile.getName(), s,
((VpnConnectingError) err).getErrorCode()); ((VpnConnectingError) err).getErrorCode());
} else if (VPN_IS_UP.equals(SystemProperties.get(VPN_STATUS))) {
m.broadcastConnectivity(mProfile.getName(), s,
VpnManager.VPN_ERROR_CONNECTION_LOST);
} else { } else {
m.broadcastConnectivity(mProfile.getName(), s, m.broadcastConnectivity(mProfile.getName(), s,
VpnManager.VPN_ERROR_CONNECTION_FAILED); VpnManager.VPN_ERROR_CONNECTION_FAILED);
@ -366,7 +378,7 @@ abstract class VpnService<E extends VpnProfile> implements Serializable {
// returns false if vpn connectivity is broken // returns false if vpn connectivity is broken
private boolean checkConnectivity() { private boolean checkConnectivity() {
if (mDaemonHelper.anyDaemonStopped() || isLocalIpChanged()) { if (mDaemonHelper.anyDaemonStopped() || isLocalIpChanged()) {
onDisconnect(); onError(new IOException("Connectivity lost"));
return false; return false;
} else { } else {
return true; return true;

View File

@ -54,6 +54,8 @@ public class VpnManager {
public static final int VPN_ERROR_CHALLENGE = 4; public static final int VPN_ERROR_CHALLENGE = 4;
/** Error code to indicate an error of remote server hanging up. */ /** Error code to indicate an error of remote server hanging up. */
public static final int VPN_ERROR_REMOTE_HUNG_UP = 5; public static final int VPN_ERROR_REMOTE_HUNG_UP = 5;
/** Error code to indicate an error of losing connectivity. */
public static final int VPN_ERROR_CONNECTION_LOST = 6;
private static final int VPN_ERROR_NO_ERROR = 0; private static final int VPN_ERROR_NO_ERROR = 0;
public static final String PROFILES_PATH = "/data/misc/vpn/profiles"; public static final String PROFILES_PATH = "/data/misc/vpn/profiles";