yeg
2026-08-26 a9e3435464f86a50ffa81a16ef92e47734adef6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.ruoyi.device.bts;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
/**
 * 提供可复用的 BTS TCP 脚本设备边界,用于闭环与恢复测试。
 */
public final class ScriptedBtsTcpSimulator implements AutoCloseable
{
    private static final byte[] FRAME_END = "#\r\n".getBytes(StandardCharsets.UTF_8);
 
    private final ServerSocket serverSocket;
    private final int port;
    private final ExecutorService executor = Executors.newSingleThreadExecutor();
    private final BlockingQueue<ScriptedAction> actions = new LinkedBlockingQueue<>();
    private final List<String> requests = new CopyOnWriteArrayList<>();
 
    public ScriptedBtsTcpSimulator() throws IOException
    {
        this(true);
    }
 
    private ScriptedBtsTcpSimulator(boolean acceptConnections) throws IOException
    {
        serverSocket = new ServerSocket(0);
        port = serverSocket.getLocalPort();
        if (acceptConnections)
            executor.submit(this::serve);
        else
            serverSocket.close();
    }
 
    public static ScriptedBtsTcpSimulator refusingConnections() throws IOException
    {
        return new ScriptedBtsTcpSimulator(false);
    }
 
    public int port()
    {
        return port;
    }
 
    public void enqueue(String response)
    {
        actions.add(new ScriptedAction(Duration.ZERO, response.getBytes(StandardCharsets.UTF_8), false));
    }
 
    public void enqueueDelayed(Duration delay, String response)
    {
        if (delay.isNegative()) throw new IllegalArgumentException("delay must not be negative");
        actions.add(new ScriptedAction(delay, response.getBytes(StandardCharsets.UTF_8), false));
    }
 
    public void enqueueRaw(byte[] response)
    {
        actions.add(new ScriptedAction(Duration.ZERO, response.clone(), false));
    }
 
    public void enqueueDisconnect()
    {
        actions.add(new ScriptedAction(Duration.ZERO, null, true));
    }
 
    public List<String> requests()
    {
        return List.copyOf(requests);
    }
 
    private void serve()
    {
        while (!serverSocket.isClosed())
        {
            try (Socket socket = serverSocket.accept())
            {
                serveConnection(socket);
            }
            catch (SocketException ex)
            {
                if (!serverSocket.isClosed()) throw new IllegalStateException(ex);
            }
            catch (Exception ignored)
            {
            }
        }
    }
 
    private void serveConnection(Socket socket) throws Exception
    {
        while (!socket.isClosed())
        {
            String request = readFrame(socket.getInputStream());
            requests.add(request);
            ScriptedAction action = actions.poll(5, TimeUnit.SECONDS);
            if (action == null) throw new IOException("No scripted BTS action available");
            TimeUnit.MILLISECONDS.sleep(action.delay().toMillis());
            if (action.disconnect()) return;
            socket.getOutputStream().write(action.payload());
            socket.getOutputStream().write(FRAME_END);
            socket.getOutputStream().flush();
        }
    }
 
    private String readFrame(InputStream input) throws IOException
    {
        return new String(BtsFrameReader.read(input, Integer.MAX_VALUE), StandardCharsets.UTF_8);
    }
 
    @Override
    public void close() throws Exception
    {
        serverSocket.close();
        executor.shutdownNow();
        executor.awaitTermination(2, TimeUnit.SECONDS);
    }
 
    private record ScriptedAction(Duration delay, byte[] payload, boolean disconnect)
    {
    }
}