Search in sources :

Example 1 with ParseUSN

use of org.apache.felix.upnp.basedriver.importer.util.ParseUSN 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 2 with ParseUSN

use of org.apache.felix.upnp.basedriver.importer.util.ParseUSN in project felix by apache.

the class MyCtrlPoint method deviceSearchResponseReceived.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.cybergarage.upnp.device.SearchResponseListener#deviceSearchResponseReceived(org.cybergarage.upnp.ssdp.SSDPPacket)
	 */
public void deviceSearchResponseReceived(SSDPPacket ssdpPacket) {
    Activator.logger.DEBUG("[Importer] deviceSearchResponseReceived");
    Activator.logger.PACKET(ssdpPacket.toString());
    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 (devices.containsKey(udn)) {
        Activator.logger.DEBUG("[Importer] Device already discovered");
        /*
			 * Exist the registered device either in OSGi and 
			 * hashtable of local device
			 */
        if (parseUSN.isService()) {
            doServiceUpdating(udn, parseUSN.getServiceType());
        }
    } else {
        doDeviceRegistration(udn);
    }
}
Also used : Dictionary(java.util.Dictionary) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) ParseUSN(org.apache.felix.upnp.basedriver.importer.util.ParseUSN) ControlPoint(org.cybergarage.upnp.ControlPoint) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

Dictionary (java.util.Dictionary)2 ParseUSN (org.apache.felix.upnp.basedriver.importer.util.ParseUSN)2 ControlPoint (org.cybergarage.upnp.ControlPoint)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 ServiceReference (org.osgi.framework.ServiceReference)2 UPnPDevice (org.osgi.service.upnp.UPnPDevice)2 Vector (java.util.Vector)1 UPnPDeviceImpl (org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPDeviceImpl)1 Device (org.cybergarage.upnp.Device)1 Service (org.cybergarage.upnp.Service)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 UPnPService (org.osgi.service.upnp.UPnPService)1