Skip to content

Commit b56ef25

Browse files
Merge pull request #40 from clightning4j/dev
Refactoring unix socket wrapper
2 parents e2ab9ce + 8245949 commit b56ef25

1 file changed

Lines changed: 39 additions & 87 deletions

File tree

src/main/java/jrpc/service/socket/UnixDomainSocketRpc.java

Lines changed: 39 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
import java.io.*;
2020
import java.lang.reflect.Type;
21-
import java.net.InetSocketAddress;
2221
import java.net.SocketException;
23-
import jrpc.clightning.exceptions.CLightningException;
2422
import jrpc.exceptions.ServiceException;
2523
import jrpc.service.CLightningLogger;
2624
import jrpc.service.converters.IConverter;
@@ -34,11 +32,8 @@ public abstract class UnixDomainSocketRpc implements ISocket {
3432
private static final Class TAG = UnixDomainSocketRpc.class;
3533
protected static final String ENCODING = "UTF-8";
3634

37-
protected AFUNIXSocket socket;
38-
protected InputStream inputStream;
39-
protected OutputStream outputStream;
4035
protected IConverter converterJson;
41-
protected String pathSocket;
36+
protected File socketFile;
4237

4338
public UnixDomainSocketRpc(String pathSocket) {
4439
if (pathSocket == null || pathSocket.isEmpty()) {
@@ -48,111 +43,68 @@ public UnixDomainSocketRpc(String pathSocket) {
4843
throw new ServiceException("Path socket is empty");
4944
}
5045
}
51-
File file = new File(pathSocket);
52-
this.pathSocket = pathSocket;
46+
this.socketFile = new File(pathSocket);
47+
this.converterJson = new JsonConverter();
48+
}
49+
50+
private AFUNIXSocket makeSocket() {
5351
try {
54-
this.socket = AFUNIXSocket.newInstance();
55-
this.socket.connect(AFUNIXSocketAddress.of(file));
56-
this.inputStream = socket.getInputStream();
57-
this.outputStream = socket.getOutputStream();
58-
this.converterJson = new JsonConverter();
52+
var socket = AFUNIXSocket.newInstance();
53+
socket.connect(AFUNIXSocketAddress.of(this.socketFile));
54+
return socket;
5955
} catch (IOException e) {
60-
CLightningLogger.getInstance().error(TAG, e.getMessage());
61-
throw new ServiceException(
62-
"Exception inside the method deserialization to "
63-
+ this.getClass().getSimpleName()
64-
+ " with message\n"
65-
+ e.getLocalizedMessage());
56+
var message =
57+
String.format(
58+
"Exception generated to doCall method of the class %s, with message: %s",
59+
this.getClass().getSimpleName(), e.getLocalizedMessage());
60+
CLightningLogger.getInstance().error(TAG, message);
61+
throw new ServiceException(message);
6662
}
6763
}
6864

6965
@Override
7066
public int getReceiveBufferSize() throws SocketException {
71-
return socket.getReceiveBufferSize();
67+
return -1;
7268
}
7369

7470
@Override
75-
public void close() throws ServiceException {
76-
try {
77-
if (socket.isClosed()) {
78-
socket.shutdownInput();
79-
socket.shutdownOutput();
80-
socket.close();
81-
}
82-
} catch (IOException e) {
83-
throw new ServiceException(
84-
"Exception generated to doCall method of the class "
85-
+ this.getClass().getSimpleName()
86-
+ " with message\n"
87-
+ e.getLocalizedMessage());
88-
}
89-
}
71+
public void close() throws ServiceException {}
9072

9173
@Override
9274
public boolean isOpen() {
93-
return socket != null && !socket.isClosed();
75+
return false;
9476
}
9577

78+
/**
79+
* We create a new socket each time because the socket can take time to answer, and we can receive
80+
* two call at the same time, and we can not the same socket because the input message need to be
81+
* for the message sent.
82+
*/
9683
@Override
9784
public Object doCall(IWrapperSocketCall wrapperSocket, Type typeResult) throws ServiceException {
9885
if (wrapperSocket == null) {
9986
throw new IllegalArgumentException("The argument wrapperSocket is null");
10087
}
101-
if (socket.isClosed()) {
102-
try {
103-
CLightningLogger.getInstance().debug(TAG, "UnixDomainSocketRpc: path is " + pathSocket);
104-
File fileRPC = new File(pathSocket);
105-
if (fileRPC.exists()) {
106-
InetSocketAddress socketAddress = AFUNIXSocketAddress.of(fileRPC);
107-
this.socket = AFUNIXSocket.newInstance();
108-
this.socket.connect(socketAddress);
109-
this.inputStream = socket.getInputStream();
110-
this.outputStream = socket.getOutputStream();
111-
} else {
112-
CLightningLogger.getInstance()
113-
.error(TAG, "File not exist inside the path: " + pathSocket);
114-
throw new CLightningException("File not exist inside the path: " + pathSocket);
115-
}
116-
} catch (IOException e) {
117-
throw new ServiceException(
118-
"Exception generated to doCall method of the class "
119-
+ this.getClass().getSimpleName()
120-
+ " with message \n"
121-
+ e.getLocalizedMessage());
122-
}
123-
}
88+
12489
String serializationForm = converterJson.serialization(wrapperSocket);
12590
CLightningLogger.getInstance().debug(TAG, "Request: \n" + serializationForm);
91+
var socket = makeSocket();
12692
try {
127-
this.outputStream.write(serializationForm.getBytes(ENCODING));
128-
this.outputStream.flush();
129-
} catch (IOException e) {
130-
throw new ServiceException(
131-
"Exception generated to doCall method of the class "
132-
+ this.getClass().getSimpleName()
133-
+ " with message \n"
134-
+ e.getLocalizedMessage());
135-
}
136-
Object o = converterJson.deserialization(inputStream, typeResult);
137-
CLightningLogger.getInstance().debug(TAG, "Response\n" + converterJson.serialization(o));
138-
try {
139-
this.socket.close();
93+
OutputStream outputStream = socket.getOutputStream();
94+
outputStream.write(serializationForm.getBytes(ENCODING));
95+
outputStream.flush();
96+
97+
InputStream inputStream = socket.getInputStream();
98+
Object result = converterJson.deserialization(inputStream, typeResult);
99+
CLightningLogger.getInstance().debug(TAG, "Response\n" + converterJson.serialization(result));
100+
socket.close();
101+
return result;
140102
} catch (IOException e) {
141-
throw new ServiceException(
142-
"Exception generated to doCall method of the class "
143-
+ this.getClass().getSimpleName()
144-
+ " with message \n"
145-
+ e.getLocalizedMessage());
103+
var message =
104+
String.format(
105+
"Exception generated to doCall method of the class %s, with message: %s",
106+
this.getClass().getSimpleName(), e.getLocalizedMessage());
107+
throw new ServiceException(message);
146108
}
147-
return o;
148-
}
149-
150-
// get and setter
151-
public InputStream getInputStream() {
152-
return inputStream;
153-
}
154-
155-
public OutputStream getOutputStream() {
156-
return outputStream;
157109
}
158110
}

0 commit comments

Comments
 (0)