Skip to content

Commit 2b510f2

Browse files
committed
ServerSocketAcceptor can be closed. We need it in SharkMessenger.
1 parent 1743521 commit 2b510f2

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/main/java/net/sharksystem/asap/apps/TCPServerSocketAcceptor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@
1111

1212
public class TCPServerSocketAcceptor implements StreamPairCreatedListener {
1313
private final ASAPEncounterManager encounterManager;
14+
private final SocketFactory socketFactory;
1415

1516
public TCPServerSocketAcceptor(int portNumber, ASAPEncounterManager encounterManager) throws IOException {
1617
this.encounterManager = encounterManager;
17-
SocketFactory socketFactory = new SocketFactory(portNumber, this);
18+
this.socketFactory = new SocketFactory(portNumber, this);
1819

1920
Log.writeLog(this, "start socket factory - no race condition assumed");
2021
new Thread(socketFactory).start();
2122
}
2223

24+
public void close() throws IOException {
25+
this.socketFactory.close();
26+
}
27+
2328
@Override
2429
public void streamPairCreated(StreamPair streamPair) {
2530
Log.writeLog(this, "new stream pair created");

src/main/java/net/sharksystem/utils/tcp/SocketFactory.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//import net.sharksystem.asap.ASAPEncounterHelper;
44

5+
import net.sharksystem.utils.Log;
56
import net.sharksystem.utils.streams.StreamPairImpl;
67

78
import java.io.IOException;
@@ -13,6 +14,7 @@
1314

1415
public class SocketFactory implements Runnable {
1516
private final ServerSocket srv;
17+
private int port = 0;
1618
private StreamPairCreatedListener listener = null;
1719
InputStream is;
1820
OutputStream os;
@@ -21,24 +23,33 @@ public class SocketFactory implements Runnable {
2123

2224
public SocketFactory(int portNumber, StreamPairCreatedListener listener) throws IOException {
2325
this(new ServerSocket(portNumber));
26+
this.port = portNumber;
2427
this.listener = listener;
2528
}
2629

2730
public SocketFactory(ServerSocket srv) {
2831
this.srv = srv;
2932
}
3033

34+
/**
35+
* Close server socket - kills thread already running
36+
*/
37+
public void close() throws IOException {
38+
Log.writeLog(this, "close TCP server socket - do long longer accept connection attempts on port: " + this.port);
39+
this.srv.close();
40+
}
41+
3142
private boolean running = false;
3243
@Override
3344
public void run() {
3445
this.running = true;
35-
System.out.println("socket factory running");
46+
Log.writeLog(this,"socket factory running - accept connections on port: " + this.port);
3647
try {
3748
Socket socket = srv.accept();
3849
this.is = socket.getInputStream();
3950
this.os = socket.getOutputStream();
4051
this.remoteAddress = SocketFactory.getRemoteAddress(socket);
41-
System.out.println("socket created");
52+
Log.writeLog(this,"connection attempt accepted: socket created");
4253
if(this.waitForConnectionThread != null) {
4354
//this.waitForConnectionThread.interrupt();
4455
this.waitForConnectionThread.notify();

0 commit comments

Comments
 (0)