|
| 1 | +package butter.droid.base.manager.updater; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Handler; |
| 5 | +import android.os.Looper; |
| 6 | +import android.util.Log; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonParseException; |
| 9 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.github.se_bastiaan.torrentstream.TorrentStream; |
| 12 | +import com.github.se_bastiaan.torrentstream.exceptions.NotInitializedException; |
| 13 | + |
| 14 | +import org.json.JSONException; |
| 15 | +import org.libtorrent4j.Hex; |
| 16 | +import org.libtorrent4j.SessionManager; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.ArrayList; |
| 20 | + |
| 21 | +import javax.inject.Inject; |
| 22 | + |
| 23 | +import butter.droid.base.R; |
| 24 | +import butter.droid.base.content.preferences.Prefs; |
| 25 | +import butter.droid.base.utils.PrefUtils; |
| 26 | + |
| 27 | +public class DhtManager implements Runnable { |
| 28 | + |
| 29 | + private Context mContext; |
| 30 | + private ObjectMapper mMapper; |
| 31 | + |
| 32 | + private static final int UPDATE_INTERVAL = 7 * 24 * 60 * 60; |
| 33 | + |
| 34 | + @Inject |
| 35 | + public DhtManager(Context context, ObjectMapper mapper) |
| 36 | + { |
| 37 | + this.mContext = context; |
| 38 | + this.mMapper = mapper; |
| 39 | + } |
| 40 | + |
| 41 | + public void updateSettings() |
| 42 | + { |
| 43 | + int updated = PrefUtils.get(mContext, Prefs.DHT_UPDATED, 0); |
| 44 | + if (updated + UPDATE_INTERVAL > System.currentTimeMillis() / 1000) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + try { |
| 49 | + SessionManager sm = TorrentStream.getInstance().getSessionManager(); |
| 50 | + this.updateFromDHT(sm); |
| 51 | + return; |
| 52 | + } catch (NotInitializedException e) { |
| 53 | + Log.d("DHT", "NotInitializedException"); |
| 54 | + } |
| 55 | + |
| 56 | + final Handler handler = new Handler(Looper.getMainLooper()); |
| 57 | + handler.postDelayed(this, 5000); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void run() { |
| 62 | + this.updateSettings(); |
| 63 | + } |
| 64 | + |
| 65 | + protected void updateFromDHT(SessionManager sm) { |
| 66 | + Log.d("DHT", "DHT contains " + sm.stats().dhtNodes() + " nodes"); |
| 67 | + String dhtPublicKey = mContext.getResources().getString(R.string.dht_public_key); |
| 68 | + SessionManager.MutableItem data = sm.dhtGetItem(Hex.decode(dhtPublicKey), Hex.decode(""), 20); |
| 69 | + int seq = PrefUtils.get(mContext, Prefs.DHT_SEQ, 0); |
| 70 | + if (data.seq <= seq) { |
| 71 | + return; |
| 72 | + } |
| 73 | + Log.d("DHT", "DHT updated " + data.seq); |
| 74 | + PrefUtils.save(mContext, Prefs.DHT_DATA, data.item.string()); |
| 75 | + PrefUtils.save(mContext, Prefs.DHT_SEQ, data.seq); |
| 76 | + PrefUtils.save(mContext, Prefs.DHT_UPDATED, System.currentTimeMillis() / 1000); |
| 77 | + } |
| 78 | + |
| 79 | + public String[] servers() |
| 80 | + { |
| 81 | + String dht = PrefUtils.get(mContext, Prefs.DHT_DATA, ""); |
| 82 | + if (dht.isEmpty()) { |
| 83 | + return new String[0]; |
| 84 | + } |
| 85 | + try { |
| 86 | + DhtObject obj = mMapper.readValue(dht, DhtObject.class); |
| 87 | + return obj.getServers(); |
| 88 | + } catch (JsonMappingException e) { |
| 89 | + return new String[0]; |
| 90 | + } catch (JsonParseException e) { |
| 91 | + return new String[0]; |
| 92 | + } catch (IOException e) { |
| 93 | + return new String[0]; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments