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  package example.mediaserver;
16  
17  import org.fourthline.cling.binding.annotations.AnnotationLocalServiceBinder;
18  import org.fourthline.cling.controlpoint.ActionCallback;
19  import org.fourthline.cling.model.DefaultServiceManager;
20  import org.fourthline.cling.model.action.ActionInvocation;
21  import org.fourthline.cling.model.message.UpnpResponse;
22  import org.fourthline.cling.model.meta.LocalService;
23  import org.fourthline.cling.support.connectionmanager.ConnectionManagerService;
24  import org.fourthline.cling.support.connectionmanager.callback.GetProtocolInfo;
25  import org.fourthline.cling.support.model.ProtocolInfo;
26  import org.fourthline.cling.support.model.ProtocolInfos;
27  import org.seamless.util.MimeType;
28  import org.testng.annotations.Test;
29  
30  import static org.testng.Assert.assertEquals;
31  
32  /**
33   * A simple ConnectionManager for HTTP-GET
34   * <p>
35   * If your transmission protocol is based on GET requests with HTTP - that is, your
36   * media player will download or stream the media file from an HTTP server - all
37   * you need to provide with your <em>MediaServer:1</em> is a very simple
38   * <em>ConnectionManager:1</em>.
39   * </p>
40   * <p>
41   * This connection manager doesn't actually manage any connections, in fact, it doesn't
42   * have to provide any functionality at all. This is how you can create and bind this
43   * simple service with the Cling Support bundled <code>ConnectionManagerService</code>:
44   * </p>
45   * <a class="citation" href="javacode://this#retrieveProtocolInfo" style="include: BIND1;"/>
46   * <p>
47   * You can now add this service to your <em>MediaServer:1</em> device and everything will work.
48   * </p>
49   * <p>
50   * Many media servers however provide at least a list of "source" protocols. This list contains
51   * all the (MIME) protocol types your media server might potentially have resources for.
52   * A sink (renderer) would obtain this protocol information and decide upfront if
53   * any resource from your media server can be played at all, without having to browse
54   * the content and looking at each resource's type.
55   * </p>
56   * <p>
57   * First, create a list of protocol information that is supported:
58   * </p>
59   * <a class="citation" href="javacode://example.mediaserver.MediaServerSampleData#createSourceProtocols()" style="include: PROT;"/>
60   * <p>
61   * You now have to customize the instantiation of the connection manager service,
62   * passing the list of procotols as a constructor argument:
63   * </p>
64   * <a class="citation" href="javacode://this#retrieveProtocolInfo" style="include: BIND2;" id="bind2"/>
65   *
66   */
67  public class ConnectionManagerSimpleTest {
68  
69      @Test
70      public void retrieveProtocolInfo() {
71          final ProtocolInfos sourceProtocols = MediaServerSampleData.createSourceProtocols();
72  
73          LocalService<ConnectionManagerService> service =                                                // DOC: BIND1
74                  new AnnotationLocalServiceBinder().read(ConnectionManagerService.class);
75  
76          service.setManager(
77                  new DefaultServiceManager<>(
78                          service,
79                          ConnectionManagerService.class
80                  )
81          );                                                                                              // DOC: BIND1
82  
83          service = new AnnotationLocalServiceBinder().read(ConnectionManagerService.class);
84  
85          service.setManager(                                                                             // DOC: BIND2
86              new DefaultServiceManager<ConnectionManagerService>(service, null) {
87                  @Override
88                  protected ConnectionManagerService createServiceInstance() throws Exception {
89                      return new ConnectionManagerService(sourceProtocols, null);
90                  }
91              }
92          );                                                                                              // DOC: BIND2
93  
94          final boolean[] assertions = new boolean[1];
95  
96          ActionCallback getProtInfo = new GetProtocolInfo(service) {                                     // DOC: CALL
97  
98              @Override
99              public void received(ActionInvocation actionInvocation,
100                                  ProtocolInfos sinkProtocolInfos,
101                                  ProtocolInfos sourceProtocolInfos) {
102 
103                 assertEquals(sourceProtocolInfos.size(), 2);
104                 assertEquals(
105                         sourceProtocolInfos.get(0).getContentFormatMimeType(),
106                         MimeType.valueOf("audio/mpeg")
107                 );
108 
109                 MimeType supportedMimeType = MimeType.valueOf("video/mpeg");
110 
111                 for (ProtocolInfo source : sourceProtocolInfos) {
112                     if (source.getContentFormatMimeType().isCompatible(supportedMimeType))
113                         // ... It's supported!
114                         assertions[0] = true; // DOC: EXC1
115                 }
116             }
117 
118             @Override
119             public void failure(ActionInvocation invocation,
120                                 UpnpResponse operation,
121                                 String defaultMsg) {
122                 // Something is wrong
123             }
124         };                                                                                              // DOC: CALL
125 
126         getProtInfo.run();
127         
128         for (boolean assertion : assertions) {
129             assertEquals(assertion, true);
130         }
131     }
132 }