1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix.tipc;
19
20 import java.io.FileDescriptor;
21 import java.io.IOException;
22 import java.math.BigInteger;
23 import java.net.Socket;
24 import java.net.SocketException;
25
26 import org.newsclub.net.unix.AFSocket;
27 import org.newsclub.net.unix.AFSocketCapability;
28 import org.newsclub.net.unix.AFSocketFactory;
29 import org.newsclub.net.unix.AFTIPCSocketAddress;
30 import org.newsclub.net.unix.AFTIPCSocketImplExtensions;
31
32
33
34
35
36
37 public final class AFTIPCSocket extends AFSocket<AFTIPCSocketAddress> implements
38 AFTIPCSocketExtensions {
39 private static AFTIPCSocketImplExtensions staticExtensions = null;
40
41 AFTIPCSocket(FileDescriptor fdObj, AFSocketFactory<AFTIPCSocketAddress> factory)
42 throws SocketException {
43 super(new AFTIPCSocketImpl(fdObj), factory);
44 }
45
46 private static synchronized AFTIPCSocketImplExtensions getStaticImplExtensions()
47 throws IOException {
48 if (staticExtensions == null) {
49 try (AFTIPCSocket socket = new AFTIPCSocket(null, null)) {
50 staticExtensions = (AFTIPCSocketImplExtensions) socket.getImplExtensions();
51 }
52 }
53 return staticExtensions;
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public static boolean isSupported() {
69 return AFSocket.isSupported() && AFSocket.supports(AFSocketCapability.CAPABILITY_TIPC);
70 }
71
72 @Override
73 protected AFTIPCSocketChannel newChannel() {
74 return new AFTIPCSocketChannel(this);
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88 public static AFTIPCSocket newInstance() throws IOException {
89 return (AFTIPCSocket) AFSocket.newInstance(AFTIPCSocket::new, (AFTIPCSocketFactory) null);
90 }
91
92 static AFTIPCSocket newInstance(AFTIPCSocketFactory factory) throws SocketException {
93 return (AFTIPCSocket) AFSocket.newInstance(AFTIPCSocket::new, factory);
94 }
95
96
97
98
99
100
101
102
103
104
105 public static AFTIPCSocket newStrictInstance() throws IOException {
106 return (AFTIPCSocket) AFSocket.newInstance(AFTIPCSocket::new, (AFTIPCSocketFactory) null);
107 }
108
109
110
111
112
113
114
115
116 public static AFTIPCSocket connectTo(AFTIPCSocketAddress addr) throws IOException {
117 return (AFTIPCSocket) AFSocket.connectTo(AFTIPCSocket::new, addr);
118 }
119
120 @Override
121 public AFTIPCSocketChannel getChannel() {
122 return (AFTIPCSocketChannel) super.getChannel();
123 }
124
125
126
127
128
129
130
131
132 public static void main(String[] args) {
133
134
135 System.out.print(AFTIPCSocket.class.getName() + ".isSupported(): ");
136 System.out.flush();
137 System.out.println(AFTIPCSocket.isSupported());
138 }
139
140 @Override
141 public AFTIPCErrInfo getErrInfo() {
142 return AFTIPCErrInfo.of(((AFTIPCSocketImplExtensions) getImplExtensions()).getTIPCErrInfo());
143 }
144
145 @Override
146 public AFTIPCDestName getDestName() {
147 return AFTIPCDestName.of(((AFTIPCSocketImplExtensions) getImplExtensions()).getTIPCDestName());
148 }
149
150
151
152
153
154
155
156
157 public static byte[] getNodeIdentity(int peerId) throws IOException {
158 return getStaticImplExtensions().getTIPCNodeId(peerId);
159 }
160
161
162
163
164
165
166
167
168 public byte[] getNodeIdentity(AFTIPCSocketAddress address) throws IOException {
169 return AFTIPCSocket.getNodeIdentity(address.getTIPCNodeHash());
170 }
171
172
173
174
175
176
177
178
179 public static String getNodeIdHexString(int peerId) throws IOException {
180 byte[] id = getNodeIdentity(peerId);
181 return id == null ? null : new BigInteger(1, id).toString(16);
182 }
183
184
185
186
187
188
189
190
191
192 public static String getLinkName(int peerId, int bearerId) throws IOException {
193 return getStaticImplExtensions().getTIPCLinkName(peerId, bearerId);
194 }
195 }