public static void main(String[] args) throws Exception { System.out.println("Starting Cling..."); UpnpService upnpService = new UpnpServiceImpl(); long noOfDevices = args.length == 1 ? Long.valueOf(args[0]) : 10; System.out.println("Registering BinaryLight devices: " + noOfDevices); for (long i = 0; i <= noOfDevices; i++) { LocalService service = new AnnotationLocalServiceBinder().read(DemoBinaryLight.class); service.setManager(new DefaultServiceManager(service, DemoBinaryLight.class)); upnpService.getRegistry().addDevice( DemoBinaryLight.createDefaultDevice(UDN.uniqueSystemIdentifier(Long.toString(i)), "Device: " + i, service) ); } System.out.println("Completed device registration"); } }
/** * Adds a property change listener on the {@link org.fourthline.cling.model.ServiceManager}. */ synchronized public void registerOnService() { getService().getManager().getPropertyChangeSupport().addPropertyChangeListener(this); }
public StateVariableAccessor getAccessor(String stateVariableName) { StateVariable sv; return (sv = getStateVariable(stateVariableName)) != null ? getAccessor(sv) : null; }
public ActionExecutor getExecutor(String actionName) { Action action; return (action = getAction(actionName)) != null ? getExecutor(action) : null; }
@Override public Collection<StateVariableValue> getCurrentState() throws Exception { lock(); try { Collection<StateVariableValue> values = readInitialEventedStateVariableValues(); if (values != null) { log.fine("Obtained initial state variable values for event, skipping individual state variable accessors"); return values; } values = new ArrayList<>(); for (StateVariable stateVariable : getService().getStateVariables()) { if (stateVariable.getEventDetails().isSendEvents()) { StateVariableAccessor accessor = getService().getAccessor(stateVariable); if (accessor == null) throw new IllegalStateException("No accessor for evented state variable"); values.add(accessor.read(stateVariable, getImplementation())); } } return values; } finally { unlock(); } }
public MediaServer(InetAddress localAddress, Context ctx) throws ValidationException { super(null, port, null, true); Log.i(TAG, "Creating media server !"); localService = new AnnotationLocalServiceBinder() .read(ContentDirectoryService.class); localService.setManager(new DefaultServiceManager<ContentDirectoryService>( localService, ContentDirectoryService.class)); udn = UDN.valueOf(new UUID(0,10).toString()); this.localAddress = localAddress; this.ctx = ctx; createLocalDevice(); ContentDirectoryService contentDirectoryService = (ContentDirectoryService)localService.getManager().getImplementation(); contentDirectoryService.setContext(ctx); contentDirectoryService.setBaseURL(getAddress()); }
public IncomingActionRequestMessage(StreamRequestMessage source, LocalService service) throws ActionException { super(source); SoapActionHeader soapActionHeader = getHeaders().getFirstHeader(UpnpHeader.Type.SOAPACTION, SoapActionHeader.class); if (soapActionHeader == null) { throw new ActionException(ErrorCode.INVALID_ACTION, "Missing SOAP action header"); } SoapActionType actionType = soapActionHeader.getValue(); this.action = service.getAction(actionType.getActionName()); if (this.action == null) { throw new ActionException(ErrorCode.INVALID_ACTION, "Service doesn't implement action: " + actionType.getActionName()); } if (!QueryStateVariableAction.ACTION_NAME.equals(actionType.getActionName())) { if (!service.getServiceType().implementsVersion(actionType.getServiceType())) { throw new ActionException(ErrorCode.INVALID_ACTION, "Service doesn't support the requested service version"); } } this.actionNamespace = actionType.getTypeString(); }
@Override protected Collection<StateVariableValue> readInitialEventedStateVariableValues() throws Exception { // We don't use the service's internal LastChange but a fresh new one just for // this initial event. Modifying the internal one would trigger event notification's // to other subscribers! LastChange lc = new LastChange(getLastChangeParser()); // Get the current "logical" instances of the service UnsignedIntegerFourBytes[] ids = getImplementation().getCurrentInstanceIds(); if (ids.length > 0) { for (UnsignedIntegerFourBytes instanceId : ids) { // Iterate through all "logical" instances and ask them what their state is getImplementation().appendCurrentState(lc, instanceId); } } else { // Use the default "logical" instance with ID 0 getImplementation().appendCurrentState(lc, new UnsignedIntegerFourBytes(0)); } // Sum it all up and return it in the initial event to the GENA subscriber StateVariable variable = getService().getStateVariable("LastChange"); Collection<StateVariableValue> values = new ArrayList<>(); values.add(new StateVariableValue(variable, lc.toString())); return values; }
@Override public LocalService newInstance(ServiceType serviceType, ServiceId serviceId, URI descriptorURI, URI controlURI, URI eventSubscriptionURI, Action<LocalService>[] actions, StateVariable<LocalService>[] stateVariables) throws ValidationException { return new LocalService( serviceType, serviceId, actions, stateVariables ); }
@Override public Action getQueryStateVariableAction() { return getAction(QueryStateVariableAction.ACTION_NAME); }
public ActionExecutor getExecutor(String actionName) { Action action; return (action = getAction(actionName)) != null ? getExecutor(action) : null; }
public IncomingActionRequestMessage(StreamRequestMessage source, LocalService service) throws ActionException { super(source); SoapActionHeader soapActionHeader = getHeaders().getFirstHeader(UpnpHeader.Type.SOAPACTION, SoapActionHeader.class); if (soapActionHeader == null) { throw new ActionException(ErrorCode.INVALID_ACTION, "Missing SOAP action header"); } SoapActionType actionType = soapActionHeader.getValue(); this.action = service.getAction(actionType.getActionName()); if (this.action == null) { throw new ActionException(ErrorCode.INVALID_ACTION, "Service doesn't implement action: " + actionType.getActionName()); } if (!QueryStateVariableAction.ACTION_NAME.equals(actionType.getActionName())) { if (!service.getServiceType().implementsVersion(actionType.getServiceType())) { throw new ActionException(ErrorCode.INVALID_ACTION, "Service doesn't support the requested service version"); } } this.actionNamespace = actionType.getTypeString(); }
@Override public Collection<StateVariableValue> getCurrentState() throws Exception { lock(); try { Collection<StateVariableValue> values = readInitialEventedStateVariableValues(); if (values != null) { log.fine("Obtained initial state variable values for event, skipping individual state variable accessors"); return values; } values = new ArrayList(); for (StateVariable stateVariable : getService().getStateVariables()) { if (stateVariable.getEventDetails().isSendEvents()) { StateVariableAccessor accessor = getService().getAccessor(stateVariable); if (accessor == null) throw new IllegalStateException("No accessor for evented state variable"); values.add(accessor.read(stateVariable, getImplementation())); } } return values; } finally { unlock(); } }
@Override protected Collection<StateVariableValue> readInitialEventedStateVariableValues() throws Exception { // We don't use the service's internal LastChange but a fresh new one just for // this initial event. Modifying the internal one would trigger event notification's // to other subscribers! LastChange lc = new LastChange(getLastChangeParser()); // Get the current "logical" instances of the service UnsignedIntegerFourBytes[] ids = getImplementation().getCurrentInstanceIds(); if (ids.length > 0) { for (UnsignedIntegerFourBytes instanceId : ids) { // Iterate through all "logical" instances and ask them what their state is getImplementation().appendCurrentState(lc, instanceId); } } else { // Use the default "logical" instance with ID 0 getImplementation().appendCurrentState(lc, new UnsignedIntegerFourBytes(0)); } // Sum it all up and return it in the initial event to the GENA subscriber StateVariable variable = getService().getStateVariable("LastChange"); Collection<StateVariableValue> values = new ArrayList(); values.add(new StateVariableValue(variable, lc.toString())); return values; }
public LocalService read(Class<?> clazz, ServiceId id, ServiceType type, boolean supportsQueryStateVariables, Set<Class> stringConvertibleTypes) throws LocalServiceBindingException { Map<StateVariable, StateVariableAccessor> stateVariables = readStateVariables(clazz, stringConvertibleTypes); Map<Action, ActionExecutor> actions = readActions(clazz, stateVariables, stringConvertibleTypes); // Special treatment of the state variable querying action if (supportsQueryStateVariables) { actions.put(new QueryStateVariableAction(), new QueryStateVariableExecutor()); } try { return new LocalService(type, id, actions, stateVariables, stringConvertibleTypes, supportsQueryStateVariables); } catch (ValidationException ex) { log.severe("Could not validate device model: " + ex.toString()); for (ValidationError validationError : ex.getErrors()) { log.severe(validationError.toString()); } throw new LocalServiceBindingException("Validation of model failed, check the log"); } }
@Override public Action getQueryStateVariableAction() { return getAction(QueryStateVariableAction.ACTION_NAME); }
new AnnotationLocalServiceBinder().read(DemoBinaryLight.class); service.setManager( new DefaultServiceManager(service) { @Override
/** * Removes a property change listener on the {@link org.fourthline.cling.model.ServiceManager}. */ synchronized public void end(CancelReason reason) { try { getService().getManager().getPropertyChangeSupport().removePropertyChangeListener(this); } catch (Exception ex) { log.warning("Removal of local service property change listener failed: " + Exceptions.unwrap(ex)); } ended(reason); }
protected Collection<StateVariableValue> getCurrentState(String[] variableNames) throws Exception { lock(); try { Collection<StateVariableValue> values = new ArrayList<>(); for (String variableName : variableNames) { variableName = variableName.trim(); StateVariable stateVariable = getService().getStateVariable(variableName); if (stateVariable == null || !stateVariable.getEventDetails().isSendEvents()) { log.fine("Ignoring unknown or non-evented state variable: " + variableName); continue; } StateVariableAccessor accessor = getService().getAccessor(stateVariable); if (accessor == null) { log.warning("Ignoring evented state variable without accessor: " + variableName); continue; } values.add(accessor.read(stateVariable, getImplementation())); } return values; } finally { unlock(); } }
public ActionExecutor getExecutor(String actionName) { Action action; return (action = getAction(actionName)) != null ? getExecutor(action) : null; }