Android Open Source - dissertation-project Resilience Controller






From Project

Back to project page dissertation-project.

License

The source code is released under:

MIT License

If you think the Android project dissertation-project listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.fyp.resilience;
//from   www  .j  av  a  2 s.c  om
import java.io.File;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;

import com.fyp.resilience.connection.Connectable;
import com.fyp.resilience.database.ResilienceDbManager;
import com.fyp.resilience.database.model.DataPiece;
import com.fyp.resilience.database.model.DataWhole;
import com.fyp.resilience.event.ClientListChanged;
import com.fyp.resilience.event.ConnectionsModified;
import com.fyp.resilience.event.WholeModified;
import com.fyp.resilience.swarm.model.SwarmClient;

import de.greenrobot.event.EventBus;

/**
 * Controls each aspect of the {@link DataWhole} cache, {@link Connectable}
 * list, and {@link SwarmClient} list.
 */
public class ResilienceController {

    private final List<Connectable> mConnectionList;
    private final Map<String, DataWhole> mCacheDataWholeList;
    private final List<SwarmClient> mSwarmList;

    private final Context mContext;

    /**
     * Must ensure that the controller returned is that which is related to this
     * Context. Failure to do so may lead to undesirable errors.
     * 
     * @param context {@link Context}
     * @return {@link ResilienceController}
     */
    public static ResilienceController getInstance(final Context context) {
        return ResilienceApplication.getApplication(context).getResilienceController();
    }

    /**
     * Upon construction, the object constructs a new cache array list, which is
     * then filled from the DB.
     * 
     * @param context {@link Context}
     */
    ResilienceController(final Context context) {

        mContext = context;

        mCacheDataWholeList = new HashMap<String, DataWhole>();
        mConnectionList = new ArrayList<Connectable>();
        mSwarmList = new ArrayList<SwarmClient>();

        List<DataWhole> wholeCache = ResilienceDbManager.getDataWholes(mContext);
        if (null != wholeCache) {
            for (final DataWhole dataWhole : wholeCache) {
                mCacheDataWholeList.put(dataWhole.getKey(), dataWhole);
            }
        }
    }

