Search in sources :

Example 1 with UPnPService

use of org.osgi.service.upnp.UPnPService in project felix by apache.

the class BuildDevice method addServices.

/**
 * Method used to create a new Service in CyberLink world without creating the XML
 *
 * @param id ServiceId
 * @param devUPnP the CyberLink device that where the new Service will be created
 * @param sr ServiceReference to OSGi Device that used as source of the information
 *              for the creation of the device
 */
private static void addServices(String id, Device devUPnP, ServiceReference sr) {
    UPnPDevice devOSGi = (UPnPDevice) Activator.bc.getService(sr);
    if (devOSGi == null) {
        // added by twa to prevent a null pointer exception
        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " is deregistered from the framework while is exported");
        return;
    }
    UPnPService[] services = devOSGi.getServices();
    if (services == null || services.length == 0)
        return;
    for (int i = 0; i < services.length; i++) {
        Service ser = new Service();
        devUPnP.addService(ser);
        ser.setServiceType(services[i].getType());
        ser.setServiceID(services[i].getId());
        ser.setSCPDURL(id + "/service/" + i + "/gen-desc.xml");
        ser.setDescriptionURL(id + "/service/" + i + "/gen-desc.xml");
        ser.setControlURL(id + "/service/" + i + "/ctrl");
        ser.setEventSubURL(id + "/service/" + i + "/event");
        UPnPAction[] actions = services[i].getActions();
        for (int j = 0; j < actions.length; j++) {
            boolean valid = true;
            Action act = new Action(ser.getServiceNode());
            act.setName(actions[j].getName());
            ArgumentList al = new ArgumentList();
            String[] names = actions[j].getInputArgumentNames();
            if (names != null) {
                for (int k = 0; k < names.length; k++) {
                    UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
                    if (variable == null) {
                        /*
                             * //TODO Create a stict and relaxed behavior of the base driver which 
                             * export as much it can or export only 100% complaint UPnPDevice service 
                             */
                        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Input argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
                        valid = false;
                        break;
                    }
                    Argument a = new Argument();
                    a.setDirection(Argument.IN);
                    a.setName(names[k]);
                    a.setRelatedStateVariableName(variable.getName());
                    al.add(a);
                }
            }
            names = actions[j].getOutputArgumentNames();
            if (names != null && valid) {
                for (int k = 0; k < names.length; k++) {
                    UPnPStateVariable variable = actions[j].getStateVariable(names[k]);
                    if (variable == null) {
                        /*
                             * //TODO Create a stict and relaxed behavior of the base driver which 
                             * export as much it can or export only 100% complaint UPnPDevice service 
                             */
                        Activator.logger.WARNING("UPnP Device that cotains serviceId=" + id + " contains the action " + actions[j].getName() + " with the Output argument " + names[k] + " not related to any UPnPStateVariable. Thus this action won't be exported");
                    }
                    Argument a = new Argument();
                    a.setDirection(Argument.OUT);
                    a.setName(names[k]);
                    a.setRelatedStateVariableName(variable.getName());
                    al.add(a);
                }
            }
            if (valid) {
                act.setArgumentList(al);
                ser.addAction(act);
            }
        }
        UPnPStateVariable[] vars = services[i].getStateVariables();
        for (int j = 0; j < vars.length; j++) {
            StateVariable var = new StateVariable();
            var.setDataType(vars[j].getUPnPDataType());
            var.setName(vars[j].getName());
            var.setSendEvents(vars[j].sendsEvents());
            String[] values = vars[j].getAllowedValues();
            if (values != null) {
                AllowedValueList avl = new AllowedValueList(values);
                var.setAllowedValueList(avl);
            } else if (vars[j].getMaximum() != null) {
                AllowedValueRange avr = new AllowedValueRange(vars[j].getMaximum(), vars[j].getMinimum(), vars[j].getStep());
                var.setAllowedValueRange(avr);
            }
            if (vars[j].getDefaultValue() != null)
                try {
                    var.setDefaultValue(Converter.toString(vars[j].getDefaultValue(), vars[j].getUPnPDataType()));
                } catch (Exception ignored) {
                }
            ser.addStateVariable(var);
        }
        Activator.bc.ungetService(sr);
    }
}
Also used : UPnPAction(org.osgi.service.upnp.UPnPAction) Action(org.cybergarage.upnp.Action) AllowedValueList(org.cybergarage.upnp.AllowedValueList) AllowedValueRange(org.cybergarage.upnp.AllowedValueRange) Argument(org.cybergarage.upnp.Argument) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) StateVariable(org.cybergarage.upnp.StateVariable) UPnPService(org.osgi.service.upnp.UPnPService) Service(org.cybergarage.upnp.Service) UPnPStateVariable(org.osgi.service.upnp.UPnPStateVariable) ArgumentList(org.cybergarage.upnp.ArgumentList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPService(org.osgi.service.upnp.UPnPService) UPnPAction(org.osgi.service.upnp.UPnPAction)

Example 2 with UPnPService

use of org.osgi.service.upnp.UPnPService 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 3 with UPnPService

use of org.osgi.service.upnp.UPnPService in project felix by apache.

the class UPnPDeviceImpl method getServices.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.service.upnp.UPnPDevice#getServices()
	 */
public UPnPService[] getServices() {
    Enumeration e = services.elements();
    if (e == null) {
        // TODO should I return null or an empty array? The specification seems to said to return null
        return null;
    }
    UPnPService[] uPnPser = new UPnPService[services.size()];
    int i = 0;
    while (e.hasMoreElements()) {
        uPnPser[i] = (UPnPServiceImpl) e.nextElement();
        i++;
    }
    return uPnPser;
}
Also used : Enumeration(java.util.Enumeration) UPnPService(org.osgi.service.upnp.UPnPService)

Example 4 with UPnPService

use of org.osgi.service.upnp.UPnPService in project felix by apache.

the class ConfigurationPrinterImpl method print.

private void print(PrintWriter pw, ServiceReference ref) {
    pw.println(nameOf(ref));
    pw.println("  Properties:");
    String[] properties = ref.getPropertyKeys();
    Arrays.sort(properties);
    for (int i = 0; i < properties.length; i++) {
        String key = properties[i];
        if (OBJECTCLASS.equals(key) || SERVICE_ID.equals(key)) {
            continue;
        }
        Object val = ref.getProperty(key);
        val = WebConsoleUtil.toString(val);
        // $NON-NLS-1$
        pw.print("    ");
        pw.print(key);
        // $NON-NLS-1$
        pw.print(" = ");
        pw.println(val);
    }
    UPnPDevice dev = (UPnPDevice) tracker.getService(ref);
    if (null == dev)
        return;
    UPnPService[] services = dev.getServices();
    if (null != services && services.length > 0) {
        pw.println("  Services:");
        for (int i = 0; i < services.length; i++) {
            print(pw, services[i]);
        }
    }
    pw.println();
}
Also used : UPnPDevice(org.osgi.service.upnp.UPnPDevice) UPnPService(org.osgi.service.upnp.UPnPService)

Example 5 with UPnPService

use of org.osgi.service.upnp.UPnPService in project felix by apache.

the class ControlServlet method doPost.

/**
 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 */
@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Map<String, Object> json = new HashMap<String, Object>();
        // $NON-NLS-1$
        String method = request.getParameter("action");
        if (// $NON-NLS-1$
        "listDevices".equals(method)) {
            getSession(request).unsubscribe();
            ServiceReference[] refs = tracker.getServiceReferences();
            // add root devices only
            for (int i = 0; refs != null && i < refs.length; i++) {
                if (refs[i] != null && refs[i].getProperty(UPnPDevice.PARENT_UDN) == null) {
                    Map<String, Object> deviceJSON = deviceTreeToJSON(refs[i]);
                    if (null != deviceJSON) {
                        @SuppressWarnings("unchecked") List<Object> // $NON-NLS-1$
                        list = (List<Object>) json.get("devices");
                        if (list == null) {
                            list = new ArrayList<Object>();
                            // $NON-NLS-1$
                            json.put("devices", list);
                        }
                        list.add(deviceJSON);
                    }
                }
            }
        } else if (// $NON-NLS-1$
        "serviceDetails".equals(method)) {
            UPnPService service = requireService(request);
            SessionObject session = // 
            getSession(request).subscribe(require("udn", request), // $NON-NLS-1$
            service.getId());
            json = Serializer.serviceToJSON(service, session);
        } else if (// $NON-NLS-1$
        "invokeAction".equals(method)) {
            UPnPService service = requireService(request);
            // $NON-NLS-1$
            UPnPAction action = service.getAction(require("actionID", request));
            // $NON-NLS-1$
            String[] names = request.getParameterValues("names");
            if (null == names) {
                // $NON-NLS-1$
                names = request.getParameterValues("names[]");
            }
            // $NON-NLS-1$
            String[] vals = request.getParameterValues("vals");
            if (null == vals) {
                // $NON-NLS-1$
                vals = request.getParameterValues("vals[]");
            }
            json = invoke(action, names, vals);
        } else {
            throw new ServletException("Invalid action: " + method);
        }
        // $NON-NLS-1$
        response.setContentType("application/json");
        // $NON-NLS-1$
        response.setCharacterEncoding("UTF-8");
        final JSONWriter writer = new JSONWriter(response.getWriter());
        writer.value(json);
        writer.flush();
    } catch (ServletException e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        // $NON-NLS-1$
        response.setContentType("text/plain");
        e.printStackTrace(response.getWriter());
        response.flushBuffer();
    } catch (Exception e) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        // $NON-NLS-1$
        response.setContentType("text/plain");
        e.printStackTrace(response.getWriter());
        response.flushBuffer();
    }
}
Also used : JSONWriter(org.apache.felix.utils.json.JSONWriter) HashMap(java.util.HashMap) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) ServletException(javax.servlet.ServletException) ArrayList(java.util.ArrayList) List(java.util.List) UPnPService(org.osgi.service.upnp.UPnPService) UPnPAction(org.osgi.service.upnp.UPnPAction)

Aggregations

UPnPService (org.osgi.service.upnp.UPnPService)8 UPnPDevice (org.osgi.service.upnp.UPnPDevice)5 UPnPAction (org.osgi.service.upnp.UPnPAction)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 UPnPStateVariable (org.osgi.service.upnp.UPnPStateVariable)3 Dictionary (java.util.Dictionary)2 Enumeration (java.util.Enumeration)2 UPnPServiceImpl (org.apache.felix.upnp.basedriver.importer.core.upnp.UPnPServiceImpl)2 Argument (org.cybergarage.upnp.Argument)2 ArgumentList (org.cybergarage.upnp.ArgumentList)2 ControlPoint (org.cybergarage.upnp.ControlPoint)2 Service (org.cybergarage.upnp.Service)2 ServiceReference (org.osgi.framework.ServiceReference)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Properties (java.util.Properties)1 Vector (java.util.Vector)1