-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b47d69b
commit 298f4e3
Showing
10 changed files
with
234 additions
and
41 deletions.
There are no files selected for viewing
14 changes: 2 additions & 12 deletions
14
vpn-router-api/src/main/java/vpnrouter/api/client/ClientDetectionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,7 @@ | ||
package vpnrouter.api.client; | ||
|
||
public interface ClientDetectionService { | ||
boolean isInProgress(); | ||
|
||
void detectAndSave(CompletionListener completionListener); | ||
|
||
interface CompletionListener { | ||
|
||
void onAlreadyRunning(); | ||
|
||
void onNewClientsNotFound(); | ||
|
||
void onNewClientsFound(int newClientsCount); | ||
|
||
void onFailure(Exception exception); | ||
} | ||
void detectAndSave(); | ||
} |
9 changes: 9 additions & 0 deletions
9
...i/src/main/java/vpnrouter/api/event/concrete/client/ClientDetectionClientsFoundEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package vpnrouter.api.event.concrete.client; | ||
|
||
import lombok.Data; | ||
import vpnrouter.api.event.Event; | ||
|
||
@Data | ||
public class ClientDetectionClientsFoundEvent implements Event { | ||
private final int newClientsCount; | ||
} |
6 changes: 6 additions & 0 deletions
6
...rc/main/java/vpnrouter/api/event/concrete/client/ClientDetectionClientsNotFoundEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package vpnrouter.api.event.concrete.client; | ||
|
||
import vpnrouter.api.event.Event; | ||
|
||
public class ClientDetectionClientsNotFoundEvent implements Event { | ||
} |
9 changes: 9 additions & 0 deletions
9
...er-api/src/main/java/vpnrouter/api/event/concrete/client/ClientDetectionFailureEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package vpnrouter.api.event.concrete.client; | ||
|
||
import lombok.Data; | ||
import vpnrouter.api.event.Event; | ||
|
||
@Data | ||
public class ClientDetectionFailureEvent implements Event { | ||
private final Exception exception; | ||
} |
6 changes: 6 additions & 0 deletions
6
...er-api/src/main/java/vpnrouter/api/event/concrete/client/ClientDetectionStartedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package vpnrouter.api.event.concrete.client; | ||
|
||
import vpnrouter.api.event.Event; | ||
|
||
public class ClientDetectionStartedEvent implements Event { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...router-web/src/main/java/vpnrouter/web/ui/clients/detection/ClientDetectionComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package vpnrouter.web.ui.clients.detection; | ||
|
||
import com.vaadin.flow.component.button.Button; | ||
import com.vaadin.flow.component.icon.VaadinIcon; | ||
import com.vaadin.flow.component.progressbar.ProgressBar; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ClientDetectionComponent { | ||
|
||
private final Button startButton; | ||
private final ProgressBar progressBar; | ||
|
||
public ClientDetectionComponent(State initialState, Runnable onDetectionStartListener) { | ||
this.startButton = buildStartButton(onDetectionStartListener); | ||
this.progressBar = buildProgressBar(); | ||
setState(initialState); | ||
} | ||
|
||
private Button buildStartButton(Runnable onDetectionStartListener) { | ||
var clientDetectionButton = new Button(VaadinIcon.REFRESH.create()); | ||
clientDetectionButton.addClickListener(event -> onDetectionStartListener.run()); | ||
return clientDetectionButton; | ||
} | ||
|
||
private ProgressBar buildProgressBar() { | ||
var progressBar = new ProgressBar(); | ||
progressBar.setIndeterminate(true); | ||
return progressBar; | ||
} | ||
|
||
public void setState(State state) { | ||
startButton.setEnabled(state == State.IDLE); | ||
progressBar.setVisible(state == State.IN_PROGRESS); | ||
} | ||
|
||
public enum State { | ||
IDLE, | ||
IN_PROGRESS, | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
...web/src/main/java/vpnrouter/web/ui/clients/detection/ClientDetectionComponentFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package vpnrouter.web.ui.clients.detection; | ||
|
||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.component.UIDetachedException; | ||
import com.vaadin.flow.component.notification.Notification; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import vpnrouter.api.client.ClientDetectionService; | ||
import vpnrouter.api.event.EventSubscriber; | ||
import vpnrouter.api.event.EventSubscriberRegistry; | ||
import vpnrouter.api.event.concrete.client.ClientDetectionClientsFoundEvent; | ||
import vpnrouter.api.event.concrete.client.ClientDetectionClientsNotFoundEvent; | ||
import vpnrouter.api.event.concrete.client.ClientDetectionFailureEvent; | ||
import vpnrouter.api.event.concrete.client.ClientDetectionStartedEvent; | ||
import vpnrouter.web.ui.clients.detection.ClientDetectionComponent.State; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ClientDetectionComponentFactory { | ||
|
||
private final ClientDetectionService clientDetectionService; | ||
private final EventSubscriberRegistry eventSubscriberRegistry; | ||
|
||
public ClientDetectionComponent build() { | ||
var initialState = clientDetectionService.isInProgress() ? State.IN_PROGRESS : State.IDLE; | ||
var clientDetectionComponent = new ClientDetectionComponent(initialState, clientDetectionService::detectAndSave); | ||
registerHandlers(clientDetectionComponent); | ||
return clientDetectionComponent; | ||
} | ||
|
||
private void registerHandlers(ClientDetectionComponent clientDetectionComponent) { | ||
eventSubscriberRegistry.addSubscriber( | ||
ClientDetectionStartedEvent.class, | ||
new DetectionStartedEventHandler(UI.getCurrent(), clientDetectionComponent) | ||
); | ||
eventSubscriberRegistry.addSubscriber( | ||
ClientDetectionClientsFoundEvent.class, | ||
new ClientsFoundEventHandler(UI.getCurrent(), clientDetectionComponent) | ||
); | ||
eventSubscriberRegistry.addSubscriber( | ||
ClientDetectionClientsNotFoundEvent.class, | ||
new ClientsNotFoundEventHandler(UI.getCurrent(), clientDetectionComponent) | ||
); | ||
eventSubscriberRegistry.addSubscriber( | ||
ClientDetectionFailureEvent.class, | ||
new DetectionFailureEventHandler(UI.getCurrent(), clientDetectionComponent) | ||
); | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@EqualsAndHashCode(of = "ui") | ||
private class DetectionStartedEventHandler implements EventSubscriber<ClientDetectionStartedEvent> { | ||
|
||
private final UI ui; | ||
private final ClientDetectionComponent clientDetectionComponent; | ||
|
||
@Override | ||
public void receive(ClientDetectionStartedEvent event) { | ||
try { | ||
ui.access(() -> { | ||
clientDetectionComponent.setState(State.IN_PROGRESS); | ||
}); | ||
} catch (UIDetachedException e) { | ||
eventSubscriberRegistry.removeSubscriber(ClientDetectionStartedEvent.class, this); | ||
} | ||
} | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@EqualsAndHashCode(of = "ui") | ||
private class ClientsFoundEventHandler implements EventSubscriber<ClientDetectionClientsFoundEvent> { | ||
|
||
private final UI ui; | ||
private final ClientDetectionComponent clientDetectionComponent; | ||
|
||
@Override | ||
public void receive(ClientDetectionClientsFoundEvent event) { | ||
try { | ||
var newClientsCount = event.getNewClientsCount(); | ||
ui.access(() -> { | ||
Notification.show("%d new clients found".formatted(newClientsCount)); | ||
clientDetectionComponent.setState(State.IDLE); | ||
}); | ||
} catch (UIDetachedException e) { | ||
eventSubscriberRegistry.removeSubscriber(ClientDetectionClientsFoundEvent.class, this); | ||
} | ||
} | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@EqualsAndHashCode(of = "ui") | ||
private class ClientsNotFoundEventHandler implements EventSubscriber<ClientDetectionClientsNotFoundEvent> { | ||
|
||
private final UI ui; | ||
private final ClientDetectionComponent clientDetectionComponent; | ||
|
||
@Override | ||
public void receive(ClientDetectionClientsNotFoundEvent event) { | ||
try { | ||
ui.access(() -> { | ||
Notification.show("No new clients found"); | ||
clientDetectionComponent.setState(State.IDLE); | ||
}); | ||
} catch (UIDetachedException e) { | ||
eventSubscriberRegistry.removeSubscriber(ClientDetectionClientsNotFoundEvent.class, this); | ||
} | ||
} | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@EqualsAndHashCode(of = "ui") | ||
private class DetectionFailureEventHandler implements EventSubscriber<ClientDetectionFailureEvent> { | ||
|
||
private final UI ui; | ||
private final ClientDetectionComponent clientDetectionComponent; | ||
|
||
@Override | ||
public void receive(ClientDetectionFailureEvent event) { | ||
try { | ||
var exception = event.getException(); | ||
ui.access(() -> { | ||
clientDetectionComponent.setState(State.IDLE); | ||
throw new RuntimeException(exception); | ||
}); | ||
} catch (UIDetachedException e) { | ||
eventSubscriberRegistry.removeSubscriber(ClientDetectionFailureEvent.class, this); | ||
} | ||
} | ||
} | ||
} |