Search in sources :

Example 6 with UTTimerEvent

use of com.biglybt.pif.utils.UTTimerEvent in project BiglyBT by BiglySoftware.

the class UTTimerImpl method addEvent.

@Override
public UTTimerEvent addEvent(long when, final UTTimerEventPerformer ext_performer) {
    if (destroyed) {
        throw (new RuntimeException("Timer has been destroyed"));
    }
    final timerEvent res = new timerEvent();
    TimerEventPerformer performer = new TimerEventPerformer() {

        @Override
        public void perform(TimerEvent ev) {
            UtilitiesImpl.callWithPluginThreadContext(plugin_interface, new Runnable() {

                @Override
                public void run() {
                    res.perform(ext_performer);
                }
            });
        }
    };
    if (timer == null) {
        res.setEvent(SimpleTimer.addEvent("Plugin:" + ext_performer.getClass(), when, performer));
    } else {
        res.setEvent(timer.addEvent("Plugin:" + ext_performer.getClass(), when, performer));
    }
    return (res);
}
Also used : UTTimerEventPerformer(com.biglybt.pif.utils.UTTimerEventPerformer) UTTimerEvent(com.biglybt.pif.utils.UTTimerEvent)

Example 7 with UTTimerEvent

use of com.biglybt.pif.utils.UTTimerEvent in project BiglyBT by BiglySoftware.

the class SSDPCore method received.

