⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 upnppresenceadvertisementhandling.java

📁 国外的j2me播放器软件
💻 JAVA
字号:
package no.auc.one.portableplayer.communication;

import java.io.*;
import javax.microedition.io.*;

import org.apache.log4j.*;

public class UPnPPresenceAdvertisementHandling extends java.lang.Thread {
    private java.util.Hashtable handlers = null;
    private static Logger LOG;

    public UPnPPresenceAdvertisementHandling() {
        super();

        LOG = Logger.getLogger("UPnPPAH");

        // Set initial capacity of handlers. Does not expect to need 
        // too many handlers, therefore a quite low amount of storage capacity
        // is set.
        handlers = new java.util.Hashtable(5);
    }
    
    public void registerHandler(UPnPPresenceAdvertisementHandler pah) {
        if (!handlers.contains(pah)) {
            handlers.put(pah.getHandledNT(), pah);
        } else {
            throw new IllegalArgumentException(
                "UPnP Presence Advertisement handler (" + pah.getHandledNT() + 
                ") is already registered");
        }
    }

    public void unregisterHandler(UPnPPresenceAdvertisementHandler pah) {
        unregisterHandler(pah.getHandledNT());
    }

    public void unregisterHandler(String key) {
        if (handlers.containsKey(key)) {
            handlers.remove(key);
        } else {
            throw new IllegalArgumentException(
                "UPnP Presence Advertisement handler (" + key + 
                ") is not registered");
        }
    }
    
    public void run() {
        LOG.debug("PAH Thread started (ID=" + Thread.currentThread() + ")");
        DatagramConnection conn = null;
    
        try{
            String url = "datagram://239.255.255.250:1900";
            LOG.debug("URL = " + url);
            Datagram dgRecv = null;
            
            LOG.debug("Sleep");
            Thread.sleep(5000);
            LOG.debug("Awake");
            LOG.debug("Open connection");
            conn = (DatagramConnection)Connector.open(url);
            
            LOG.debug("Create new datagram");
            dgRecv = conn.newDatagram(2048);
                
            while(true) {
                try {
                    LOG.debug(
                        "Waiting for UPnP Presence notification... ");
                    conn.receive(dgRecv);
                    LOG.debug("OK! (" + dgRecv.getLength());
               
                    String msg = new String(
                        dgRecv.getData(),
                        dgRecv.getOffset(),
                        dgRecv.getLength());

                    UPnPPresenceInformation pi = new UPnPPresenceInformation(
                        msg);
                    
                    boolean isAvailableRequest = 
                        pi.getNTS().equals("ssdp:alive") ? true : false;

                    if(handlers.contains(pi.getNT())) {
                        UPnPPresenceAdvertisementHandler h = 
                        (UPnPPresenceAdvertisementHandler)handlers.get(
                            pi.getNT());

                        if (isAvailableRequest) {
                            h.deviceAvailable(pi);
                        } else {
                            h.deviceUnavailable(pi);
                        }
                    }
                                                                           
                    msg = null;
                    dgRecv.reset();
                    dgRecv.setLength(2048);
                } catch(IOException e) {
                    LOG.fatal("UPnP PA Handling IOException ");
                    LOG.fatal(e);
                } catch(NullPointerException npe) {
                    LOG.fatal("Null pointer exception caugh");
                    LOG.fatal(npe);
                } catch(Exception e) {
                    LOG.fatal("UPnP PA Handling exception occured :/");
                    LOG.fatal(e);
                }
            }  
        } catch(InterruptedException ie) {
            LOG.fatal(ie);
        } catch(IllegalArgumentException iae) {
            LOG.fatal(iae);
        } catch(ConnectionNotFoundException cnfe) {
            LOG.fatal(cnfe);
        } catch(InterruptedIOException ioe) {
            LOG.fatal(ioe);
        } catch(IOException e) {
            LOG.fatal("UPnP PA Handling IOException (open)");
            LOG.fatal(e);
        } finally {
            try { 
                conn.close(); 
            } catch (IOException ioe) { 
                LOG.debug("Exception caught while closing connection");
                LOG.debug(ioe);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -