package serverUtils; import java.io.IOException; import java.nio.channels.SelectionKey;…
- User
package serverUtils;
import java.io.IOException; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.util.Iterator;
public class Console {
public void start() throws IOException { ConnectionManager connectionManager = new ConnectionManager(6789);
connectionManager.initialize();
Selector selector = Selector.open(); connectionManager.registerSelector(selector);
while (true) { int readyChannelsCount = selector.select(); if (readyChannelsCount == 0) { continue; } Iterator selectedKeysIterator = selector.selectedKeys().iterator(); while (selectedKeysIterator.hasNext()) { SelectionKey key = selectedKeysIterator.next(); selectedKeysIterator.remove(); connectionManager.processQuery(selector, key); } } } }
package serverUtils;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.nio.charset.StandardCharsets;
public class ConnectionManager { private static final String ACK_MESSAGE_PREFIX = “<%s>Ack”; private static final int BUFFER_SIZE = 1024 * 1024;
ServerSocketChannel serverSocketChannel; int port;
public ConnectionManager(int port) { this.port = port; }
public void initialize() throws IOException { serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(port)); serverSocketChannel.configureBlocking(false); System.out.println(“Server started on port " + port); System.out.println(”============ * ============"); }
public void registerSelector(Selector selector) throws ClosedChannelException { serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); }
public void processQuery(Selector selector, SelectionKey key) throws IOException { if (!key.isValid()) { return; } if (key.isAcceptable()) { acceptConnection(key, selector); } else if (key.isReadable()) { handleRead(key); } } private void acceptConnection(SelectionKey key, Selector selector) throws IOException { ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); SocketChannel clientSocketChannel = serverSocketChannel.accept(); clientSocketChannel.configureBlocking(false); clientSocketChannel.register(selector, SelectionKey.OP_READ);
System.out.println(“Accepted connection from " + clientSocketChannel.getRemoteAddress()); }
private void handleRead(SelectionKey key) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); SocketChannel clientSocketChannel = (SocketChannel) key.channel(); int bytesRead; while ((bytesRead = clientSocketChannel.read(buffer)) > 0) { buffer.flip();
// проверяем наличие данных в буфере if (buffer.remaining() < 8) { continue; }
// читаем длину имени файла в байтах int nameLength = buffer.getInt();
// проверяем наличие данных в буфере if (buffer.remaining() < nameLength + 4) { buffer.position(buffer.position() - 4); buffer.compact(); continue; }
// читаем имя файла byte[] nameBytes = new byte[nameLength]; buffer.get(nameBytes); String fileName = new String(nameBytes, StandardCharsets.UTF_8);
// читаем длину файла в байтах int fileLength = buffer.getInt();
// проверяем наличие данных в буфере if (buffer.remaining() < fileLength + 8) { buffer.position(buffer.position() - 8); buffer.compact(); continue; }
// читаем содержимое файла byte[] fileContent = new byte[fileLength]; buffer.get(fileContent, 0, fileLength);
// проверяем разделитель long separator = buffer.getLong(); if (separator != 0) { throw new RuntimeException(“Invalid separator”); }
// очищаем буфер buffer.clear();
// сохраняем файл saveFile(fileName, fileContent);
// отправляем подтверждение о сохранении файла sendAck(clientSocketChannel, fileName); }
if (bytesRead == -1) { System.out.println(“Closed connection with " + clientSocketChannel.getRemoteAddress()); clientSocketChannel.close(); key.cancel(); System.out.println(”============ * ============”); } }
private void saveFile(String filename, byte[] fileContent) throws IOException { File file = new File(“files\” + filename);
System.out.println("Saving file " + filename);
try (FileOutputStream outputStream = new FileOutputStream(file)) { outputStream.write(fileContent); } System.out.println(“File " + filename + " saved successfully”); }
private void sendAck(SocketChannel clientSocketChannel, String filename) throws IOException { String ackMessage = String.format(ACK_MESSAGE_PREFIX, filename); byte[] ackMessageBytes = ackMessage.getBytes(); ByteBuffer buffer = ByteBuffer.allocate(ackMessageBytes.length + Integer.BYTES); buffer.putInt(ackMessageBytes.length); buffer.put(ackMessageBytes); buffer.flip();
while (buffer.hasRemaining()) { clientSocketChannel.write(buffer); }
System.out.println("Sent acknowledgement for " + filename); buffer.clear(); // Очищаем буфер перед чтением новых данных } }
Есть приложение на java. В серверную часть нужно добавить возможность обработки нескольких клиентов. Например, если один из клиентов остановился на передаче файла где-то на половине, и чтобы не крутиться в холостом цикле, в это время необходимо обрабатывать запросы других клиентов. Для достижения данного условия необходимо создать контекст соединения и связать его с конкретным соединением (Selection Key) и получать его через метод attachment. В этом же контексте можно хранить название файла, текущий состояние буффера, этап передачи файла. Использовать многопоточность нельзя
- Assistant
, поэтому нужно использовать неблокирующий ввод/вывод (non-blocking I/O). Для этого необходимо использовать классы Selector, SelectionKey и Channel вместо Socket и ServerSocket.
- Добавим класс Connection и ConnectionManager, чтобы хранить контексты соединения:
public class Connection { private String fileName; private ByteBuffer buffer; private SocketChannel socketChannel; public Connection(String fileName, ByteBuffer buffer, SocketChannel socketChannel) { this.fileName = fileName; this.buffer = buffer; this.socketChannel = socketChannel; } public String getFileName() { return fileName; } public ByteBuffer getBuffer() { return buffer; } public SocketChannel getSocketChannel() { return socketChannel; } } public class ConnectionManager { private final Selector selector; private final ServerSocketChannel serverChannel; private final Map<SelectionKey, Connection> connections = new ConcurrentHashMap<>(); private static final int BUFFER_SIZE = 1024 * 1024; public ConnectionManager(int port) throws IOException { selector = Selector.open(); serverChannel = ServerSocketChannel.open(); serverChannel.socket().bind(new InetSocketAddress(port)); serverChannel.configureBlocking(false); serverChannel.register(selector, SelectionKey.OP_ACCEPT); System.out.println("Server started on port " + port); System.out.println("============ * ============"); } public void start() throws IOException { while (true) { int keyCount = selector.select(); if (keyCount == 0) { continue; } Set<SelectionKey> keys = selector.selectedKeys(); Iterator<SelectionKey> iterator = keys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); try { if (!key.isValid()) { continue; } if (key.isAcceptable()) { handleAccept(key); } else if (key.isWritable()) { handleWrite(key); } else if (key.isReadable()) { handleRead(key); } } catch (IOException e) { System.err.println("Error processing key: " + e.getMessage()); key.cancel(); } } } } private void handleAccept(SelectionKey key) throws IOException { ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); SocketChannel clientChannel = serverSocketChannel.accept(); clientChannel.configureBlocking(false); clientChannel.register(selector, SelectionKey.OP_READ); } private void handleRead(SelectionKey key) throws IOException { SocketChannel clientChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); int bytesRead = clientChannel.read(buffer); if (bytesRead == -1) { key.cancel(); clientChannel.close(); return; } Connection connection = connections.get(key); if (connection == null) { // новый контекст для нового соединения buffer.flip(); int nameLength = buffer.getInt(); byte[] nameBytes = new byte[nameLength]; buffer.get(nameBytes); String fileName = new String(nameBytes, StandardCharsets.UTF_8); int fileLength = buffer.getInt(); ByteBuffer fileBuffer = ByteBuffer.allocate(fileLength); int remaining = buffer.remaining(); if (remaining > 0) { fileBuffer.put(buffer.array(), buffer.position(), remaining); } connections.put(key, new Connection(fileName, fileBuffer, clientChannel)); if (fileBuffer.remaining() == 0) { // отправить подтверждение, если файл полностью получен selectionKey.interestOps(SelectionKey.OP_WRITE); } } else { // продолжить получение файла connection.getBuffer().put(buffer); if (!buffer.hasRemaining()) { // отправить подтверждение, если файл полностью получен key.interestOps(SelectionKey.OP_WRITE); } } } private void handleWrite(SelectionKey key) throws IOException { Connection connection = connections.get(key); String fileName = connection.getFileName(); ByteBuffer ackBuffer = StandardCharsets.UTF_8.encode(String.format("<%s>Ack", fileName)); ackBuffer.rewind(); connection.getSocketChannel().write(ackBuffer); connection.getSocketChannel().close(); key.cancel(); // закрыть соединение connections.remove(key); // удалить контекст соединения } }- Изменим класс Console, чтобы использовать ConnectionManager:
public class Console { public void start() throws IOException { ConnectionManager connectionManager = new ConnectionManager(6789); connectionManager.start(); } }Теперь серверная часть может обработать несколько клиентов одновременно.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.