Search in sources :

Example 1 with ListenerModified

use of org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerModified 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 ListenerModified

use of org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerModified in project felix by apache.

the class SubScriber method run.

public void run() {
    while (running) {
        Object msg = subQueue.dequeue();
        if (running) {
            if (msg instanceof FirstMessage) {
                FirstMessage firstmsg = (FirstMessage) msg;
                Service service = firstmsg.getService();
                if (!service.isSubscribed()) {
                    // is not subscribe
                    boolean ok = ctrl.subscribe(service, 120000);
                    String sid = "";
                    if (ok) {
                        // subcribe ok
                        sid = service.getSID();
                        firstmsg.setSid(sid);
                        monitor.addListener(sid, firstmsg.getListener());
                    } else {
                        // subscribe not ok
                        Activator.logger.log(LogService.LOG_ERROR, "Sucribe failed");
                    }
                } else {
                    // already subscribe
                    monitor.addListener(service.getSID(), firstmsg.getListener());
                }
            } else if (msg instanceof ListenerModified) {
                monitor.updateListener((ListenerModified) msg, subQueue, ctrl);
            } else if (msg instanceof ListenerUnRegistration) {
                ListenerUnRegistration unreg = (ListenerUnRegistration) msg;
                monitor.delListener(unreg.getListener(), ctrl);
            } else if (msg instanceof SidExipired) {
                Activator.logger.WARNING("[Importer] Please report the bug. Used code - should be checked and removed");
            }
        }
    }
}
Also used : ListenerUnRegistration(org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerUnRegistration) SidExipired(org.apache.felix.upnp.basedriver.importer.core.event.message.SidExipired) ListenerModified(org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerModified) FirstMessage(org.apache.felix.upnp.basedriver.importer.core.event.message.FirstMessage) LogService(org.osgi.service.log.LogService) Service(org.cybergarage.upnp.Service)

Aggregations

FirstMessage (org.apache.felix.upnp.basedriver.importer.core.event.message.FirstMessage)2 ListenerModified (org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerModified)2 ListenerUnRegistration (org.apache.felix.upnp.basedriver.importer.core.event.message.ListenerUnRegistration)2 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 Vector (java.util.Vector)1 SidExipired (org.apache.felix.upnp.basedriver.importer.core.event.message.SidExipired)1 UPnPServiceImpl (org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl)1 ControlPoint (org.cybergarage.upnp.ControlPoint)1 Service (org.cybergarage.upnp.Service)1 Filter (org.osgi.framework.Filter)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1 LogService (org.osgi.service.log.LogService)1 UPnPDevice (org.osgi.service.upnp.UPnPDevice)1 UPnPEventListener (org.osgi.service.upnp.UPnPEventListener)1 UPnPService (org.osgi.service.upnp.UPnPService)1 UPnPStateVariable (org.osgi.service.upnp.UPnPStateVariable)1