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.IOException;
21  import java.net.Socket;
22  import java.net.SocketException;
23  
24  import org.newsclub.net.unix.AFSocketFactory;
25  import org.newsclub.net.unix.AFTIPCSocketAddress;
26  import org.newsclub.net.unix.AFTIPCSocketAddress.Scope;
27  
28  /**
29   * The base for a SocketFactory that connects to TIPC sockets.
30   *
31   * Typically, the "hostname" is used as a reference to a socketFile on the file system. The actual
32   * mapping is left to the implementor.
33   */
34  public abstract class AFTIPCSocketFactory extends AFSocketFactory<AFTIPCSocketAddress> {
35    /**
36     * Creates a {@link AFTIPCSocketFactory}.
37     */
38    protected AFTIPCSocketFactory() {
39      super(AFTIPCSocketAddress.class);
40    }
41  
42    @Override
43    public final Socket createSocket() throws SocketException {
44      return configure(AFTIPCSocket.newInstance(this));
45    }
46  
47    @Override
48    protected final AFTIPCSocket connectTo(AFTIPCSocketAddress addr) throws IOException {
49      return configure(AFTIPCSocket.connectTo(addr));
50    }
51  
52    /**
53     * Performs some optional configuration on a newly created socket.
54     *
55     * @param sock The socket.
56     * @return The very socket.
57     * @throws SocketException on error.
58     */
59    protected AFTIPCSocket configure(AFTIPCSocket sock) throws SocketException {
60      return sock;
61    }
62  
63    /**
64     * Always connects sockets to the given TIPC type and instance.
65     *
66     * @author Christian Kohlschütter
67     */
68    public static class ServiceAddress extends AFTIPCSocketFactory {
69      private final int type;
70      private final int instance;
71  
72      /**
73       * Creates an {@link AFTIPCSocketFactory} that always uses the given TIPC service type and
74       * instance, implying cluster scope.
75       *
76       * @param type The service type.
77       * @param instance The service instance.
78       */
79      public ServiceAddress(int type, int instance) {
80        super();
81        this.type = type;
82        this.instance = instance;
83      }
84  
85      @Override
86      public final AFTIPCSocketAddress addressFromHost(String host, int port) throws SocketException {
87        return AFTIPCSocketAddress.ofService(port, Scope.SCOPE_CLUSTER, type, instance, 0);
88      }
89    }
90  }