Search in sources :

Example 1 with UPnPServiceImpl

use of org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl in project felix by apache.

the class MyCtrlPoint method serviceChanged.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
	 */
public void serviceChanged(ServiceEvent event) {
    Activator.logger.DEBUG("[Importer] serviceChanged");
    Activator.logger.DEBUG("Event::" + event.toString());
    if (event.getType() == ServiceEvent.REGISTERED) {
        /* check new listener registration */
        ServiceReference serRef = event.getServiceReference();
        Object obj = serRef.getProperty(UPnPEventListener.UPNP_FILTER);
        /* obtain interested devices for the listener */
        ServiceReference[] devicesRefs = null;
        if (obj != null) {
            Filter filter = (Filter) obj;
            String filtra = filter.toString();
            /*
				 * Avoid to implement the notification for device 
				 * that are not been created by BaseDriver
				 */
            String newfilter = "(&" + filtra + IMPORT_FLTR + ")";
            // System.out.println(newfilter);
            try {
                devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
            } catch (InvalidSyntaxException e) {
                e.printStackTrace();
            }
            if (devicesRefs != null) {
                /*
										   * 
										   * only if there is a compatibile device
										   */
                Dictionary dic = new Hashtable();
                for (int i = 0; i < devicesRefs.length; i++) {
                    UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
                    dic.put(UPnPDevice.ID, device.getDescriptions(null).get(UPnPDevice.UDN));
                    dic.put(UPnPDevice.TYPE, device.getDescriptions(null).get(UPnPDevice.TYPE));
                    UPnPService[] services = device.getServices();
                    if (services != null) {
                        for (int j = 0; j < services.length; j++) {
                            dic.put(UPnPService.ID, services[j].getId());
                            dic.put(UPnPService.TYPE, services[j].getType());
                            // TODO add method boolean serviceEvented() so we can remove the below cycle
                            UPnPStateVariable[] stateVars = services[j].getStateVariables();
                            boolean hasEventedVars = false;
                            for (int k = 0; k < stateVars.length && !hasEventedVars; k++) {
                                hasEventedVars = stateVars[k].sendsEvents();
                                if (hasEventedVars) {
                                    if (filter.match(dic)) {
                                        UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
                                        FirstMessage msg = new FirstMessage(((UPnPServiceImpl) services[j]).getCyberService(), listener);
                                        subQueue.enqueue(msg);
                                    }
                                }
                            }
                        }
                    }
                    context.ungetService(devicesRefs[i]);
                }
            }
        } else {
            /* obj==null (interested in all devices) */
            try {
                String newfilter = "(!" + EXPORT_FLTR + ")";
                devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
            } catch (InvalidSyntaxException e) {
                e.printStackTrace();
            }
            if (devicesRefs != null) {
                for (int i = 0; i < devicesRefs.length; i++) {
                    UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
                    UPnPService[] services = device.getServices();
                    if (services != null) {
                        for (int j = 0; j < services.length; j++) {
                            UPnPStateVariable[] stateVars = services[j].getStateVariables();
                            boolean bool = false;
                            for (int k = 0; k < stateVars.length; k++) {
                                bool = stateVars[k].sendsEvents();
                                if (bool) {
                                    break;
                                }
                            }
                            if (bool) {
                                UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
                                FirstMessage msg = new FirstMessage(((UPnPServiceImpl) services[j]).getCyberService(), listener);
                                subQueue.enqueue(msg);
                            }
                        }
                    }
                    context.ungetService(devicesRefs[i]);
                }
            }
        }
    } else if (event.getType() == ServiceEvent.MODIFIED) {
        Vector newServices = new Vector();
        ServiceReference serRef = event.getServiceReference();
        Filter filter = (Filter) serRef.getProperty(UPnPEventListener.UPNP_FILTER);
        UPnPEventListener listener = (UPnPEventListener) context.getService(serRef);
        ServiceReference[] devicesRefs = null;
        if (filter != null) {
            try {
                String filtra = filter.toString();
                String newfilter = "(&" + filtra + "(!" + EXPORT_FLTR + ")" + ")";
                devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
            } catch (InvalidSyntaxException e) {
                e.printStackTrace();
            }
            if (devicesRefs != null) {
                /*
										   * 
										   * only if there is a compatibile device
										   */
                Dictionary dic = new Hashtable();
                /* 
					 * look for the service that match
					 */
                for (int i = 0; i < devicesRefs.length; i++) {
                    UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
                    dic.put(UPnPDevice.ID, device.getDescriptions(null).get(UPnPDevice.UDN));
                    dic.put(UPnPDevice.TYPE, device.getDescriptions(null).get(UPnPDevice.TYPE));
                    UPnPService[] services = device.getServices();
                    if (services != null) {
                        for (int j = 0; j < services.length; j++) {
                            dic.put(UPnPService.ID, services[j].getId());
                            dic.put(UPnPService.TYPE, services[j].getType());
                            UPnPStateVariable[] stateVars = services[j].getStateVariables();
                            boolean hasEventedVars = false;
                            for (int k = 0; k < stateVars.length; k++) {
                                hasEventedVars = stateVars[k].sendsEvents();
                                if (hasEventedVars) {
                                    break;
                                }
                            }
                            if (!hasEventedVars) {
                                continue;
                            }
                            boolean bool = filter.match(dic);
                            if (bool) {
                                newServices.add(((UPnPServiceImpl) services[j]).getCyberService());
                            }
                        }
                    // for services
                    }
                    // services ==null
                    context.ungetService(devicesRefs[i]);
                }
                // for devicesRefs
                ListenerModified msg = new ListenerModified(newServices, listener);
                subQueue.enqueue(msg);
            }
        // devicesrefs !=null
        } else {
            // interrested in all devices
            try {
                String newfilter = "(!(" + UPnPDevice.UPNP_EXPORT + "=*" + ")" + ")";
                devicesRefs = context.getServiceReferences(UPnPDevice.class.getName(), newfilter);
            } catch (InvalidSyntaxException e) {
                e.printStackTrace();
            }
            if (devicesRefs != null) {
                for (int i = 0; i < devicesRefs.length; i++) {
                    UPnPDevice device = (UPnPDevice) context.getService(devicesRefs[i]);
                    UPnPService[] services = device.getServices();
                    if (services != null) {
                        for (int j = 0; j < services.length; j++) {
                            UPnPStateVariable[] stateVars = services[j].getStateVariables();
                            boolean hasEventedVars = false;
                            for (int k = 0; k < stateVars.length; k++) {
                                hasEventedVars = stateVars[k].sendsEvents();
                                if (hasEventedVars) {
                                    break;
                                }
                            }
                            if (hasEventedVars) {
                                newServices.add(((UPnPServiceImpl) services[j]).getCyberService());
                            }
                        // hasEventedvars
                        }
                    // for services
                    }
                    // services !=null
                    context.ungetService(devicesRefs[i]);
                }
                // for devicesRefs
                subQueue.enqueue(new ListenerModified(newServices, listener));
            }
        // devicesRefs !=null
        }
    } else if (event.getType() == ServiceEvent.UNREGISTERING) {
        UPnPEventListener listener = (UPnPEventListener) context.getService(event.getServiceReference());
        if (listener != null) {
            ListenerUnRegistration msg = new ListenerUnRegistration(listener);
            subQueue.enqueue(msg);
        }
        context.ungetService(event.getServiceReference());
    }
}
Also used : Dictionary(java.util.Dictionary) ListenerUnRegistration(org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerUnRegistration) Hashtable(java.util.Hashtable) FirstMessage(org.apache.felix.upnp.basedriver.importer.core.event.message.FirstMessage) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) ControlPoint(org.cybergarage.upnp.ControlPoint) ServiceReference(org.osgi.framework.ServiceReference) Filter(org.osgi.framework.Filter) UPnPServiceImpl(org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl) ListenerModified(org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerModified) UPnPEventListener(org.osgi.service.upnp.UPnPEventListener) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPService(org.osgi.service.upnp.UPnPService) Vector(java.util.Vector)

