Search in sources :

Example 1 with MagnetPlugin

use of com.biglybt.plugin.magnet.MagnetPlugin in project BiglyBT by BiglySoftware.

the class SubscriptionManagerImpl method downloadTorrent.

protected Object[] downloadTorrent(byte[] hash, int update_size) {
    if (!isSubsDownloadEnabled()) {
        log("    Can't download subscription " + Base32.encode(hash) + " as feature disabled");
        return (null);
    }
    final MagnetPlugin magnet_plugin = getMagnetPlugin();
    if (magnet_plugin == null) {
        log("    Can't download, no magnet plugin");
        return (null);
    }
    try {
        final InetSocketAddress[] sender = { null };
        byte[] torrent_data = magnet_plugin.download(new MagnetPluginProgressListener() {

            @Override
            public void reportSize(long size) {
            }

            @Override
            public void reportActivity(String str) {
                log("    MagnetDownload: " + str);
            }

            @Override
            public void reportCompleteness(int percent) {
            }

            @Override
            public void reportContributor(InetSocketAddress address) {
                synchronized (sender) {
                    sender[0] = address;
                }
            }

            @Override
            public boolean verbose() {
                return (false);
            }

            @Override
            public boolean cancelled() {
                return (false);
            }
        }, hash, "", new InetSocketAddress[0], 300 * 1000, MagnetPlugin.FL_DISABLE_MD_LOOKUP);
        if (torrent_data == null) {
            log("    download failed - timeout");
            return (null);
        }
        log("Subscription torrent downloaded");
        TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedByteArray(torrent_data);
        if (torrent.getSize() > update_size + 10 * 1024) {
            log("Subscription download abandoned, torrent size is " + torrent.getSize() + ", underlying data size is " + update_size);
            return (null);
        }
        if (torrent.getSize() > 4 * 1024 * 1024) {
            log("Subscription download abandoned, torrent size is too large (" + torrent.getSize() + ")");
            return (null);
        }
        synchronized (sender) {
            return (new Object[] { torrent, sender[0] });
        }
    } catch (Throwable e) {
        log("    download failed", e);
        return (null);
    }
}
Also used : MagnetPluginProgressListener(com.biglybt.plugin.magnet.MagnetPluginProgressListener) InetSocketAddress(java.net.InetSocketAddress) TOTorrent(com.biglybt.core.torrent.TOTorrent) MagnetPlugin(com.biglybt.plugin.magnet.MagnetPlugin)

Example 2 with MagnetPlugin

use of com.biglybt.plugin.magnet.MagnetPlugin in project BiglyBT by BiglySoftware.

the class BuddyPlugin method handleUPTorrent.

