Skip to content

Commit

Permalink
Add force reconnection on network state change feature.
Browse files Browse the repository at this point in the history
Closes #10.
  • Loading branch information
Vilbrekin committed Mar 23, 2014
1 parent 3406539 commit 447540b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ making a donation</a> if you like this program.
<string name="pref_title_debug_level">Debug level</string>
<string name="pref_title_super_user">Execute as Super User</string>
<string name="pref_title_autostart_boot">Auto start on boot</string>
<string name="pref_title_force_reconnect">Force reconnection on network change</string>

</resources>
1 change: 1 addition & 0 deletions res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ListPreference android:defaultValue="2" android:key="pref_key_debug_level" android:title="@string/pref_title_debug_level" android:entries="@array/debug_lvl_entries" android:entryValues="@array/debug_lvl_values"/>
<CheckBoxPreference android:defaultValue="true" android:key="pref_key_super_user" android:title="@string/pref_title_super_user"/>
<CheckBoxPreference android:defaultValue="false" android:key="pref_key_autostart_boot" android:title="@string/pref_title_autostart_boot"/>
<CheckBoxPreference android:defaultValue="false" android:key="pref_key_force_reconnect" android:title="@string/pref_title_force_reconnect"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_title_current_config" android:key="pref_key_config">
</PreferenceCategory><PreferenceCategory android:title="@string/pref_title_hosts" android:key="pref_key_hosts">
Expand Down
2 changes: 1 addition & 1 deletion src/org/poirsouille/tinc_gui/BootReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void onReceive(Context iContext, Intent iIntent)
// Auto start service if enabled
Log.d(Tools.TAG, "Boot notif");
SharedPreferences aSharedPref = iContext.getSharedPreferences("org.poirsouille.tinc_gui_preferences", Context.MODE_PRIVATE);
Boolean aAutoStart = false;
boolean aAutoStart = false;
aAutoStart = aSharedPref.getBoolean("pref_key_autostart_boot", aAutoStart);
if (aAutoStart)
{
Expand Down
75 changes: 74 additions & 1 deletion src/org/poirsouille/tinc_gui/TincdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.os.Binder;
import android.os.IBinder;
import android.preference.PreferenceManager;
Expand All @@ -61,6 +65,8 @@ public class TincdService extends Service implements ICallback
SharedPreferences _sharedPref;
public int _maxLogSize = 1000;
private OnSharedPreferenceChangeListener _prefChangeListener;
private final ConnectivityroadcastReceiver _broadcastReceiver = new ConnectivityroadcastReceiver();
private boolean _reconnectOnNetChange = false;

public ICallback _callback = null;

Expand All @@ -81,6 +87,63 @@ TincdService getService()
}
}

/**
* Network state events receiver.
*/
public class ConnectivityroadcastReceiver extends BroadcastReceiver
{
/// Ensure we don't try to un-register several times
private boolean _receiverRegistered = false;

/**
* Listen for network change events, and force tincd reconnection as soon as connectivity is back.
*/
@Override
public void onReceive(Context context, Intent intent)
{
String aAction = intent.getAction();
if(aAction.equals(ConnectivityManager.CONNECTIVITY_ACTION))
{
// Check if we have connectivity
boolean aConnectivity = ! intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
Log.d(Tools.TAG, "Network state changed - network available? " + aConnectivity);
if (aConnectivity)
{
Log.i(Tools.TAG, "Network state changed - forcing reconnection");
// Send SIGALRM to tincd to force immediate reconnection to other nodes
signal("SIGALRM");
}
}
}

/**
* Register a broadcast receiver to get notified on network state change.
*/
public void register()
{
// Only register if force reconnect is enabled and tincd is started
if (_reconnectOnNetChange && _started)
{
IntentFilter aFilter = new IntentFilter();
aFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(_broadcastReceiver, aFilter);
_receiverRegistered = true;
}
}

/**
* Unregister broadcast receiver.
*/
public void unregister()
{
if (_receiverRegistered)
{
unregisterReceiver(_broadcastReceiver);
_receiverRegistered = false;
}
}
};

/**
* Execute given command, with either su or sh as shell.
* @param command
Expand All @@ -95,6 +158,7 @@ public List<String> run(String command, ICallback ioCallBack)
return Tools.Run(aShell, new String[] {command}, ioCallBack);
}


public void startTinc()
{
if (! _started)
Expand Down Expand Up @@ -122,10 +186,13 @@ public void run()

_started = true;
_debug = false;
// Register a broadcast receiver to get notified on network state change
_broadcastReceiver.register();
// Use exec to replace shell with executable. umask is used to ensure pidfile will be world readable.
TincdService.this.run("umask 022; exec " + getFileStreamPath(TINCBIN) + " -D -d" + _debugLvl + " -c " + _configPath + " --pidfile=" + getFileStreamPath(PIDFILE), TincdService.this);
// Process returns only when ended
_started = false;
_broadcastReceiver.unregister();
Log.d(Tools.TAG, "End of tincd thread");
TincdService.this.stopTincd();
}
Expand Down Expand Up @@ -301,6 +368,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
_sharedPref.registerOnSharedPreferenceChangeListener(_prefChangeListener);
// Refresh at startup as well
refreshPrefs("");

// Re-register if needed
_broadcastReceiver.register();
}

/**
Expand All @@ -313,11 +383,12 @@ private void refreshPrefs(String iKey)
_maxLogSize = Integer.parseInt(_sharedPref.getString("pref_key_max_log_size", "" + _maxLogSize));
_debugLvl = Integer.parseInt(_sharedPref.getString("pref_key_debug_level", "" + _debugLvl));
_useSU = _sharedPref.getBoolean("pref_key_super_user", _useSU);
_reconnectOnNetChange = _sharedPref.getBoolean("pref_key_force_reconnect", _reconnectOnNetChange);

if (iKey.equals("pref_key_autostart_boot"))
{
// Enable/disable boot time notification
Boolean aAutoStart = false;
boolean aAutoStart = false;
aAutoStart = _sharedPref.getBoolean("pref_key_autostart_boot", aAutoStart);
this.getPackageManager().setComponentEnabledSetting(new ComponentName(this, BootReceiver.class),
aAutoStart ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
Expand All @@ -329,6 +400,7 @@ private void refreshPrefs(String iKey)
public void onDestroy ()
{
stopTincd();
_broadcastReceiver.unregister();
Log.d(Tools.TAG, "Service destroyed");
}

Expand Down Expand Up @@ -421,4 +493,5 @@ void checkAndStopSelf()
stopSelf();
}
}

}

0 comments on commit 447540b

Please sign in to comment.