-
Notifications
You must be signed in to change notification settings - Fork 1
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
7afecaf
commit 5dd18a7
Showing
9 changed files
with
324 additions
and
15 deletions.
There are no files selected for viewing
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
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
60 changes: 60 additions & 0 deletions
60
app/src/main/java/dev/flowty/bowlby/app/srv/ServerListeners.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,60 @@ | ||
package dev.flowty.bowlby.app.srv; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
import com.sun.net.httpserver.HttpHandler; | ||
|
||
/** | ||
* Mechanism for behaviours that are interested in server activity | ||
*/ | ||
public class ServerListeners { | ||
|
||
private final List<Consumer<Boolean>> listeners = new ArrayList<>(); | ||
|
||
/** | ||
* Wraps a handler in activity-notification behaviour | ||
* | ||
* @param handler The handler | ||
* @return a handler that will notify listeners on behaviour | ||
*/ | ||
public HttpHandler wrap( HttpHandler handler ) { | ||
return exchange -> { | ||
try( Notifying n = notifying() ) { | ||
handler.handle( exchange ); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @param listener will be supplied with <code>true</code> when request handling | ||
* begins starts, and <code>false</code> when it ends | ||
* @return <code>this</code> | ||
*/ | ||
public ServerListeners with( Consumer<Boolean> listener ) { | ||
listeners.add( listener ); | ||
return this; | ||
} | ||
|
||
/** | ||
* @return an {@link AutoCloseable} that handles listener notification | ||
*/ | ||
private Notifying notifying() { | ||
return new Notifying(); | ||
} | ||
|
||
/** | ||
* Handles listener notification | ||
*/ | ||
private class Notifying implements AutoCloseable { | ||
private Notifying() { | ||
listeners.forEach( l -> l.accept( true ) ); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
listeners.forEach( l -> l.accept( false ) ); | ||
} | ||
} | ||
} |
Oops, something went wrong.