Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 2b6a61a

Browse files
committed
Documentation
1 parent 5c13df0 commit 2b6a61a

File tree

6 files changed

+159
-31
lines changed

6 files changed

+159
-31
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/handler/ConnectionThrottler.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,37 @@
33
import com.sun.net.httpserver.HttpExchange;
44

55
/**
6+
* Determines how connections are handled by the {@link ThrottledHandler}.
67
*
8+
* @see ServerThrottler
9+
* @see SessionThrottler
10+
* @since 03.03.00
11+
* @version 03.03.00
12+
* @author Ktt Development
713
*/
814
abstract class ConnectionThrottler {
915

16+
/**
17+
* Adds a connection to the pool.
18+
*
19+
* @param exchange exchange to process
20+
* @return if exchange was able to be added
21+
*
22+
* @see #deleteConnection(HttpExchange)
23+
* @since 03.03.00
24+
* @author Ktt Development
25+
*/
1026
abstract boolean addConnection(final HttpExchange exchange);
1127

28+
/**
29+
* Removes a connection from the pool.
30+
*
31+
* @param exchange exchange to process
32+
*
33+
* @see #addConnection(HttpExchange)
34+
* @since 03.03.00
35+
* @author Ktt Development
36+
*/
1237
abstract void deleteConnection(final HttpExchange exchange);
1338

1439
}

src/main/java/com/kttdevelopment/simplehttpserver/handler/RedirectHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
4040

4141
//
4242