Example 2 with UPnPServiceImpl

use of org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl in project felix by apache.

the class MyCtrlPoint method serviceFromSid.

/*
	   * (non-Javadoc)
	   * 
	   * @see org.cybergarage.upnp.event.EventListener#eventNotifyReceived(java.lang.String,
	   *      long, java.lang.String, java.lang.String)
	   */
/*
	 * public void eventNotifyReceived(String uuid, long seq, String varName,
	 * String value) { // TODO Auto-generated method stub StateChanged msg = new
	 * StateChanged(uuid, varName, value,seq);,serviceFromSid(uuid)
	 * notifierQueue.enqueue(msg); }
	 */
public Service serviceFromSid(String sid) {
    Enumeration e = devices.elements();
    Service cyberService = null;
    while (e.hasMoreElements()) {
        OSGiDeviceInfo deviceinfo = (OSGiDeviceInfo) e.nextElement();
        UPnPDevice device = deviceinfo.getOSGiDevice();
        UPnPService[] services = (UPnPService[]) device.getServices();
        UPnPServiceImpl[] servicesImpl = new UPnPServiceImpl[services.length];
        for (int i = 0; i < servicesImpl.length; i++) {
            servicesImpl[i] = (UPnPServiceImpl) services[i];
        }
        for (int i = 0; i < servicesImpl.length; i++) {
            cyberService = servicesImpl[i].getCyberService();
            boolean bool = cyberService.isSubscribed();
            if (bool) {
                if (cyberService.getSID().equals(sid)) {
                    return cyberService;
                }
            }
        }
    }
    return null;
}
Also used : Enumeration(java.util.Enumeration) UPnPServiceImpl(org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPService(org.osgi.service.upnp.UPnPService) ControlPoint(org.cybergarage.upnp.ControlPoint)

Example 3 with UPnPServiceImpl

use of org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl in project felix by apache.

the class DeviceSetup method deviceSetup.

public static void deviceSetup(Dictionary properties, Device dev, Vector icons, Hashtable services) {
    // TODO if I don't have any device, the size of devlist is 0
    DeviceList devList = dev.getDeviceList();
    /* childrenUDN property */
    if (devList.size() > 0) {
        String[] childrenUDN = new String[devList.size()];
        for (int i = 0; i < devList.size(); i++) {
            childrenUDN[i] = devList.getDevice(i).getUDN();
        }
        properties.put(UPnPDevice.CHILDREN_UDN, childrenUDN);
    }
    /* DEVICE CATEGORY */
    properties.put(org.osgi.service.device.Constants.DEVICE_CATEGORY, new String[] { UPnPDevice.DEVICE_CATEGORY });
    /*UPNP_IMPORT*/
    properties.put(Constants.UPNP_IMPORT, "http://felix.apache.org");
    /* FRIENDLY_NAME */
    // check the implementation fo getFriendlyName made by CyberLink
    properties.put(UPnPDevice.FRIENDLY_NAME, dev.getFriendlyName());
    /* MANUFACTURER */
    properties.put(UPnPDevice.MANUFACTURER, dev.getManufacture());
    /* MANUFACTURER_URL */
    properties.put(UPnPDevice.MANUFACTURER_URL, dev.getManufactureURL());
    /* MODEL_DESCRIPTION */
    properties.put(UPnPDevice.MODEL_DESCRIPTION, dev.getModelDescription());
    /* MODEL_NAME */
    properties.put(UPnPDevice.MODEL_NAME, dev.getModelName());
    /* MODEL_NUMBER */
    properties.put(UPnPDevice.MODEL_NUMBER, dev.getModelNumber());
    /* MODEL_URL */
    properties.put(UPnPDevice.MODEL_URL, dev.getModelURL());
    /* PARENT_UDN */
    if (!dev.isRootDevice()) {
        Device parent = dev.getParentDevice();
        /*Device root = dev.getRootDevice();
			if (root == null) {
				System.out.println("il device " + dev.getFriendlyName()
						+ "non ha root !!!");
			}*/
        properties.put(UPnPDevice.PARENT_UDN, parent.getUDN());
    }
    /* PRESENTATION_URL */
    properties.put(UPnPDevice.PRESENTATION_URL, dev.getPresentationURL());
    /* SERIAL_NUMBER */
    properties.put(UPnPDevice.SERIAL_NUMBER, dev.getSerialNumber());
    /* TYPE */
    properties.put(UPnPDevice.TYPE, dev.getDeviceType());
    /* UDN */
    properties.put(UPnPDevice.UDN, dev.getUDN());
    /* UPC */
    properties.put(UPnPDevice.UPC, dev.getUPC());
    IconList iconsList = dev.getIconList();
    if (iconsList.size() != 0) {
        for (int i = 0; i < iconsList.size(); i++) {
            UPnPIcon icon = new UPnPIconImpl(iconsList.getIcon(i), dev);
            icons.add(icon);
        }
    }
    /* 
		 * service of this device
		 */
    ServiceList serviceList = dev.getServiceList();
    /*
		 * if dev contain no service I'll get an empty SserviceList object
		 */
    String[] servicesIDProperty = new String[serviceList.size()];
    String[] servicesTypeProperty;
    HashSet serTypeSet = new HashSet();
    for (int i = 0; i < serviceList.size(); i++) {
        Service service = serviceList.getService(i);
        UPnPServiceImpl serviceImpl = new UPnPServiceImpl(service);
        services.put(service.getServiceID(), serviceImpl);
        servicesIDProperty[i] = serviceImpl.getId();
        serTypeSet.add(serviceImpl.getType());
    }
    servicesTypeProperty = new String[serTypeSet.size()];
    Iterator iter = serTypeSet.iterator();
    int i = 0;
    while (iter.hasNext()) {
        servicesTypeProperty[i] = (String) iter.next();
        i++;
    }
    properties.put(UPnPService.ID, servicesIDProperty);
    properties.put(UPnPService.TYPE, servicesTypeProperty);
}
Also used : UPnPIcon(org.osgi.service.upnp.UPnPIcon) UPnPDevice(org.osgi.service.upnp.UPnPDevice) Device(org.cybergarage.upnp.Device) ServiceList(org.cybergarage.upnp.ServiceList) UPnPIconImpl(org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPIconImpl) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) DeviceList(org.cybergarage.upnp.DeviceList) IconList(org.cybergarage.upnp.IconList) UPnPServiceImpl(org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Aggregations

UPnPServiceImpl (org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl)3 UPnPDevice (org.osgi.service.upnp.UPnPDevice)3 UPnPService (org.osgi.service.upnp.UPnPService)3 ControlPoint (org.cybergarage.upnp.ControlPoint)2 Service (org.cybergarage.upnp.Service)2 Dictionary (java.util.Dictionary)1 Enumeration (java.util.Enumeration)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 Vector (java.util.Vector)1 FirstMessage (org.apache.felix.upnp.basedriver.importer.core.event.message.FirstMessage)1 ListenerModified (org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerModified)1 ListenerUnRegistration (org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerUnRegistration)1 UPnPIconImpl (org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPIconImpl)1 Device (org.cybergarage.upnp.Device)1 DeviceList (org.cybergarage.upnp.DeviceList)1 IconList (org.cybergarage.upnp.IconList)1 ServiceList (org.cybergarage.upnp.ServiceList)1 Filter (org.osgi.framework.Filter)1