Android Open Source - swazam Peer Connector






From Project

Back to project page swazam.

License

The source code is released under:

MIT License

If you think the Android project swazam 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 at.ac.tuwien.swa13.swazam.p2p;
// ww w.j a v a2s. c o m
import ac.at.tuwien.infosys.swa.audio.Fingerprint;
import at.ac.tuwien.swa13.swazam.NetworkConnection;
import at.ac.tuwien.swa13.swazam.fingerprint.impl.FingerPrintCreator;
import at.ac.tuwien.swa13.swazam.library.ILibraryManager;
import at.ac.tuwien.swa13.swazam.library.ISong;
import java.net.InetAddress;
import java.util.LinkedList;
import java.util.Random;
import net.tomp2p.futures.FutureDiscover;
import net.tomp2p.p2p.Peer;
import net.tomp2p.p2p.PeerMaker;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress;
import net.tomp2p.rpc.ObjectDataReply;

public class PeerConnector {
    private final ILibraryManager libraryManager;
    private final String username;
    private String serverAddress;
    
    private LinkedList<String> previousTasks;

    private Peer peer;

    private static final int serverPort = 4000;

    public PeerConnector(ILibraryManager library, String username)
    {
        this.libraryManager = library;
        this.username = username;

        this.previousTasks = new LinkedList<String>();
        
        System.out.println("Created PeerConnector");
    }

    public void listen(String ipAddress) throws Exception
    {
        this.serverAddress = ipAddress;

        // TODO: Listen to Master Peer for new data and do stuff
        this.peer = new PeerMaker(new Number160(new Random(43L))).makeAndListen();
        PeerAddress supernode = new PeerAddress(Number160.ZERO, InetAddress.getByName(ipAddress), serverPort, serverPort);
        FutureDiscover fd = peer.discover().setPeerAddress(supernode).start();
        fd.awaitUninterruptibly();

        if (fd.isSuccess()) {
            //FutureBootstrap fb = peer.bootstrap().setPeerAddress(supernode).start();
            //fb.awaitUninterruptibly();

            System.out.println("Bootstrap complete (" + peer.getPeerAddress() + "). Waiting for work...");
        } else {
            System.out.println("Discover failed: " + fd.getFailedReason());

            return;
        }

        // Setup request handler
        peer.setObjectDataReply(new ObjectDataReply() {
            @Override
            public Object reply(PeerAddress sender, Object request) throws Exception
            {
                System.out.println("Received task from network. Looking for song...");

                String[] rData = (String[])request;
                
                if (previousTasks.contains(rData[0])) {
                    System.out.println("Had this request before. Dropping it...");
                    return null;
                } else {
                    previousTasks.add(rData[0]);
                    // Trim it
                    if (previousTasks.size() > 100)
                        previousTasks.remove();
                }
                
                findSong(rData[0], rData[1]);

                // We don't care about the return
                return null;
            }
        });
    }

    private void notifyNeighbors(String taskId, String songFragment)
    {
        System.out.println("Song not found. notifing neighbors...");

        // Ask known peers to work on this task
        String[] data = { taskId, songFragment };
        peer.send(Number160.createHash(taskId)).setObject(data).start();
    }

    private void findSong(String taskId, String songFragmentString)
    {
        // Convert fingerprint to usable object
        Fingerprint songFragment = FingerPrintCreator.fromString(songFragmentString);

        ISong foundSong = this.libraryManager.findSong(songFragment);

        System.out.println(foundSong);
        
        if (foundSong == null) {
            // Notify neighboring peers to start looking for the song
            this.notifyNeighbors(taskId, songFragmentString);
        } else {
            // Notify server via REST interface that we have found the song
            NetworkConnection con = new NetworkConnection(this.serverAddress);
            con.makeRequest(taskId, this.username, foundSong);
        }
    }
}




Java Source Code List

