• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bernardladenthin / BitcoinAddressFinder / #359

29 Jul 2025 11:07AM UTC coverage: 72.779% (+0.2%) from 72.611%
#359

push

bernardladenthin
Add WebSocket.

35 of 41 new or added lines in 5 files covered. (85.37%)

1679 of 2307 relevant lines covered (72.78%)

0.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.25
/src/main/java/net/ladenthin/bitcoinaddressfinder/keyproducer/KeyProducerJavaWebSocket.java
1
// @formatter:off
2
/**
3
 * Copyright 2025 Bernard Ladenthin bernard.ladenthin@gmail.com
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *    http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
// @formatter:on
19
package net.ladenthin.bitcoinaddressfinder.keyproducer;
20

21
import net.ladenthin.bitcoinaddressfinder.KeyUtility;
22
import net.ladenthin.bitcoinaddressfinder.BitHelper;
23
import net.ladenthin.bitcoinaddressfinder.PublicKeyBytes;
24
import net.ladenthin.bitcoinaddressfinder.configuration.CKeyProducerJavaWebSocket;
25
import org.java_websocket.WebSocket;
26
import org.java_websocket.server.WebSocketServer;
27
import org.java_websocket.handshake.ClientHandshake;
28
import org.slf4j.Logger;
29

30
import java.net.InetSocketAddress;
31
import java.nio.ByteBuffer;
32
import java.util.concurrent.Executors;
33

34
public class KeyProducerJavaWebSocket extends AbstractKeyProducerQueueBuffered<CKeyProducerJavaWebSocket> {
35

36
    private WebSocketServer webSocketServer;
37

38
    public KeyProducerJavaWebSocket(CKeyProducerJavaWebSocket config, KeyUtility keyUtility, BitHelper bitHelper, Logger logger) {
39
        super(config, keyUtility, logger);
1✔
40
        initWebSocketServer();
1✔
41
    }
1✔
42

43
    private void initWebSocketServer() {
44
        webSocketServer = new WebSocketServer(new InetSocketAddress(cKeyProducerJava.getPort())) {
1✔
45
            @Override
46
            public void onOpen(WebSocket conn, ClientHandshake handshake) {
47
                logger.info("WebSocket connection opened from: {}", conn.getRemoteSocketAddress());
1✔
48
            }
1✔
49

50
            @Override
51
            public void onClose(WebSocket conn, int code, String reason, boolean remote) {
52
                logger.info("WebSocket closed: {}", conn.getRemoteSocketAddress());
1✔
53
            }
1✔
54

55
            @Override
56
            public void onMessage(WebSocket conn, ByteBuffer message) {
57
                if (shouldStop) return;
1✔
58
                if (message.remaining() == PublicKeyBytes.PRIVATE_KEY_MAX_NUM_BYTES) {
1✔
59
                    byte[] secret = new byte[PublicKeyBytes.PRIVATE_KEY_MAX_NUM_BYTES];
1✔
60
                    message.get(secret);
1✔
61
                    addSecret(secret);
1✔
62
                } else {
1✔
63
                    logger.warn("Invalid message length: {}", message.remaining());
1✔
64
                }
65
            }
1✔
66

67
            @Override
68
            public void onError(WebSocket conn, Exception ex) {
NEW
69
                logger.error("WebSocket error", ex);
×
NEW
70
            }
×
71

72
            @Override
73
            public void onStart() {
74
                logger.info("WebSocket server started on port: {}", getPort());
1✔
75
            }
1✔
76

77
            @Override
78
            public void onMessage(WebSocket ws, String string) {
NEW
79
                logger.info("onMessage: {}", string);
×
NEW
80
            }
×
81
        };
82

83
        Executors.newSingleThreadExecutor().submit(webSocketServer::start);
1✔
84
    }
1✔
85

86
    @Override
87
    protected int getReadTimeout() {
88
        return cKeyProducerJava.timeout;
1✔
89
    }
90

91
    @Override
92
    public void interrupt() {
93
        shouldStop = true;
1✔
94
        if (webSocketServer != null) {
1✔
95
            try {
96
                webSocketServer.stop();
1✔
NEW
97
            } catch (Exception e) {
×
NEW
98
                logger.warn("Error stopping WebSocket server", e);
×
99
            }
1✔
100
        }
101
    }
1✔
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc