Search in sources :

Example 1 with IPCException

use of com.biglybt.pif.ipc.IPCException in project BiglyBT by BiglySoftware.

the class Handler method getProxy.

private URL getProxy(URL u) throws IOException {
    PluginInterface pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByID("azwebtorrent");
    if (pi == null) {
        installPlugin();
        throw (new IOException("'WebTorrent Support Plugin' is required - go to 'Tools->Plugins->Installation Wizard' to install."));
    }
    IPCInterface ipc = pi.getIPC();
    try {
        URL url = (URL) ipc.invoke("getProxyURL", new Object[] { u });
        return (url);
    } catch (IPCException ipce) {
        Throwable e = ipce;
        if (e.getCause() != null) {
            e = e.getCause();
        }
        throw (new IOException("Communication error with WebTorrent Support Plugin: " + Debug.getNestedExceptionMessage(e)));
    }
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface) IPCException(com.biglybt.pif.ipc.IPCException) IOException(java.io.IOException) IPCInterface(com.biglybt.pif.ipc.IPCInterface) URL(java.net.URL)

Example 2 with IPCException

use of com.biglybt.pif.ipc.IPCException in project BiglyBT by BiglySoftware.

the class AZPluginConnection method connect.

@Override
public void connect() throws IOException {
    String url = getURL().toString();
    int pos = url.indexOf("?");
    if (pos == -1) {
        throw (new IOException("Malformed URL - ? missing"));
    }
    url = url.substring(pos + 1);
    String[] bits = url.split("&");
    Map args = new HashMap();
    for (int i = 0; i < bits.length; i++) {
        String bit = bits[i];
        String[] x = bit.split("=");
        if (x.length == 2) {
            String lhs = x[0];
            String rhs = UrlUtils.decode(x[1]);
            args.put(lhs.toLowerCase(), rhs);
        }
    }
    String plugin_id = (String) args.get("id");
    if (plugin_id == null) {
        throw (new IOException("Plugin id missing"));
    }
    String plugin_name = (String) args.get("name");
    String arg = (String) args.get("arg");
    if (arg == null) {
        arg = "";
    }
    String plugin_str;
    if (plugin_name == null) {
        plugin_str = "with id '" + plugin_id + "'";
    } else {
        plugin_str = "'" + plugin_name + "' (id " + plugin_id + ")";
    }
    if (plugin_id.equals("subscription")) {
        SubscriptionManager manager = SubscriptionManagerFactory.getSingleton();
        if (manager == null) {
            throw (new IOException("Subscriptions are not available"));
        }
        try {
            manager.subscribeToSubscription(arg);
            input_stream = new ByteArrayInputStream(VuzeFileHandler.getSingleton().create().exportToBytes());
        } catch (Throwable e) {
            throw (new IOException("Subscription addition failed: " + Debug.getNestedExceptionMessage(e)));
        }
    } else {
        // AZPluginConnection is called via reflection
        // Let's just assume that the Core is avail..
        PluginInterface pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByID(plugin_id);
        if (pi == null) {
            throw (new IOException("Plugin " + plugin_str + " is required - go to 'Tools->Plugins->Installation Wizard' to install."));
        }
        IPCInterface ipc = pi.getIPC();
        try {
            if (ipc.canInvoke("handleURLProtocol", new Object[] { this, arg })) {
                input_stream = (InputStream) ipc.invoke("handleURLProtocol", new Object[] { this, arg });
            } else {
                input_stream = (InputStream) ipc.invoke("handleURLProtocol", new Object[] { arg });
            }
        } catch (IPCException ipce) {
            Throwable e = ipce;
            if (e.getCause() != null) {
                e = e.getCause();
            }
            throw (new IOException("Communication error with plugin '" + plugin_str + "': " + Debug.getNestedExceptionMessage(e)));
        }
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) PluginInterface(com.biglybt.pif.PluginInterface) IPCException(com.biglybt.pif.ipc.IPCException) IOException(java.io.IOException) SubscriptionManager(com.biglybt.core.subs.SubscriptionManager) HashMap(java.util.HashMap) Map(java.util.Map) IPCInterface(com.biglybt.pif.ipc.IPCInterface)

Example 3 with IPCException

use of com.biglybt.pif.ipc.IPCException in project BiglyBT by BiglySoftware.

the class BuddyPlugin method handleURLProtocol.

public InputStream handleURLProtocol(AZPluginConnection connection, String arg_str) throws IPCException {
    if (arg_str.toLowerCase(Locale.US).startsWith("chat:")) {
        if (!beta_enabled_param.getValue()) {
            throw (new IPCException("Decentralized chat not enabled"));
        }
        try {
            InputStream result = beta_plugin.handleURI(arg_str, false);
            if (result != null) {
                return (result);
            }
            return (new ByteArrayInputStream(VuzeFileHandler.getSingleton().create().exportToBytes()));
        } catch (Throwable e) {
            throw (new IPCException(e));
        }
    } else {
        String[] args = arg_str.split("&");
        String pk = null;
        String category_or_tag = "All";
        byte[] hash = null;
        for (String arg : args) {
            String[] bits = arg.split("=");
            String lhs = bits[0];
            String rhs = UrlUtils.decode(bits[1]);
            if (lhs.equals("pk")) {
                pk = rhs;
            } else if (lhs.equals("cat")) {
                category_or_tag = rhs;
            } else if (lhs.equals("hash")) {
                hash = Base32.decode(rhs);
            }
        }
        if (pk == null) {
            throw (new IPCException("Public key missing from '" + arg_str + "'"));
        }
        BuddyPluginBuddy buddy = getBuddyFromPublicKey(pk);
        if (buddy == null) {
            throw (new IPCException("Buddy with public key '" + pk + "' not found"));
        }
        if (hash == null) {
            return (handleUPRSS(connection, buddy, category_or_tag));
        } else {
            return (handleUPTorrent(connection, buddy, category_or_tag, hash));
        }
    }
}
Also used : IPCException(com.biglybt.pif.ipc.IPCException)

Example 4 with IPCException

use of com.biglybt.pif.ipc.IPCException 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 5 with IPCException

use of com.biglybt.pif.ipc.IPCException in project BiglyBT by BiglySoftware.

the class BuddyPlugin method handleUPRSS.

public InputStream handleUPRSS(final AZPluginConnection connection, BuddyPluginBuddy buddy, String tag_or_category) throws IPCException {
    if (!buddy.isOnline(true)) {
        throw (new IPCException("Buddy isn't online"));
    }
    Map<String, Object> msg = new HashMap<>();
    final String if_mod = connection.getRequestProperty("If-Modified-Since");
    try {
        msg.put("cat", tag_or_category.getBytes("UTF-8"));
        if (if_mod != null) {
            msg.put("if_mod", if_mod);
        }
    // String etag		= connection.getRequestProperty( "If-None-Match" );
    } catch (Throwable e) {
        Debug.out(e);
    }
    final Object[] result = { null };
    final AESemaphore result_sem = new AESemaphore("BuddyPlugin:rss");
    final String etag = buddy.getPublicKey() + "-" + tag_or_category;
    az2_handler.sendAZ2RSSMessage(buddy, msg, new BuddyPluginAZ2TrackerListener() {

        @Override
        public Map messageReceived(BuddyPluginBuddy buddy, Map message) {
            try {
                byte[] bytes = (byte[]) message.get("rss");
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                result[0] = bais;
                connection.setHeaderField("ETag", etag);
                byte[] b_last_mod = (byte[]) message.get("last_mod");
                if (b_last_mod != null) {
                    String last_mod = new String(b_last_mod, "UTF-8");
                    connection.setHeaderField("Last-Modified", last_mod);
                    if (if_mod != null && if_mod.equals(last_mod) && bytes.length == 0) {
                        connection.setResponse(HttpURLConnection.HTTP_NOT_MODIFIED, "Not Modified");
                    }
                }
                result_sem.release();
            } catch (Throwable e) {
                messageFailed(buddy, e);
            }
            return (null);
        }

        @Override
        public void messageFailed(BuddyPluginBuddy buddy, Throwable cause) {
            result[0] = new IPCException("Read failed", cause);
            result_sem.release();
        }
    });
    result_sem.reserve(60 * 1000);
    if (result[0] == null) {
        throw (new IPCException("Timeout"));
    } else if (result[0] instanceof InputStream) {
        return ((InputStream) result[0]);
    } else {
        throw ((IPCException) result[0]);
    }
}
Also used : IPCException(com.biglybt.pif.ipc.IPCException)

Aggregations

IPCException (com.biglybt.pif.ipc.IPCException)9 PluginInterface (com.biglybt.pif.PluginInterface)4 IPCInterface (com.biglybt.pif.ipc.IPCInterface)4 AERunnable (com.biglybt.core.util.AERunnable)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 DownloadManagerStateAttributeListener (com.biglybt.core.download.DownloadManagerStateAttributeListener)1 MessageText (com.biglybt.core.internat.MessageText)1 SubscriptionManager (com.biglybt.core.subs.SubscriptionManager)1 TOTorrentAnnounceURLGroup (com.biglybt.core.torrent.TOTorrentAnnounceURLGroup)1 MagnetPlugin (com.biglybt.plugin.magnet.MagnetPlugin)1 MagnetPluginProgressListener (com.biglybt.plugin.magnet.MagnetPluginProgressListener)1 UIFunctions (com.biglybt.ui.UIFunctions)1 BufferedLabel (com.biglybt.ui.swt.components.BufferedLabel)1 LinkLabel (com.biglybt.ui.swt.components.LinkLabel)1 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)1 UISWTViewCoreEventListener (com.biglybt.ui.swt.pifimpl.UISWTViewCoreEventListener)1 SWTRunnable (com.biglybt.ui.swt.utils.SWTRunnable)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1