Search in sources :

Example 6 with Device

use of org.cybergarage.upnp.Device in project felix by apache.

the class MyCtrlPoint method findDeviceCtrl.

public Device findDeviceCtrl(ControlPoint ctrl, String udn) {
    /* 
		 * this return the device looking in all the struct
		 */
    DeviceList devList = getDeviceList();
    Device dev = null;
    int i = 0;
    while (i < devList.size() && (dev == null)) {
        if (devList.getDevice(i).getUDN().equals(udn)) {
            dev = devList.getDevice(i);
            return dev;
        }
        dev = findDevice(udn, devList.getDevice(i));
        i++;
    }
    return dev;
}
Also used : UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) DeviceList(org.cybergarage.upnp.DeviceList) ControlPoint(org.cybergarage.upnp.ControlPoint)

Example 7 with Device

use of org.cybergarage.upnp.Device in project felix by apache.

the class MyCtrlPoint method newEventArrived.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.felix.upnpbase.importer.MyEventListener#newEventArrived(java.lang.String,
	 *      long, java.util.Dictionary)
	 */
public void newEventArrived(String uuid, long seq, PropertyList props) {
    Activator.logger.DEBUG("[Importer] newEventArrived");
    Service service = serviceFromSid(uuid);
    if (service != null) {
        int size = props.size();
        Hashtable hash = new Hashtable();
        for (int i = 0; i < size; i++) {
            Property prop = props.getProperty(i);
            String varName = prop.getName();
            String varValue = prop.getValue();
            String upnpType = service.getStateVariable(varName).getDataType();
            Object valueObj;
            try {
                valueObj = Converter.parseString(varValue, upnpType);
            } catch (Exception e) {
                Activator.logger.ERROR("[Importer] Bad data value in Notify event: " + "var name=" + varName + " value=" + varValue + " type=" + upnpType + "\n" + e);
                return;
            }
            hash.put(varName, valueObj);
        }
        Device device = service.getDevice();
        StateChanged msg = new StateChanged(uuid, seq, hash, device, service);
        notifierQueue.enqueue(msg);
    }
}
Also used : Hashtable(java.util.Hashtable) UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) StateChanged(org.apache.felix.upnp.basedriver.importer.core.event.message.StateChanged) Property(org.cybergarage.upnp.event.Property) ControlPoint(org.cybergarage.upnp.ControlPoint) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 8 with Device

use of org.cybergarage.upnp.Device in project felix by apache.

the class MyCtrlPoint method deviceNotifyReceived.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.cybergarage.upnp.device.NotifyListener#deviceNotifyReceived(org.cybergarage.upnp.ssdp.SSDPPacket)
	 */
public void deviceNotifyReceived(SSDPPacket ssdpPacket) {
    Activator.logger.DEBUG("[Importer] deviceNotifyReceived");
    Activator.logger.PACKET(ssdpPacket.toString());
    /*
		 * if the packet is 
		 * 		NOTIFY or ISALIVE or *new* ROOT	then create and register the UPnPDevice and 
		 * 										all the embeeded device too
		 * 		DEVICE or SERVICE	then if they already exist in OSGi do nothing otherwise I'll create and 
		 * 							register all the UPnPDevice need starting from the root device
		 * 		*root* BYEBYE		then I'll unregister it and all its children from OSGi Framework 
		 * 		*service* BYEBYE	then I'll re-register the UPnPDevice that contain the service with the updated
		 * 							properties 
		 * 		*device* BYEBYE		then I'll re-register the UPnPDevice that contain the device with the updated
		 * 							properties and also unregister the UPnPDevice that has left
		 */
    String usn = ssdpPacket.getUSN();
    ParseUSN parseUSN = new ParseUSN(usn);
    String udn = parseUSN.getUDN();
    ServiceReference[] refs = null;
    String filter = "(&" + UPNP_DEVICE_FLTR + EXPORT_FLTR + ")";
    try {
        refs = context.getServiceReferences(UPnPDevice.class.getName(), filter);
    } catch (InvalidSyntaxException e) {
        e.printStackTrace();
    }
    if (refs != null) {
        for (int i = 0; i < refs.length; i++) {
            UPnPDevice dev = (UPnPDevice) context.getService(refs[i]);
            Dictionary dic = dev.getDescriptions(null);
            if (((String) dic.get(UPnPDevice.UDN)).equals(udn)) {
                return;
            }
        }
    }
    if (ssdpPacket.isAlive()) {
        Activator.logger.DEBUG("[Importer] ssdpPacket.isAlive");
        if (devices.containsKey(udn)) {
            Activator.logger.DEBUG("[Importer] Device already discovered");
            if (parseUSN.isService()) {
                doServiceUpdating(udn, parseUSN.getServiceType());
            }
        } else {
            doDeviceRegistration(udn);
        }
    } else if (ssdpPacket.isByeBye()) {
        Activator.logger.DEBUG("[Importer] ssdpPacket.isByeBye");
        synchronized (devices) {
            if (devices.containsKey(udn)) {
                if (parseUSN.isDevice()) {
                    Activator.logger.DEBUG("[Importer] parseUSN.isDevice ...unregistering all the children devices ");
                    // unregistering all the children devices
                    UPnPDeviceImpl dev = ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
                    removeOSGiandUPnPDeviceHierarchy(dev);
                } else if (parseUSN.isService()) {
                    Activator.logger.DEBUG("[Importer] parseUSN.isService ...registering modified device again ");
                    /* 
						 * I have to unregister the UPnPDevice and register it again 
						 * with the updated properties  
						 */
                    UPnPDeviceImpl device = ((OSGiDeviceInfo) devices.get(udn)).getOSGiDevice();
                    ServiceRegistration registar = ((OSGiDeviceInfo) devices.get(udn)).getRegistration();
                    String[] oldServicesID = (String[]) (device.getDescriptions(null).get(UPnPService.ID));
                    String[] oldServiceType = (String[]) (device.getDescriptions(null).get(UPnPService.TYPE));
                    Device cyberDevice = findDeviceCtrl(this, udn);
                    Vector vec = new Vector();
                    for (int i = 0; i < oldServiceType.length; i++) {
                        Service ser = cyberDevice.getService(oldServicesID[i]);
                        if (!(ser.getServiceType().equals(parseUSN.getServiceType()))) {
                            vec.add(oldServicesID[i]);
                        }
                    }
                    // new serviceID
                    String[] actualServicesID = new String[vec.size()];
                    actualServicesID = (String[]) vec.toArray(new String[] {});
                    // new serviceType
                    String[] actualServiceType = new String[oldServiceType.length - 1];
                    vec.clear();
                    for (int i = 0; i < oldServiceType.length; i++) {
                        if (!(oldServiceType[i].equals(parseUSN.getServiceType()))) {
                            vec.add(oldServiceType[i]);
                        }
                    }
                    actualServiceType = (String[]) vec.toArray(new String[] {});
                    // unrigistering and registering again with the new properties
                    unregisterUPnPDevice(registar);
                    device.setProperty(UPnPService.ID, actualServicesID);
                    device.setProperty(UPnPService.TYPE, actualServiceType);
                    registerUPnPDevice(null, device, device.getDescriptions(null));
                    searchForListener(cyberDevice);
                }
            }
        }
    // synchronized(devices)
    } else {
    /*
			 * if it is a service means that it has deleted when the 
			 * owner was unregister so I can skip this bye-bye
			 * 
			 * //TODO Understand the comment
			 *
			 */
    }
}
Also used : Dictionary(java.util.Dictionary) UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) UPnPDeviceImpl(org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPDeviceImpl) ControlPoint(org.cybergarage.upnp.ControlPoint) ServiceReference(org.osgi.framework.ServiceReference) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) ParseUSN(org.apache.felix.upnp.basedriver.importer.util.ParseUSN) Vector(java.util.Vector) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 9 with Device

