public Registry getRegistry() { return upnpService.getRegistry(); }
protected void removeDevice() { LocalDevice device; if ((device = upnpService.getRegistry().getLocalDevice(DEMO_DEVICE_UDN, true)) != null) { upnpService.getRegistry().removeDevice(device); } }
@Override public void responseException(Throwable t) { if (subscription == null) return; // Nothing to do, we didn't get that far log.fine("Response could not be send to subscriber, removing local GENA subscription: " + subscription); getUpnpService().getRegistry().removeLocalSubscription(subscription); } }
protected Service resolveService() { Service service = upnpService.getRegistry().getService(serviceReference); if (service == null) { onConnectionFailure("Device service not registered/available"); return null; } return service; }
@Override protected boolean waitBeforeExecution() throws InterruptedException { Integer mx = getInputMessage().getMX(); if (mx == null) { log.fine("Invalid search request, did not contain MX header: " + getInputMessage()); return false; } // Spec says we should assume "less" if it's 120 or more // From the spec, MX should be "greater than or equal to 1" // Prevent negative MX to make nextInt() throw IllegalArgumentException below if (mx > 120 || mx <= 0) mx = MXHeader.DEFAULT_VALUE; // Only wait if there is something to wait for if (getUpnpService().getRegistry().getLocalDevices().size() > 0) { int sleepTime = randomGenerator.nextInt(mx * 1000); log.fine("Sleeping " + sleepTime + " milliseconds to avoid flooding with search responses"); Thread.sleep(sleepTime); } return true; }
protected void onRenewalFailure() { log.fine("Subscription renewal failed, removing subscription from registry"); getUpnpService().getRegistry().removeRemoteSubscription(subscription); getUpnpService().getConfiguration().getRegistryListenerExecutor().execute( new Runnable() { public void run() { subscription.end(CancelReason.RENEWAL_FAILED, null); } } ); } }
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"); } }
public void run() { URL deviceURL = rd.getIdentity().getDescriptorURL(); // Performance optimization, try to avoid concurrent GET requests for device descriptor, // if we retrieve it once, we have the hydrated device. There is no different outcome // processing this several times concurrently. if (activeRetrievals.contains(deviceURL)) { log.finer("Exiting early, active retrieval for URL already in progress: " + deviceURL); return; } // Exit if it has been discovered already, could be we have been waiting in the executor queue too long if (getUpnpService().getRegistry().getRemoteDevice(rd.getIdentity().getUdn(), true) != null) { log.finer("Exiting early, already discovered: " + deviceURL); return; } try { activeRetrievals.add(deviceURL); describe(); } catch (RouterException ex) { log.log(Level.WARNING, "Descriptor retrieval failed: " + deviceURL, ex ); } finally { activeRetrievals.remove(deviceURL); } }
protected OutgoingSubscribeResponseMessage processRenewal(LocalService service, IncomingSubscribeRequestMessage requestMessage) { subscription = getUpnpService().getRegistry().getLocalSubscription(requestMessage.getSubscriptionId()); // Error conditions UDA 1.0 section 4.1.1 and 4.1.2 if (subscription == null) { log.fine("Invalid subscription ID for renewal request: " + getInputMessage()); return new OutgoingSubscribeResponseMessage(UpnpResponse.Status.PRECONDITION_FAILED); } log.fine("Renewing subscription: " + subscription); subscription.setSubscriptionDuration(requestMessage.getRequestedTimeoutSeconds()); if (getUpnpService().getRegistry().updateLocalSubscription(subscription)) { return new OutgoingSubscribeResponseMessage(subscription); } else { log.fine("Subscription went away before it could be renewed: " + getInputMessage()); return new OutgoingSubscribeResponseMessage(UpnpResponse.Status.PRECONDITION_FAILED); } }
synchronized public boolean notifyDiscoveryStart(final RemoteDevice device) { // Exit if we have it already, this is atomic inside this method, finally if (getUpnpService().getRegistry().getRemoteDevice(device.getIdentity().getUdn(), true) != null) { log.finer("Not notifying listeners, already registered: " + device); return false; } for (final RegistryListener listener : getListeners()) { getConfiguration().getRegistryListenerExecutor().execute( new Runnable() { public void run() { listener.remoteDeviceDiscoveryStarted(RegistryImpl.this, device); } } ); } return true; }
protected boolean isAdvertisementDisabled(LocalDevice device) { DiscoveryOptions options = getUpnpService().getRegistry().getDiscoveryOptions(device.getIdentity().getUdn()); return options != null && !options.isAdvertised(); }
protected void onUnsubscribe(final StreamResponseMessage response) { // Always remove from the registry and end the subscription properly - even if it's failed getUpnpService().getRegistry().removeRemoteSubscription(subscription); getUpnpService().getConfiguration().getRegistryListenerExecutor().execute( new Runnable() { public void run() { if (response == null) { log.fine("Unsubscribe failed, no response received"); subscription.end(CancelReason.UNSUBSCRIBE_FAILED, null); } else if (response.getOperation().isFailed()) { log.fine("Unsubscribe failed, response was: " + response); subscription.end(CancelReason.UNSUBSCRIBE_FAILED, response.getOperation()); } else { log.fine("Unsubscribe successful, response was: " + response); subscription.end(null, response.getOperation()); } } } ); } }
@Override public void run() { MediaRenderer.APP.log(Level.INFO, "Initializing GStreamer backend and registering MediaRenderer device..."); try { // Pick a display method boolean fullscreen = args == null || !Arrays.asList(args).contains(ARG_USE_WINDOWS); fullscreenCheckbox.setSelected(fullscreen); DisplayHandler displayHandler = fullscreen ? new FullscreenDisplayHandler() : new WindowedDisplayHandler(); // Initialize the GStreamer backend (this also sets log level to WARNING for org.gstreamer) Gst.init(MediaRenderer.APPNAME, args); mediaRenderer = new GstMediaRenderer(SUPPORTED_INSTANCES, displayHandler); getUpnpService().getRegistry().addDevice( mediaRenderer.getDevice() ); MediaRenderer.APP.log(Level.INFO, "Initialization complete!"); } catch (Throwable t) { MediaRenderer.APP.log(Level.SEVERE, "Initialization of GStreamer backend failed: " + t); throw new RuntimeException("Initialization of GStreamer backend failed", t); } } }.start();
@Override public void responseSent(StreamResponseMessage responseMessage) { if (subscription == null) return; // Preconditions failed very early on if (responseMessage != null && !responseMessage.getOperation().isFailed() && subscription.getCurrentSequence().getValue() == 0) { // Note that renewals should not have 0 // This is a minor concurrency issue: If we now register on the service and henceforth send a new // event message whenever the state of the service changes, there is still a chance that the initial // event message arrives later than the first on-change event message. Shouldn't be a problem as the // subscriber is supposed to figure out what to do with out-of-sequence messages. I would be // surprised though if actual implementations won't crash! log.fine("Establishing subscription"); subscription.registerOnService(); subscription.establish(); log.fine("Response to subscription sent successfully, now sending initial event asynchronously"); getUpnpService().getConfiguration().getAsyncProtocolExecutor().execute( getUpnpService().getProtocolFactory().createSendingEvent(subscription) ); } else if (subscription.getCurrentSequence().getValue() == 0) { log.fine("Subscription request's response aborted, not sending initial event"); if (responseMessage == null) { log.fine("Reason: No response at all from subscriber"); } else { log.fine("Reason: " + responseMessage.getOperation()); } log.fine("Removing subscription from registry: " + subscription); getUpnpService().getRegistry().removeLocalSubscription(subscription); } }
protected void sendSearchResponseDeviceType(DeviceType deviceType, NetworkAddress activeStreamServer) throws RouterException{ log.fine("Responding to device type search: " + deviceType); Collection<Device> devices = getUpnpService().getRegistry().getDevices(deviceType); for (Device device : devices) { if (device instanceof LocalDevice) { if (isAdvertisementDisabled((LocalDevice)device)) continue; log.finer("Sending matching device type search result for: " + device); OutgoingSearchResponse message = new OutgoingSearchResponseDeviceType( getInputMessage(), getDescriptorLocation(activeStreamServer, (LocalDevice) device), (LocalDevice) device ); prepareOutgoingSearchResponse(message); getUpnpService().getRouter().send(message); } } }
protected void sendSearchResponseServiceType(ServiceType serviceType, NetworkAddress activeStreamServer) throws RouterException { log.fine("Responding to service type search: " + serviceType); Collection<Device> devices = getUpnpService().getRegistry().getDevices(serviceType); for (Device device : devices) { if (device instanceof LocalDevice) { if (isAdvertisementDisabled((LocalDevice)device)) continue; log.finer("Sending matching service type search result: " + device); OutgoingSearchResponse message = new OutgoingSearchResponseServiceType( getInputMessage(), getDescriptorLocation(activeStreamServer, (LocalDevice) device), (LocalDevice) device, serviceType ); prepareOutgoingSearchResponse(message); getUpnpService().getRouter().send(message); } } }
protected void sendSearchResponseRootDevices(NetworkAddress activeStreamServer) throws RouterException { log.fine("Responding to root device search with advertisement messages for all local root devices"); for (LocalDevice device : getUpnpService().getRegistry().getLocalDevices()) { if (isAdvertisementDisabled(device)) continue; OutgoingSearchResponse message = new OutgoingSearchResponseRootDevice( getInputMessage(), getDescriptorLocation(activeStreamServer, device), device ); prepareOutgoingSearchResponse(message); getUpnpService().getRouter().send(message); } }
protected void sendSearchResponseUDN(UDN udn, NetworkAddress activeStreamServer) throws RouterException { Device device = getUpnpService().getRegistry().getDevice(udn, false); if (device != null && device instanceof LocalDevice) { if (isAdvertisementDisabled((LocalDevice)device)) return; log.fine("Responding to UDN device search: " + udn); OutgoingSearchResponse message = new OutgoingSearchResponseUDN( getInputMessage(), getDescriptorLocation(activeStreamServer, (LocalDevice) device), (LocalDevice) device ); prepareOutgoingSearchResponse(message); getUpnpService().getRouter().send(message); } }
protected StreamResponseMessage executeSync() throws RouterException { if (!getInputMessage().hasHostHeader()) { log.fine("Ignoring message, missing HOST header: " + getInputMessage()); return new StreamResponseMessage(new UpnpResponse(UpnpResponse.Status.PRECONDITION_FAILED)); } URI requestedURI = getInputMessage().getOperation().getURI(); Resource foundResource = getUpnpService().getRegistry().getResource(requestedURI); if (foundResource == null) { foundResource = onResourceNotFound(requestedURI); if (foundResource == null) { log.fine("No local resource found: " + getInputMessage()); return null; } } return createResponse(requestedURI, foundResource); }
protected OutgoingSubscribeResponseMessage executeSync() throws RouterException { ServiceEventSubscriptionResource resource = getUpnpService().getRegistry().getResource( ServiceEventSubscriptionResource.class, getInputMessage().getUri() ); if (resource == null) { log.fine("No local resource found: " + getInputMessage()); return null; } log.fine("Found local event subscription matching relative request URI: " + getInputMessage().getUri()); IncomingSubscribeRequestMessage requestMessage = new IncomingSubscribeRequestMessage(getInputMessage(), resource.getModel()); // Error conditions UDA 1.0 section 4.1.1 and 4.1.2 if (requestMessage.getSubscriptionId() != null && (requestMessage.hasNotificationHeader() || requestMessage.getCallbackURLs() != null)) { log.fine("Subscription ID and NT or Callback in subscribe request: " + getInputMessage()); return new OutgoingSubscribeResponseMessage(UpnpResponse.Status.BAD_REQUEST); } if (requestMessage.getSubscriptionId() != null) { return processRenewal(resource.getModel(), requestMessage); } else if (requestMessage.hasNotificationHeader() && requestMessage.getCallbackURLs() != null){ return processNewSubscription(resource.getModel(), requestMessage); } else { log.fine("No subscription ID, no NT or Callback, neither subscription or renewal: " + getInputMessage()); return new OutgoingSubscribeResponseMessage(UpnpResponse.Status.PRECONDITION_FAILED); } }