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.util.Objects;
22  
23  import org.eclipse.jdt.annotation.NonNull;
24  import org.eclipse.jdt.annotation.NonNullByDefault;
25  import org.newsclub.net.unix.NamedInteger;
26  
27  /**
28   * The TIPC-specific error info response that may be included as ancillary data.
29   *
30   * @author Christian Kohlschütter
31   */
32  public final class AFTIPCErrInfo implements Serializable {
33    private static final long serialVersionUID = 1L;
34  
35    /**
36     * Error code.
37     */
38    private final ErrorCode errorCode;
39  
40    /**
41     * The length of the returned data.
42     */
43    private final int dataLength;
44  
45    /**
46     * Some TIPC error code.
47     *
48     * @author Christian Kohlschütter
49     */
50    @NonNullByDefault
51    public static final class ErrorCode extends NamedInteger {
52      private static final long serialVersionUID = 1L;
53  
54      /**
55       * No error.
56       */
57      public static final ErrorCode TIPC_OK;
58  
59      /**
60       * Destination port name is unknown.
61       */
62      public static final ErrorCode TIPC_ERR_NO_NAME;
63  
64      /**
65       * Destination port id does not exist.
66       */
67      public static final ErrorCode TIPC_ERR_NO_PORT;
68  
69      /**
70       * Destination node is unreachable.
71       */
72      public static final ErrorCode TIPC_ERR_NO_NODE;
73  
74      /**
75       * Destination is congested.
76       */
77      public static final ErrorCode TIPC_ERR_OVERLOAD;
78  
79      /**
80       * Normal connection shutdown occurred.
81       */
82      public static final ErrorCode TIPC_ERR_CONN_SHUTDOWN;
83  
84      private static final @NonNull ErrorCode[] VALUES = init(new @NonNull ErrorCode[] {
85          TIPC_OK = new ErrorCode("TIPC_OK", 0), //
86          TIPC_ERR_NO_NAME = new ErrorCode("TIPC_ERR_NO_NAME", 1), //
87          TIPC_ERR_NO_PORT = new ErrorCode("TIPC_ERR_NO_PORT", 2), //
88          TIPC_ERR_NO_NODE = new ErrorCode("TIPC_ERR_NO_NODE", 3), //
89          TIPC_ERR_OVERLOAD = new ErrorCode("TIPC_ERR_OVERLOAD", 4), //
90          TIPC_ERR_CONN_SHUTDOWN = new ErrorCode("TIPC_ERR_CONN_SHUTDOWN", 5), //
91      });
92  
93      private ErrorCode(int id) {
94        super(id);
95      }
96  
97      private ErrorCode(String name, int id) {
98        super(name, id);
99      }
100 
101     /**
102      * Returns an {@link ErrorCode} instance given an integer.
103      *
104      * @param v The value.
105      * @return The instance.
106      */
107     public static ErrorCode ofValue(int v) {
108       return ofValue(VALUES, ErrorCode::new, v);
109     }
110   }
111 
112   /**
113    * Creates a new instance.
114    *
115    * @param errorCode The error code.
116    * @param dataLength The length of the returned data.
117    */
118   public AFTIPCErrInfo(ErrorCode errorCode, int dataLength) {
119     this.errorCode = errorCode;
120     this.dataLength = dataLength;
121   }
122 
123   /**
124    * Returns the error code.
125    *
126    * @return The error code.
127    */
128   public ErrorCode getErrorCode() {
129     return errorCode;
130   }
131 
132   /**
133    * The length of the corresponding data.
134    *
135    * @return The length in bytes.
136    */
137   public int getDataLength() {
138     return dataLength;
139   }
140 
141   @Override
142   public String toString() {
143     return getClass().getName() + "(" + errorCode + ";dataLength=" + dataLength + ")";
144   }
145 
146   @Override
147   public int hashCode() {
148     return Objects.hash(dataLength, errorCode);
149   }
150 
151   @Override
152   public boolean equals(Object obj) {
153     if (this == obj) {
154       return true;
155     } else if (obj == null) {
156       return false;
157     } else if (getClass() != obj.getClass()) {
158       return false;
159     }
160     AFTIPCErrInfo other = (AFTIPCErrInfo) obj;
161     return dataLength == other.dataLength && errorCode.equals(other.errorCode);
162   }
163 
164   @SuppressWarnings("PMD.ShortMethodName")
165   static AFTIPCErrInfo of(int[] tipcErrInfo) {
166     if (tipcErrInfo == null) {
167       return null;
168     }
169     if (tipcErrInfo.length != 2) {
170       throw new IllegalArgumentException();
171     }
172     return new AFTIPCErrInfo(ErrorCode.ofValue(tipcErrInfo[0]), tipcErrInfo[1]);
173   }
174 }