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.model.gena;
17  
18  import java.util.LinkedHashMap;
19  import java.util.Map;
20  
21  import org.fourthline.cling.model.UserConstants;
22  import org.fourthline.cling.model.meta.Service;
23  import org.fourthline.cling.model.state.StateVariableValue;
24  import org.fourthline.cling.model.types.UnsignedIntegerFourBytes;
25  
26  /**
27   * An established subscription, with identifer, expiration duration, sequence handling, and state variable values.
28   * <p>
29   * For every subscription, no matter if it's an incoming subscription to a local service,
30   * or a local control point subscribing to a remote servce, an instance is maintained by
31   * the {@link org.fourthline.cling.registry.Registry}.
32   * </p>
33   *
34   * @author Christian Bauer
35   */
36  public abstract class GENASubscription<S extends Service> {
37  
38      protected S service;
39      protected String subscriptionId;
40      protected int requestedDurationSeconds = UserConstants.DEFAULT_SUBSCRIPTION_DURATION_SECONDS;
41      protected int actualDurationSeconds;
42      protected UnsignedIntegerFourBytes currentSequence;
43      protected Map<String, StateVariableValue<S>> currentValues = new LinkedHashMap<>();
44  
45      /**
46       * Defaults to {@link org.fourthline.cling.model.UserConstants#DEFAULT_SUBSCRIPTION_DURATION_SECONDS}.
47       */
48      protected GENASubscription(S  service) {
49          this.service = service;
50      }
51  
52      public GENASubscription(S service, int requestedDurationSeconds) {
53          this(service);
54          this.requestedDurationSeconds = requestedDurationSeconds;
55      }
56  
57      synchronized public S getService() {
58          return service;
59      }
60  
61      synchronized public String getSubscriptionId() {
62          return subscriptionId;
63      }
64  
65      synchronized public void setSubscriptionId(String subscriptionId) {
66          this.subscriptionId = subscriptionId;
67      }
68  
69      synchronized public int getRequestedDurationSeconds() {
70          return requestedDurationSeconds;
71      }
72  
73      synchronized public int getActualDurationSeconds() {
74          return actualDurationSeconds;
75      }
76  
77      synchronized public void setActualSubscriptionDurationSeconds(int seconds) {
78          this.actualDurationSeconds = seconds;
79      }
80  
81      synchronized public UnsignedIntegerFourBytes getCurrentSequence() {
82          return currentSequence;
83      }
84  
85      synchronized public Map<String, StateVariableValue<S>> getCurrentValues() {
86          return currentValues;
87      }
88  
89      public abstract void established();
90      public abstract void eventReceived();
91  
92      @Override
93      public String toString() {
94          return "(GENASubscription, SID: " + getSubscriptionId() + ", SEQUENCE: " + getCurrentSequence() + ")";
95      }
96  }