Skip to content

Commit 3eac82c

Browse files
author
Robin Duda
committed
Connection: add proxy support for write operation + fix hashCode/equals.
1 parent 7939af8 commit 3eac82c

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

core/main/java/com/codingchili/core/listener/transport/Connection.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codingchili.core.listener.transport;
22

33
import java.util.*;
4+
import java.util.function.BiConsumer;
45
import java.util.function.Consumer;
56

67
import com.codingchili.core.listener.Messageable;
@@ -15,8 +16,9 @@
1516
public class Connection implements Messageable {
1617
private final Map<String, String> properties = new HashMap<>();
1718
private final Map<String, Runnable> closeHandlers = new HashMap<>();
18-
private final Consumer<Object> writer;
1919
private static final String ID = "id";
20+
private final Consumer<Object> writer;
21+
private BiConsumer<Consumer<Object>, Object> proxy;
2022

2123
/**
2224
* Creates a new stateful connection that properly implements the ID method.
@@ -29,9 +31,24 @@ public Connection(Consumer<Object> writer, String id) {
2931
properties.put(ID, id);
3032
}
3133

34+
/**
35+
* Allows all writing to this connection to be handled by a proxy.
36+
*
37+
* @param proxy the method that will invoke the writer.
38+
* @return fluent.
39+
*/
40+
public Connection proxy(BiConsumer<Consumer<Object>, Object> proxy) {
41+
this.proxy = proxy;
42+
return this;
43+
}
44+
3245
@Override
3346
public void write(Object object) {
34-
writer.accept(object);
47+
if (proxy == null) {
48+
writer.accept(object);
49+
} else {
50+
proxy.accept(writer, object);
51+
}
3552
}
3653

3754
/**
@@ -113,4 +130,18 @@ public Connection setProperty(String key, String value) {
113130
properties.put(key, value);
114131
return this;
115132
}
133+
134+
@Override
135+
public int hashCode() {
136+
return id().hashCode();
137+
}
138+
139+
@Override
140+
public boolean equals(Object other) {
141+
if (other instanceof Connection) {
142+
return ((Connection) other).id().equals(id());
143+
} else {
144+
return false;
145+
}
146+
}
116147
}

core/main/java/com/codingchili/core/listener/transport/WebsocketListener.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,8 @@ private Connection connected(ServerWebSocket socket) {
100100
} else {
101101
socket.writeTextMessage(buffer.toString());
102102
}
103-
}, socket.textHandlerID()).setProperty(PROTOCOL_CONNECTION, socket.remoteAddress().host());
104-
}
105-
106-
@Override
107-
public void stop(Promise<Void> stop) {
108-
handler.stop(stop);
103+
}, socket.textHandlerID())
104+
.setProperty(PROTOCOL_CONNECTION, socket.remoteAddress().host());
109105
}
110106

111107
private void handle(Connection connection, Buffer buffer) {
@@ -118,6 +114,11 @@ private void handle(Connection connection, Buffer buffer) {
118114
}
119115
}
120116

117+
@Override
118+
public void stop(Promise<Void> stop) {
119+
handler.stop(stop);
120+
}
121+
121122
@Override
122123
public String toString() {
123124
return handler.getClass().getSimpleName() + LOG_AT + handler.address() + " port :" +

0 commit comments

Comments
 (0)