@Override
public void received(NetworkInterface network_interface, InetAddress local_address, final InetSocketAddress originator, byte[] packet_data, int length) {
    String str = new String(packet_data, 0, length);
    if (first_response) {
        first_response = false;
        adapter.trace("UPnP:SSDP: first response:\n" + str);
    }
    // example notify event
    /*
			NOTIFY * HTTP/1.1
			HOST: 239.255.255.250:1900
			CACHE-CONTROL: max-age=3600
			LOCATION: http://192.168.0.1:49152/gateway.xml
			NT: urn:schemas-upnp-org:service:WANIPConnection:1
			NTS: ssdp:byebye
			SERVER: Linux/2.4.17_mvl21-malta-mips_fp_le, UPnP/1.0, Intel SDK for UPnP devices /1.2
			USN: uuid:ab5d9077-0710-4373-a4ea-5192c8781666::urn:schemas-upnp-org:service:WANIPConnection:1
			*/
    // if ( originator.getAddress().getHostAddress().equals( "192.168.0.135" )){
    // System.out.println( originator + ":" + str );
    // }
    List<String> lines = new ArrayList<>();
    int pos = 0;
    while (true) {
        int p1 = str.indexOf(NL, pos);
        String line;
        if (p1 == -1) {
            line = str.substring(pos);
        } else {
            line = str.substring(pos, p1);
            pos = p1 + 1;
        }
        lines.add(line.trim());
        if (p1 == -1) {
            break;
        }
    }
    if (lines.size() == 0) {
        adapter.trace("SSDP::receive packet - 0 line reply");
        return;
    }
    String header = (String) lines.get(0);
    // Gudy's  Root: http://192.168.0.1:5678/igd.xml, uuid:upnp-InternetGatewayDevice-1_0-12345678900001::upnp:rootdevice, upnp:rootdevice
    // Parg's  Root: http://192.168.0.1:49152/gateway.xml, uuid:824ff22b-8c7d-41c5-a131-44f534e12555::upnp:rootdevice, upnp:rootdevice
    URL location = null;
    String usn = null;
    String nt = null;
    String nts = null;
    String st = null;
    String al = null;
    String mx = null;
    String server = null;
    for (int i = 1; i < lines.size(); i++) {
        String line = (String) lines.get(i);
        int c_pos = line.indexOf(":");
        if (c_pos == -1) {
            continue;
        }
        String key = line.substring(0, c_pos).trim().toUpperCase();
        String val = line.substring(c_pos + 1).trim();
        if (key.equals("LOCATION")) {
            try {
                if (!val.equals("*")) {
                    location = new URL(val);
                }
            } catch (MalformedURLException e) {
                if (!val.contains("//")) {
                    // seen missing protocol
                    val = "http://" + val;
                    try {
                        location = new URL(val);
                    } catch (Throwable f) {
                    }
                }
                if (location == null) {
                    adapter.log(e);
                }
            }
        } else if (key.equals("NT")) {
            nt = val;
        } else if (key.equals("USN")) {
            usn = val;
        } else if (key.equals("NTS")) {
            nts = val;
        } else if (key.equals("ST")) {
            st = val;
        } else if (key.equals("AL")) {
            al = val;
        } else if (key.equals("MX")) {
            mx = val;
        } else if (key.equals("SERVER")) {
            server = val;
        }
    }
    if (server != null) {
        if (server.toLowerCase().startsWith("xbox")) {
            String host = originator.getAddress().getHostAddress();
            synchronized (ignore_mx) {
                ignore_mx.add(host);
            }
        }
    }
    if (mx != null) {
        String host = originator.getAddress().getHostAddress();
        synchronized (ignore_mx) {
            if (ignore_mx.contains(host)) {
                mx = null;
            }
        }
    }
    if (header.startsWith("M-SEARCH")) {
        if (st != null) {
            /*
				HTTP/1.1 200 OK
				CACHE-CONTROL: max-age=600
				DATE: Tue, 20 Dec 2005 13:07:31 GMT
				EXT:
				LOCATION: http://192.168.1.1:2869/gatedesc.xml
				SERVER: Linux/2.4.17_mvl21-malta-mips_fp_le UPnP/1.0
				ST: upnp:rootdevice
				USN: uuid:UUID-InternetGatewayDevice-1234::upnp:rootdevice
				*/
            String[] response = informSearch(network_interface, local_address, originator.getAddress(), st);
            if (response != null) {
                String UUID = response[0];
                String url = response[1];
                if (url.startsWith("/")) {
                    url = url.substring(1);
                }
                // Server MUST be in this alpha-case for Xbox to work (SERVER doesn't)...
                String data = "HTTP/1.1 200 OK" + NL + "USN: " + UUID + "::" + st + NL + "ST: " + st + NL + "EXT:" + NL + "Location: http://" + local_address.getHostAddress() + ":" + mc_group.getControlPort() + "/" + url + NL + "Server: " + Constants.APP_NAME + "/" + Constants.AZUREUS_VERSION + " UPnP/1.0 " + Constants.APP_NAME + "/" + Constants.AZUREUS_VERSION + NL + "Cache-Control: max-age=3600" + NL + "Date: " + TimeFormatter.getHTTPDate(SystemTime.getCurrentTime()) + NL + "Content-Length: 0" + NL + NL;
                final byte[] data_bytes = data.getBytes();
                if (timer == null) {
                    timer = adapter.createTimer("SSDPCore:MX");
                }
                int delay = 0;
                if (mx != null) {
                    try {
                        delay = Integer.parseInt(mx) * 1000;
                        delay = RandomUtils.nextInt(delay);
                    } catch (Throwable e) {
                    }
                }
                final Runnable task = new Runnable() {

                    @Override
                    public void run() {
                        try {
                            mc_group.sendToMember(originator, data_bytes);
                        } catch (Throwable e) {
                            adapter.log(e);
                        }
                    }
                };
                if (delay == 0) {
                    task.run();
                } else {
                    long target_time = SystemTime.getCurrentTime() + delay;
                    boolean schedule_event;
                    synchronized (timer_queue) {
                        timer_queue.add(task);
                        schedule_event = time_event_next == 0 || target_time < time_event_next;
                        if (schedule_event) {
                            time_event_next = target_time;
                        }
                    }
                    if (schedule_event) {
                        timer.addEvent(target_time, new UTTimerEventPerformer() {

                            @Override
                            public void perform(UTTimerEvent event) {
                                while (true) {
                                    Runnable t;
                                    synchronized (timer_queue) {
                                        if (timer_queue.size() > 0) {
                                            t = (Runnable) timer_queue.remove(0);
                                        } else {
                                            time_event_next = 0;
                                            return;
                                        }
                                    }
                                    try {
                                        t.run();
                                    } catch (Throwable e) {
                                        Debug.printStackTrace(e);
                                    }
                                }
                            }
                        });
                    }
                }
            }
        } else {
            adapter.trace("SSDP::receive M-SEARCH - bad header:" + header);
        }
    } else if (header.startsWith("NOTIFY")) {
        if (nt != null && nts != null) {
            informNotify(network_interface, local_address, originator.getAddress(), usn, location, nt, nts);
        } else {
            adapter.trace("SSDP::receive NOTIFY - bad header:" + header);
        }
    } else if (header.startsWith("HTTP") && header.contains("200")) {
        if (location != null && st != null) {
            informResult(network_interface, local_address, originator.getAddress(), usn, location, st, al);
        } else {
            adapter.trace("SSDP::receive HTTP - bad header:" + header);
        }
    } else {
        adapter.trace("SSDP::receive packet - bad header:" + header);
    }
}
Also used : UTTimerEvent(com.biglybt.pif.utils.UTTimerEvent) UTTimerEventPerformer(com.biglybt.pif.utils.UTTimerEventPerformer)

