Search in sources :

Example 1 with PluginEvent

use of com.biglybt.pif.PluginEvent in project BiglyBT by BiglySoftware.

the class DeviceManagerUPnPImpl method startUp.

protected void startUp() {
    UPnPAdapter adapter = new UPnPAdapter() {

        @Override
        public SimpleXMLParserDocument parseXML(String data) throws SimpleXMLParserDocumentException {
            return (plugin_interface.getUtilities().getSimpleXMLParserDocumentFactory().create(data));
        }

        @Override
        public ResourceDownloaderFactory getResourceDownloaderFactory() {
            return (plugin_interface.getUtilities().getResourceDownloaderFactory());
        }

        @Override
        public UTTimer createTimer(String name) {
            return (plugin_interface.getUtilities().createTimer(name));
        }

        @Override
        public void createThread(String name, final Runnable runnable) {
            plugin_interface.getUtilities().createThread(name, runnable);
        }

        @Override
        public Comparator getAlphanumericComparator() {
            return (plugin_interface.getUtilities().getFormatters().getAlphanumericComparator(true));
        }

        @Override
        public void log(Throwable e) {
            Debug.printStackTrace(e);
        }

        @Override
        public void trace(String str) {
        // System.out.println( str );
        }

        @Override
        public void log(String str) {
        // System.out.println( str );
        }

        @Override
        public String getTraceDir() {
            return (plugin_interface.getPluginDirectoryName());
        }
    };
    try {
        upnp = UPnPFactory.getSingleton(adapter, null);
        upnp.addRootDeviceListener(new UPnPListener() {

            @Override
            public boolean deviceDiscovered(String USN, URL location) {
                return (true);
            }

            @Override
            public void rootDeviceFound(UPnPRootDevice device) {
                handleDevice(device, true);
            }
        });
        upnp.getSSDP().addListener(new UPnPSSDPListener() {

            private Map<InetAddress, Boolean> liveness_map = new HashMap<>();

            @Override
            public void receivedResult(NetworkInterface network_interface, InetAddress local_address, InetAddress originator, String USN, URL location, String ST, String AL) {
            }

            @Override
            public void receivedNotify(NetworkInterface network_interface, InetAddress local_address, InetAddress originator, String USN, URL location, String NT, String NTS) {
                alive(originator, !NTS.contains("byebye"));
            }

            @Override
            public String[] receivedSearch(NetworkInterface network_interface, InetAddress local_address, InetAddress originator, String ST) {
                alive(originator, true);
                return (null);
            }

            @Override
            public void interfaceChanged(NetworkInterface network_interface) {
            }

            private void alive(InetAddress address, boolean alive) {
                synchronized (liveness_map) {
                    Boolean b = liveness_map.get(address);
                    if (b != null && b == alive) {
                        return;
                    }
                    liveness_map.put(address, alive);
                }
                DeviceImpl[] devices = manager.getDevices();
                for (DeviceImpl d : devices) {
                    if (d instanceof DeviceMediaRendererImpl) {
                        DeviceMediaRendererImpl r = (DeviceMediaRendererImpl) d;
                        InetAddress device_address = r.getAddress();
                        if (device_address != null && device_address.equals(address)) {
                            if (r.isAlive() != alive) {
                                if (alive) {
                                    r.alive();
                                } else {
                                    r.dead();
                                }
                            }
                        }
                    }
                }
            }
        });
    } catch (Throwable e) {
        manager.log("UPnP device manager failed", e);
    }
    try {
        plugin_interface.addEventListener(new PluginEventListener() {

            @Override
            public void handleEvent(PluginEvent ev) {
                int type = ev.getType();
                if (type == PluginEvent.PEV_PLUGIN_OPERATIONAL || type == PluginEvent.PEV_PLUGIN_NOT_OPERATIONAL) {
                    PluginInterface pi = (PluginInterface) ev.getValue();
                    if (pi.getPluginID().equals("azupnpav")) {
                        if (type == PluginEvent.PEV_PLUGIN_OPERATIONAL) {
                            upnpav_ipc = pi.getIPC();
                            addListener(pi);
                        } else {
                            upnpav_ipc = null;
                        }
                    }
                }
            }
        });
        PluginInterface pi = plugin_interface.getPluginManager().getPluginInterfaceByID("azupnpav");
        if (pi == null) {
            manager.log("No UPnPAV plugin found");
        } else {
            upnpav_ipc = pi.getIPC();
            addListener(pi);
        }
    } catch (Throwable e) {
        manager.log("Failed to hook into UPnPAV", e);
    }
    manager.UPnPManagerStarted();
}
Also used : PluginEventListener(com.biglybt.pif.PluginEventListener) PluginInterface(com.biglybt.pif.PluginInterface) NetworkInterface(java.net.NetworkInterface) URL(java.net.URL) InetAddress(java.net.InetAddress) PluginEvent(com.biglybt.pif.PluginEvent)

Example 2 with PluginEvent

use of com.biglybt.pif.PluginEvent in project BiglyBT by BiglySoftware.

the class BuddyPluginBeta method startup.

protected void startup() {
    plugin_interface.addEventListener(new PluginEventListener() {

        @Override
        public void handleEvent(PluginEvent ev) {
            int type = ev.getType();
            if (type == PluginEvent.PEV_INITIAL_SHARING_COMPLETE) {
                try {
                    ShareManager share_manager = plugin_interface.getShareManager();
                    share_manager.addListener(new ShareManagerListener() {

                        @Override
                        public void resourceModified(ShareResource old_resource, ShareResource new_resource) {
                            checkTag(new_resource);
                        }

                        @Override
                        public void resourceDeleted(ShareResource resource) {
                        }

                        @Override
                        public void resourceAdded(ShareResource resource) {
                            checkTag(resource);
                        }

                        @Override
                        public void reportProgress(int percent_complete) {
                        }

                        @Override
                        public void reportCurrentTask(String task_description) {
                        }
                    });
                    ShareResource[] existing = share_manager.getShares();
                    for (ShareResource sr : existing) {
                        checkTag(sr);
                    }
                } catch (Throwable e) {
                    Debug.out(e);
                }
            } else if (type == PluginEvent.PEV_PLUGIN_OPERATIONAL) {
                pluginAdded((PluginInterface) ev.getValue());
            } else if (type == PluginEvent.PEV_PLUGIN_NOT_OPERATIONAL) {
                pluginRemoved((PluginInterface) ev.getValue());
            }
        }
    });
    PluginInterface[] plugins = plugin_interface.getPluginManager().getPlugins(true);
    for (PluginInterface pi : plugins) {
        if (pi.getPluginState().isOperational()) {
            pluginAdded(pi);
        }
    }
}
Also used : PluginEventListener(com.biglybt.pif.PluginEventListener) PluginInterface(com.biglybt.pif.PluginInterface) PluginEvent(com.biglybt.pif.PluginEvent)

Aggregations

PluginEvent (com.biglybt.pif.PluginEvent)2 PluginEventListener (com.biglybt.pif.PluginEventListener)2 PluginInterface (com.biglybt.pif.PluginInterface)2 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1 URL (java.net.URL)1