View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.newsclub.net.unix.tipc;
19  
20  import java.io.Serializable;
21  import java.net.SocketException;
22  
23  import org.newsclub.net.unix.AFTIPCSocketAddress;
24  import org.newsclub.net.unix.AFTIPCSocketAddress.Scope;
25  
26  /**
27   * The TIPC-specific DestName response that may be included as ancillary data.
28   *
29   * @author Christian Kohlschütter
30   */
31  public final class AFTIPCDestName implements Serializable {
32    private static final long serialVersionUID = 1L;
33    /**
34     * Type value.
35     */
36    private final int type;
37  
38    /**
39     * Lower service range value.
40     */
41    private final int lower;
42  
43    /**
44     * Upper service range value.
45     */
46    private final int upper;
47  
48    /**
49     * Creates a new instance.
50     *
51     * @param type The "type" value.
52     * @param lower The "lower" service range value (or the service "instance" if {@code lower} and
53     *          {@code upper} are the same).
54     * @param upper The "upper" service range value (or the service "instance" if {@code lower} and
55     *          {@code upper} are the same).
56     */
57    public AFTIPCDestName(int type, int lower, int upper) {
58      this.type = type;
59      this.lower = lower;
60      this.upper = upper;
61    }
62  
63    /**
64     * Returns the "type" value.
65     *
66     * @return The type.
67     */
68    public int getType() {
69      return type;
70    }
71  
72    /**
73     * Returns the "lower" value of the service range (or the service "instance" if identical to the
74     * upper value).
75     *
76     * @return The lower value.
77     */
78    public int getLower() {
79      return lower;
80    }
81  
82    /**
83     * Returns the "upper" value of the service range (or the service "instance" if identical to the
84     * lower value).
85     *
86     * @return The upper value.
87     */
88    public int getUpper() {
89      return upper;
90    }
91  
92    /**
93     * Checks if this DestName describes a service range (as opposed to a service) address.
94     *
95     * @return {@code true} if the {@link #getLower()} value is different from the {@link #getUpper()}
96     *         value.
97     */
98    public boolean isServiceRange() {
99      return lower != upper;
100   }
101 
102   /**
103    * Converts this DestName to a proper {@link AFTIPCSocketAddress}, by using the given
104    * {@link Scope} (which is otherwise not included).
105    *
106    * @param scope The scope to use.
107    * @param alwaysRange If {@code true}, a service range address is even returned when a service
108    *          address would suffice.
109    * @return The address.
110    */
111   public AFTIPCSocketAddress toSocketAddress(Scope scope, boolean alwaysRange) {
112     try {
113       if (alwaysRange || isServiceRange()) {
114         return AFTIPCSocketAddress.ofServiceRange(scope, type, lower, upper);
115       } else {
116         return AFTIPCSocketAddress.ofService(scope, type, lower);
117       }
118     } catch (SocketException e) {
119       throw new IllegalStateException(e);
120     }
121   }
122 
123   @SuppressWarnings("PMD.ShortMethodName")
124   static AFTIPCDestName of(int[] tipcDestName) {
125     if (tipcDestName == null) {
126       return null;
127     }
128     if (tipcDestName.length != 3) {
129       throw new IllegalArgumentException();
130     }
131     return new AFTIPCDestName(tipcDestName[0], tipcDestName[1], tipcDestName[2]);
132   }
133 }