ac.tuwien.sa13.beans.HibernateAwareObjectMapper.java
ac.tuwien.sa13.controller.HomeController.java
ac.tuwien.sa13.controller.LoginController.java
ac.tuwien.sa13.controller.RequestController.java
ac.tuwien.sa13.controller.TransactionController.java
ac.tuwien.sa13.controller.api.UserRestController.java
ac.tuwien.sa13.dao.HibernateDAO.java
ac.tuwien.sa13.dao.IGenericDAO.java
ac.tuwien.sa13.dao.IRequestDAO.java
ac.tuwien.sa13.dao.ITransactionDAO.java
ac.tuwien.sa13.dao.IUserDAO.java
ac.tuwien.sa13.dao.RequestDAO.java
ac.tuwien.sa13.dao.TransactionDAO.java
ac.tuwien.sa13.dao.UserDAO.java
ac.tuwien.sa13.entity.Request.java
ac.tuwien.sa13.entity.Transaction.java
ac.tuwien.sa13.entity.User.java
ac.tuwien.sa13.service.IGenericService.java
ac.tuwien.sa13.service.IRequestService.java
ac.tuwien.sa13.service.ITransactionService.java
ac.tuwien.sa13.service.IUserService.java
ac.tuwien.sa13.service.PeerManager.java
ac.tuwien.sa13.service.RequestService.java
ac.tuwien.sa13.service.TransactionService.java
ac.tuwien.sa13.service.UserService.java
ac.tuwien.sa13.utilities.SessionIdentifierGenerator.java
ac.tuwien.sa13.utilities.Token.java
ac.tuwien.sa13.validator.LoginValidator.java
at.ac.tuwien.swa13.swazam.NetworkConnection.java
at.ac.tuwien.swa13.swazam.Peer.java
at.ac.tuwien.swa13.swazam.fingerprint.IFingerPrintCreator.java
at.ac.tuwien.swa13.swazam.fingerprint.impl.FingerPrintCreator.java
at.ac.tuwien.swa13.swazam.library.ILibraryManager.java
at.ac.tuwien.swa13.swazam.library.ISong.java
at.ac.tuwien.swa13.swazam.library.impl.LibraryManager.java
at.ac.tuwien.swa13.swazam.library.impl.Song.java
at.ac.tuwien.swa13.swazam.p2p.PeerConnector.java
at.saws2013.szazam.App.java
at.saws2013.szazam.BaseActivity.java
at.saws2013.szazam.HistoryActivity.java
at.saws2013.szazam.LoginActivity.java
at.saws2013.szazam.RecognitionActivity.java
at.saws2013.szazam.SettingsActivity.java
at.saws2013.szazam.entities.IListItem.java
at.saws2013.szazam.entities.ListItemTypes.java
at.saws2013.szazam.entities.Request.java
at.saws2013.szazam.entities.Transaction.java
at.saws2013.szazam.fingerprint.IFingerPrintSystem.java
at.saws2013.szazam.fingerprint.impl.FingerPrintCreator.java
at.saws2013.szazam.fragments.SettingsFragment.java
at.saws2013.szazam.media.IAudioFilePicker.java
at.saws2013.szazam.media.IAudioRecorder.java
at.saws2013.szazam.media.impl.AudioFilePicker.java
at.saws2013.szazam.media.impl.AudioRecorder.java
at.saws2013.szazam.store.IAuthStore.java
at.saws2013.szazam.store.IRequestStore.java
at.saws2013.szazam.store.ITransactionStore.java
at.saws2013.szazam.store.impl.AuthStore.java
at.saws2013.szazam.store.impl.RequestStore.java
at.saws2013.szazam.store.impl.TransactionStore.java
at.saws2013.szazam.ui.CustomListAdapter.java
at.saws2013.szazam.ui.ViewTools.java
at.saws2013.szazam.volley.CustomVolleyStringRequest.java
de.passsy.holocircularprogressbar.HoloCircularProgressBar.java