use of org.cybergarage.upnp.Device in project i2p.i2p by i2p.

the class UPnP method main.

/**
 *  Dumps out device info in semi-HTML format
 */
public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.setProperty(PROP_ADVANCED, "true");
    I2PAppContext ctx = new I2PAppContext(props);
    UPnP cp = new UPnP(ctx);
    org.cybergarage.upnp.UPnP.setEnable(org.cybergarage.upnp.UPnP.USE_ONLY_IPV4_ADDR);
    Debug.initialize(ctx);
    cp.setHTTPPort(49152 + ctx.random().nextInt(5000));
    cp.setSSDPPort(54152 + ctx.random().nextInt(5000));
    long start = System.currentTimeMillis();
    cp.start();
    long s2 = System.currentTimeMillis();
    System.err.println("Start took " + (s2 - start) + "ms");
    System.err.println("Searching for UPnP devices");
    start = System.currentTimeMillis();
    cp.search();
    s2 = System.currentTimeMillis();
    System.err.println("Search kickoff took " + (s2 - start) + "ms");
    System.err.println("Waiting 10 seconds for responses");
    Thread.sleep(10000);
    DeviceList list = cp.getDeviceList();
    if (list.isEmpty()) {
        System.err.println("No UPnP devices found");
        System.exit(1);
    }
    System.err.println("Found " + list.size() + " devices.");
    System.err.println("Redirect the following output to an html file and view in a browser.");
    StringBuilder sb = new StringBuilder();
    Iterator<Device> it = list.iterator();
    int i = 0;
    while (it.hasNext()) {
        Device device = it.next();
        cp.listSubDev(device.toString(), device, sb);
        System.out.println("<h3>Device " + (++i) + ": " + DataHelper.escapeHTML(device.getFriendlyName()) + "</h3>");
        System.out.println("<p>UDN: " + DataHelper.escapeHTML(device.getUDN()));
        System.out.println("<br>IP: " + getIP(device));
        System.out.println(sb.toString());
        sb.setLength(0);
    }
    System.exit(0);
}
Also used : I2PAppContext(net.i2p.I2PAppContext) Device(org.cybergarage.upnp.Device) DeviceList(org.cybergarage.upnp.DeviceList) Properties(java.util.Properties) ControlPoint(org.cybergarage.upnp.ControlPoint)

Example 10 with Device

use of org.cybergarage.upnp.Device in project felix by apache.

the class BuildDevice method createCyberLinkDevice.

public static Device createCyberLinkDevice(ServiceReference sr) {
    Node root = buildRootNode();
    Device devUPnP = buildRootDeviceNode(root, sr);
    return devUPnP;
}
Also used : UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) Node(org.cybergarage.xml.Node)

Aggregations

Device (org.cybergarage.upnp.Device)21 UPnPDevice (org.osgi.service.upnp.UPnPDevice)15 ControlPoint (org.cybergarage.upnp.ControlPoint)10 DeviceList (org.cybergarage.upnp.DeviceList)9 Service (org.cybergarage.upnp.Service)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 ServiceRegistration (org.osgi.framework.ServiceRegistration)5 ServiceReference (org.osgi.framework.ServiceReference)4 Node (org.cybergarage.xml.Node)3 UPnPService (org.osgi.service.upnp.UPnPService)3 Enumeration (java.util.Enumeration)2 Properties (java.util.Properties)2 Vector (java.util.Vector)2 UPnPDeviceImpl (org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPDeviceImpl)2 ServiceList (org.cybergarage.upnp.ServiceList)2 DeviceData (org.cybergarage.upnp.xml.DeviceData)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1