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 example.localservice;
17  
18  import org.fourthline.cling.binding.annotations.UpnpAction;
19  import org.fourthline.cling.binding.annotations.UpnpInputArgument;
20  import org.fourthline.cling.binding.annotations.UpnpOutputArgument;
21  import org.fourthline.cling.binding.annotations.UpnpService;
22  import org.fourthline.cling.binding.annotations.UpnpServiceId;
23  import org.fourthline.cling.binding.annotations.UpnpServiceType;
24  import org.fourthline.cling.binding.annotations.UpnpStateVariable;
25  import org.fourthline.cling.binding.annotations.UpnpStateVariables;
26  import org.fourthline.cling.model.profile.RemoteClientInfo;
27  
28  @UpnpService(
29      serviceId = @UpnpServiceId("SwitchPower"),
30      serviceType = @UpnpServiceType(value = "SwitchPower", version = 1)
31  )
32  @UpnpStateVariables(
33      {
34          @UpnpStateVariable(
35              name = "Target",
36              defaultValue = "0",
37              sendEvents = false
38          ),
39          @UpnpStateVariable(
40              name = "Status",
41              defaultValue = "0"
42          )
43      }
44  )
45  public class SwitchPowerWithClientInfo {
46  
47      private boolean power;
48  
49      // DOC:CLIENT_INFO
50      @UpnpAction
51      public void setTarget(@UpnpInputArgument(name = "NewTargetValue")
52                            boolean newTargetValue,
53                            RemoteClientInfo clientInfo) {
54          power = newTargetValue;
55          System.out.println("Switch is: " + power);
56  
57          if (clientInfo != null) {
58              System.out.println(
59                  "Client's address is: " + clientInfo.getRemoteAddress()
60              );
61              System.out.println(
62                  "Received message on: " + clientInfo.getLocalAddress()
63              );
64              System.out.println(
65                  "Client's user agent is: " + clientInfo.getRequestUserAgent()
66              );
67              System.out.println(
68                  "Client's custom header is: " +
69                  clientInfo.getRequestHeaders().getFirstHeader("X-MY-HEADER")
70              );
71  
72              // Return some extra headers in the response
73              clientInfo.getExtraResponseHeaders().add("X-MY-HEADER", "foobar");
74          }
75      }
76      // DOC:CLIENT_INFO
77  
78      @UpnpAction(out = @UpnpOutputArgument(name = "RetTargetValue"))
79      public boolean getTarget() {
80          return power;
81      }
82  
83      @UpnpAction(out = @UpnpOutputArgument(name = "ResultStatus"))
84      public boolean getStatus() {
85          return power;
86      }
87  }                                                                               // DOC:INC1