View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.controlpoint;
17  
18  import org.fourthline.cling.UpnpServiceConfiguration;
19  import org.fourthline.cling.controlpoint.event.ExecuteAction;
20  import org.fourthline.cling.controlpoint.event.Search;
21  import org.fourthline.cling.model.message.header.MXHeader;
22  import org.fourthline.cling.model.message.header.STAllHeader;
23  import org.fourthline.cling.model.message.header.UpnpHeader;
24  import org.fourthline.cling.protocol.ProtocolFactory;
25  import org.fourthline.cling.registry.Registry;
26  
27  import javax.enterprise.context.ApplicationScoped;
28  import javax.enterprise.event.Observes;
29  import javax.inject.Inject;
30  import java.util.concurrent.ExecutorService;
31  import java.util.concurrent.Future;
32  import java.util.logging.Logger;
33  
34  /**
35   * Default implementation.
36   * <p>
37   * This implementation uses the executor returned by
38   * {@link org.fourthline.cling.UpnpServiceConfiguration#getSyncProtocolExecutorService()}.
39   * </p>
40   *
41   * @author Christian Bauer
42   */
43  @ApplicationScoped
44  public class ControlPointImpl implements ControlPoint {
45  
46      private static Logger log = Logger.getLogger(ControlPointImpl.class.getName());
47  
48      protected UpnpServiceConfiguration configuration;
49      protected ProtocolFactory protocolFactory;
50      protected Registry registry;
51  
52      protected ControlPointImpl() {
53      }
54  
55      @Inject
56      public ControlPointImpl(UpnpServiceConfiguration configuration, ProtocolFactory protocolFactory, Registry registry) {
57          log.fine("Creating ControlPoint: " + getClass().getName());
58          
59          this.configuration = configuration;
60          this.protocolFactory = protocolFactory;
61          this.registry = registry;
62      }
63  
64      public UpnpServiceConfiguration getConfiguration() {
65          return configuration;
66      }
67  
68      public ProtocolFactory getProtocolFactory() {
69          return protocolFactory;
70      }
71  
72      public Registry getRegistry() {
73          return registry;
74      }
75  
76      public void search(@Observes Search search) {
77          search(search.getSearchType(), search.getMxSeconds());
78      }
79  
80      public void search() {
81          search(new STAllHeader(), MXHeader.DEFAULT_VALUE);
82      }
83  
84      public void search(UpnpHeader searchType) {
85          search(searchType, MXHeader.DEFAULT_VALUE);
86      }
87  
88      public void search(int mxSeconds) {
89          search(new STAllHeader(), mxSeconds);
90      }
91  
92      public void search(UpnpHeader searchType, int mxSeconds) {
93          log.fine("Sending asynchronous search for: " + searchType.getString());
94          getConfiguration().getAsyncProtocolExecutor().execute(
95                  getProtocolFactory().createSendingSearch(searchType, mxSeconds)
96          );
97      }
98  
99      public void execute(ExecuteAction executeAction) {
100         execute(executeAction.getCallback());
101     }
102 
103     public Future execute(ActionCallback callback) {
104         log.fine("Invoking action in background: " + callback);
105         callback.setControlPoint(this);
106         ExecutorService executor = getConfiguration().getSyncProtocolExecutorService();
107         return executor.submit(callback);
108     }
109 
110     public void execute(SubscriptionCallback callback) {
111         log.fine("Invoking subscription in background: " + callback);
112         callback.setControlPoint(this);
113         getConfiguration().getSyncProtocolExecutorService().execute(callback);
114     }
115 }