Skip to content

Commit

Permalink
Merge pull request #194 from sopra-fs24-group-09/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zihanltesla authored May 17, 2024
2 parents de56700 + 09d7410 commit 5707c6e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
14 changes: 14 additions & 0 deletions src/main/java/ch/uzh/ifi/hase/soprafs24/Application.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ch.uzh.ifi.hase.soprafs24;

import java.util.concurrent.Executor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand Down Expand Up @@ -35,4 +38,15 @@ public void addCorsMappings(@SuppressWarnings("null") CorsRegistry registry) {
}
};
}

@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(25);
executor.setThreadNamePrefix("Async-");
executor.initialize();
return executor;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
package ch.uzh.ifi.hase.soprafs24.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.messaging.simp.config.ChannelRegistration;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
public void registerStompEndpoints(@SuppressWarnings("null") StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.setAllowedOriginPatterns("*")
.withSockJS();
// .setInterceptors(new RoomIdInterceptor());
}


@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
public void configureMessageBroker(@SuppressWarnings("null") MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/user","/game","/plays/info","/games/info","/plays/audio", "/response","/lobby/info");
registry.enableSimpleBroker("/user", "/game", "/plays/info", "/games/info", "/plays/audio", "/response", "/lobby/info");
registry.setUserDestinationPrefix("/user");
}
}

@Bean(name = "webSocketTaskExecutor")
public ThreadPoolTaskExecutor webSocketTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(50);
executor.setThreadNamePrefix("WebSocket-");
executor.initialize();
return executor;
}

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.taskExecutor(webSocketTaskExecutor());
}

@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
registration.taskExecutor(webSocketTaskExecutor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void checkIfAllReady(Room room) {
}
}

@Async
@Async("taskExecutor")
public void startGame(Room room) {
// Initialize a new game object and player objects, then save them to the
// database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import ch.uzh.ifi.hase.soprafs24.entity.User;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -62,6 +63,7 @@ public SocketService(SimpMessagingTemplate simpMessagingTemplate,
}

// helper function for sending message to destination with JSON format
@Async("webSocketTaskExecutor")
public <T> void sendMessage(String destination, String roomId, T info, String receiptId) {
try {
// Wrapping the info object within Timestamped
Expand Down Expand Up @@ -228,6 +230,7 @@ public void broadcastLobbyInfo() {
sendMessage("/lobby/info", null, lobbyInfo, null);
}

@Async("webSocketTaskExecutor")
public void broadcastResponse(String userId, String roomId,boolean isSuccess, boolean isAuth, String response, String receiptId) {
Response responseObj = new Response();
responseObj.setSuccess(isSuccess);
Expand Down

0 comments on commit 5707c6e

Please sign in to comment.