    /**
     * @return A boolean indicating whether any {@link DataPiece}s require
     *         upload.
     */
    public boolean hasFilesWaiting() {
        synchronized (mCacheDataWholeList) {
            for (final DataWhole dataWhole : mCacheDataWholeList.values()) {
                if (!dataWhole.isAvailable() && null != dataWhole.getPieces()) {
                    for (DataPiece dataPiece : dataWhole.getPieces()) {
                        if (dataPiece.getState() == DataPiece.STATE_NONE || dataPiece.requiresRetry()) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }

    /**
     * Returns a new ArrayList that contains all elements within the cache.
     * 
     * @return {@link List}
     */
    public List<DataWhole> getDataWholeCache() {
        synchronized (mCacheDataWholeList) {
            return new ArrayList<DataWhole>(mCacheDataWholeList.values());
        }
    }

    /**
     * Returns a new {@link List} containing the Swarm Clients.
     * 
     * @return
     */
    public List<SwarmClient> getSwarmList() {
        return new ArrayList<SwarmClient>(mSwarmList);
    }

    /**
     * Gets a {@link SwarmClient} from the Swarm list with the provided
     * {@link InetAddress}.
     * 
     * @param address {@link InetAddress}
     * @return {@link SwarmClient}
     */
    public SwarmClient getClientFromListWithAddress(final InetAddress address) {
        for (final SwarmClient client : mSwarmList) {
            if (client.getAddress() != null && client.getAddress().equals(address)) {
                return client;
            }
        }
        return null;
    }

    /**
     * Adds a {@link SwarmClient} to the Swarm list providing it hasn't already.
     * 
     * @param client - The {@link SwarmClient} to be added.
     */
    public void addClientToList(final SwarmClient client) {
        if (!mSwarmList.contains(client)) {
            mSwarmList.add(client);
            postToEventBus(new ClientListChanged());
        }
    }

    /**
     * Removes the provided {@link SwarmClient} from the Swarm list.
     * 
     * @param client - The {@link SwarmClient} to be removed.
     */
    public void removeClientFromList(final SwarmClient client) {
        mSwarmList.remove(client);
        postToEventBus(new ClientListChanged());
    }

    /**
     * Clears the Swarm list of all entities.
     */
    public void clearClientList() {
        mSwarmList.clear();
        postToEventBus(new ClientListChanged());
    }

    /**
     * @return A new {@link List} of {@link Connectable}s.
     */
    public List<Connectable> getConnectionList() {
        synchronized (mConnectionList) {
            return new ArrayList<Connectable>(mConnectionList);
        }
    }

    /**
     * Adds a {@link Connectable} to the Connection list.
     * 
     * @param connection - The {@link Connectable} to be added to the Connection
     *            list.
     */
    public void addConnection(final Connectable connection) {
        if (null != connection) {
            synchronized (mConnectionList) {
                if (!mConnectionList.contains(connection)) {
                    mConnectionList.add(connection);
                }
                postToEventBus(new ConnectionsModified(connection.getDataWhole()));
            }
        }
    }

    /**
     * Removes a {@link Connectable} from the Connection list. Posts a
     * {@link ConnectionsModified} to the EventBus.
     * 
     * @param connection - The {@link Connectable} to be removed
     */
    public void removeConnection(final Connectable connection) {
        if (null != connection) {
            synchronized (mConnectionList) {
                mConnectionList.remove(connection);

                if (null != connection.getDataWhole()) {
                    ResilienceDbManager.persistDataWhole(mContext, connection.getDataWhole());
                }
                postToEventBus(new ConnectionsModified(connection.getDataWhole()));
            }
        }
    }

    /**
     * @param dataWhole - The {@link DataWhole} to be added
     * @return A boolean to dictate whether the DataWhole was persisted or not
     */
    public void addDataWhole(final DataWhole dataWhole) {
        if (null != dataWhole) {
            synchronized (mCacheDataWholeList) {
                if (!mCacheDataWholeList.containsKey(dataWhole.getKey())) {
                    mCacheDataWholeList.put(dataWhole.getKey(), dataWhole);
                }
                ResilienceDbManager.persistDataWhole(mContext, dataWhole);
                postToEventBus(new WholeModified());
            }
        }
    }

    /**
     * @param dataWhole - The {@link DataWhole} to be removed
     * @return A boolean to dictate whether the DataWhole was persisted or not
     */
    public void removeDataWhole(final DataWhole dataWhole) {
        if (null != dataWhole) {
            synchronized (mCacheDataWholeList) {
                mCacheDataWholeList.remove(dataWhole.getKey());
                ResilienceDbManager.removeDataWhole(mContext, dataWhole);
                postToEventBus(new WholeModified());
            }
        }
    }

    /**
     * Removes {@link DataPiece}s from the specified {@link DataWhole}
     * 
     * @param dataWhole - {@link DataWhole} whose {@link DataPiece}s should be
     *            removed.
     */
    public void removeDataPieces(final DataWhole dataWhole) {
        if (null != dataWhole) {
            synchronized (mCacheDataWholeList) {

                final List<DataPiece> pieces = dataWhole.getPieces();
                if (null != pieces) {
                    for (final DataPiece dataPiece : pieces) {
                        File pieceFile = dataPiece.getFile(mContext);
                        if (null != pieceFile) {
                            final boolean result = dataPiece.getFile(mContext).delete();
                            Log.d("PIECE_DELETION", "Piece deleted: " + result);
                        }
                    }
                }

                dataWhole.setPieces(null);
                ResilienceDbManager.removeDataPieces(mContext, pieces);
            }
        }
    }

    /**
     * Searches through the {@link DataWhole} cache and searches for a
     * {@link DataPiece} that requires upload.
     * 
     * @return The first {@link DataPiece} that is available for upload.
     */
    public DataPiece getNextDataPieceToUpload() {
        synchronized (mCacheDataWholeList) {
            for (final DataWhole dataWhole : mCacheDataWholeList.values()) {

                if ((dataWhole.getState() == DataWhole.STATE_IN_PROGRESS
                        || dataWhole.getState() == DataWhole.STATE_NONE)
                        && !dataWhole.isAvailable()
                        && null != dataWhole.getPieces()) {

                    for (final DataPiece dataPiece : dataWhole.getPieces()) {

                        final int state = dataPiece.getState();
                        if (state == DataPiece.STATE_NONE ||
                                state == DataPiece.STATE_UPLOADED_TO_DEVICE ||
                                dataPiece.requiresRetry()) {

                            return dataPiece;
                        }
                    }
                }
            }
            /* Indicates that there is NOTHING waiting to be uploaded */
            return null;
        }
    }

    /**
     * Searches through the {@link DataWhole} cache and searches for all
     * {@link DataPiece}s that require upload.
     * 
     * @return The first {@link DataPiece} that is available for upload or null.
     */
    public List<DataPiece> getAllPiecesToUpload() {
        synchronized (mCacheDataWholeList) {
            final List<DataPiece> uploadPieces = new ArrayList<DataPiece>();
            for (final DataWhole dataWhole : mCacheDataWholeList.values()) {

                if ((dataWhole.getState() == DataWhole.STATE_IN_PROGRESS
                        || dataWhole.getState() == DataWhole.STATE_NONE)
                        && !dataWhole.isAvailable()
                        && null != dataWhole.getPieces()) {

                    for (final DataPiece dataPiece : dataWhole.getPieces()) {
                        if (dataPiece.getState() == DataPiece.STATE_NONE) {
                            uploadPieces.add(dataPiece);
                        }
                    }
                }
            }
            return uploadPieces;
        }
    }

    /**
     * Searches through the cache for a {@link DataWhole} with the specified ID.
     * 
     * @param key - The ID to be searched.
     * @return The {@link DataWhole} or null.
     */
    public DataWhole getDataWholeById(final String key) {
        synchronized (mCacheDataWholeList) {
            final DataWhole dataWhole = mCacheDataWholeList.get(key);
            return dataWhole != null ? dataWhole : null;
        }
    }

    /**
     * A helper method to post an event to the EventBus
     * 
     * @param event - An event object
     */
    private void postToEventBus(final Object event) {
        EventBus.getDefault().post(event);
    }
}




Java Source Code List

com.fyp.resilience.Constants.java
com.fyp.resilience.Flags.java
com.fyp.resilience.GCMIntentService.java
com.fyp.resilience.PreferenceConstants.java
com.fyp.resilience.ResilienceApplication.java
com.fyp.resilience.ResilienceController.java
com.fyp.resilience.activity.LicenceActivity.java
com.fyp.resilience.activity.ResilienceActivity.java
com.fyp.resilience.activity.SettingsActivity.java
com.fyp.resilience.adapter.ClientListAdapter.java
com.fyp.resilience.adapter.ConnectionListAdapter.java
com.fyp.resilience.adapter.FileListAdapter.java
com.fyp.resilience.connection.Connectable.java
com.fyp.resilience.connection.ServerDownloadConnectable.java
com.fyp.resilience.connection.ServerUploadConnectable.java
com.fyp.resilience.connection.UploadConnectable.java
com.fyp.resilience.connection.WifiDownloadConnectable.java
com.fyp.resilience.connection.WifiUploadConnectable.java
com.fyp.resilience.database.ResilienceDbHelper.java
com.fyp.resilience.database.ResilienceDbManager.java
com.fyp.resilience.database.model.DataPiece.java
com.fyp.resilience.database.model.DataWhole.java
com.fyp.resilience.event.ClientListChanged.java
com.fyp.resilience.event.ClientModified.java
com.fyp.resilience.event.ConnectionProgressChange.java
com.fyp.resilience.event.ConnectionStateChange.java
com.fyp.resilience.event.ConnectionsModified.java
com.fyp.resilience.event.PieceStateChange.java
com.fyp.resilience.event.ServerRegistrationChanged.java
com.fyp.resilience.event.ServerUploadFinished.java
com.fyp.resilience.event.WholeModified.java
com.fyp.resilience.event.WifiDownloadFinished.java
com.fyp.resilience.event.WifiUploadFinished.java
com.fyp.resilience.fragment.ClientsFragment.java
com.fyp.resilience.fragment.ConnectionsFragment.java
com.fyp.resilience.fragment.FilesFragment.java
com.fyp.resilience.interfaces.Messagable.java
com.fyp.resilience.interfaces.Partialable.java
com.fyp.resilience.proto.ProtoBuffSpecification.java
com.fyp.resilience.receiver.AbstractConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.BootReceiver.java
com.fyp.resilience.receiver.ConnectivityBroadcastReceiver.java
com.fyp.resilience.receiver.WiFiDirectBroadcastReceiver.java
com.fyp.resilience.register.RegisterRequestInitializer.java
com.fyp.resilience.register.RegisterRequest.java
com.fyp.resilience.register.RegisterScopes.java
com.fyp.resilience.register.Register.java
com.fyp.resilience.register.model.DeviceInfo.java
com.fyp.resilience.service.PieceUploadService.java
com.fyp.resilience.stream.PiecedRandomAccessFile.java
com.fyp.resilience.swarm.helper.NsdHelper.java
com.fyp.resilience.swarm.helper.SwarmHelperInterface.java
com.fyp.resilience.swarm.helper.WifiDirectSdHelper.java
com.fyp.resilience.swarm.model.SwarmClient.java
com.fyp.resilience.thread.ResilienceRunnable.java
com.fyp.resilience.thread.ResilienceThreadFactory.java
com.fyp.resilience.util.ConnectionUtils.java
com.fyp.resilience.util.Utils.java
com.fyp.resilience.view.ClientView.java
com.fyp.resilience.view.ConnectionView.java
com.fyp.resilience.view.FileView.java
com.fyp.resilience.view.PieceProgressIndicator.java
com.fyp.resilience.widerst.WiderstRequestInitializer.java
com.fyp.resilience.widerst.WiderstRequest.java
com.fyp.resilience.widerst.WiderstScopes.java
com.fyp.resilience.widerst.Widerst.java
com.fyp.resilience.widerst.model.DataPiecePartial.java
com.fyp.resilience.widerst.model.DataWholePartial.java
com.fyp.resilience.widerst.model.PostResponse.java
com.fyp.widerst.Constants.java
com.fyp.widerst.WiderstObjectifyService.java
com.fyp.widerst.backend.FileJoinerBackend.java
com.fyp.widerst.cron.CronJobServlet.java
com.fyp.widerst.endpoint.DataPieceEndpoint.java
com.fyp.widerst.endpoint.DeviceInfoEndpoint.java
com.fyp.widerst.entity.DataPiece.java
com.fyp.widerst.entity.DataWhole.java
com.fyp.widerst.entity.DeviceInfo.java
com.fyp.widerst.handler.BlobstoreUploadHandler.java
com.fyp.widerst.partial.DataPiecePartial.java
com.fyp.widerst.partial.DataWholePartial.java
com.fyp.widerst.response.PostResponse.java
com.fyp.widerst.servlet.WholeFileServer.java
com.fyp.widerst.util.DbHelper.java
com.fyp.widerst.util.GcmHelper.java