Example 8 with UTTimerEvent

use of com.biglybt.pif.utils.UTTimerEvent in project BiglyBT by BiglySoftware.

the class DHTTrackerPlugin method initialise.

protected void initialise() {
    is_running = true;
    plugin_interface.getDownloadManager().addListener(new DownloadManagerListener() {

        @Override
        public void downloadAdded(Download download) {
            addDownload(download);
        }

        @Override
        public void downloadRemoved(Download download) {
            removeDownload(download);
        }
    });
    plugin_interface.getUtilities().createTimer("DHT Tracker", true).addPeriodicEvent(15000, new UTTimerEventPerformer() {

        private int ticks;

        private String prev_alt_status = "";

        @Override
        public void perform(UTTimerEvent event) {
            ticks++;
            processRegistrations(ticks % 8 == 0);
            if (ticks == 2 || ticks % 4 == 0) {
                processNonRegistrations();
            }
            if (alt_lookup_handler != null) {
                if (ticks % 4 == 0) {
                    String alt_status = alt_lookup_handler.getString();
                    if (!alt_status.equals(prev_alt_status)) {
                        log.log("Alternative stats: " + alt_status);
                        prev_alt_status = alt_status;
                    }
                }
            }
        }
    });
}
Also used : UTTimerEvent(com.biglybt.pif.utils.UTTimerEvent) UTTimerEventPerformer(com.biglybt.pif.utils.UTTimerEventPerformer)

Aggregations

UTTimerEvent (com.biglybt.pif.utils.UTTimerEvent)8 UTTimerEventPerformer (com.biglybt.pif.utils.UTTimerEventPerformer)8 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)2 UIManager (com.biglybt.pif.ui.UIManager)2 BasicPluginConfigModel (com.biglybt.pif.ui.model.BasicPluginConfigModel)2 BasicPluginViewModel (com.biglybt.pif.ui.model.BasicPluginViewModel)2 DHT (com.biglybt.core.dht.DHT)1 DHTLogger (com.biglybt.core.dht.DHTLogger)1 DHTControlActivity (com.biglybt.core.dht.control.DHTControlActivity)1 DHTControlContact (com.biglybt.core.dht.control.DHTControlContact)1 DHTNATPuncher (com.biglybt.core.dht.nat.DHTNATPuncher)1 DHTRouterContact (com.biglybt.core.dht.router.DHTRouterContact)1 DHTTransportContact (com.biglybt.core.dht.transport.DHTTransportContact)1 DHTTransportFullStats (com.biglybt.core.dht.transport.DHTTransportFullStats)1 DHTTransportUDP (com.biglybt.core.dht.transport.udp.DHTTransportUDP)1 DHTTransportUDPImpl (com.biglybt.core.dht.transport.udp.impl.DHTTransportUDPImpl)1 PluginConfig (com.biglybt.pif.PluginConfig)1 PluginListener (com.biglybt.pif.PluginListener)1 DistributedDatabase (com.biglybt.pif.ddb.DistributedDatabase)1 DistributedDatabaseEvent (com.biglybt.pif.ddb.DistributedDatabaseEvent)1