43-
4443
@SuppressWarnings("StringBufferReplaceableByString")
4544
@Override
4645
public String toString(){

src/main/java/com/kttdevelopment/simplehttpserver/handler/SSEHandler.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
import java.util.concurrent.TimeUnit;
1313
import java.util.concurrent.atomic.AtomicInteger;
1414

15+
/**
16+
* A SSE handler allows server to client events by using an <code>text/event-stream</code>. Events are sent using {@link #push(String)} or {@link #push(String, int, String)}.
17+
*
18+
* @see SimpleHttpHandler
19+
* @since 03.01.00
20+
* @version 03.03.00
21+
* @author Ktt Development
22+
*/
1523
public class SSEHandler implements SimpleHttpHandler {
1624

1725
private final List<OutputStream> listeners = new ArrayList<>();
@@ -48,10 +56,30 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
4856
listeners.add(exchange.getOutputStream());
4957
}
5058

59+
/**
60+
* Pushes an event to the stream.
61+
*
62+
* @param data data to send
63+
*
64+
* @see #push(String, int, String)
65+
* @since 03.01.00
66+
* @author Ktt Development
67+
*/
5168
public synchronized final void push(final String data){
5269
push(data,0,"");
5370
}
5471

72+
/**
73+
* Pushes an event to the stream.
74+
*
75+
* @param data data to send
76+
* @param retry how long to retry for
77+
* @param event event type
78+
*
79+
* @see #push(String)
80+
* @since 03.01.00
81+
* @author Ktt Development
82+
*/
5583
public synchronized final void push(final String data, final int retry, final String event){
5684
eventId.addAndGet(1);
5785
final EventStreamRecord record = new EventStreamRecord(retry,event,data);

src/main/java/com/kttdevelopment/simplehttpserver/handler/ServerThrottler.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,55 @@
55
import java.util.concurrent.atomic.AtomicInteger;
66
import java.util.function.Predicate;
77

8+
/**
9+
* Limits connections for the server.
10+
*
11+
* @see ThrottledHandler
12+
* @see SessionThrottler
13+
* @since 03.03.00
14+
* @version 03.03.00
15+
* @author Ktt Development
16+
*/
817
public class ServerThrottler extends ConnectionThrottler {
918

10-
private final Predicate<HttpExchange> usesLimit;
19+
private final Predicate<HttpExchange> countsTowardsLimit;
1120
private final AtomicInteger connections = new AtomicInteger(0);
1221

13-
private int maxConnections = 0;
22+
private final AtomicInteger maxConnections = new AtomicInteger(0);
1423

1524
public ServerThrottler(){
16-
usesLimit = exchange -> true;
25+
countsTowardsLimit = exchange -> true;
1726
}
1827

1928
public ServerThrottler(final int maxConnections){
20-
usesLimit = exchange -> true;
21-
this.maxConnections = maxConnections;
29+
countsTowardsLimit = exchange -> true;
30+
this.maxConnections.set(maxConnections);
2231
}
2332

24-
public ServerThrottler(final Predicate<HttpExchange> counts){
25-
this.usesLimit = counts;
33+
public ServerThrottler(final Predicate<HttpExchange> countsTowardsLimit){
34+
this.countsTowardsLimit = countsTowardsLimit;
2635
}
2736

28-
public ServerThrottler(final Predicate<HttpExchange> contributeToLimit, final int maxConnections){
29-
this.usesLimit = contributeToLimit;
30-
this.maxConnections = maxConnections;
37+
public ServerThrottler(final Predicate<HttpExchange> countsTowardsLimit, final int maxConnections){
38+
this.countsTowardsLimit = countsTowardsLimit;
39+
this.maxConnections.set(maxConnections);
40+
}
41+
42+
public final int getMaxConnections(){
43+
return maxConnections.get();
44+
}
45+
46+
public synchronized final void setMaxConnections(final int maxConnections){
47+
if(maxConnections >= 0)
48+
this.maxConnections.set(maxConnections);
3149
}
3250

3351
final boolean addConnection(final HttpExchange exchange){
34-
if(!usesLimit.test(exchange)){
52+
if(!countsTowardsLimit.test(exchange)){
3553
return true;
3654
}else{
3755
synchronized(this){
38-
if(connections.get() + 1 <= maxConnections){
56+
if(connections.get() + 1 <= maxConnections.get()){
3957
connections.incrementAndGet();
4058
return true;
4159
}
@@ -45,21 +63,23 @@ final boolean addConnection(final HttpExchange exchange){
4563
}
4664

4765
final void deleteConnection(final HttpExchange exchange){
48-
if(usesLimit.test(exchange))
66+
if(countsTowardsLimit.test(exchange))
4967
synchronized(this){
5068
connections.decrementAndGet();
5169
}
5270
}
5371

5472
//
5573

74+
@SuppressWarnings("StringBufferReplaceableByString")
5675
@Override
5776
public String toString(){
5877
final StringBuilder OUT = new StringBuilder();
5978

6079
OUT.append("ConnectionThrottler") .append('{');
61-
OUT.append("connections") .append('=') .append(connections) .append(", ");
62-
OUT.append("maxConnections") .append('=') .append(maxConnections);
80+
OUT.append("condition") .append('=') .append(countsTowardsLimit) .append(", ");
81+
OUT.append("connections") .append('=') .append(connections.get()) .append(", ");
82+
OUT.append("maxConnections") .append('=') .append(maxConnections.get());
6383
OUT.append('}');
6484

6585
return OUT.toString();

src/main/java/com/kttdevelopment/simplehttpserver/handler/SessionThrottler.java

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,65 @@
77
import java.util.concurrent.atomic.AtomicInteger;
88
import java.util.function.Predicate;
99

10+
/**
11+
* Limits connections per http session.
12+
*
13+
* @see ThrottledHandler
14+
* @see ServerThrottler
15+
* @see HttpSession
16+
* @since 03.03.00
17+
* @version 03.03.00
18+
* @author Ktt Development
19+
*/
1020
public class SessionThrottler extends ConnectionThrottler {
1121

1222
private final HttpSessionHandler sessionHandler;
1323

14-
private final Predicate<HttpSession> usesLimit;
24+
private final Predicate<HttpSession> countsTowardsLimit;
1525
private final Map<HttpSession, AtomicInteger> sessions = Collections.synchronizedMap(new HashMap<>());
1626

17-
private int maxConnections = 0;
27+
private final AtomicInteger maxConnections = new AtomicInteger(0);
1828

1929
public SessionThrottler(final HttpSessionHandler sessionHandler){
2030
this.sessionHandler = sessionHandler;
21-
usesLimit = (exchange) -> true;
31+
countsTowardsLimit = (exchange) -> true;
2232
}
2333

2434
public SessionThrottler(final HttpSessionHandler sessionHandler, final int maxConnections){
2535
this.sessionHandler = sessionHandler;
26-
usesLimit = (exchange) -> true;
27-
this.maxConnections = maxConnections;
36+
countsTowardsLimit = (exchange) -> true;
37+
this.maxConnections.set(maxConnections);
2838
}
2939

30-
public SessionThrottler(final HttpSessionHandler sessionHandler, final Predicate<HttpSession> usesLimit){
40+
public SessionThrottler(final HttpSessionHandler sessionHandler, final Predicate<HttpSession> countsTowardsLimit){
3141
this.sessionHandler = sessionHandler;
32-
this.usesLimit = usesLimit;
42+
this.countsTowardsLimit = countsTowardsLimit;
3343
}
3444

35-
public SessionThrottler(final HttpSessionHandler sessionHandler, final Predicate<HttpSession> usesLimit, final int maxConnections){
45+
public SessionThrottler(final HttpSessionHandler sessionHandler, final Predicate<HttpSession> countsTowardsLimit, final int maxConnections){
3646
this.sessionHandler = sessionHandler;
37-
this.usesLimit = usesLimit;
38-
this.maxConnections = maxConnections;
47+
this.countsTowardsLimit = countsTowardsLimit;
48+
this.maxConnections.set(maxConnections);
49+
}
50+
51+
public final int getMaxConnections(){
52+
return maxConnections.get();
53+
}
54+
55+
public synchronized final void setMaxConnections(final int maxConnections){
56+
if(maxConnections >= 0)
57+
this.maxConnections.set(maxConnections);
3958
}
4059

4160
@Override
4261
final boolean addConnection(final HttpExchange exchange){
4362
final HttpSession session = sessionHandler.getSession(exchange);
44-
if(!usesLimit.test(session)){
63+
if(!countsTowardsLimit.test(session)){
4564
return true;
4665
}else{
4766
synchronized(this){
4867
final AtomicInteger conn = sessions.get(session);
49-
if(conn.get() + 1 <= maxConnections){
68+
if(conn.get() + 1 <= maxConnections.get()){
5069
conn.incrementAndGet();
5170
return true;
5271
}
@@ -58,10 +77,26 @@ final boolean addConnection(final HttpExchange exchange){
5877
@Override
5978
final void deleteConnection(final HttpExchange exchange){
6079
final HttpSession session = sessionHandler.getSession(exchange);
61-
if(usesLimit.test(session))
80+
if(countsTowardsLimit.test(session))
6281
synchronized(this){
6382
sessions.get(session).decrementAndGet();
6483
}
6584
}
6685

86+
//
87+
88+
@SuppressWarnings("StringBufferReplaceableByString")
89+
@Override
90+
public String toString(){
91+
final StringBuilder OUT = new StringBuilder();
92+
93+
OUT.append("SessionThrottler") .append('{');
94+
OUT.append("condition") .append('=') .append(countsTowardsLimit) .append(", ");
95+
OUT.append("sessions") .append('=') .append(sessions.toString()) .append(", ");
96+
OUT.append("maxConnections") .append('=') .append(maxConnections.get());
97+
OUT.append('}');
98+
99+
return OUT.toString();
100+
}
101+
67102
}

src/main/java/com/kttdevelopment/simplehttpserver/handler/ThrottledHandler.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,33 @@
55

66
import java.io.IOException;
77

8+
/**
9+
* This handler limits the amount of active connections to a handler. This can be used to limit the amount of simultaneous downloads, or prevent duplicate connections by users.
10+
*
11+
* @see ServerThrottler
12+
* @see SessionThrottler
13+
* @since 03.03.00
14+
* @version 03.03.00
15+
* @author Ktt Development
16+
*/
817
public class ThrottledHandler implements HttpHandler {
918

1019
private final HttpHandler handler;
11-
private final ServerThrottler throttler;
20+
private final ConnectionThrottler throttler;
1221

13-
public ThrottledHandler(final HttpHandler handler, final ServerThrottler throttler){
22+
/**
23+
* Creates a throttled handler using a throttler.
24+
*
25+
* @param handler handler to use
26+
* @param throttler how to throttle connections
27+
*
28+
* @see HttpHandler
29+
* @see ServerThrottler
30+
* @see SessionThrottler
31+
* @since 03.03.00
32+
* @author Ktt Development
33+
*/
34+
public ThrottledHandler(final HttpHandler handler, final ConnectionThrottler throttler){
1435
this.handler = handler;
1536
this.throttler = throttler;
1637
}

0 commit comments

Comments
 (0)