Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Домашняя работа №8 #11

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix in homework 8 again
  • Loading branch information
arhostcode committed Dec 2, 2023
commit 3529d9a8ef688ba7e4eaaa6fc9bcf766fc0d0c9a
2 changes: 1 addition & 1 deletion src/main/java/edu/hw8/task1/QuotesServer.java
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ public void start() {
@SneakyThrows
private void processConnections(ServerSocketChannel channel, Selector selector) {
while (channel.isOpen()) {
if (selector.selectNow() > 0) {
if (selector.selectNow() > 0 || !selector.selectedKeys().isEmpty()) {
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
4 changes: 3 additions & 1 deletion src/main/java/edu/hw8/task1/QuotesServerWorker.java
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ public class QuotesServerWorker implements Runnable {

private final SocketChannel clientChannel;
private final QuotesStorage quotesStorage;
private boolean isConnected = true;
private final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
private Runnable after;
private Consumer<String> messageConsumer;
@@ -24,14 +25,15 @@ public void run() {
Selector selector = Selector.open();
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_READ);
while (clientChannel.isConnected()) {
while (isConnected) {
if (selector.selectNow() > 0) {
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
if (key.isReadable()) {
String message = readMessageFromClient();
if (message == null) {
isConnected = false;
break;
}
if (messageConsumer != null) {
9 changes: 8 additions & 1 deletion src/test/java/edu/hw8/task1/QuotesTest.java
Original file line number Diff line number Diff line change
@@ -17,13 +17,20 @@ public void quotes_shouldCorrectlyWork() {
List<String> userRequests = new ArrayList<>();
List<String> serverResponses = new ArrayList<>();
QuotesStorage quotesStorage = QuotesStorage.createDefault();
QuotesServer quotesServer = new QuotesServer(12345, quotesStorage, 1);
QuotesServer quotesServer = new QuotesServer(12345, quotesStorage, 2);
QuotesClient quotesClient = new QuotesClient("localhost", 12345);

quotesServer.setMessageConsumer(userRequests::add);
Executors.newSingleThreadExecutor().execute(quotesServer::start);
Thread.sleep(1000);

// Imitate multiconnnection
quotesClient.start();
quotesClient.close();
quotesClient.start();
quotesClient.close();
quotesClient.start();

serverResponses.add(quotesClient.requestQuote("личности"));
serverResponses.add(quotesClient.requestQuote("оскорбления"));