Skip to content

Commit

Permalink
Move DHTNodeDetails to its own class. Also attempt to save to file do…
Browse files Browse the repository at this point in the history
…wnloaded node details to be read when unable to access the tox website
  • Loading branch information
markwinter committed May 9, 2014
1 parent b16c1aa commit 4eee83f
Show file tree
Hide file tree
Showing 22 changed files with 256 additions and 415 deletions.
6 changes: 0 additions & 6 deletions app/src/main/java/im/tox/antox/activities/AboutActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@

import im.tox.antox.R;

/**
* About activity where contact and version information is displayed
* @author Mark Winter (Astonex) & David Lohle (Proplex) & sudden6
*/

public class AboutActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@
import im.tox.antox.tox.ToxService;
import im.tox.antox.utils.DhtNode;

/**
* Activity to allow the user to add a friend. Also as a URI handler to automatically insert public
* keys from tox:// links. See AndroidManifest.xml for more information on the URI handler.
*
* @author Mark Winter (Astonex)
*/

public class AddFriendActivity extends ActionBarActivity implements PinDialogFragment.PinDialogListener {

String _friendID = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
import im.tox.antox.R;
import im.tox.antox.data.AntoxDB;

/**
* Created by soft on 24/03/14.
*/
public class FriendProfileActivity extends ActionBarActivity {

String friendName = null;
Expand Down
190 changes: 10 additions & 180 deletions app/src/main/java/im/tox/antox/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
Expand All @@ -39,7 +40,14 @@
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
Expand All @@ -62,6 +70,7 @@
import im.tox.antox.fragments.ChatFragment;
import im.tox.antox.utils.Constants;
import im.tox.antox.fragments.ContactsFragment;
import im.tox.antox.utils.DHTNodeDetails;
import im.tox.antox.utils.DhtNode;
import im.tox.antox.utils.Friend;
import im.tox.antox.utils.FriendRequest;
Expand Down Expand Up @@ -287,7 +296,7 @@ protected void onCreate(Bundle savedInstanceState) {
// Executes in a separate thread so UI experience isn't affected
// Downloads the DHT node details
if(DhtNode.ipv4.size() == 0)
new DHTNodeDetails().execute();
new DHTNodeDetails(getApplicationContext()).execute();
}
else {
showAlertDialog(MainActivity.this, getString(R.string.main_no_internet),
Expand Down Expand Up @@ -833,185 +842,6 @@ public void onClick(DialogInterface dialog, int which) {
alertDialog.show();
}


// Downloads the the first working DHT node
private class DHTNodeDetails extends AsyncTask<Void, Void, Void> {
final String[] nodeDetails = new String[7];

@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect("http://wiki.tox.im/Nodes").timeout(10000).get();
Elements nodeRows = document.getElementsByTag("tr");

for (Element nodeRow : nodeRows) {
Elements nodeElements = nodeRow.getElementsByTag("td");
int c = 0;
for (Element nodeElement : nodeElements)
nodeDetails[c++] = nodeElement.text();


if (nodeDetails[6] != null && nodeDetails[6].equals("WORK")) {
DhtNode.ipv4.add(nodeDetails[0]);
DhtNode.ipv6.add(nodeDetails[1]);
DhtNode.port.add(nodeDetails[2]);
DhtNode.key.add(nodeDetails[3]);
DhtNode.owner.add(nodeDetails[4]);
DhtNode.location.add(nodeDetails[5]);
}
}
} catch (Exception e) {
DhtNode.ipv4.add("192.254.75.98");
DhtNode.ipv6.add("2607:5600:284::2");
DhtNode.location.add("US");
DhtNode.owner.add("stqism");
DhtNode.port.add("33445");
DhtNode.key.add("FE3914F4616E227F29B2103450D6B55A836AD4BD23F97144E2C4ABE8D504FE1B");

DhtNode.ipv4.add("144.76.60.215");
DhtNode.ipv6.add("2a01:4f8:191:64d6::1");
DhtNode.location.add("DE");
DhtNode.owner.add("sonofra");
DhtNode.port.add("33445");
DhtNode.key.add("04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F");

DhtNode.ipv4.add("37.187.46.132");
DhtNode.ipv6.add("2001:41d0:0052:0300::0507");
DhtNode.location.add("FR");
DhtNode.owner.add("mouseym");
DhtNode.port.add("33445");
DhtNode.key.add("C021232F9AC83914A45DFCF242129B216FED5ED34683F385D932A66BC9178270");

DhtNode.ipv4.add("109.169.46.133");
DhtNode.ipv6.add("");
DhtNode.location.add("UK");
DhtNode.owner.add("astonex");
DhtNode.port.add("33445");
DhtNode.key.add("7F31BFC93B8E4016A902144D0B110C3EA97CB7D43F1C4D21BCAE998A7C838821");

DhtNode.ipv4.add("54.199.139.199");
DhtNode.ipv6.add("");
DhtNode.location.add("JP");
DhtNode.owner.add("aitjcize");
DhtNode.port.add("33445");
DhtNode.key.add("7F9C31FE850E97CEFD4C4591DF93FC757C7C12549DDD55F8EEAECC34FE76C029");

DhtNode.ipv4.add("31.192.105.19");
DhtNode.ipv6.add("");
DhtNode.location.add("RU");
DhtNode.owner.add("zlacki");
DhtNode.port.add("33445");
DhtNode.key.add("D59F99384592DE4C8AB9D534D5197DB90F4755CC9E975ED0C565E18468A1445B");
}

Log.d(TAG, "DhtNode size: " + DhtNode.ipv4.size());
/**
* Ping servers to find quickest connection - Threading this would be goood
*/
int times[][] = new int[DhtNode.ipv4.size()][1];

// Initialise array
for(int i = 0; i < DhtNode.ipv4.size(); i++)
times[i][0] = 500;

final ExecutorService service;
// Create a thread pool equal to the amount of nodes
service = Executors.newFixedThreadPool(DhtNode.ipv4.size());
List<Future<int[]>> list = new ArrayList<Future<int[]>>();
for(int i = 0; i < DhtNode.ipv4.size(); i++) {
Callable<int[]> worker = new PingServers(i);
Future<int[]> submit = service.submit(worker);
list.add(submit);
}

// Get all the times back from the threads
for(Future<int[]> future : list) {
try {
int tmp[] = future.get();
times[tmp[0]][0] = tmp[1];
} catch(ExecutionException e) {
e.printStackTrace();
} catch(InterruptedException e) {
e.printStackTrace();
}
}

// Find shortest time
int shortest = 500;
int pos = -1;
for(int i = 0; i < DhtNode.ipv4.size(); i++) {
if(times[i][0] < shortest) {
shortest = times[i][0];
pos = i;
}
}

// Move shortest node to front
if(pos != -1) {
DhtNode.ipv4.add(0, DhtNode.ipv4.get(pos));
DhtNode.ipv6.add(0, DhtNode.ipv6.get(pos));
DhtNode.port.add(0, DhtNode.port.get(pos));
DhtNode.key.add(0, DhtNode.key.get(pos));
DhtNode.owner.add(0, DhtNode.owner.get(pos));
DhtNode.location.add(0, DhtNode.location.get(pos));
Log.d(TAG, "DHT Nodes have been sorted");
DhtNode.sorted = true;
}

return null;
}

private class PingServers implements Callable<int[]> {
final int number;

public PingServers(int i) {
this.number = i;
}

public int[] call() {
int result[] = { number, 500 };
try {
long currentTime = System.currentTimeMillis();
boolean reachable = InetAddress.getByName(DhtNode.ipv4.get(number)).isReachable(400);
long elapsedTime = System.currentTimeMillis() - currentTime;
if(reachable)
result[1] = (int)elapsedTime;
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace();
}

return result;
}
}

@Override
protected void onPostExecute(Void result)
{
/**
* There is a chance that downloading finishes later than the bootstrapping call in the
* ToxService, because both are in separate threads. In that case to make sure the nodes
* are bootstrapped we restart the ToxService
*/
if(!DhtNode.connected)
{
Log.d(TAG, "Restarting START_TOX as DhtNode.connected returned false");
Intent restart = new Intent(getApplicationContext(), ToxDoService.class);
restart.setAction(Constants.START_TOX);
getApplicationContext().startService(restart);
}

/* Restart intent if it was connected before nodes were sorted */
if(DhtNode.connected && !DhtNode.sorted) {
Log.d(TAG, "Restarting START_TOX as DhtNode.sorted was false");
Intent restart = new Intent(getApplicationContext(), ToxDoService.class);
restart.setAction(Constants.START_TOX);
getApplicationContext().startService(restart);
}
}
}

@Override
public void onBackPressed() {
if (!pane.isOpen()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@
import im.tox.antox.utils.UserDetails;
import im.tox.jtoxcore.ToxUserStatus;

/**
* Profile Activity where the user can change their username, status, and note.
* @author Mark Winter (Astonex) & David Lohle (Proplex)
*/

public class ProfileActivity extends ActionBarActivity {
/**
* Spinner for displaying acceptable statuses (online/away/busy) to the users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
import im.tox.antox.utils.UserDetails;
import im.tox.jtoxcore.ToxUserStatus;

/**
* Welcome Activity is displayed when the user is using the app for the very first time to get
* some required details such as a username.
*
* @author Mark Winter (Astonex)
*/

public class WelcomeActivity extends ActionBarActivity {

TextView username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import im.tox.antox.utils.AntoxFriend;
import im.tox.jtoxcore.callbacks.OnActionCallback;

/**
* Created by soft on 03/03/14.
*/
public class AntoxOnActionCallback implements OnActionCallback<AntoxFriend> {

private final static String TAG = "im.tox.antox.TAG";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import im.tox.antox.tox.ToxService;
import im.tox.jtoxcore.callbacks.OnConnectionStatusCallback;

/**
* Created by soft on 03/03/14.
*/
public class AntoxOnConnectionStatusCallback implements OnConnectionStatusCallback<AntoxFriend> {

private final static String TAG = "im.tox.antox.TAG";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import im.tox.antox.tox.ToxService;
import im.tox.jtoxcore.callbacks.OnFriendRequestCallback;

/**
* Created by soft on 02/03/14.
*/
public class AntoxOnFriendRequestCallback implements OnFriendRequestCallback {

private static final String TAG = "im.tox.antox.TAG";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import im.tox.antox.utils.Constants;
import im.tox.jtoxcore.callbacks.OnNameChangeCallback;

/**
* Created by soft on 03/03/14.
*/
public class AntoxOnNameChangeCallback implements OnNameChangeCallback<AntoxFriend> {

private final static String TAG = "im.tox.antox.TAG";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import im.tox.antox.tox.ToxService;
import im.tox.jtoxcore.callbacks.OnReadReceiptCallback;

/**
* Created by soft on 03/03/14.
*/
public class AntoxOnReadReceiptCallback implements OnReadReceiptCallback<AntoxFriend> {

private final static String TAG = "im.tox.antox.TAG";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import im.tox.antox.utils.Constants;
import im.tox.jtoxcore.callbacks.OnStatusMessageCallback;

/**
* Created by soft on 03/03/14.
*/
public class AntoxOnStatusMessageCallback implements OnStatusMessageCallback<AntoxFriend> {

private final static String TAG = "im.tox.antox.TAG";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import im.tox.antox.utils.AntoxFriend;
import im.tox.jtoxcore.callbacks.OnTypingChangeCallback;

/**
* Created by soft on 27/03/14.
*/
public class AntoxOnTypingChangeCallback implements OnTypingChangeCallback<AntoxFriend> {

private static final String TAG = "OnTypingChangeCallback";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import im.tox.jtoxcore.ToxUserStatus;
import im.tox.jtoxcore.callbacks.OnUserStatusCallback;

/**
* Created by soft on 03/03/14.
*/
public class AntoxOnUserStatusCallback implements OnUserStatusCallback<AntoxFriend> {

private final static String TAG = "im.tox.antox.TAG";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import im.tox.antox.activities.MainActivity;
import im.tox.antox.adapters.LeftPaneAdapter;


/**
* Created by ollie on 28/02/14.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

import im.tox.antox.R;

/**
* Created by soft on 03/04/14.
*/
public class PinDialogFragment extends DialogFragment {
public interface PinDialogListener {
public void onDialogPositiveClick(DialogFragment dialog, String pin);
Expand Down

1 comment on commit 4eee83f

@markwinter
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file saving and loading still needs some work done to it

Please sign in to comment.