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.support.model;
17  
18  import org.fourthline.cling.model.ServiceReference;
19  
20  /**
21   * Immutable type encapsulating the state of a single connection.
22   *
23   * @author Alessio Gaeta
24   * @author Christian Bauer
25   */
26  public class ConnectionInfo {
27  
28      public enum Status {
29          OK,
30          ContentFormatMismatch,
31          InsufficientBandwidth,
32          UnreliableChannel,
33          Unknown
34      }
35  
36      public enum Direction {
37          Output,
38          Input;
39  
40          public Direction getOpposite() {
41              return this.equals(Output) ? Input : Output;
42          }
43  
44      }
45  
46      final protected int connectionID;
47  
48      final protected int rcsID;
49      final protected int avTransportID;
50  
51      final protected ProtocolInfo protocolInfo;
52  
53      final protected ServiceReference peerConnectionManager;
54      final protected int peerConnectionID;
55  
56      final protected Direction direction;
57      protected Status connectionStatus = Status.Unknown;
58  
59      /**
60       * Creates a default instance with values expected for the default connection ID "0".
61       * <p>
62       * The ConnectionManager 1.0 specification says:
63       * </p>
64       * <p>
65       * If optional action PrepareForConnection is not implemented then (limited) connection
66       * information can be retrieved for ConnectionID 0. The device should return all known
67       * information:
68       * </p>
69       * <ul>
70       * <li>RcsID should be 0 or -1</li>
71       * <li>AVTransportID should be 0 or -1</li>
72       * <li>ProtocolInfo should contain accurate information if it is known, otherwhise
73       *     it should be NULL (empty string)</li>
74       * <li>PeerConnectionManager should be NULL (empty string)</li>
75       * <li>PeerConnectionID should be -1</li>
76       * <li>Direction should be Input or Output</li>
77       * <li>Status should be OK or Unknown</li>
78       * </ul>
79       */
80      public ConnectionInfo() {
81          this(0, 0, 0, null, null, -1, Direction.Input, Status.Unknown);
82      }
83  
84  
85      public ConnectionInfo(int connectionID,
86                            int rcsID, int avTransportID,
87                            ProtocolInfo protocolInfo,
88                            ServiceReference peerConnectionManager, int peerConnectionID,
89                            Direction direction, Status connectionStatus) {
90          this.connectionID = connectionID;
91          this.rcsID = rcsID;
92          this.avTransportID = avTransportID;
93          this.protocolInfo = protocolInfo;
94          this.peerConnectionManager = peerConnectionManager;
95          this.peerConnectionID = peerConnectionID;
96          this.direction = direction;
97          this.connectionStatus = connectionStatus;
98      }
99  
100     public int getConnectionID() {
101         return connectionID;
102     }
103 
104     public int getRcsID() {
105         return rcsID;
106     }
107 
108     public int getAvTransportID() {
109         return avTransportID;
110     }
111 
112     public ProtocolInfo getProtocolInfo() {
113         return protocolInfo;
114     }
115 
116     public ServiceReference getPeerConnectionManager() {
117         return peerConnectionManager;
118     }
119 
120     public int getPeerConnectionID() {
121         return peerConnectionID;
122     }
123 
124     public Direction getDirection() {
125         return direction;
126     }
127 
128     synchronized public Status getConnectionStatus() {
129         return connectionStatus;
130     }
131 
132     synchronized public void setConnectionStatus(Status connectionStatus) {
133         this.connectionStatus = connectionStatus;
134     }
135 
136     @Override
137     public boolean equals(Object o) {
138         if (this == o) return true;
139         if (o == null || getClass() != o.getClass()) return false;
140 
141         ConnectionInfo that = (ConnectionInfo) o;
142 
143         if (avTransportID != that.avTransportID) return false;
144         if (connectionID != that.connectionID) return false;
145         if (peerConnectionID != that.peerConnectionID) return false;
146         if (rcsID != that.rcsID) return false;
147         if (connectionStatus != that.connectionStatus) return false;
148         if (direction != that.direction) return false;
149         if (peerConnectionManager != null ? !peerConnectionManager.equals(that.peerConnectionManager) : that.peerConnectionManager != null)
150             return false;
151         if (protocolInfo != null ? !protocolInfo.equals(that.protocolInfo) : that.protocolInfo != null) return false;
152 
153         return true;
154     }
155 
156     @Override
157     public int hashCode() {
158         int result = connectionID;
159         result = 31 * result + rcsID;
160         result = 31 * result + avTransportID;
161         result = 31 * result + (protocolInfo != null ? protocolInfo.hashCode() : 0);
162         result = 31 * result + (peerConnectionManager != null ? peerConnectionManager.hashCode() : 0);
163         result = 31 * result + peerConnectionID;
164         result = 31 * result + direction.hashCode();
165         result = 31 * result + connectionStatus.hashCode();
166         return result;
167     }
168 
169     @Override
170     public String toString() {
171         return "(" + getClass().getSimpleName() + ") ID: " + getConnectionID() + ", Status: " + getConnectionStatus();
172     }
173 }