public InputStream handleUPTorrent(final AZPluginConnection connection, final BuddyPluginBuddy buddy, String tag_or_category, final byte[] hash) throws IPCException {
    final long timeout = 120 * 1000;
    final Object[] result = { null };
    final AESemaphore result_sem = new AESemaphore("BuddyPlugin:upt");
    log("Attempting to download torrent for " + Base32.encode(hash));
    if (buddy.isOnline(true)) {
        try {
            Map<String, Object> msg = new HashMap<>();
            try {
                msg.put("cat", tag_or_category.getBytes("UTF-8"));
                msg.put("hash", hash);
            } catch (Throwable e) {
                Debug.out(e);
            }
            az2_handler.sendAZ2RSSMessage(buddy, msg, new BuddyPluginAZ2TrackerListener() {

                private boolean result_set;

                @Override
                public Map messageReceived(BuddyPluginBuddy buddy, Map message) {
                    try {
                        byte[] bytes = (byte[]) message.get("torrent");
                        log("    torrent downloaded from buddy");
                        setResult(bytes);
                    } catch (Throwable e) {
                        messageFailed(buddy, e);
                    }
                    return (null);
                }

                @Override
                public void messageFailed(BuddyPluginBuddy buddy, Throwable cause) {
                    setResult(new IPCException("Read failed", cause));
                }

                protected void setResult(Object obj) {
                    synchronized (result) {
                        if (result_set) {
                            return;
                        }
                        result_set = true;
                        if (!(result[0] instanceof byte[])) {
                            result[0] = obj;
                        }
                        result_sem.release();
                    }
                }
            });
        } catch (Throwable e) {
            result[0] = new IPCException("Buddy torrent get failed", e);
            result_sem.release();
        }
    } else {
        result[0] = new IPCException("Buddy is offline");
        result_sem.release();
    }
    // second try and get via magnet
    final MagnetPlugin magnet_plugin = getMagnetPlugin();
    if (magnet_plugin == null) {
        synchronized (result) {
            if (result[0] == null) {
                result[0] = new IPCException("Magnet plugin unavailable");
            }
        }
        result_sem.release();
    } else {
        new AEThread2("BuddyPlugin:mag", true) {

            private boolean result_set;

            @Override
            public void run() {
                try {
                    if (buddy.isOnline(true)) {
                        Thread.sleep(10 * 1000);
                    }
                    synchronized (result) {
                        if (result[0] instanceof byte[]) {
                            setResult(null);
                            return;
                        }
                    }
                    byte[] torrent_data = magnet_plugin.download(!logger.isEnabled() ? null : new MagnetPluginProgressListener() {

                        @Override
                        public void reportSize(long size) {
                        }

                        @Override
                        public void reportActivity(String str) {
                            log("    MagnetDownload: " + str);
                        }

                        @Override
                        public void reportCompleteness(int percent) {
                        }

                        @Override
                        public void reportContributor(InetSocketAddress address) {
                        }

                        @Override
                        public boolean verbose() {
                            return (false);
                        }

                        @Override
                        public boolean cancelled() {
                            return (false);
                        }
                    }, hash, "", new InetSocketAddress[0], timeout, MagnetPlugin.FL_NONE);
                    if (torrent_data == null) {
                        setResult(new IPCException("Magnet timeout"));
                    } else {
                        log("    torrent downloaded from magnet");
                        setResult(torrent_data);
                    }
                } catch (Throwable e) {
                    setResult(new IPCException("Magnet get failed", e));
                }
            }

            protected void setResult(Object obj) {
                synchronized (result) {
                    if (result_set) {
                        return;
                    }
                    result_set = true;
                    if (obj != null) {
                        if (result[0] == null || (obj instanceof byte[] && !(result[0] instanceof byte[]))) {
                            result[0] = obj;
                        }
                    }
                    result_sem.release();
                }
            }
        }.start();
    }
    long start = SystemTime.getMonotonousTime();
    if (result_sem.reserve(timeout)) {
        if (!(result[0] instanceof byte[])) {
            long rem = timeout - (SystemTime.getMonotonousTime() - start);
            if (rem > 0) {
                result_sem.reserve(rem);
            }
        }
    }
    if (result[0] == null) {
        log("    torrent download timeout");
        throw (new IPCException("Timeout"));
    } else if (result[0] instanceof byte[]) {
        return (new ByteArrayInputStream((byte[]) result[0]));
    } else {
        IPCException error = (IPCException) result[0];
        log("    torrent downloaded failed: " + Debug.getNestedExceptionMessage(error));
        throw (error);
    }
}
Also used : MagnetPluginProgressListener(com.biglybt.plugin.magnet.MagnetPluginProgressListener) InetSocketAddress(java.net.InetSocketAddress) IPCException(com.biglybt.pif.ipc.IPCException) MagnetPlugin(com.biglybt.plugin.magnet.MagnetPlugin)

Example 3 with MagnetPlugin

use of com.biglybt.plugin.magnet.MagnetPlugin in project BiglyBT by BiglySoftware.

the class ExternalStimulusHandler method initialise.

protected static void initialise(Core core) {
    PluginInterface pi = core.getPluginManager().getPluginInterfaceByClass(MagnetPlugin.class);
    if (pi != null) {
        MagnetPlugin temp = (MagnetPlugin) pi.getPlugin();
        List to_add;
        synchronized (ExternalStimulusHandler.class) {
            magnet_plugin = temp;
            to_add = pending_listeners;
            pending_listeners = null;
        }
        if (to_add != null) {
            for (int i = 0; i < to_add.size(); i++) {
                addListener((ExternalStimulusListener) to_add.get(i));
            }
        }
    } else {
        Debug.out("Failed to resolve magnet plugin");
    }
    // Debug.outNoStack( "ExternalStimulus debug" );
    addListener(new ExternalStimulusListener() {

        @Override
        public boolean receive(String name, Map values) {
            return (name.equals("ExternalStimulus.test"));
        }

        @Override
        public int query(String name, Map values) {
            return (Integer.MIN_VALUE);
        }
    });
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface) List(java.util.List) ArrayList(java.util.ArrayList) MagnetPlugin(com.biglybt.plugin.magnet.MagnetPlugin) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

MagnetPlugin (com.biglybt.plugin.magnet.MagnetPlugin)3 MagnetPluginProgressListener (com.biglybt.plugin.magnet.MagnetPluginProgressListener)2 InetSocketAddress (java.net.InetSocketAddress)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 PluginInterface (com.biglybt.pif.PluginInterface)1 IPCException (com.biglybt.pif.ipc.IPCException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1